This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import inspect | |
| import yaml | |
| from typing import Callable, Optional | |
| def export_function_interface_to_yaml(func: Callable, file_name: str) -> None: | |
| """ | |
| Export the interface of a Python function to a YAML file. | |
| Args: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import platform | |
| from pip import main | |
| from deprecated import deprecated | |
| print(f"Platform: {platform.platform()}") | |
| print(f"Machine: {platform.machine()}") | |
| print(f"Processor: {platform.processor()}") | |
| print(f"Architecture: {(' ').join(list(platform.architecture()))}") | |
| print("Python environment:") | |
| print(f" Version: {platform.python_version()}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os, json | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| if __name__ == "__main__": | |
| base_dir = os.path.dirname(os.path.abspath(__file__)) | |
| all_dirs = os.listdir(base_dir) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SimpleITK as sitk | |
| image_to_cast = sitk.ReadImage("/path/to/image.nii.gz") | |
| options_for_casting = { | |
| "int64": sitk.sitkInt64, | |
| "int32": sitk.sitkInt32, | |
| "int16": sitk.sitkInt16, | |
| "int8": sitk.sitkInt8, | |
| "uint64": sitk.sitkUInt64, | |
| "uint32": sitk.sitkUInt32, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os, time, traceback, pathlib | |
| import SimpleITK as sitk | |
| def downsample_dicom( | |
| dicom_path: str, output_path: str, downsample_factor: int = 0.5 | |
| ) -> None: | |
| """ | |
| Downsample DICOM images and save them to the output path. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import SimpleITK as sitk | |
| input_directory = os.path.join(os.getcwd(), "data") | |
| for filename in os.listdir(input_directory): | |
| if filename.endswith(".nii.gz"): | |
| image = sitk.ReadImage(os.path.join(input_directory, filename)) | |
| # properties |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Union, OrderedDict | |
| import SimpleITK as sitk | |
| import numpy as np | |
| def one_hot_encode( | |
| input_mask: Union[str, sitk.Image], | |
| encoding_logic: dict = {"NET": [1], "TC": [1, 4], "WT": [1, 2, 4]}, | |
| ) -> dict: | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SimpleITK as sitk | |
| # read image to copy information from | |
| image_reference = sitk.ReadImage('/path/to/reference.nii.gz') | |
| image_to_copy_from = sitk.ReadImage('/path/to/image_to_change.nii.gz') | |
| image_to_copy_from.CopyInformation(image_reference) | |
| # write the image | |
| sitk.WriteImage(image_to_copy_from, '/path/to/result.nii.gz') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SimpleITK as sitk | |
| import numpy as np | |
| def sanity_checker_with_files( | |
| image_file_baseline: str, image_file_compare: str, threshold: float = 1e-5 | |
| ) -> bool: | |
| """ | |
| This function performs sanity check on 2 image files WITHOUT loading images into memory. | |
| Supported image formats: https://simpleitk.readthedocs.io/en/master/IO.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Ref: https://github.com/razorx89/pydicom-seg#getting-started | |
| import pydicom | |
| import pydicom_seg | |
| import SimpleITK as sitk | |
| dcm_path = pydicom.dcmread('/path/to/segmentation.dcm') | |
| reader = pydicom_seg.MultiClassReader() | |
| result = reader.read(dcm_path) |
NewerOlder