Skip to content

Instantly share code, notes, and snippets.

Avatar
🙃

Mike Clark wassname

🙃
View GitHub Profile
View gist:2a9bd840771e23897dc6b6105793edd6
# load mask
maskp = '../data/raw/NBIA/ProstateX/PROSTATEx_masks/Files/lesions/Masks/For_dcm2niix_files/ADC/\
ProstateX-0142-Finding3-ep2d_diff_tra_DYNDIST_ADC0_ROI.nii.gz'
y = nib.load(ypath).get_fdata()
# 1. reverse what nii2dcm did
y = y.transpose((1, 0, 2))[::-1, :, ::-1]
# load dicom
@wassname
wassname / pdshow.py
Last active Sep 22, 2021
show a pandas data frame in full
View pdshow.py
from IPython.display import display
import pandas as pd
def pdshow(df):
"""
This shows a pandas dataframe in full/
Also consider .to_html() and https://pbpython.com/dataframe-gui-overview.html
View pandas_cache.py
"""
Implements on disk caching of transformed dataframes
Used on a function that returns a single pandas object,
this decorator will execute the function, cache the dataframe as a pickle
file using the hash of function and subdirectory, and the arguments and filename.
The next time the function runs, if the hashes match what is on disk, the decoratored function will simply load and return
the pickled pandas object.
This can result in speedups of 10 to 100 times, or more, depending on the
complexity of the function that creates the dataframe.
@wassname
wassname / dcm2df.py
Last active Jun 9, 2021
dicoms2df.py
View dcm2df.py
"""
read dicom header and cache
url https://gist.github.com/wassname/a2cdf0b9b511f8a4769cbbe040a87900
"""
from diskcache import Cache, JSONDisk
import pandas as pd
from tqdm.contrib.concurrent import thread_map, process_map
import logging
from pathlib import Path
from functools import partial
View split_by_unique_col.py
from sklearn.model_selection import train_test_split
import pandas as pd
def shuffle_df(df, random_seed=42):
return df.sample(frac=1, random_state=random_seed, replace=False)
def split_by_unique_col(df, col='patient_id', stratify_cols=[], random_seed=42):
"""
Make a dataframe of unique ids, with our stratification data
@wassname
wassname / dicom_over_http.py
Last active Apr 10, 2021
how to read only metadata from a dicom url
View dicom_over_http.py
"""
how to read only metadata from a dicom url to save bandwith
- note the server must support HTTP Range, e.g. s3 buckets or azure blobs.
- note that if you don't mind reading the whole thing, it's easier to just read the whole thing, then pass it into pydicom as io.BytesIO
url: https://gist.github.com/wassname/70106b2d66a7c6e83e4b0300c9d1d4d3
"""
View rich_tqdm.py
from rich.progress import (
ProgressColumn,
BarColumn,
DownloadColumn,
TextColumn,
TransferSpeedColumn,
TimeRemainingColumn,
Progress,
TaskID,
TimeElapsedColumn,
@wassname
wassname / leftKNN.py
Last active Jan 15, 2021
Causal k nearest neighbors (KNN) that only looks back
View leftKNN.py
# %%
import numpy as np
from functools import partial
from pykdtree.kdtree import KDTree
class LeftKDTree(KDTree):
"""
KNN that only looks left.
@wassname
wassname / stablenormal.py
Created Nov 1, 2020
pytorch stable normal using log_scale
View stablenormal.py
import math
import torch
from torch.distributions import Normal
from torch.distributions.utils import broadcast_all, _standard_normal
from torch.distributions.kl import register_kl
class StableNormal(Normal):
"""Modified version that uses log_scale for stability of grad."""
def __init__(self, loc, log_scale):
View exercise.md

Exercise

Description:

  1. first
  2. second