Skip to content

Instantly share code, notes, and snippets.

View dermen's full-sized avatar
🤖
*_*

Derek Mendez dermen

🤖
*_*
View GitHub Profile
@dermen
dermen / quick_nanobragg.py
Last active September 17, 2025 21:04
simulate two orientations of the same crystal with nanoBragg+simtbx
import numpy as np
from scipy.spatial.transform import Rotation
import pylab as plt
from dxtbx.model import Crystal
from dxtbx.model.beam import BeamFactory
from scitbx.matrix import sqr
from simtbx import nanoBragg
from simtbx.nanoBragg.sim_data import Amatrix_dials2nanoBragg, SimData
from scipy import constants
import psana
import os
os.environ['PS_SRV_NODES']='1'
exp="mfx100861624"
run=14
tag="test3"
@dermen
dermen / test_parallel_mpi.py
Created May 10, 2025 16:49
simple test for parallel hdf5 + mpi4py and h5py
from mpi4py import MPI
import h5py
import numpy as np
COMM = MPI.COMM_WORLD
nimg = COMM.size
with h5py.File("test.h5", "w", driver="mpio", comm=COMM) as h:
dset = h.create_dataset("data", shape=(nimg,512,512), dtype=np.float32)
for i in range(nimg):
# After running dials.stills_process, convert the integrated.refls to an rs dataframe, and dump as an MTZ
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("dirname", type=str, help="diffBragg.stills_process output folder with integrated.refls")
parser.add_argument("mtz", type=str, help="output mtz name")
parser.add_argument("--max", default=2500, type=int, help="max number of refls in a shot (default 2500)")
parser.add_argument("--ucell", default=None, nargs=6, type=float, help="unit cell params (default will be average experiment crystal)")
parser.add_argument("--symbol",type=str, default=None)
parser.add_argument("--batchFromFilename", action="store_true", help="get the batch ID from the filenames assuming it ends in the standard e.g. #####.cbf or #####.mccd ")
import argparse
par = argparse.ArgumentParser()
par.add_argument("input", help="input mtz from carelsss (*xval*.mtz)", type=str)
par.add_argument("output", default=None, help="output ASCII shelx file (FORMAT 3I4,2F8.2 for h,k,l,F,SigF).", type=str)
par.add_argument("--scale", type=float, default=1000, help="scale the F,SigF from Careless by this factor (needed for when careless SigFs are too small for F8.2 format)")
args = par.parse_args()
import numpy as np
import reciprocalspaceship as rs
@dermen
dermen / sim_laue_basic.py
Last active September 19, 2024 19:18
generates laue CBF files
"""
Generate pseudo laue stills, saves as CBFs
Unless --noWaveImg flag is passed, an HDF5 file will be written containing the per-pixel wavelengths
Need to get 3 files first:
wget https://raw.githubusercontent.com/dermen/e080_laue/master/air.stol
wget https://raw.githubusercontent.com/dermen/e080_laue/master/from_vukica.lam
iotbx.fetch_pdb 7lvc
"""
#@profile
# Compares the cost of renting vs owning a home
# Example usage: python own_vs_rent.py --price 3400000 --extra 500 --apr 0.0675 --nyears 10 --down 0.2 --growth 0.02
def main(args):
PRICE = args.price #350000 # home value (sales price)
TAX_RATE = args.tax # annual tax rate (estimate for Pflugerville, Texas)
DOWNPAYMENT = PRICE*args.down # downpayment amount
LOAN = PRICE-DOWNPAYMENT # loan amount
YEARS = args.term # loan term (years)
@dermen
dermen / sim_dxtbx_and_pdb.py
Created February 1, 2024 17:29
simulate a PDB file using simtbx
import os
from dxtbx.model.beam import BeamFactory
from dxtbx.model.crystal import CrystalFactory
from dxtbx.model.detector import DetectorFactory, Detector, Panel
from simtbx.diffBragg import utils as db_utils
from simtbx.modeling import forward_models
from simtbx.nanoBragg import utils as nb_utils
# simulate this PDB (will be downloaded to current folder if doesnt exist
pdb_file = "4bs7.pdb"
@dermen
dermen / sim_sym.py
Last active October 31, 2023 14:19
test diffraction pattern symmetry
import os
import sys
from scipy.spatial.transform import Rotation
import numpy as np
from iotbx import pdb as iotbx_pdb
from simtbx.nanoBragg import nanoBragg
from scitbx.matrix import sqr
from dxtbx.model import Crystal
@dermen
dermen / weather.py
Created September 5, 2021 20:16
weather plot example
from pylab import *
def get_color(r,g,b):
return r/255.,g/255.,b/255.
def get_patches(tvals_start, tvals_stop, rank, tcolors):
patches = []
for i_t, (t1,t2) in enumerate(zip(tvals_start, tvals_stop)):
xy = np.array([(t1,rank-0.5), (t1, rank+0.5), (t2, rank+0.5), (t2, rank-0.5)])
patch = mpl.patches.Polygon(xy=xy, color=tcolors[i_t], closed=True) #, ec='k', lw=0.5)