-
Notifications
You must be signed in to change notification settings - Fork 12
DynaCell Metrics #242
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
Draft
edyoshikun
wants to merge
32
commits into
main
Choose a base branch
from
dynacell_metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
DynaCell Metrics #242
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
96f266f
protype of metrics and files to modify
edyoshikun 3aa3ddf
fix ruff settings
ieivanov d5394d3
add panoptica to deps
ieivanov 93524ea
remove metrics module and add optional dtype casting to SegmentationD…
ieivanov 71eb7ed
rename Segmentation to TargetPrediction modules
ieivanov 5275349
add dynacell data module draft
ieivanov 56a0119
dynacell dataloader WIP
ieivanov 10a33c7
style
ieivanov c13914f
style
ieivanov 48df3de
debug
ieivanov 620e368
splitting the logic for computing metrics by accepting two databases …
edyoshikun 149d36e
fix demo
edyoshikun bc93f89
CLI prototype to compute metrics
edyoshikun d3c3d8a
support z-slice 3d via list to slice object conversion
edyoshikun b7141d9
allow for independent target and prediction databases
ieivanov 4f2e13a
refactor demo script
ieivanov 9bc4f49
rename demo script
ieivanov e9ab7a1
docs
ieivanov db86e3c
test of specified positions
ieivanov 5b1b683
fixing the trainer from 'auto' for resources to 'cpu' and limiting th…
edyoshikun ea4ed97
adding transforms to do normalization.
edyoshikun 0e9dd7c
WIP
ieivanov 7215723
bugfix - convert data to float32
ieivanov a10ac34
segment prototype. can be deleted later
edyoshikun ab8617b
add plotting
edyoshikun 8da84e9
vs metrics v1
ieivanov 411aeb6
compute metrics on multiple conditions at a time
ieivanov 1de0525
use multiple workers
ieivanov dbd9b56
cleaner messaging
ieivanov 9467a91
use gpu acceleration
ieivanov 62d2703
add note on ssim data_range
ieivanov c80e4aa
ivan's VS metrics scripts
ieivanov 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
Empty file.
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,178 @@ | ||
| """ | ||
| This script is a demo script for the DynaCell application. | ||
| It loads the ome-zarr 0.4v format, calculates metrics and saves the results as csv files | ||
| """ | ||
|
|
||
| import datetime | ||
| import tempfile | ||
| from pathlib import Path | ||
| from typing import Literal | ||
|
|
||
| import pandas as pd | ||
| import torch | ||
| from lightning.pytorch.loggers import CSVLogger | ||
|
|
||
| from viscy.data.dynacell import DynaCellDataBase, DynaCellDataModule | ||
| from viscy.trainer import Trainer | ||
| from viscy.translation.evaluation import IntensityMetrics, SegmentationMetrics | ||
|
|
||
| # Set float32 matmul precision for better performance on Tensor Cores | ||
| torch.set_float32_matmul_precision("high") | ||
|
|
||
| csv_database_path = Path( | ||
| "/home/eduardo.hirata/repos/viscy/applications/DynaCell/dynacell_summary_table.csv" | ||
| ).expanduser() | ||
|
||
| tmp_path = Path("/home/eduardo.hirata/repos/viscy/applications/DynaCell/demo_metrics") | ||
| tmp_path.mkdir(parents=True, exist_ok=True) | ||
|
|
||
|
|
||
| def main( | ||
| method: Literal["segmentation2D", "segmentation3D", "intensity"] = "intensity", | ||
| use_z_slice_range: bool = False, | ||
| ): | ||
| """ | ||
| Run DynaCell metrics computation. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| method : Literal["segmentation2D", "segmentation3D", "intensity"], optional | ||
| Type of metrics to compute, by default "intensity" | ||
| use_z_slice_range : bool, optional | ||
| Whether to use a z-slice range instead of a single slice, by default False | ||
| """ | ||
| # Generate timestamp for unique versioning | ||
| timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") | ||
|
|
||
| # Set z_slice based on whether to use range or single slice | ||
| z_slice_value = slice(15, 17) if use_z_slice_range else 16 | ||
|
|
||
| # Create target database | ||
| target_db = DynaCellDataBase( | ||
| database_path=csv_database_path, | ||
| cell_types=["HEK293T"], | ||
| organelles=["HIST2H2BE"], | ||
| infection_conditions=["Mock"], | ||
| channel_name="Organelle", | ||
| z_slice=z_slice_value, | ||
| ) | ||
|
|
||
| if method == "segmentation2D": | ||
| # For segmentation, use same channel for pred and target (self-comparison) | ||
| pred_db = DynaCellDataBase( | ||
| database_path=csv_database_path, | ||
| cell_types=["HEK293T"], | ||
| organelles=["HIST2H2BE"], | ||
| infection_conditions=["Mock"], | ||
| channel_name="Organelle", | ||
| z_slice=z_slice_value, | ||
| ) | ||
|
|
||
| # Create data module with both databases | ||
| dm = DynaCellDataModule( | ||
| target_database=target_db, | ||
| pred_database=pred_db, | ||
| batch_size=1, | ||
| num_workers=0, | ||
| ) | ||
| dm.setup(stage="test") | ||
|
|
||
| # Print a sample to verify metadata | ||
| sample = next(iter(dm.test_dataloader())) | ||
| print(f"Sample keys: {sample.keys()}") | ||
| print(f"Cell type: {sample['cell_type']}") | ||
| print(f"Organelle: {sample['organelle']}") | ||
| print(f"Infection condition: {sample['infection_condition']}") | ||
|
|
||
| # Run segmentation metrics | ||
| lm = SegmentationMetrics() | ||
| # Use the method name and timestamp for unique identification | ||
| name = f"segmentation_{timestamp}" | ||
| version = "1" | ||
|
|
||
| output_dir = tmp_path / "segmentation" | ||
| output_dir.mkdir(exist_ok=True) | ||
|
|
||
| # Use the CSVLogger without version (we'll use our own naming) | ||
| logger = CSVLogger(save_dir=output_dir, name=name, version=version) | ||
| trainer = Trainer(logger=logger) | ||
| trainer.test(lm, datamodule=dm) | ||
|
|
||
| # Find the metrics file - use the correct relative pattern | ||
| metrics_file = output_dir / name / version / "metrics.csv" | ||
| if metrics_file.exists(): | ||
| metrics = pd.read_csv(metrics_file) | ||
| print(f"Segmentation metrics saved to: {metrics_file}") | ||
| print(f"Segmentation metrics columns: {metrics.columns.tolist()}") | ||
| else: | ||
| print(f"Warning: Metrics file not found at {metrics_file}") | ||
| metrics = None | ||
|
|
||
| return metrics | ||
|
|
||
| elif method == "segmentation3D": | ||
| raise NotImplementedError("Segmentation3D is not implemented yet") | ||
|
|
||
| elif method == "intensity": | ||
| # For intensity comparison, use the same channel to compare to itself | ||
| pred_db = DynaCellDataBase( | ||
| database_path=csv_database_path, | ||
| cell_types=["HEK293T"], | ||
| organelles=["HIST2H2BE"], | ||
| infection_conditions=["Mock"], | ||
| channel_name="Organelle", | ||
| z_slice=z_slice_value, | ||
| ) | ||
|
|
||
| # Create data module with both databases | ||
| dm = DynaCellDataModule( | ||
| target_database=target_db, | ||
| pred_database=pred_db, | ||
| batch_size=1, | ||
| num_workers=0, | ||
| ) | ||
| dm.setup(stage="test") | ||
|
|
||
| # Print a sample to verify metadata | ||
| sample = next(iter(dm.test_dataloader())) | ||
| print(f"Sample keys: {sample.keys()}") | ||
| print(f"Cell type: {sample['cell_type']}") | ||
| print(f"Organelle: {sample['organelle']}") | ||
| print(f"Infection condition: {sample['infection_condition']}") | ||
|
|
||
| # Run intensity metrics | ||
| lm = IntensityMetrics() | ||
| # Indicate whether z-slice range was used in the name | ||
| range_suffix = "_range" if use_z_slice_range else "" | ||
| name = f"intensity{range_suffix}_{timestamp}" | ||
| version = "1" | ||
|
|
||
| output_dir = tmp_path / "intensity" | ||
| output_dir.mkdir(exist_ok=True) | ||
|
|
||
| # Use the CSVLogger without version (we'll use our own naming) | ||
| logger = CSVLogger(save_dir=output_dir, name=name, version=version) | ||
| trainer = Trainer(logger=logger) | ||
| trainer.test(lm, datamodule=dm) | ||
|
|
||
| # Find the metrics file - use the correct relative pattern | ||
| metrics_file = output_dir / name / version / "metrics.csv" | ||
| if metrics_file.exists(): | ||
| metrics = pd.read_csv(metrics_file) | ||
| print(f"Intensity metrics saved to: {metrics_file}") | ||
| print(f"Intensity metrics columns: {metrics.columns.tolist()}") | ||
| else: | ||
| print(f"Warning: Metrics file not found at {metrics_file}") | ||
| metrics = None | ||
|
|
||
| return metrics | ||
| else: | ||
| raise ValueError(f"Invalid method: {method}") | ||
|
|
||
|
|
||
| # %% | ||
| if __name__ == "__main__": | ||
| # print("Running intensity metrics with single z-slice...") | ||
| # intensity_metrics = main("intensity", use_z_slice_range=False) | ||
|
|
||
| print("\nRunning intensity metrics with z-slice range...") | ||
| intensity_metrics_range = main("intensity", use_z_slice_range=True) | ||
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,34 @@ | ||
| # Example configuration for DynaCell metrics computation | ||
|
|
||
| # Required parameters | ||
| target_database: /path/to/target_database.csv | ||
| pred_database: /path/to/prediction_database.csv | ||
| output_dir: ./metrics_output | ||
| method: intensity # Options: 'intensity' or 'segmentation2D' | ||
|
|
||
| # Target dataset parameters | ||
| target_channel: Organelle | ||
| # Z-slice can be a single integer (e.g., 16) or a range specified as a list of two integers [start, end] (e.g., [15, 17]) | ||
| target_z_slice: 16 # Use -1 for all slices, or a list like [15, 17] for a range | ||
| target_cell_types: | ||
| - HEK293T | ||
| target_organelles: | ||
| - HIST2H2BE | ||
| target_infection_conditions: | ||
| - Mock | ||
|
|
||
| # Prediction dataset parameters | ||
| pred_channel: Organelle | ||
| # Z-slice can be a single integer (e.g., 16) or a range specified as a list of two integers [start, end] (e.g., [15, 17]) | ||
| pred_z_slice: 16 # Use -1 for all slices, or a list like [15, 17] for a range | ||
| pred_cell_types: | ||
| - HEK293T | ||
| pred_organelles: | ||
| - HIST2H2BE | ||
| pred_infection_conditions: | ||
| - Mock | ||
|
|
||
| # Processing parameters | ||
| batch_size: 1 | ||
| num_workers: 0 | ||
| version: "1" |
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.