cellmaps_image_embedding package

Submodules

cellmaps_image_embedding.cellmaps_image_embeddingcmd module

cellmaps_image_embedding.cellmaps_image_embeddingcmd.main(args)[source]

Main entry point for program

Parameters:

args (list) – arguments passed to command line usually sys.argv[1:]()

Returns:

return value of cellmaps_image_embedding.runner.CellmapsImageEmbedder.run() or 2 if an exception is raised

Return type:

int

cellmaps_image_embedding.dataset module

class cellmaps_image_embedding.dataset.ProteinDataset(image_dir, outdir, image_size=512, crop_size=0, in_channels=4, suffix='.jpg', alt_image_ids=None)[source]

Bases: Dataset

crop_image(image)[source]
image_to_tensor(image, mean=0, std=1.0)[source]
read_rgby(image_id)[source]
set_random_crop(random_crop=False)[source]
set_transform(transform=None)[source]
cellmaps_image_embedding.dataset.augment_default(image)[source]
cellmaps_image_embedding.dataset.augment_fliplr(image)[source]
cellmaps_image_embedding.dataset.augment_fliplr_transpose(image)[source]
cellmaps_image_embedding.dataset.augment_flipud(image)[source]
cellmaps_image_embedding.dataset.augment_flipud_lr(image)[source]
cellmaps_image_embedding.dataset.augment_flipud_lr_transpose(image)[source]
cellmaps_image_embedding.dataset.augment_flipud_transpose(image)[source]
cellmaps_image_embedding.dataset.augment_transpose(image)[source]

cellmaps_image_embedding.exceptions module

exception cellmaps_image_embedding.exceptions.CellMapsImageEmbeddingError[source]

Bases: Exception

Base exception for CellMapsImageEmbedding

cellmaps_image_embedding.models module

class cellmaps_image_embedding.models.ArcMarginProduct(in_features, out_features)[source]

Bases: Module

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(features)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

reset_parameters()[source]
class cellmaps_image_embedding.models.DensenetClass(backbone='densenet121', num_classes=28, in_channels=4, pretrained=None, large=False)[source]

Bases: Module

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

load_pretrained(pretrained)[source]
class cellmaps_image_embedding.models.ResnetClass(backbone='resnet34', num_classes=28, in_channels=4, pretrained=None)[source]

Bases: Module

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

load_pretrained(pretrained)[source]
cellmaps_image_embedding.models.class_densenet121_dropout(num_classes=28, in_channels=4, pretrained=None)[source]
cellmaps_image_embedding.models.class_densenet121_large_dropout(num_classes=28, in_channels=4, pretrained=None)[source]
cellmaps_image_embedding.models.class_resnet50_dropout(num_classes=28, in_channels=4, pretrained=None)[source]

cellmaps_image_embedding.runner module

class cellmaps_image_embedding.runner.CellmapsImageEmbedder(outdir=None, inputdir=None, embedding_generator=None, skip_logging=True, name=None, organization_name=None, project_name=None, input_data_dict=None, provenance_utils=<cellmaps_utils.provenance.ProvenanceUtil object>, provenance=None)[source]

Bases: object

Class to run algorithm

Constructor

Parameters:
  • outdir (str) – Directory to write the results of this tool

  • inputdir (str) – Directory with images that will be embedded, containing color-separated subdirectorie. Output directory from cellmaps_imagedownloader.

  • embedding_generator (EmbeddingGenerator) – An object implementing the embedding generation logic, typically a subclass of EmbeddingGenerator, such as DensenetEmbeddingGenerator or FakeEmbeddingGenerator.

  • skip_logging (bool) – If True skip logging, if None or False do NOT skip logging

  • name (str or None) – Optional name for the dataset, used in provenance tracking. If not provided, it will be inferred from the RO-Crate in the input directory or fallback defaults.

  • organization_name (str or None) – Optional name of the organization creating the dataset. Used for provenance metadata.

  • project_name (str or None) – Optional name of the project associated with the dataset. Used for provenance metadata.

  • input_data_dict (dict or None) –

    Dictionary of input parameters used for execution. This is captured and stored in metadata as part of reproducibility and provenance. If not provided, a default dictionary is generated.

    Example:

    {'outdir': '/output/path', 'inputdir': '/input/path'}
    

  • provenance_utils (ProvenanceUtil) – Utility class instance for handling RO-Crate creation, software registration, and FAIRSCAPE dataset tracking. Default is a new ProvenanceUtil object.

  • provenance (dict or None) –

    Optional dictionary containing explicit provenance metadata, such as dataset name, project, organization, keywords, and description. If present, it will override missing information in RO-Crate.

    Example:

    {
        'name': 'Cell Image Embedding Dataset',
        'organization-name': 'CM4AI',
        'project-name': 'Cell Image Project',
        'keywords': ['embedding', 'microscopy'],
        'description': 'Embedding of IF microscopy images.'
    }
    

generate_readme()[source]
get_image_embedding_file()[source]

Gets image embedding file :return:

get_image_probability_file()[source]

Gets image probability file :return:

run()[source]

Runs cellmaps_image_embedding

Returns:

class cellmaps_image_embedding.runner.DensenetEmbeddingGenerator(inputdir, dimensions=1024, outdir=None, model_path=None, suffix='.jpg', fold=1, img_emd_translator=None)[source]

Bases: EmbeddingGenerator

Runs densenet bundled with this tool via command line to generate embedding. Why do it this way? Easier transition from the original Densenet code and no memory leaks

Constructor

Parameters:
  • inputdir (str) – Directory where red, blue, green, and yellow image directories reside

  • dimensions (int) – Desired size of output embedding vector

  • pythonbinary (str) – Path to python binary, if set to None the version of python that invoked this command will be used

  • predict (str) – Path to prediction script. Default value is the script bundled with this tool

  • model_path (str) – Path to model file

  • suffix (str) – Image suffix with starting .

  • img_emd_translator

get_datasets_that_need_to_be_registered()[source]

Gets model.pth dataset that needs to be registered with FAIRSCAPE.

Warning

Must not be called before invocation of get_next_embedding()

Raises:

CellMapsImageEmbeddingError – If this method is called before at least one invocation of get_next_embedding()

Returns:

list of tuples in format of (dict, filepath as str)

Return type:

list

get_next_embedding()[source]

Generator method for getting next embedding.

Returns:

Embedding vector with 1st element

Return type:

list

class cellmaps_image_embedding.runner.EmbeddingGenerator(dimensions=1024, fold=1)[source]

Bases: object

Base class for implementations that generate network embeddings

Constructor

DEFAULT_FOLD = 1
DIMENSIONS = 1024
SUFFIX = '.jpg'
get_datasets_that_need_to_be_registered()[source]

Gets any datasets that need to be registered with FAIRSCAPE

Returns:

list of tuples in format of (dict, filepath as str)

Return type:

list

get_dimensions()[source]

Gets number of dimensions this embedding will generate

Returns:

number of dimensions aka vector length

Return type:

int

get_fold()[source]

Gets fold :return: :rtype: int

get_next_embedding()[source]

Generator method for getting next embedding. Caller should implement with yield operator

Raises:

NotImplementedError: Subclasses should implement this

Returns:

Embedding

Return type:

list

class cellmaps_image_embedding.runner.FakeEmbeddingGenerator(inputdir, dimensions=1024, fold=1, suffix='.jpg', img_emd_translator=None)[source]

Bases: EmbeddingGenerator

Fakes image embedding

Constructor

Parameters:
  • inputdir (str) – Directory where images reside under red, green, blue, and yellow directories

  • dimensions (int) – Desired size of output embedding

  • suffix (str) – Image suffix with starting .

get_next_embedding()[source]

Generator method for getting next embedding. Caller should implement with yield operator

Raises:

NotImplementedError: Subclasses should implement this

Returns:

Embedding

Return type:

list

class cellmaps_image_embedding.runner.ImageEmbeddingFilterAndNameTranslator(image_downloaddir=None, fold=1)[source]

Bases: object

Converts image embedding names and filters keeping only one per gene

Constructor

get_name_mapping()[source]

Gets mapping of old name to new name

Returns:

mapping of old name to new name

Return type:

dict

Module contents

Top-level package for cellmaps_image_embedding.