Skip to content

Instantly share code, notes, and snippets.

@elistevens
elistevens / pathfinding.py
Last active December 21, 2021 06:42
Pathfinding using A8-like algo, but attempts to minimize damage within a travel budget
class Path:
_offset_to_distance: dict[tuple[int, int], float] = {
(0, 0): 0,
(0, 1): 1,
(0, -1): 1,
(1, 0): 1,
(1, 1): 2 ** 0.5,
(1, -1): 2 ** 0.5,
(-1, 0): 1,
(-1, 1): 2 ** 0.5,
@elistevens
elistevens / augment.py
Created April 6, 2019 16:11
3D data augmentation from Deep Learning with PyTorch (untested)
def getCtAugmentedNodule(augmentation_dict, series_uid, center_xyz, width_mm, voxels_int, maxWidth_mm=32.0, use_cache=True):
assert width_mm <= maxWidth_mm
if use_cache:
cubic_chunk, center_irc = getCtCubicChunk(series_uid, center_xyz, maxWidth_mm)
else:
ct = getCt(series_uid)
ct_chunk, center_irc = ct.getCubicInputChunk(center_xyz, maxWidth_mm)
slice_list = []
@elistevens
elistevens / test_affine.py
Created June 10, 2018 21:24
Tests for volumetric affine grid generation in pytorch
import math
import random
import numpy as np
import scipy.ndimage
import torch
import pytest
@elistevens
elistevens / affine.py
Created May 12, 2018 05:39
affine grid for volumetric arrays
import math
import random
import warnings
import numpy as np
import scipy.ndimage
import torch
from torch.autograd import Function
from torch.autograd.function import once_differentiable
from __future__ import print_function
import torch
from torch.nn.parallel.data_parallel import DataParallel
from torchvision.models import resnet18
from torch.autograd import Variable
model_list = [DataParallel(resnet18(), output_device=(i % torch.cuda.device_count())).cuda() for i in range(20)]
@elistevens
elistevens / xdice.py
Created August 5, 2014 21:08
Scratch code that calculates probabilities of dice rolls for the FFG X-Wing minis game.
import itertools
def attack(n, rerolls=0, focus=False, r='', p=1.0):
s = 'CHHHffxx'
focus = 'h' if focus else None
result_dict = dice(s, n, r, p)
result_dict = reroll(result_dict, s, rerolls, focus)
#print 'attack', n, rerolls, focus, repr(r), p, result_dict
result_dict = usefocus(result_dict, focus)
--==538e7a179d2544ed8eddbce0d3c5eba3==
Content-Disposition: form-data; name="_doc"
Content-Length: 297
Content-MD5: p16qTKDlTHqJ3YTeGy8s3w==
Content-Type: application/json
{"int": "couchable:pickle:self.int", "couchable:": {"src_md5": "40ab314f74581b4d1ee79790703715c2", "class": "Simple", "module": "couchable.testing.test_couchable"}, "_id": "couchable.testing.test_couchable.Simple:45dc8c54-8743-11e0-85af-c42c033c1b93", "_rev": "1-f3d8e3363e3cb4ead433e3cbf855adb9"}
--==538e7a179d2544ed8eddbce0d3c5eba3==
Content-Disposition: form-data; name="attachments"; filename="pickles"
Content-Length: 54