Skip to content

Instantly share code, notes, and snippets.

@somada141
somada141 / copy_tree.py
Last active August 29, 2015 14:03
How to Recursively Copy a Folder (Directory) in Python #python #fileIO #shutil
import shutil
def copyDirectory(src, dest):
try:
shutil.copytree(src, dest)
# Directories are the same
except shutil.Error as e:
print('Directory not copied. Error: %s' % e)
# Any error saying that the directory doesn't exist
except OSError as e:
@somada141
somada141 / ct2hu.md
Last active March 7, 2023 21:43
Converting CT Data to Hounsfield Units #medical #CT #imagedata #dicom #hounsfield #math

Converting CT Data to Hounsfield Units

The formula is:

hu = pixel_value * slope + intercept

Normally, these values are stored in the DICOM file itself. The tags are generally called the Rescale Slope and Rescale Intercept, and typically have values of 1 and -1024, respectively.

@somada141
somada141 / mayavi_vis_mesh.py
Last active August 29, 2015 14:03
Visualize Mesh (MayaVi) #python #visualization #meshing #mayavi #mlab
# mesh created with
# verts, faces = skimage.measure.marching_cubes(volume, level, spacing=(1.0, 1.0, 1.0))
from mayavi import mlab
verts, faces = marching_cubes(myvolume, 0.0, (1., 1., 2.))
mlab.triangular_mesh([vert[0] for vert in verts],
[vert[1] for vert in verts],
[vert[2] for vert in verts],
faces)
mlab.show()
@somada141
somada141 / ipython_notebook_progressbar.md
Last active August 29, 2015 14:03
IPython Notebook ProgressBar #python #visualization #ipython #progressbar

IPython Notebook ProgressBar

Class

import sys, time
try:
    from IPython.display import clear_output
    have_ipython = True
except ImportError:
@somada141
somada141 / tcc_disable_internal.md
Created June 30, 2014 01:25
Take Command Prompt: Disable an internal command

This option allows you to disable or enable internal commands. To disable a command, precede the command name with a minus [-]. To re-enable a command, precede it with a plus [+]. For example, to disable the internal LIST command to force TCC to use an external command:

setdos /i-list

To re-enable all disabled commands use /I*.

source:

@somada141
somada141 / os_path_walk.md
Last active August 29, 2015 14:03
Traverse a directory with os.path.walk #python #fileIO #os

Traverse a directory with os.path.walk

# Import the os module, for the os.walk function
import os
 
# Set the directory you want to start from
rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir):
 print('Found directory: %s' % dirName)
@somada141
somada141 / ipython_notebook_vtk.md
Last active January 31, 2022 20:36
Display VTK renders into IPython Notebook #python #visualization #vtk #ipython #ipythonnotebook

This function takes a renderer and displays the output directly into IPython

def vtk_show(renderer, w=100, h=100):
    """
    Takes vtkRenderer instance and returns an IPython Image with the rendering.
    """
    renderWindow = vtkRenderWindow()
    renderWindow.SetOffScreenRendering(1)
 renderWindow.AddRenderer(renderer)
@somada141
somada141 / python_get_directory_of_file.md
Last active August 29, 2015 14:06
Get the file directory in Python #python #os
@somada141
somada141 / python_download_requests.py
Last active December 31, 2016 09:36
Download files with Python and the requests package #python #fileIO #web #requests
import requests
def download_file(url):
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
@somada141
somada141 / vtk_triangle_normals_glyph.tcl
Last active August 29, 2015 14:06
How to render glyphs on triangle normals with VTK #tcl #vtk #visualization
# This example shows how to manually construct triangle cell using unstructured grids
# and display its surface normal.
#
package require vtk
package require vtkinteraction
# Create an unstructured grids containing a triangle cell.
vtkPoints trianglePoints
trianglePoints SetNumberOfPoints 3