Skip to content

Instantly share code, notes, and snippets.

View jmmshn's full-sized avatar

Jimmy Shen jmmshn

  • LLNL
  • Livermore
View GitHub Profile
@jmmshn
jmmshn / con_elec_from_pd.py
Last active October 15, 2022 01:07
ConversionElectrodes from PhaseDiagram
from pymatgen.ext.matproj import MPRester
from pymatgen.analysis.phase_diagram import PhaseDiagram, Element, Composition
from pymatgen.apps.battery.conversion_battery import ConversionElectrode
from collections import defaultdict
mpr = MPRester()
ents = mpr.get_entries_in_chemsys("Fe-Li-O")
pd = PhaseDiagram(ents)
def get_conversion_electrodes(pd: PhaseDiagram, working_ion_symbol: str):
"""Compute all of the conversion electrodes for a given phase diagram."""
@jmmshn
jmmshn / astronaut_eye.py
Created October 10, 2022 07:30
Demonstrate Fourier interpolation around the eye
from skimage import data
from skimage.color import rgb2gray
img = data.astronaut()
img = rgb2gray(img)
a,b,c,d = 90,112,190,220 # crop around the eye
fig, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(20,20))
ax1.axis("off")
from matplotlib import pyplot as plt
def func1(X):
return np.sin(6 * X)
x_coarse = np.linspace(0, np.pi, 10, endpoint=False)
x_fine = np.linspace(0, np.pi, 100, endpoint=False)
y_coarse = func1(x_coarse)
y_fine = interpolate_fourier(y_coarse, [100,])
@jmmshn
jmmshn / fourier_interp.py
Created October 10, 2022 05:57
Fourier Interpolation python
import numpy as np
import numpy.typing as npt
def pad_arr(arr_in: npt.NDArray, shape: list[int]) -> npt.NDArray:
"""Pad a function on a hypercube.
Parameters
----------
arr_in:
Data to be padded with zeros
@jmmshn
jmmshn / connect_jobstore.py
Created May 3, 2022 13:54
[connect to jobstore]
from jobflow.settings import JobflowSettings
settings = JobflowSettings()
store = jstore.docs_store
@jmmshn
jmmshn / update_jobflow_metadata.py
Created April 29, 2022 18:31
[update job metadata] update the metadata of a jobflow job
def update_metadata(flow, metadata_updates: dict):
for job in flow.jobs:
if isinstance(job, Flow):
update_metadata(job, metadata_updates)
else:
job.metadata.update(metadata_updates)
@jmmshn
jmmshn / stackprinter_hearder.py
Created March 4, 2022 16:05
[stackprinter header] load stack printer #python #debugging
import stackprinter
stackprinter.set_excepthook(style='darkbg2') # for jupyter notebooks try style='lightbg'
@jmmshn
jmmshn / unique_random_samples.py
Last active November 5, 2020 04:24
Generate a uniue set of random samples of different sizes
from collections import defaultdict
import random
insert_idx = defaultdict(set)
ll = range(6)
for n in range(1, 7):
for i in range(20):
sites = random.sample(ll, n)
insert_idx[n].add(frozenset(sites))
@jmmshn
jmmshn / connect_via_ssh.py
Last active June 17, 2020 23:41
Connecting to maggma mongostore via through ssh_tunnel
# requires maggma>= 0.19
# Works the same way for MongoStore
# For MongoClient just change host to localhost(127.0.0.1) and port to the one from local bind address
from maggma.stores.advanced_stores import MongograntStore
from sshtunnel import open_tunnel
ms = MongograntStore("ro:mongodb03/db_name", 'collection')
# Start the ssh_tunnel
server = open_tunnel(
("dtn03.nersc.gov", 22),
@jmmshn
jmmshn / get_filtered_pos.py
Created April 15, 2020 22:56
[Get Filtered Positions] Take an array and a filtering function return all the positions and values for which taht function is true
from itertools import product
def get_filtered_pos(arr, func):
"""
Take any ndarray and return the position
"""
d_ranges = [range(_) for _ in arr.shape]
for j in product(*d_ranges):
if func(arr[j]):
yield j, arr[j]