Skip to content

Instantly share code, notes, and snippets.

@jpeoples
jpeoples / extract_toplevel_bookmarks.py
Last active October 20, 2023 21:58
Extract all toplevel bookmarks in a pdf (preserving page numbers, links, and further nested bookmarks)
import fitz # PyMuPDF
import re
import sys
import argparse
import os
def chapter_titles_and_page_ranges(pdf_document: fitz.Document):
# Get the outline information (top-level bookmarks)
toc = pdf_document.get_toc(simple=False)
labels = pdf_document.get_page_labels()
@jpeoples
jpeoples / sitk_dice.py
Created September 27, 2023 17:03
dice score sitk for TCIA data set
import SimpleITK as sitk
import numpy
def load_ct(p):
"""Load CT from path p"""
return sitk.ReadImage(p)
def load_segment(p, ct, bgval=0):
"""Load mask from path p and ensure same size of underlying array as CT image ct"""
m = sitk.ReadImage(p) != bgval
@jpeoples
jpeoples / xnat_DICOM_data.md
Last active September 8, 2023 19:52
Accessing Dates and DICOM headers on XNAT
@jpeoples
jpeoples / window_level.py
Last active August 1, 2023 14:06
Applying windowing to SimpleITK images.
import SimpleITK as sitk
def wl_to_range(wl):
"""Convert window, level, to range"""
w, l = wl
lower, upper = l-w/2, l+w/2
return lower, upper
def remap_range(img, range):
"""Remap image intensities so that the range is transformed to [0,1]"""
@jpeoples
jpeoples / Get Connected Components of 3D Mask with SimpleITK.md
Last active July 26, 2023 19:44
Getting the connected components from a 3D segmentation mask with SimpleITK

The SimpleITK library has an easy to use class for computing connected components in a segmentation mask: sitk.ConnectedComponentImageFilter.

Here is a sample usage (here the “mask” argument would be a binary mask (0 = background, 1 = foreground)

def get_components(mask):
    f = sitk.ConnectedComponentImageFilter()
 labeled = f.Execute(mask)
@jpeoples
jpeoples / boxplot_statistics.m
Created September 24, 2019 23:26
Compute the statistics used in matlab boxplots.
function [q1,q2,q3,w0,w1,outliers] = boxplot_statistics(data, whisker)
if ~exist('whisker', 'var')
% whisker is 1.5 by default
whisker = 1.5;
end
% quantile(data,3) will return the 25th, 50th, and 75th percentile
% for each column
quants = quantile(data, 3);
q1 = quants(1,:);
@jpeoples
jpeoples / bf.c
Created November 5, 2017 02:42
A brainfuck interpreter in C with dynamic data array
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum error_codes {
SUCCESS = 0,
ALLOCATION_ERROR,
INVALID_DATA_POINTER_ERROR,
STREAM_OUT_ERROR,