Skip to content

Instantly share code, notes, and snippets.

View ericspod's full-sized avatar

Eric Kerfoot ericspod

View GitHub Profile
@ericspod
ericspod / clean_code.md
Created April 20, 2023 11:44 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules

@ericspod
ericspod / format_json.py
Last active June 8, 2022 14:52 — forked from jannismain/CompactJSONEncoder.py
A JSON Encoder in Python, that puts small lists on single lines.
import os
import sys
import json
import datetime
from typing import Union
# from https://gist.github.com/jannismain/e96666ca4f059c3e5bc28abb711b5c92
class CompactJSONEncoder(json.JSONEncoder):
@ericspod
ericspod / attachable_test.py
Created April 26, 2020 21:37
Attachable Test
from ignite.engine import Events, Engine
class Attachable:
"""
This class contains an internal dictionary `attach_map` associating events with the bound methods to be triggered
on those events. This dictionary can be populated by the contructor or by external clients after construction and
before `attach` is called.
"""
def __init__(self, attach_map):
self.attach_map = dict(attach_map)
@ericspod
ericspod / baseengine.py
Last active April 11, 2021 23:29
Base Engine, Trainer, and Evaluator
import torch
import warnings
import threading
import numpy as np
from ignite.engine.engine import Engine, Events
def ensure_tuple(vals):
"""
Returns a tuple containing just `vals` if it is not a list or tuple, or `vals` converted to a tuple otherwise.
@ericspod
ericspod / matrixops.py
Created July 29, 2019 15:56
Matrix operations not needed
def matZero(n,m):
'''Return a list of lists with the given dimensions containing zeros.'''
return [[0]*m for i in range(n)]
def matIdent(n):
'''Return a list of lists defining the identity matrix of rank `n'.'''
mat=matZero(n,n)
for nn in range(n):
mat[nn][nn]=1.0
@ericspod
ericspod / decimatetets.py
Created August 10, 2018 15:40
Decimate Tets with VTK
import vtk
import os,sys,math
# read unstructured grid
r=vtk.vtkXMLUnstructuredGridReader()
r.SetFileName(sys.argv[1]) # first command line argument after script name
r.Update()
orig=r.GetOutput()
@ericspod
ericspod / pcrtest.py
Created May 16, 2018 19:22
Piecewise CR Test
from eidolon import *
# The Catmull-Rom basis types assume a fixed set of control points: 4 for 1D, 4**2 for 2D, and 4**3 for 3D.
# The piecewise Catmull-Rom allows a definition with an arbitrarily sized lattice of control points which is
# then divided up into individual elements which share control points of neighbours. The line example in the
# wiki shows an element composed of 3 lines. To allow the configuration of the element type, the basis function
# which returns coefficients for piecewise Catmull-Rom accepts extra parameters in addition to the xi coordinates
# (u,v,w): `ul', `vl', `wl' stating the dimensions of the control point lattice, `limits' stating for each dimension if
@ericspod
ericspod / cropZT.py
Last active January 16, 2018 12:55
Cropping an Image in Z and T dimensions
# Copy this function into the Eidolon console then you can call it as described below.
def cropImage(img,minz,maxz,mint,maxt):
'''Crop image `img' in the Z (minz to maxz) and T dimensions (mint to maxt).'''
arr=img.plugin.getImageObjectArray(img) # break the image down into spatial components and numpy array
arr['array']=arr['array'][:,:,minz:maxz,mint:maxt] # crop the numpy array
arr['pos']=img.images[img.getVolumeStacks()[0][minz]].position # set the position to the bottom of the new stack
newimg=img.plugin.createObjectFromArray(img.getName()+'Crop',**arr) # create new image object
mgr.addSceneObject(newimg) # add image to scene
return newimg # return image