-
Notifications
You must be signed in to change notification settings - Fork 1
add loaders #9
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
Open
MikeLippincott
wants to merge
2
commits into
WayScience:main
Choose a base branch
from
MikeLippincott:loaders
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.
Open
add loaders #9
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,3 +172,4 @@ cython_debug/ | |
|
|
||
| # PyPI configuration file | ||
| .pypirc | ||
| testing.ipynb | ||
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
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,12 @@ | ||
| from .feature_writing_utils import ( | ||
| FeatureMetadata, | ||
| format_morphology_feature_name, | ||
| remove_underscores_from_string, | ||
| save_features_as_parquet, | ||
| ) | ||
| from .loading_classes import ( | ||
| ImageSetConfig, | ||
| ImageSetLoader, | ||
| ObjectLoader, | ||
| TwoObjectLoader, | ||
| ) |
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,191 @@ | ||
| """Functions for formatting morphology feature names in a consistent way. | ||
|
|
||
| Formats morphology feature names and saves features as parquet files. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import dataclasses | ||
| import pathlib | ||
|
|
||
| import pandas | ||
| import pandera.pandas as pa | ||
| from beartype import beartype | ||
|
|
||
| FEATURE_NAME_COMPONENT_COLUMNS = ( | ||
| "compartment", | ||
| "channel", | ||
| "feature_type", | ||
| "measurement", | ||
| ) | ||
|
|
||
|
|
||
| def _coerce_dataframe_column_names_to_strings( | ||
| dataframe: pandas.DataFrame, | ||
| ) -> pandas.DataFrame: | ||
| """Ensure DataFrame column labels are string-typed before writing.""" | ||
| parsed_dataframe = dataframe.copy() | ||
| parsed_dataframe.columns = [str(column) for column in parsed_dataframe.columns] | ||
| return parsed_dataframe | ||
|
|
||
|
|
||
| def _coerce_feature_name_components( | ||
| dataframe: pandas.DataFrame, | ||
| ) -> pandas.DataFrame: | ||
| """Normalize feature-name components using shared delimiter cleanup.""" | ||
| parsed_dataframe = dataframe.copy() | ||
| for column in FEATURE_NAME_COMPONENT_COLUMNS: | ||
| if column in parsed_dataframe.columns: | ||
| parsed_dataframe[column] = parsed_dataframe[column].map( | ||
| remove_underscores_from_string | ||
| ) | ||
| return parsed_dataframe | ||
|
|
||
|
|
||
| FEATURE_OUTPUT_SCHEMA = pa.DataFrameSchema( | ||
| columns={}, | ||
| strict=False, | ||
| parsers=[pa.Parser(_coerce_dataframe_column_names_to_strings)], | ||
| ) | ||
|
|
||
|
|
||
| FEATURE_NAME_COMPONENT_SCHEMA = pa.DataFrameSchema( | ||
| columns={ | ||
| "compartment": pa.Column(object, nullable=False, coerce=True), | ||
| "channel": pa.Column(object, nullable=False, coerce=True), | ||
| "feature_type": pa.Column(object, nullable=False, coerce=True), | ||
| "measurement": pa.Column(object, nullable=False, coerce=True), | ||
| }, | ||
| strict=True, | ||
| parsers=[pa.Parser(_coerce_feature_name_components)], | ||
| ) | ||
|
|
||
|
|
||
| @beartype | ||
| def remove_underscores_from_string(string: object) -> str: | ||
| """ | ||
| Remove unwanted delimiters from a string and replace them with hyphens. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| string : str | ||
| The string to remove unwanted delimiters from. | ||
|
|
||
| Returns | ||
| ------- | ||
| str | ||
| The string with unwanted delimiters removed and replaced with hyphens. | ||
| """ | ||
| if not isinstance(string, str): | ||
| try: | ||
| string = str(string) | ||
| except Exception as e: | ||
| msg = ( | ||
| f"Input string must be a string or convertible to a string. " | ||
| f"Received input: {string!r} of type {type(string)}" | ||
| ) | ||
| raise ValueError(msg) from e | ||
| string = string.translate( | ||
| str.maketrans( | ||
| { | ||
| "_": "-", | ||
| ".": "-", | ||
| " ": "-", | ||
| "/": "-", | ||
| } | ||
| ) | ||
| ) | ||
|
|
||
| return string | ||
|
|
||
|
|
||
| @beartype | ||
| def format_morphology_feature_name( | ||
|
MikeLippincott marked this conversation as resolved.
|
||
| compartment: object, channel: object, feature_type: object, measurement: object | ||
| ) -> str: | ||
| """ | ||
| Format a morphology feature name in a consistent way across all morphology features. | ||
| This format follows specification for the following: | ||
| https://github.com/WayScience/NF1_3D_organoid_profiling_pipeline/blob/main/docs/RFC-2119-Feature-Naming-Convention.md | ||
|
|
||
| Parameters | ||
| ---------- | ||
| compartment : str | ||
| The compartment name. | ||
| channel : str | ||
| The channel name. | ||
| feature_type : str | ||
| The feature type. | ||
| measurement : str | ||
| The measurement name. | ||
|
|
||
| Returns | ||
| ------- | ||
| str | ||
| The formatted feature name. | ||
| """ | ||
|
|
||
| component_frame = pandas.DataFrame( | ||
| [ | ||
| { | ||
| "compartment": compartment, | ||
| "channel": channel, | ||
| "feature_type": feature_type, | ||
| "measurement": measurement, | ||
| } | ||
| ] | ||
| ) | ||
| coerced_components = FEATURE_NAME_COMPONENT_SCHEMA.validate(component_frame) | ||
| parsed_row = coerced_components.iloc[0] | ||
| return ( | ||
| f"{parsed_row['compartment']}_{parsed_row['channel']}_" | ||
| f"{parsed_row['feature_type']}_{parsed_row['measurement']}" | ||
| ) | ||
|
|
||
|
|
||
| @beartype | ||
| @dataclasses.dataclass | ||
| class FeatureMetadata: | ||
| """Metadata for feature output.""" | ||
|
|
||
| compartment: str | ||
| channel: str | ||
| feature_type: str | ||
| cpu_or_gpu: str | ||
|
|
||
|
|
||
| @beartype | ||
| def save_features_as_parquet( | ||
|
MikeLippincott marked this conversation as resolved.
MikeLippincott marked this conversation as resolved.
|
||
| parent_path: pathlib.Path, | ||
| df: pandas.DataFrame, | ||
| metadata: FeatureMetadata, | ||
| ) -> pathlib.Path: | ||
| """Save features as parquet files in a consistent way. | ||
|
|
||
| Saves features as parquet files with consistent naming across morphology | ||
| features. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| parent_path : pathlib.Path | ||
| The parent path to save the features to. | ||
| df : pandas.DataFrame | ||
| The dataframe containing the features to save. | ||
| metadata : FeatureMetadata | ||
| Metadata for the feature output (compartment, channel, feature_type, | ||
| cpu_or_gpu). | ||
|
|
||
| Returns | ||
| ------- | ||
| pathlib.Path | ||
| """ | ||
| validated_df = FEATURE_OUTPUT_SCHEMA.validate(df) | ||
| output_prefix = format_morphology_feature_name( | ||
| metadata.compartment, | ||
| metadata.channel, | ||
| metadata.feature_type, | ||
| metadata.cpu_or_gpu, | ||
| ) | ||
| save_path = parent_path / f"{output_prefix}_features.parquet" | ||
| validated_df.to_parquet(save_path, index=False) | ||
| return save_path | ||
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.
Uh oh!
There was an error while loading. Please reload this page.