Skip to content

Instantly share code, notes, and snippets.

View cshimmin's full-sized avatar

Chase cshimmin

  • Yale University
  • New Haven, CT
View GitHub Profile
import numpy as np
import scipy.optimize
import scipy.optimize
def norm_or_zero(v):
# normalize vectors along the last axis, avoiding div0 for zero vectors.
denom = np.linalg.norm(v, axis=-1, keepdims=True)
return np.where(denom > 0, v / denom, 0)
na = np.loadtxt('../nanoamps')
_,bins,_ = plt.hist(na, bins=40, color='gray', alpha=0.2, density=False);
plt.hist(na.reshape(4,-1).T, bins=bins, histtype='step', density=False);
na_ = na.reshape(4,-1)
plt.hist(np.mean(na_, axis=0), histtype='step', bins=30);
_,bins,_ = plt.hist(na, bins=40, color='gray', alpha=0.2, density=False);
plt.hist(np.std(na_, axis=0, ddof=1), bins=bins);
np.argmax(np.std(na_,axis=0,ddof=1))
@cshimmin
cshimmin / masked_mse.py
Created February 6, 2020 19:54
pseudo-keras code for masked MSE loss
encoder_input = layers.Input( input_shape )
# ... define encoder sub-network
encoder = keras.models.Model(encoder_input, ...)
decoder_input = layers.Input( latent_shape )
# ... define decoder sub-network
@cshimmin
cshimmin / ecf.py
Last active July 15, 2019 17:09
calculate ECFs on jet constituents using einsum
import itertools as it
import numpy as np
# calculates ECF for a batch of jet constituents.
# x should have a shape like [batch_axis, particle_axis, 3]
# the last axis should contain (pT, eta, phi)
def ecf_numpy(N, beta, x):
pt = x[:,:,0]
eta = x[:,:,1:2]
phi = x[:,:,2:]
def some_mode():
auto_input = K.Input((whatever,))
auto_z = encoder(auto_input)
auto_output = decoder(auto_z)
model = Model(input_layer, output_layer)
loss1_ = K.mean(K.square(auto_input - auto_output))
loss2_ = K.mean(K.square(auto_z))
model.hp_loss2_weight = K.variable(1.0, name='loss2_weight')
@cshimmin
cshimmin / rotation2d.py
Created June 13, 2019 21:17
keras layer to rotate a (tensor ending with) 2d vector
class RotationLayer(layers.Layer):
def __init__(self, theta, learning_phase_only=True, **kwargs):
self.theta = theta
self.vec_dim = dim
self.R = K.constant([[1,0],[0,1]])*tf.cos(theta) \
+ K.constant([[0,-1],[1,0]])*tf.sin(theta)
self.uses_learning_phase = learning_phase_only
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/usr/bin/env python
import ROOT as r
import numpy as np
class FatJet:
def __init__(self, tree, idx):
# make an object representing the jet at index `idx` by pulling
# interesting attributes out of the tree.
self.m = tree.fjet_m[idx]
@cshimmin
cshimmin / hough.py
Created June 23, 2016 15:30
sketch of hough transform based track finding/fitting
#!/usr/bin/env python
import ROOT as r
import sys
import itertools as it
import numpy as np
import pylab as pl
from scipy.special import cotdg
from scipy.sparse import dok_matrix
def hough_radial(points, radius=5):