Skip to content

Instantly share code, notes, and snippets.

View william-silversmith's full-sized avatar

William Silversmith william-silversmith

View GitHub Profile
@william-silversmith
william-silversmith / resin_mask.py
Last active July 17, 2025 03:27
Visualize a TEM section with each subtile marked as resin or not resin.
"""
Extracts tissue from an overview montage and calculates the centroid.
python resin_mask.py .../bladeseq-2025.02.03-11.41.01/s021-2025.02.03-11.41.01/
To install dependencies:
pip install simplejpeg tqdm pyspng-seunglab numpy scipy connected-components-3d fill_voids tinybrain edt opencv-python -U
"""
from typing import List, Dict, Union, Optional, Tuple
@william-silversmith
william-silversmith / detect_bbox_collision.py
Last active July 16, 2025 20:15
Check if a CAVE skeleton passes through a bbox.
# pip install caveclient osteoid cloud-volume vtk microviewer numpy
from osteoid import Skeleton, Bbox
from caveclient import CAVEclient
from cloudvolume import CloudVolume
import numpy as np
# import microviewer
import time
import sys
@william-silversmith
william-silversmith / cube_figure.py
Last active May 28, 2025 23:10
Create a 3D image cube suitable for building a figure in a presentation or paper.
"""
Draws a 3D cube from a 3D image.
Can be posed in any orientation.
To install:
pip install numpy cloud-volume mayavi fastremap
pip install PyQt5
"""
@william-silversmith
william-silversmith / tissue_finder.py
Last active December 4, 2024 20:15
TEM montage overview ROI centroid finder
"""
Extracts tissue from an overview montage and calculates the centroid.
To install dependencies:
pip install simplejpeg pyspng-seunglab numpy scipy connected-components-3d tinybrain edt -U
"""
from typing import List, Dict, Union, Optional, Tuple
import csv
import io
@william-silversmith
william-silversmith / FAFB_data.py
Created September 11, 2019 00:25
Accessing FlyWire Proofread Reconstructions with CloudVolume
from cloudvolume import CloudVolume
cv = CloudVolume(
'graphene://https://fafbv2.dynamicannotationframework.com/segmentation/1.0/fly_v31', # neuroglancer layer
use_https=True # uses the public access interface
)
# Download and Save a Mesh to Disk
mesh = cv.mesh.get(720575940621065107)
with open('720575940621065107.ply', 'wb') as f:
@william-silversmith
william-silversmith / contrast_correct.py
Created January 31, 2019 02:27
Contrast correct an hdf5 file.
import numpy as np
import h5py
from tqdm import tqdm
from cloudvolume import view
def find_section_clamping_values(zlevel, lowerfract, upperfract):
filtered = np.copy(zlevel)
# remove pure black from frequency counts as
@william-silversmith
william-silversmith / countless_stippled.py
Last active July 10, 2018 19:18
COUNTLESS variant that treats zero as "background" that doesn't count for the purposes of choosing the mode.
def countless_stippled(data):
"""
Vectorized implementation of downsampling a 2D
image by 2 on each side using the COUNTLESS algorithm
that treats zero as "background" that doesn't count
for the purposes of choosing the mode.
data is a 2D numpy array with even dimensions.
"""
sections = []
@william-silversmith
william-silversmith / countlessND.py
Last active January 31, 2018 06:21
Fully generalized COUNTLESS algorithm with a (relatively) memory efficient dynamic programming solution
from itertools import combinations
from functools import reduce
import numpy as np
def countlessND(data, factor):
assert len(data.shape) == len(factor)
sections = []
mode_of = reduce(lambda x,y: x * y, factor)
@william-silversmith
william-silversmith / dynamic_zero_corrected_countless3d.py
Last active February 18, 2018 04:06
COUNTLESS3D + Zero Correction + Dynamic Programming
from itertools import combinations
from functools import reduce
import numpy as np
def dynamic_zero_corrected_countless3d(data):
sections = []
# shift zeros up one so they don't interfere with bitwise operators
# we'll shift down at the end
data += 1
@william-silversmith
william-silversmith / zero_corrected_countless3d.py
Last active January 31, 2018 04:53
COUNTLESS8 + Avoiding trouble with 0
from itertools import combinations
from functools import reduce
import numpy as np
def countless3d(data):
"""Now write countless8 in such a way that it could be used
to process an image."""
sections = []
# shift zeros up one so they don't interfere with bitwise operators