Skip to content

Instantly share code, notes, and snippets.

@alisterburt
alisterburt / barrett.py
Created September 18, 2024 22:08
R5 parsing for Barrett
import ast
import einops
import numpy as np
import starfile
from scipy.spatial.transform import Rotation as R
DO_VIS = True
particle_star = starfile.read('matching.star')
import numpy as np
import h3
import einops
def get_h3_grid_at_resolution(resolution: int) -> list[str]:
"""Get h3 cells (their h3 index) at a given resolution.
Each cell appears once
- resolution 0: 122 cells, every ~20 degrees
- resolution 1: 842 cells, every ~7.5 degrees
@alisterburt
alisterburt / warp_tm_vis.py
Created August 2, 2024 11:00
warp template matching visualisation
from enum import Enum
from pathlib import Path
import starfile
import mrcfile
import napari
import numpy as np
from magicgui import magicgui, widgets
RECONSTRUCTION_DIR = '/Users/burta2/Downloads/10499/reconstruction'
from pathlib import Path
from typing import Sequence
import dask
import mrcfile
import numpy as np
import torch
import pandas as pd
import dask.array as da
@alisterburt
alisterburt / 3d_correlation_from_2d_correlations.py
Created June 13, 2024 00:20
3D correlations from multiple 2D correlations for Will Wan
import einops
import numpy as np
import torch
import mrcfile
import napari
from torch_fourier_slice import project_3d_to_2d
from torch_image_lerp import sample_image_2d
from torch_grid_utils import coordinate_grid
from scipy.spatial.transform import Rotation as R
@alisterburt
alisterburt / aysecan.py
Created June 11, 2024 21:50
constructing rotation matrices and generating RELION compatible euler angles from them
import numpy as np
from scipy.spatial.transform import Rotation as R
# set up rotation matrices for rotations around X axis
rotation_angles = np.linspace(0, 90, 50)
Rx = R.from_euler('x', angles=rotation_angles, degrees=True).as_matrix()
print(Rx.shape)
# set up column vector which points along y
z_vec = np.array([0, 0, 1]).reshape((3, 1))
https://docs.google.com/presentation/d/1QD6J1SR2_Q_7DivvtIE_P1ScrFGcU9yk-46ZmVavmf0/edit#slide=id.g27333d73966_0_16
@alisterburt
alisterburt / eric_bogdan_thing.py
Created May 17, 2024 11:06
Interpret/visualise composition of prior and pose in RELION
import numpy as np
import starfile
import napari
from scipy.spatial.transform import Rotation as R
df = starfile.read('particles_warp.star')
xyz = df[['rlnCoordinateX', 'rlnCoordinateY', 'rlnCoordinateZ']].to_numpy()
eulers = df[['rlnAngleTiltPrior', 'rlnAnglePsiPrior']].to_numpy()
rotation_matrices = R.from_euler(seq='YZ', degrees=True, angles=eulers).inv().as_matrix()
@alisterburt
alisterburt / mpl_gradient_overlay.py
Last active May 7, 2024 17:24
matplotlib gradient overlays - gradient resolution is different from image resolution
import matplotlib.pyplot as plt
import numpy as np
from cmap import Colormap
h, w = 512, 868
image = np.random.random(size=(h, w))
defocus = np.array([[0, 1],
[1, 0]])
@alisterburt
alisterburt / josh.py
Last active March 13, 2024 01:05
recentering/reorienting RELION particles for Josh Hutchings
"""Recenter and reorient RELION particles
This shows the gist of how to manually move/reorient RELION particles in 3D.
You define a set of shifts and orientations in the coordinate system of the
reference for all 'subparticles' you want to generate from each existing particle.
"""
import pandas as pd
import starfile
import numpy as np
from scipy.spatial.transform import Rotation as R