Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lauraporta committed Jul 2, 2024
1 parent c19e091 commit 576a9ad
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 71 deletions.
41 changes: 26 additions & 15 deletions derotation/analysis/full_rotation_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from sklearn.mixture import GaussianMixture
from tifffile import imsave

from derotation.derotate_by_line import rotate_an_image_array_line_by_line
from derotation.derotate_by_line_plot import rotate_an_image_array_line_by_line
from derotation.load_data.custom_data_loaders import (
get_analog_signals,
read_randomized_stim_table,
Expand Down Expand Up @@ -55,7 +55,7 @@ def __call__(self):
- adding a circular mask to the rotated image stack
- saving the masked image stack
"""
self.contrast_enhancement()
# self.contrast_enhancement()
self.process_analog_signals()
rotated_images = self.rotate_frames_line_by_line()
masked = self.add_circle_mask(rotated_images, self.mask_diameter)
Expand Down Expand Up @@ -236,18 +236,29 @@ def process_analog_signals(self):
)
self.rotation_on = self.create_signed_rotation_array()

self.drop_ticks_outside_of_rotation()
if self.adjust_increment:
self.drop_ticks_outside_of_rotation()
self.check_number_of_rotations()

self.check_number_of_rotations()
if not self.is_number_of_ticks_correct() and self.adjust_increment:
(
self.corrected_increments,
self.ticks_per_rotation,
) = self.adjust_rotation_increment()
if not self.is_number_of_ticks_correct():
(
self.corrected_increments,
self.ticks_per_rotation,
) = self.adjust_rotation_increment()
else:
self.corrected_increments = [
self.rotation_increment
] * self.number_of_rotations
self.ticks_per_rotation = (
self.rot_deg
* self.rotation_increment
* self.number_of_rotations
)

self.interpolated_angles = self.get_interpolated_angles()

self.remove_artifacts_from_interpolated_angles()
if self.adjust_increment:
self.remove_artifacts_from_interpolated_angles()

(
self.line_start,
Expand Down Expand Up @@ -419,7 +430,7 @@ def drop_ticks_outside_of_rotation(self) -> np.ndarray:

inter_roatation_interval = [
idx
for i in range(self.number_of_rotations + 1)
for i in range(len(edited_ends))
for idx in range(
edited_ends[i],
rolled_starts[i],
Expand Down Expand Up @@ -457,8 +468,8 @@ def check_number_of_rotations(self):
raise ValueError(
"Start and end of rotations have different lengths"
)
if self.rot_blocks_idx["start"].shape[0] != self.number_of_rotations:
raise ValueError("Number of rotations is not as expected")
# if self.rot_blocks_idx["start"].shape[0] != self.number_of_rotations:
# raise ValueError("Number of rotations is not as expected")

logging.info("Number of rotations is as expected")

Expand Down Expand Up @@ -550,7 +561,7 @@ def get_interpolated_angles(self) -> np.ndarray:

ticks_with_increment = [
item
for i in range(self.number_of_rotations)
for i in range(len(self.corrected_increments))
for item in [self.corrected_increments[i]]
* self.ticks_per_rotation[i]
]
Expand Down Expand Up @@ -599,7 +610,7 @@ def remove_artifacts_from_interpolated_angles(self):
rotation_end = np.where(np.diff(thresholded) < 0)[0]

assert len(rotation_start) == len(rotation_end)
assert len(rotation_start) == self.number_of_rotations
# assert len(rotation_start) == self.number_of_rotations

for i, (start, end) in enumerate(
zip(rotation_start[1:], rotation_end[:-1])
Expand Down
2 changes: 1 addition & 1 deletion derotation/analysis/incremental_rotation_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __call__(self):
After processing the analog signals, the image stack is rotated by
frame and then registered using phase cross correlation.
"""
self.contrast_enhancement()
# self.contrast_enhancement()
super().process_analog_signals()
rotated_images = self.roatate_by_frame()
masked_unregistered = self.add_circle_mask(rotated_images)
Expand Down
47 changes: 27 additions & 20 deletions derotation/config/full_rotation.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
paths_read:
path_to_randperm: "your_path_to/stimlus_randperm.mat"
path_to_aux: "your_path_to/rotation.bin"
path_to_tif: "your_path_to/rotation.tif"
path_to_randperm: /Users/lauraporta/local_data/rotation/stimlus_randperm.mat
path_to_aux: /Users/lauraporta/local_data/rotation/230731_25_micron_grid/aux_stim/230731_grid_1_001.bin
path_to_tif: /Users/lauraporta/local_data/rotation/230731_25_micron_grid/imaging/rotation_zf2_all_speeds_00002_enhanced.tif
paths_write:
debug_plots_folder: "your_path_to/debug_plots/"
logs_folder: "your_path_to/logs/"
derotated_tiff_folder: "your_path_to/data_folder/"
saving_name: "derotated_image_stack"
debug_plots_folder: /Users/lauraporta/local_data/rotation/230731_25_micron_grid/full/debug_plots/
logs_folder: /Users/lauraporta/local_data/rotation/230731_25_micron_grid/full/logs/
derotated_tiff_folder: /Users/lauraporta/local_data/rotation/230731_25_micron_grid/full/derotated/
saving_name: derotated_image_stack_CE
# paths_read:
# path_to_randperm: /Users/lauraporta/local_data/rotation/stimlus_randperm.mat
# path_to_aux: /Users/lauraporta/local_data/rotation/230802_CAA_1120182/aux_stim/230802_CAA_1120182_rotation_1_001.bin
# path_to_tif: /Users/lauraporta/local_data/rotation/230802_CAA_1120182/imaging/rotation_00001.tif
# paths_write:
# debug_plots_folder: /Users/lauraporta/local_data/rotation/230802_CAA_1120182/full/debug_plots/
# logs_folder: /Users/lauraporta/local_data/rotation/230802_CAA_1120182/full/logs/
# derotated_tiff_folder: /Users/lauraporta/local_data/rotation/230802_CAA_1120182/full/derotated/
# saving_name: derotated_image_stack_NO_ce


channel_names: [
"camera",
"scanimage_frameclock",
"scanimage_lineclock",
"photodiode2",
"PI_rotON",
"PI_rotticks",
]
channel_names:
- camera
- scanimage_frameclock
- scanimage_lineclock
- photodiode2
- PI_rotON
- PI_rotticks

rotation_increment: 0.2
adjust_increment: True
adjust_increment: false
rot_deg: 360

debugging_plots: True
debugging_plots: false

contrast_enhancement: 0.35

Expand All @@ -35,5 +42,5 @@ analog_signals_processing:
angle_interpolation_artifact_threshold: 0.15

interpolation:
line_use_start: True
frame_use_start: True
line_use_start: true
frame_use_start: true
16 changes: 8 additions & 8 deletions derotation/config/incremental_rotation.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
paths_read:
path_to_randperm: "your_path_to/stimlus_randperm.mat"
path_to_aux: "your_path_to/rotation.bin"
path_to_tif: "your_path_to/rotation.tif"
path_to_randperm: "/Users/lauraporta/local_data/rotation/stimlus_randperm.mat"
path_to_aux: "/Users/lauraporta/local_data/rotation/230802_CAA_1120182/aux_stim/230802_CAA_1120182_rotationincrement_1_001.bin"
path_to_tif: "/Users/lauraporta/local_data/rotation/230802_CAA_1120182/imaging/rotation_increment_00001.tif"
paths_write:
debug_plots_folder: "your_path_to/debug_plots/"
logs_folder: "your_path_to/logs/"
derotated_tiff_folder: "your_path_to/data_folder/"
saving_name: "derotated_image_stack"
debug_plots_folder: "/Users/lauraporta/local_data/rotation/230802_CAA_1120182/incremental/debug_plots/"
logs_folder: "/Users/lauraporta/local_data/rotation/230802_CAA_1120182/incremental/logs/"
derotated_tiff_folder: "/Users/lauraporta/local_data/rotation/230802_CAA_1120182/incremental/derotated/"
saving_name: "derotated_incremental_image_stack_NO_ce"


channel_names: [
Expand All @@ -22,7 +22,7 @@ rotation_increment: 0.2
adjust_increment: True
rot_deg: 360

debugging_plots: True
debugging_plots: False

contrast_enhancement: 0.35

Expand Down
18 changes: 9 additions & 9 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,22 @@
# Automatically generate stub pages for API
autosummary_generate = True
numpydoc_class_members_toctree = False # stops stubs warning
#toc_object_entries_show_parents = "all"
# toc_object_entries_show_parents = "all"
html_show_sourcelink = False

#html_sidebars = { this is not working...
# html_sidebars = { this is not working...
# "index": [],
# "**": [],
#}
# }

autodoc_default_options = {
'members': True,
"members": True,
"member-order": "bysource",
'special-members': False,
'private-members': False,
'inherited-members': False,
'undoc-members': True,
'exclude-members': "",
"special-members": False,
"private-members": False,
"inherited-members": False,
"undoc-members": True,
"exclude-members": "",
}

# List of patterns, relative to source directory, that match files and
Expand Down
71 changes: 56 additions & 15 deletions examples/improve_video_using_suite2p_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,62 @@
from suite2p.io import BinaryFile
from suite2p.registration.nonrigid import make_blocks, spatial_taper

from derotation.analysis.incremental_rotation_pipeline import (
IncrementalPipeline,
)

derotator = IncrementalPipeline("incremental_rotation")
derotator()
# derotator = IncrementalPipeline("incremental_rotation")
# derotator()


# extract luminance variations across angles
zero_rotation_mean_image = derotator.get_target_image(derotator.masked)
mean_images_across_angles = derotator.calculate_mean_images(derotator.masked)
# # extract luminance variations across angles
# zero_rotation_mean_image = derotator.get_target_image(derotator.masked)
# mean_images_across_angles = derotator.calculate_mean_images(derotator.masked)


# load registered bin file of suite2p
path_to_bin_file = Path(
"/Users/lauraporta/local_data/rotation/230802_CAA_1120182/incremental/derotated/suite2p/plane0/data.bin"
)
shape_image = zero_rotation_mean_image.shape
shape_image = [256, 256]
registered = BinaryFile(
Ly=shape_image[0], Lx=shape_image[0], filename=path_to_bin_file
).file

time, x, y = registered.shape
registered = registered.reshape(time, x * y)
model = PCA(n_components=10, random_state=0).fit(registered)


print("debug")


path_options = Path("/Users/lauraporta/local_data/laura_ops.npy")
ops = np.load(path_options, allow_pickle=True).item()

bin_size = int(
max(1, ops["nframes"] // ops["nbinned"], np.round(ops["tau"] * ops["fs"]))
)
plt.imshow(registered[0])
plt.show()

# load options
with BinaryFile(filename=path_to_bin_file, Ly=ops["Ly"], Lx=ops["Lx"]) as f:
registered = f.bin_movie(
bin_size=bin_size,
bad_frames=ops.get("badframes"),
y_range=ops["yrange"],
x_range=ops["xrange"],
)

mov_mean = registered.mean(axis=0)
registered -= mov_mean
model = PCA(n_components=8, random_state=0).fit(registered)

result = (registered @ model.components_.T) @ model.components_


# use PCA as in the suite2p code - this takes in account for non rigid
# registration, that we are not going to do
path_options = Path("/Users/lauraporta/local_data/laura_ops.npy")
ops = np.load(path_options)
ops = np.load(path_options, allow_pickle=True).item()

mov_mean = registered.mean(axis=0)
registered -= mov_mean

# use PCA as in the suite2p code
block_size = [ops["block_size"][0] // 2, ops["block_size"][1] // 2]
nframes, Ly, Lx = registered.shape
yblock, xblock, _, block_size, _ = make_blocks(Ly, Lx, block_size=block_size)
Expand All @@ -54,5 +81,19 @@
block_re[i] = (block @ model.components_.T) @ model.components_
norm[yblock[i][0] : yblock[i][-1], xblock[i][0] : xblock[i][-1]] += maskMul

reconstruction = np.zeros_like(registered)

block_re = block_re.reshape(nblocks, nframes, Lyb, Lxb)
block_re *= maskMul
for i in range(nblocks):
reconstruction[
:, yblock[i][0] : yblock[i][-1], xblock[i][0] : xblock[i][-1]
] += block_re[i]
reconstruction /= norm
reconstruction += mov_mean

plt.imshow(norm)
plt.show()


print("debug")
Loading

0 comments on commit 576a9ad

Please sign in to comment.