Skip to content

Instantly share code, notes, and snippets.

@somada141
somada141 / python_dicom_load_pydicom.md
Created March 2, 2015 10:30
Load DICOM data into a NumPy array with PyDICOM #python #dicom #medical #imagedata #pydicom #fileIO
@somada141
somada141 / builtin_type_convert_vtk_simpleitk.md
Created March 2, 2015 09:59
Convert VTK built-in types to SimpleITK/ITK built-ins and vice-versa #python #vtk #simpleitk #itk

We start by importing vtk and SimpleITK:

import vtk
import SimpleITK

We then create two dictionaries containing the matching built-in types:

@somada141
somada141 / find_non_ascii_chars.md
Last active August 29, 2015 14:16
Identify non-ASCII characters in a file #shell #unix #osx #perl

The following Perl command, which should work directly in a UNIX/OSX bash, checks a file and prints any line with a non-ascii character

perl -ne 'print "$. $_" if m/[\x80-\xFF]/'  file.txt
@somada141
somada141 / python_run_method_background.md
Last active April 16, 2023 09:32
Running a Python class method in the background through a thread #python #parallel #threading #asynchronous

Here’s a little code snippet for running class methods as background threads in Python. The run() method does some work forever and in this use case you want it to do that in the background (until the main application dies), while the rest of the application continues it’s work.

import threading
import time


class ThreadingExample(object):
    """ Threading example class
@somada141
somada141 / system_info_python_platform.md
Last active August 29, 2015 14:15
Retrieving system info using Python the 'platform' package #python #system

Using the platform package one can retrieve a lot of information on the running system.

In order to retrieve a detailed string on the platform one can use:

import platform
platform.platform()

and will get a response like this:

@somada141
somada141 / mac_osx_uptime.md
Last active August 29, 2015 14:15
Get uptime on mac #python #system #shell

Using the following terminal command one can retrieve the uptime in seconds:

sysctl kern.boottime

getting a response like this:

kern.boottime: { sec = 1362633455, usec = 0 } Wed Mar 6 21:17:35 2013
@somada141
somada141 / python_math_rotate_point.py
Last active May 27, 2022 02:10
Rotate a point coordinates around another point in Python #python #graphics #math
#source: http://stackoverflow.com/questions/20023209/python-function-for-rotating-2d-objects
import math
def rotatePolygon(polygon,theta):
"""Rotates the given polygon which consists of corners represented as (x,y),
around the ORIGIN, clock-wise, theta degrees"""
theta = math.radians(theta)
rotatedPolygon = []
for corner in polygon :
@somada141
somada141 / file_download_progressbar.py
Last active August 20, 2024 19:35
File download & progressbar with Python's urllib2 #python #fileIO #os #web #requests #urllib
# source:
# http://stackoverflow.com/questions/5783517/downloading-progress-bar-urllib2-python
import urllib2, sys
def chunk_report(bytes_so_far, chunk_size, total_size):
percent = float(bytes_so_far) / total_size
percent = round(percent*100, 2)
sys.stdout.write("Downloaded %d of %d bytes (%0.2f%%)\r" %
(bytes_so_far, total_size, percent))
@somada141
somada141 / vtk_load_stl.md
Last active January 27, 2022 06:46
Load an the contents of an STL file into a vtkPolyData object with Python and VTK #python #vtk #fileIO #visualizaton #stl

Assuming we have an .stl file under filenameSTL we can load the contents of that file using the vtkSTLReader class as such:

readerSTL = vtk.vtkSTLReader()
readerSTL.SetFileName(filenameSTL)
# 'update' the reader i.e. read the .stl file
readerSTL.Update()

polydata = readerSTL.GetOutput()
@somada141
somada141 / vtk_resample_imagedata.md
Last active August 29, 2015 14:08
Resample a vtkImageData object using the vtkImageReslice class #python #vtk #imagedata #dicom #visualization

Assuming we have a vtkImageData object under the name of image this is how we'd go about resampling that image data with a factor of 2. Here we assume that image has a spacing of 1.0 mm across all axes

resliceFilter = vtk.vtkImageReslice()
resliceFilter.SetInput(image)
resliceFilter.SetOutputSpacing(0.5, 0.5, 0.5)
resliceFilter.SetInterpolationModeToCubic()
resliceFilter.Update()
imageResampled = resliceFilter.GetOutput()