Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: docstrings and overall design for expectation maximization iterative refinement #21

Open
wants to merge 175 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
175 commits
Select commit Hold shift + click to select a range
a2629da
Init
jedyeo Mar 20, 2022
ccc4359
precommit yaml
jedyeo Mar 20, 2022
186fddc
remove ref
jedyeo Mar 20, 2022
62b9f42
Format code with black and isort
deepsource-autofix[bot] Mar 20, 2022
ee5adab
Docstrings, precommit broken?
jedyeo Mar 20, 2022
b027987
Format code with black and isort
deepsource-autofix[bot] Mar 20, 2022
26dcf79
Update docstrings
jedyeo Mar 21, 2022
2110a8a
Format code with black and isort
deepsource-autofix[bot] Mar 21, 2022
f493da6
Dummy tests
Mar 22, 2022
00b8afb
docstrings and tests
jedyeo Mar 22, 2022
387c8ed
Format code with black and isort
deepsource-autofix[bot] Mar 22, 2022
be31e4c
Minor ds change. Fixed tests w/ fixture
jedyeo Mar 22, 2022
bb3b232
Minor ds change. Fixed tests w/ fixture
jedyeo Mar 22, 2022
7d42322
Format code with black and isort
deepsource-autofix[bot] Mar 22, 2022
e3becc4
Minor docstring changes
jedyeo Mar 23, 2022
e12673d
minor ds changes
jedyeo Mar 23, 2022
dd4d35b
ds changes
jedyeo Mar 23, 2022
2429d1b
Change class docstring
jedyeo Mar 24, 2022
e8503c2
Remove todo's breaking deepsource
jedyeo Mar 24, 2022
87a9a87
Format code with black and isort
deepsource-autofix[bot] Mar 24, 2022
65273f0
DeepSource changes
jedyeo Mar 24, 2022
5309ac8
deepsoruce changes
jedyeo Mar 24, 2022
d5ea497
Format code with black and isort
deepsource-autofix[bot] Mar 24, 2022
1a79772
literals removed
jedyeo Mar 24, 2022
c473d43
literals removed
jedyeo Mar 24, 2022
38a9a5e
add pre commit
jedyeo Mar 24, 2022
7a4de6f
Init tests
jedyeo Mar 24, 2022
df6c749
temporary static methods
jedyeo Mar 24, 2022
6849b0c
Format code with black and isort
deepsource-autofix[bot] Mar 24, 2022
4e927ab
temporary static methods
jedyeo Mar 24, 2022
93ca7fc
ds changes
jedyeo Mar 24, 2022
ea2c766
ds changes
jedyeo Mar 24, 2022
714b33d
changed deepsource
jedyeo Mar 24, 2022
5b4db6e
lint
jedyeo Mar 24, 2022
ccd0d04
add black back
jedyeo Mar 24, 2022
c8f3c60
Format code with black
deepsource-autofix[bot] Mar 24, 2022
c2726b2
update toml
jedyeo Mar 24, 2022
7f96273
ds
jedyeo Mar 24, 2022
3d4ad77
added .ini for pytest
jedyeo Mar 24, 2022
b293181
init
jedyeo Mar 24, 2022
02eec0a
Codecov files
Mar 24, 2022
49138c7
Linting files
Mar 24, 2022
6135c72
Dev requirements
Mar 24, 2022
f54ece8
Environment config
Mar 24, 2022
b9b540e
Added setup.py
Mar 24, 2022
afd7290
env yaml
jedyeo Mar 24, 2022
5a4a56e
Reformatted docstrings
Mar 24, 2022
e504e32
Format code with black
deepsource-autofix[bot] Mar 24, 2022
a282ffc
Testing workspace changes
Mar 24, 2022
43a440e
Merge branch 'itr_ref_docstrings' of github.com:compSPI/reconstructSP…
Mar 24, 2022
1a0af09
Fixed test docstrings
Mar 24, 2022
3d1be9b
Format code with black
deepsource-autofix[bot] Mar 24, 2022
5268b73
Edited module name
Mar 24, 2022
3e638af
Merge branch 'itr_ref_docstrings' of github.com:compSPI/reconstructSP…
Mar 24, 2022
984af8a
Linting fixes
Mar 24, 2022
eecb97e
Format code with black
deepsource-autofix[bot] Mar 24, 2022
f7f98b4
Linting fixes
Mar 24, 2022
1a79652
Linting fixes
Mar 24, 2022
d2faa25
Fixed imports
Mar 24, 2022
a3eedd6
Readded simSPI import
Mar 24, 2022
c898048
Fixed function reference
Mar 24, 2022
71356a5
Fixed test_build_ctf_array
Mar 24, 2022
0522a91
tets
jedyeo Mar 24, 2022
ee0ef8f
Format code with black
deepsource-autofix[bot] Mar 24, 2022
7141f37
Reworked tests to use consistent array sizing
Mar 24, 2022
5d0d1a1
Tab snuck in
Mar 24, 2022
7289762
Format code with black
deepsource-autofix[bot] Mar 24, 2022
f1bd414
Comment change to force checks
Mar 24, 2022
756adbe
Fixes to tests
Mar 24, 2022
40a6fb0
Refactored tests a bit
Mar 24, 2022
5c74fb3
Fix to test_split_array
Mar 24, 2022
c210d36
Fix to test_generate_xy_plane
Mar 24, 2022
8753a1f
Docstring fix
Mar 24, 2022
1ce2209
Deepsource tricked me
Mar 24, 2022
0673ebb
Dosctring to force checks
Mar 24, 2022
ae8c96f
Fixture fix:
Mar 24, 2022
16a0b90
Format code with black
deepsource-autofix[bot] Mar 24, 2022
3c3595c
Format code with black
deepsource-autofix[bot] Mar 24, 2022
a5bc9ee
Format code with black
deepsource-autofix[bot] Mar 24, 2022
b76ec98
Format code with black
deepsource-autofix[bot] Mar 24, 2022
b58ba2f
Forcing checks again...
Mar 24, 2022
1fb62fa
Merge branch 'itr_ref_docstrings' of github.com:compSPI/reconstructSP…
Mar 24, 2022
98a879c
Format code with black
deepsource-autofix[bot] Mar 24, 2022
a1dc0cd
Better ctf_info for tests
Mar 24, 2022
5818e9e
Merge branch 'itr_ref_docstrings' of github.com:compSPI/reconstructSP…
Mar 24, 2022
4bcf15a
Format code with black
deepsource-autofix[bot] Mar 24, 2022
ecee780
Strings not variables...
Mar 24, 2022
f9c4196
Merging
Mar 24, 2022
0ac923c
Format code with black
deepsource-autofix[bot] Mar 24, 2022
cb7b069
Fix
Mar 24, 2022
0fb3b1b
Fix
Mar 24, 2022
966ed3c
test fixes
Mar 24, 2022
ccbd07b
Format code with black
deepsource-autofix[bot] Mar 24, 2022
4858d54
Test fixes
Mar 24, 2022
b7b8688
Merge branch 'itr_ref_docstrings' of github.com:compSPI/reconstructSP…
Mar 24, 2022
bc88498
Force checks
Mar 24, 2022
ab014c5
Format code with black
deepsource-autofix[bot] Mar 24, 2022
656b5d2
test fixes
Mar 24, 2022
4a86e20
Merge branch 'itr_ref_docstrings' of github.com:compSPI/reconstructSP…
Mar 24, 2022
b9e72ad
Forcing checks
Mar 24, 2022
5577ded
Reshape fix
Mar 24, 2022
bb6e258
Shape fix
Mar 24, 2022
81e5faa
Structure changes
Mar 24, 2022
a2f4b52
Test framework adjustment
Mar 24, 2022
17f94ca
Import change
Mar 24, 2022
9f7ee21
Small typo fixes
thisFreya Mar 24, 2022
0d418b9
Added a slightly more comprehensive split test
thisFreya Mar 24, 2022
e6410cd
Format code with black
deepsource-autofix[bot] Mar 24, 2022
511d105
Added back references
Mar 24, 2022
b575e13
Added the big method
thisFreya Mar 25, 2022
41a5807
Format code with black
deepsource-autofix[bot] Mar 25, 2022
22efe76
Pre-commit and black changes
thisFreya Mar 25, 2022
5a22371
Merging
thisFreya Mar 25, 2022
bda6275
Deepsource
thisFreya Mar 25, 2022
976be19
Expanded split_array to allow lists and tuples
thisFreya Mar 25, 2022
00e7281
Format code with black
deepsource-autofix[bot] Mar 25, 2022
ff36588
Deepsource fix
thisFreya Mar 25, 2022
8de2090
Merge branch 'itr_ref_docstrings' of https://github.com/compSPI/recon…
thisFreya Mar 25, 2022
b8e37ef
Fixed split_array expansion
thisFreya Mar 25, 2022
abfd9aa
Fix to fsc test
thisFreya Mar 25, 2022
e1cd120
Data type fix
thisFreya Mar 25, 2022
83d9cf7
Concatenate fix
thisFreya Mar 25, 2022
f8dc07a
Change to insert slice shape
Mar 25, 2022
352a989
Format code with black
deepsource-autofix[bot] Mar 25, 2022
d381e3c
fix to numpy array initialization
Mar 25, 2022
56f02a1
Merging
Mar 25, 2022
294d4b5
Format code with black
deepsource-autofix[bot] Mar 25, 2022
3819306
num rotations change
Mar 25, 2022
2b25434
Merge branch 'itr_ref_docstrings' of github.com:compSPI/reconstructSP…
Mar 25, 2022
f28ccfa
Format code with black
deepsource-autofix[bot] Mar 25, 2022
c053980
fsc fix
Mar 25, 2022
89cdaa8
Merge branch 'itr_ref_docstrings' of github.com:compSPI/reconstructSP…
Mar 25, 2022
7083a54
Forgot what a dot product did for a second there
Mar 25, 2022
2a1eda7
Updated iterative refinement test to reflect fsc fix
Mar 25, 2022
cba9320
Removed superfluous imports
Mar 27, 2022
d40e6ae
Updated docstrings, fixed splitting arrs
Mar 27, 2022
6c6e1f0
Split n into n_pix, n_particles
Mar 27, 2022
1e11a9d
Format code with black
deepsource-autofix[bot] Mar 27, 2022
d621fb1
Added back numba
Mar 27, 2022
b48b5ef
Merge branch 'itr_ref_docstrings' of github.com:compSPI/reconstructSP…
Mar 27, 2022
496f88a
Force checks
Mar 27, 2022
8aaff78
n_pix in fft docstrings
Mar 28, 2022
33e3229
Normalizing half maps
Mar 28, 2022
01ceaf5
Format code with black
deepsource-autofix[bot] Mar 28, 2022
20f2232
Removed comments, refactored big method, fixed bayesian weights shapes
Mar 31, 2022
e76d8a2
Format code with black
deepsource-autofix[bot] Mar 31, 2022
6e7ed32
Infrastructure files
Mar 31, 2022
6a9ee69
Infrastructure files
Mar 31, 2022
fd9cefa
Missed a name
Mar 31, 2022
bb8b274
Removed last few comments, will re-add if needed
Mar 31, 2022
66c0ef3
Format code with black
deepsource-autofix[bot] Mar 31, 2022
b9e8c10
Merging changes
Mar 31, 2022
579f716
Fixed variable name
Mar 31, 2022
6fe971c
Merge branch 'itr_ref_docstrings' of github.com:compSPI/reconstructSP…
Mar 31, 2022
55ee718
Changed codecov parameters
Mar 31, 2022
0d18f66
Removed __init__.py files
Mar 31, 2022
7f81456
Revert "Removed __init__.py files"
Mar 31, 2022
887bff7
Merging
Mar 31, 2022
e583a54
Revert "Changed codecov parameters"
Mar 31, 2022
aa607f3
Merge branch 'reconstructSPI_infrastructure' of github.com:compSPI/re…
Mar 31, 2022
15840cf
Refactoring library format
Mar 31, 2022
2f91bc6
Directory fixes
Mar 31, 2022
bccb41c
Revert "Refactoring library format"
Mar 31, 2022
2c42e8d
Revert "Directory fixes"
Mar 31, 2022
9126f86
Removed dependencies
Mar 31, 2022
3330c11
Testing something
Mar 31, 2022
aaf1078
Merge branch 'reconstructSPI_infrastructure' of github.com:compSPI/re…
Mar 31, 2022
ef10249
Testing things
Mar 31, 2022
d15bdb6
Merge branch 'reconstructSPI_infrastructure' of github.com:compSPI/re…
Mar 31, 2022
8dda440
Force checks
Mar 31, 2022
749bcd2
Testing things
Mar 31, 2022
2ea9459
Added library requirements
Mar 31, 2022
5152547
Added dev branch to branches on which tests will run.
Mar 31, 2022
5a4ebb5
Merging changes. Have temporarily added infrastructure branch to work…
Mar 31, 2022
9d05c1a
removed itr_ref_docstrings branch checks
Mar 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/auto-assign.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ addReviewers: true
addAssignees: false

# A list of reviewers to be added to pull requests (GitHub user name)
reviewers:
reviewers:
- fredericpoitevin

# A list of keywords to be skipped the process that add reviewers if pull requests include it
# A list of keywords to be skipped the process that add reviewers if pull requests include it
skipKeywords:
- wip

Expand Down
306 changes: 306 additions & 0 deletions iterative_refinement/expectation_maximization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
"""
Iterative refinement in Bayesian expection maximization setting
for reconstruction of particles.
"""

import numpy as np
from compSPI.transforms import do_fft, do_ifft
from simSPI.transfer import eval_ctf


class IterativeRefinement:
"""
Performs interative refimenent in a Bayesian expectation
maximization setting, i.e. maximum a posteriori estimation.
thisFreya marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
map_3d_init : arr
Initial particle map.
Shape (n_pix, n_pix, n_pix)
particles : arr
Particles to be reconstructed.
Shape (n_particles, n_pix, n_pix)
ctf_info : list of dicts
Each dict contains CTF k,v pairs per particle.
Shape (n_particles,)

Returns
-------
map_3d_update : arr
Current iteration of map.
Shape (n_pix, n_pix, n_pix)
map_3d_final : arr
Final updated map.
Shape (n_pix, n_pix, n_pix)
half_map_3d_final_1 : arr
Shape (n_pix, n_pix, n_pix)
half_map_3d_final_2 : arr
Shape (n_pix, n_pix, n_pix)
fsc_1d : arr
Final one dimensional fourier shell correlation.
Shape (n_pix // 2,)
"""

def __init__(self, map_3d_init, particles, ctf_info, max_itr=7):
self.map_3d_init = map_3d_init
self.particles = particles
self.ctf_info = ctf_info
self.max_itr = max_itr

def split_array(self, arr):
"""
Split array into two halves along 0th axis.

Parameters
----------
arr : arr
Shape (n_pix, n_pix, n_pix)
jedyeo marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
arr1 : arr
Shape (n_pix // 2, n_pix, n_pix)
jedyeo marked this conversation as resolved.
Show resolved Hide resolved
arr2: arr
Shape (n_pix // 2, n_pix, n_pix)
jedyeo marked this conversation as resolved.
Show resolved Hide resolved
"""
idx_half = arr.shape[0] // 2
arr_1, arr_2 = arr[:idx_half], arr[idx_half:]

if arr_1.shape[0] != arr_2.shape[0]:
arr_2 = arr[idx_half : 2 * idx_half]

return arr_1, arr_2

def build_ctf_array(self):
"""
Build 2D array of evaluated CTFs from inputted
CTF parameters for each particle.

Returns
-------
ctfs : arr
Shape (n_ctfs, n_pix, n_pix)
jedyeo marked this conversation as resolved.
Show resolved Hide resolved
"""
n_ctfs = len(self.ctf_info)
ctfs = []

for i in range(n_ctfs):
ctfs.append(eval_ctf(**self.ctf_info[i]))
thisFreya marked this conversation as resolved.
Show resolved Hide resolved

return ctfs

def grid_SO3_uniform(self, n_rotations):
"""
Generate a discrete set of uniformly distributed rotations
across SO(3).

Parameters
----------
n_rotations : int
Number of rotations

Returns
-------
rots : arr
List of rotations.
Shape (n_rotations, 3, 3)
"""
rots = np.ones((n_rotations, 3, 3))
return rots

def generate_xy_plane(self, n_pix):
"""
Generate xy plane.

Parameters
----------
n_pix : int
Number of pixels

Returns
xy_plane : arr
Array describing xy plane in space.
Shape (n_pix**2, 3)
"""

# See how meshgrid and generate coordinates functions used
# TODO:
# https://github.com/geoffwoollard/compSPI/blob/stash_simulate/src/simulate.py#L96

xy_plane = np.ones((n_pix**2, 3))
return xy_plane

def generate_slices(self, map_3d_f, xy_plane, n_pix, rots):
"""
Generates slice coordinates by rotating xy plane.
Interpolate values from map_3d_f onto 3D coordinates.
TODO: See how scipy map_values used to interpolate in
https://github.com/geoffwoollard/compSPI/blob/stash_simulate/src/simulate.py#L111

Parameters
----------
map_3d_f : arr
Shape (n_pix, n_pix, n_pix)
rots : arr
List of rotations.
Shape (n_rotations, 3, 3)

Returns
-------
slices : arr
Slice of map_3d_f. Corresponds to Fourier transform
of projection of rotated map_3d_f.
Shape (n_rotations, n_pix, n_pix)
xyz_rotated : arr
Rotated xy plane.
Shape (n_pix**2, 3)
"""
n_rotations = rots.shape[0]
# TODO: map_values interpolation, calculate from map, rots
map_3d_f = np.ones_like(map_3d_f)
xyz_rotated = np.ones_like(xy_plane)

size = n_rotations * n_pix**2
slices = np.random.normal(size=size)
slices.reshape(n_rotations, n_pix, n_pix)
return slices, xyz_rotated

def apply_ctf_to_slice(self, particle_slice, ctf):
"""
Apply CTF to projected slice by convolution.

particle_slice : arr
Slice of map_3d_f. Corresponds to Fourier transform
of projection of rotated map_3d_f.
Shape (n_pix, n_pix)
ctf : arr
CTF parameters for particle.
Shape (n_pix,n_pix)
"""

# TODO: vectorize and have shape match
projection_f_conv_ctf = ctf * slice
return projection_f_conv_ctf

def compute_bayesian_weights(self, particle, slices):
"""
Compute Bayesian weights of particle to slice
under Gaussian white noise model.

Parameters
----------
particle : arr
Shape (n_pix // 2,n_pix,n_pix)

slices : complex64 arr
Shape (n_slices, n_pix, n_pix)

Returns
-------
bayesian_weights : float64 arr
Shape (n_slices,)
"""
n_slices = slices.shape[0]
particle = np.ones_like(particle)
bayes_factors = np.random.normal(n_slices)
return bayes_factors

def apply_wiener_filter(self, projection, ctf, small_number):
"""
Apply Wiener filter to particle projection.

Parameters
----------
projection : arr
Shape (n_pix, n_pix)
ctf : arr
Shape (n_pix, n_pix)
small_number : float
Used for tuning Wiener filter.

Returns
-------
projection_wfilter_f : arr
Shape (n_pix, n_pix) the filtered projection.
"""
wfilter = ctf / (ctf * ctf + small_number)
projection_wfilter_f = projection * wfilter
return projection_wfilter_f

def insert_slice(self, slice_real, xyz, n_pix):
"""
Rotate slice and interpolate onto a 3D grid to prepare
for insertion.

Parameters
----------
slice_real : float64 arr
Shape (n_pix, n_pix) the slice of interest.
xyz : arr
Shape (n_pix**2, 3) plane corresponding to slice rotation.
n_pix : int
Number of pixels.

Returns
-------
inserted_slice_3d : float64 arr
Rotated slice in 3D voxel array.
Shape (n_pix, n_pix)
count_3d : arr
Voxel array to count slice presence: 1 if slice present,
otherwise 0.
Shape (n_pix, n_pix, n_pix)
"""

return slice_real, xyz, n_pix

def compute_fsc(self, map_3d_f_1, map_3d_f_2):
"""
Compute Fourier shell correlation.
Estimate noise from half maps.

Parameters
----------
map_3d_f_1 : arr
Shape (n_pix, n_pix, n_pix)
map_3d_f_2 : arr
Shape (n_pix, n_pix, n_pix)

Returns
-------
fsc_1d_1 : arr
Noise estimates for map 1.
Shape (n_pix // 2,)
fsc_1d_2 : arr
Noise estimates for map 2.
Shape (n_pix // 2,)
"""
# TODO: write fast vectorized fsc from code snippets in
# https://github.com/geoffwoollard/learn_cryoem_math/blob/master/nb/fsc.ipynb
# https://github.com/geoffwoollard/learn_cryoem_math/blob/master/nb/mFSC.ipynb
# https://github.com/geoffwoollard/learn_cryoem_math/blob/master/nb/guinier_fsc_sharpen.ipynb
n_pix_1 = map_3d_f_1.shape[0]
n_pix_2 = map_3d_f_2.shape[0]
fsc_1d_1 = np.ones(n_pix_1 // 2)
fsc_1d_2 = np.ones(n_pix_2 // 2)
return fsc_1d_1, fsc_1d_2

def expand_1d_to_3d(self, arr_1d):
"""
Expand 1D array data into spherical shell.

Parameters
----------
arr_1d : arr
Shape (n_pix // 2)

Returns
-------
arr_3d : arr
Shape (spherical coords)
"""
n_pix = arr_1d.shape[0] * 2
arr_3d = np.ones((n_pix, n_pix, n_pix))
# TODO: arr_1d fsc_1d to 3d (spherical shells)
return arr_3d
Loading