Skip to content

Commit

Permalink
tutorial update
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Apr 21, 2023
1 parent 4b93374 commit d24f2ae
Show file tree
Hide file tree
Showing 9 changed files with 1,559 additions and 4 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will install Python dependencies, lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

# pylint: disable=<warning>

name: Python application

on:
push:
branches: [ main,mammo,optho ]
pull_request:
branches: [ main,mammo,optho ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install toml==0.10.2 yapf==0.31.0 isort==5.9.3 pylint==2.11.0
- name: Lint with pylint, format with yapf and isort
run: |
pylint * --verbose -r y
isort . --check-only --diff
yapf --recursive --verbose --in-place .
129 changes: 129 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Rajpurkar Lab / Harvard Medical AI
Copyright (c) 2021 Alex Tamkin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion conf/dataset/HAM10000.yaml → conf/dataset/ham10000.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
loss: cross_entropy
metric: auroc

name: HAM10000
name: ham10000
batch_size: 64
num_workers: 8
1,192 changes: 1,192 additions & 0 deletions derm-tutorial.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ h5py
pylidc
bs4
mne
SimpleITK
4 changes: 2 additions & 2 deletions src/datasets/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
'isic2019': isic2019.ISIC2019,
'isic2020': isic2020.ISIC2020,
'ddi': ddi.ddi,
'HAM10000': HAM10000.HAM10000,
'ham10000': HAM10000.HAM10000,
'pad_ufes_20': pad_ufes_20.pad_ufes_20,

# ECG
Expand Down Expand Up @@ -63,7 +63,7 @@
'mimic-cxr',
#dermatology.
'ddi',
'HAM10000',
'ham10000',
'isic2020',
'pad_ufes_20',
'isic2019',
Expand Down
93 changes: 93 additions & 0 deletions src/datasets/ct/lndb_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import csv

import numpy as np
import SimpleITK as sitk
from scipy.ndimage import zoom


def readCsv(csvfname):
# read csv to list of lists
with open(csvfname, 'r') as csvf:
reader = csv.reader(csvf)
csvlines = list(reader)
return csvlines


def writeCsv(csfname, rows):
# write csv from list of lists
with open(csfname, 'w', newline='') as csvf:
filewriter = csv.writer(csvf)
filewriter.writerows(rows)


def readMhd(filename):
# read mhd/raw image
itkimage = sitk.ReadImage(filename)
scan = sitk.GetArrayFromImage(itkimage) #3D image
# print(scan.shape)

spacing = itkimage.GetSpacing() #voxelsize
origin = itkimage.GetOrigin() #world coordinates of origin
transfmat = itkimage.GetDirection() #3D rotation matrix
# print(dir(itkimage))
return scan, spacing, origin, transfmat


def writeMhd(filename, scan, spacing, origin, transfmat):
# write mhd/raw image
itkim = sitk.GetImageFromArray(scan, isVector=False) #3D image
itkim.SetSpacing(spacing) #voxelsize
itkim.SetOrigin(origin) #world coordinates of origin
itkim.SetDirection(transfmat) #3D rotation matrix
sitk.WriteImage(itkim, filename, False)


def getImgWorldTransfMats(spacing, transfmat):
# calc image to world to image transformation matrixes
transfmat = np.array([transfmat[0:3], transfmat[3:6], transfmat[6:9]])
for d in range(3):
transfmat[0:3, d] = transfmat[0:3, d] * spacing[d]
transfmat_toworld = transfmat #image to world coordinates conversion matrix
transfmat_toimg = np.linalg.inv(transfmat) #world to image coordinates conversion matrix

return transfmat_toimg, transfmat_toworld


def convertToImgCoord(xyz, origin, transfmat_toimg):
# convert world to image coordinates
xyz = xyz - origin
xyz = np.round(np.matmul(transfmat_toimg, xyz))
return xyz


def convertToWorldCoord(xyz, origin, transfmat_toworld):
# convert image to world coordinates
xyz = np.matmul(transfmat_toworld, xyz)
xyz = xyz + origin
return xyz


def extractCube(scan, spacing, xyz, cube_size=80, cube_size_mm=51):
# Extract cube of cube_size^3 voxels and world dimensions of cube_size_mm^3 mm from scan at image coordinates xyz
xyz = np.array([xyz[i] for i in [2, 1, 0]], np.int)
spacing = np.array([spacing[i] for i in [2, 1, 0]])
scan_halfcube_size = np.array(cube_size_mm / spacing / 2, np.int)
if np.any(xyz < scan_halfcube_size) or np.any(xyz + scan_halfcube_size > scan.shape): # check if padding is necessary
maxsize = max(scan_halfcube_size)
scan = np.pad(
scan, ((
maxsize,
maxsize,
)), 'constant', constant_values=0
)
xyz = xyz + maxsize

scancube = scan[xyz[0] - scan_halfcube_size[0]:xyz[0] + scan_halfcube_size[0], # extract cube from scan at xyz
xyz[1] - scan_halfcube_size[1]:xyz[1] + scan_halfcube_size[1],
xyz[2] - scan_halfcube_size[2]:xyz[2] + scan_halfcube_size[2]]

sh = scancube.shape

scancube = zoom(scancube, (cube_size / sh[0], cube_size / sh[1], cube_size / sh[2]), order=2) #resample for cube_size

return scancube
Loading

0 comments on commit d24f2ae

Please sign in to comment.