-
Notifications
You must be signed in to change notification settings - Fork 3
Run multiple datasets and evaluate quality of derotation #38
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
Merged
Merged
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
fcf4bf7
Add script on metrics
lauraporta fd64605
Remove old slurm related script
lauraporta 169f55e
Make fit ellipse more resiliant to nans, change loss
lauraporta bfd619f
Improve fitting boundaries
lauraporta 2ad7137
Configs can now be passed as a dict
lauraporta 5839ad5
General refactoring and add velocity method
lauraporta 46bc182
Add script to analyse datasets in batches
lauraporta 12108f7
Use biased center
lauraporta 97cab25
Small bug fix
lauraporta 9e2cb40
WIP find optimal params
lauraporta bb9ea41
WIP: all datasets fixed but derotation is broken
lauraporta 7f6bc81
Fix derotation
lauraporta 6316cb6
Apply linting and cleanup from some commented out code
lauraporta 2b42ac5
Further linting.
lauraporta 6784471
Imsave is deprecated in latest tifffile version
lauraporta 31d6f33
Fix bug on defining starting and ending times of rotations appearing …
lauraporta c6c09b5
Fix bug related to csv creation
lauraporta 62d19a6
Remove logging messages that are not necessary
lauraporta a34b2d7
Add function to plot all speed profiles
lauraporta b2777dd
Store center of rotation previously found in a file
lauraporta a6cee9a
Fix velocity calculation bug
lauraporta 964c124
Log unlinking and only unlink pngs
lauraporta 19ecda3
Update derotation/analysis/full_derotation_pipeline.py
lauraporta 4a91c89
Add missing docstring in BO class
lauraporta 8f35d68
Improve signature and add docstring to metrics
lauraporta 250e5e1
Some I/O fixes
lauraporta d7cc917
Type bug fix
lauraporta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import logging | ||
| from pathlib import Path | ||
| from typing import Tuple | ||
|
|
||
| import matplotlib.pyplot as plt | ||
| import numpy as np | ||
| from bayes_opt import BayesianOptimization | ||
|
|
||
| from derotation.analysis.mean_images import calculate_mean_images | ||
| from derotation.analysis.metrics import ptd_of_most_detected_blob | ||
| from derotation.derotate_by_line import derotate_an_image_array_line_by_line | ||
|
|
||
|
|
||
| class BO_for_derotation: | ||
| def __init__( | ||
| self, | ||
| movie: np.ndarray, | ||
| rot_deg_line: np.ndarray, | ||
| rot_deg_frame: np.ndarray, | ||
| blank_pixels_value: float, | ||
| center: Tuple[int, int], | ||
| delta: int, | ||
| init_points: int = 2, | ||
| n_iter: int = 10, | ||
| debug_plots_folder: Path = Path("./debug_plots"), | ||
| ): | ||
| self.movie = movie | ||
| self.rot_deg_line = rot_deg_line | ||
| self.rot_deg_frame = rot_deg_frame | ||
| self.blank_pixels_value = blank_pixels_value | ||
| x, y = center | ||
| self.pbounds = { | ||
| "x": (x - delta, x + delta), | ||
| "y": (y - delta, y + delta), | ||
| } | ||
| self.init_points = init_points | ||
| self.n_iter = n_iter | ||
| self.debug_plots_folder = debug_plots_folder | ||
|
|
||
| self.subfolder = self.debug_plots_folder / "bo" | ||
| # remove previous dir | ||
| if self.subfolder.exists(): | ||
| for file in self.subfolder.iterdir(): | ||
| file.unlink() | ||
lauraporta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else: | ||
| self.subfolder.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| def optimize(self): | ||
| def derotate_and_get_metric( | ||
| x: float, | ||
| y: float, | ||
| ): | ||
| derotated_chunk = derotate_an_image_array_line_by_line( | ||
| image_stack=self.movie, | ||
| rot_deg_line=self.rot_deg_line, | ||
| blank_pixels_value=self.blank_pixels_value, | ||
| center=(int(x), int(y)), | ||
| ) | ||
|
|
||
| mean_images = calculate_mean_images( | ||
| derotated_chunk, self.rot_deg_frame, round_decimals=0 | ||
| ) | ||
|
|
||
| plt.imshow(mean_images[0], cmap="gray") | ||
| plt.savefig(self.subfolder / f"mean_image_0_{x:.2f}_{y:.2f}.png") | ||
| plt.close() | ||
|
|
||
| ptd = ptd_of_most_detected_blob( | ||
| mean_images, | ||
| debug_plots_folder=self.subfolder, | ||
| image_names=[ | ||
| f"blobs_{x:.2f}_{y:.2f}.png", | ||
| f"blob_centers_{x:.2f}_{y:.2f}.png", | ||
| ], | ||
| ) | ||
|
|
||
| # we are maximizing the metric, so | ||
| # we need to return the negative of the metric | ||
| return -ptd | ||
|
|
||
| optimizer = BayesianOptimization( | ||
| f=derotate_and_get_metric, | ||
| pbounds=self.pbounds, | ||
| verbose=2, | ||
| random_state=1, | ||
| ) | ||
|
|
||
| optimizer.maximize( | ||
| init_points=self.init_points, | ||
| n_iter=self.n_iter, | ||
| ) | ||
|
|
||
| for i, res in enumerate(optimizer.res): | ||
| logging.info(f"Iteration {i}: {res}") | ||
|
|
||
| return optimizer.max | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.