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 / 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
@william-silversmith
william-silversmith / countless8.py
Last active January 31, 2018 04:52
Basic implementation of COUNTLESS8
from itertools import combinations
from functools import reduce
def countless8(a,b,c,d,e,f,g,h):
"""Extend countless5 to countless8. Same deal, except we also
need to check for matches of length 4."""
sections = [ a, b, c, d, e, f, g, h ]
# PICKN = PICK with N arguments
p2 = lambda q,r: q * (q == r) # PICK2
@william-silversmith
william-silversmith / countless5.py
Last active January 31, 2018 04:51
The first generalization of COUNTLESS 2D.
from itertools import combinations
from functools import reduce
def countless5(a,b,c,d,e):
"""First stage of generalizing from countless2d.
You have five slots: A, B, C, D, E
You can decide if something is the winner by first checking for
matches of three, then matches of two, then picking just one if
@william-silversmith
william-silversmith / borda.py
Last active April 27, 2021 06:29
Movie Club Borda Count Voting
import csv
import os
import sys
from collections import defaultdict
rows = []
filename = sys.argv[1]
_, ext = os.path.splitext(filename)
@william-silversmith
william-silversmith / irv.py
Created December 14, 2017 02:43
Movie Club Instant Runoff Voting
import csv
from collections import defaultdict
import copy
import random
def tabulate(voters, round=1):
votes = defaultdict(int)
for voter in voters:
if len(voter) == 0: # abstention