deepcinac.cinac_predictor

Module Contents

Classes

CinacPredictor

Functions

fusion_cell_type_predictions_by_type(...[, ...])

Allows to associate a cell type to a prediction (a prediction being a 2d array of n_cells * n_classes)

fusion_cell_type_predictions(cell_type_pred_1, ...[, ...])

Take two cell type predictions (should be for the same movie, so the same number of cells in both),

select_activity_classifier_on_cell_type_outputs(...[, ...])

Allows to create a dictionary that will be passed to CinacPredictor.add_recording() as the model_files_dict

predict_cell_type_from_model(cinac_recording, cell, ...)

param cinac_recording

load_data_for_prediction(cinac_recording, cell, ...)

Create data that will be used to predict neural activity. The data will be representing by instances of

load_data_for_cell_type_prediction(cinac_recording, ...)

Create data that will be used to predict cell type. The data will be representing by instances of

predict_transient_from_model(cinac_recording, cell, ...)

param cinac_recording

activity_predictions_from_cinac_files(cinac_dir_name, ...)

Evaluate activity prediction on cinac file with ground truth.

evaluate_cell_type_predictions(cinac_dir_name, ...[, ...])

Evaluation the cell type prediction on cinac file with ground truth.

Attributes

TF_VERSION

deepcinac.cinac_predictor.TF_VERSION
deepcinac.cinac_predictor.fusion_cell_type_predictions_by_type(cell_type_preds_dict, default_cell_type_pred, cell_type_to_skip_if_conflict=None, cell_type_config_file=None, filename_to_save=None)

Allows to associate a cell type to a prediction (a prediction being a 2d array of n_cells * n_classes) :param cell_type_preds_dict: key: code of the cell type, value 2d array of n_cells * n_classes representing :param the predictions to associate to this cell type: :type the predictions to associate to this cell type: for cell predicted as so :param default_cell_type_pred: 2d array of n_cells * n_classes :param Same length as cell_type_preds_dict: :param cell_type_config_file: :param filename_to_save: :param cell_type_to_skip_if_conflict: int representing the cell type code, if given and there is a conflict between :param two classifier including this cell type: :param then the other one in conflict will be chosen. If a conflict with more: :param than 2 cell types: :param then the default one is chosen.:

Returns:

deepcinac.cinac_predictor.fusion_cell_type_predictions(cell_type_pred_1, cell_type_pred_2, with_new_category=None, cell_type_config_file=None, filename_to_save=None, cell_types_to_not_fusion_with_gt=None)

Take two cell type predictions (should be for the same movie, so the same number of cells in both), and fusion it so that cells that have a cell type probabilities in cell_type_pred_2 (sum of probablities equals to 1) will be transfered to cell_type_pred_1, so that the cell type for those cells in cell_type_pred_1 might change. If a yaml cell_type_config_file is provided, then verbose mode will be on and the changes will be print. It is possible to give filename_to_save and the new prediction will be saved in npy format :param cell_type_pred_1: 2d array (n_cells, n_classes), for each cell give the probability for the cell to be one of :param the cell type (n_classes represent the number of cell type possible.: :param cell_type_pred_2: 2d array (n_cells, n_classes), for each cell give the probability for the cell to be one of :param the cell type (n_classes represent the number of cell type possible.: :param cell_type_config_file: (str) yaml cell type that associate a cell type name to a code (one of the column :param in cell_type_pred): :param filename_to_save: (str) should have npy extension or it will be added, should include the full path. :param cell_types_to_not_fusion_with_gt: list of int, representing the cell types that should be replaced by the :param GT. For example if a cell is classified as Noise in pred_1 but INs in pred_2: :param then it stayed noise.: :param In that case cell_types_to_not_fusion_with_gt=[2]: :param with_new_category: list of tuple of 2 ints, if not None, then change the category in cell_type_pred_2 that :param are given to cell_type_pred_1: :param by either swaping the 2 numbers: :param or creating a new one if non existent. The first: :param number category will then be set to zero and the 2nd one will take the probability of the first one:

Returns: a 2d array (n_cells, n_classes) representing the new cell type predictions will be returned

deepcinac.cinac_predictor.select_activity_classifier_on_cell_type_outputs(cell_type_config_file, cell_type_predictions, cell_type_to_classifier, default_classifier, unknown_cell_type_action='ignore', cell_type_threshold=0.5, verbose=1)

Allows to create a dictionary that will be passed to CinacPredictor.add_recording() as the model_files_dict argument. Each cell will be given a specific activity classifier regarding its cell type. :param cell_type_config_file: yaml file containing the description of the cell classifier, allows :param to read the cell type predictions: :param cell_type_predictions: np array of 2 dimensions, (n_cells, n_cell_type), value between 0 and 1 predicting :param if the cell is this cell type: :param cell_type_threshold: use only if binary classifier, if multi_class not used; the cell_type with the maximum :param prediction is picked.: :param cell_type_to_classifier: (dict), key is the name of a cell type (ex: ‘interneuron’) it should be in the :param yaml file: :type yaml file: model file, weights file, classifier id :param value is tuple of 3 strings: :type value is tuple of 3 strings: model file, weights file, classifier id :param specific activity classifier to a cell type: :param default_classifier: (tuple of 3 strings) (model file, weights file, classifier id) that attribute a default

activity classifier in a cell has no predominant cell type

Parameters
  • unknown_cell_type_action – (str) Decide what to do when cell type is not in cell_type_to_classifier. Either

  • predicted ('ignore' meaning the cell with that cell type won't be) –

  • classifier ("default" we use the default) –

  • aborded (on it or "exception" then an exception would be raise and the excecution will be) –

  • verbose – (int) if > 0, then the number of cells by cell type will be printed

Returns: return a dict compatible with CinacPredictor.add_recording() model_files_dict argument. key is a tuple of 3 strings (model file, weights file, classifier id), and value is the cells whose activity should be classify using this model.

class deepcinac.cinac_predictor.CinacPredictor(verbose=0)
add_recording(cinac_recording, model_files_dict=None, removed_cells_mapping=None)

Add a recording as a set of a calcium imaging movie, ROIs, and a list of cell to predict :param cinac_recording: an instance of CinacRecording :param model_files_dict:

dictionary. Key is a tuple of three string representing the file_name of the json file,

the filename of the weights file and network_identifier and an identifier for the model

and weights used (will be added to the name of files containing the predictions in addition of recording identifier.) the value of the dict being a list or array of integers that represents the indices of the cell to be predicted by the given model and set of weights. If None, means all cells activity will be predicted. Cells not including, will have their prediction set to 0 for all the frames

Parameters
  • removed_cells_mapping – integers array of length the original numbers of cells

  • CinacRecording) ((such as defined in) –

  • been (and as value either of positive int representing the new index of the cell or -1 if the cell has) –

  • removed

Returns:

static get_new_cell_indices_if_cells_removed(cell_indices_array, removed_cells_mapping)

Take an array of int, and return another one with new index in case some cells would have been removed and some cells ROIs are not matching anymore :param cell_indices_array: np.array of integers, containing the cells indices to remap :param removed_cells_mapping: integers array of length the original numbers of cells :param (such as defined in CinacRecording): :param and as value either of positive int representing the new index of the cell or -1 if the cell has been: :param removed:

Returns: new_cell_indices_array an np.array that contains integers representing the new indices of cells that have not been removed original_cell_indices_mapping, np.array, for each new cell index, contains the corresponding original index

predict(results_path, overlap_value=0.5, output_file_formats='npy', cell_type_classifier_mode=False, n_segments_to_use_for_prediction=2, cell_type_pred_fct=np.mean, create_dir_for_results=True, cell_buffer=None, all_pixels=True, time_verbose=True, **kwargs)

Will predict the neural activity state of the cell for each frame of the calcium imaging movies. Recordings have to be added previously though the add_recording method. :param results_path: if None, predictions are not saved in a file :param overlap_value: :param cell_type_classifier_mode: means we want to classify each cell to know its type. So far return a prediction :param value between 0 and 1: :type value between 0 and 1: should be interneuron then :param 1 meaning 100% a pyramidal cell: :type 1 meaning 100% a pyramidal cell: should be interneuron then :param 0 100% sure not: :type 0 100% sure not: should be interneuron then :param output_file_formats: a string or list of string, representing the format in which saving the 2d array :param representing the prediction for each movie. The choices are: “mat” or “npy” or “npz”. npy and npz will both :param comes to a npz format. Two keys will be in the .mat or .npz: :param one is “predictions” containing a 1d or 2 array: :param with predictions inside (1d array for cell activity predictions: :param 2d array for cell type activity predicctions: :param (n_cells: :param n_cells_type): :param and the second key is “cells” which is a 1d array containing the indices of: :param the cells that has been predicted: :type the cells that has been predicted: other cells will have a 0 value prediction in the predictions array :param n_segments_to_use_for_prediction: used when cell_type_classifier_mode is at True. Indicate how many segment :param of window_len should be used to predict the cell_type. For example if the value is 2 and window_len is 200: :param : :param then we take the mean of the prediction over 2 segments of 200 frames. Those segments are selected based: :param on their activity: :type on their activity: the more a cell is active (amplitudes) the best chance the segment is to be selected :param cell_type_pred_fct: fct to use to average the predictons if it is done in more than one segment :param create_dir_for_results: if True, create a dir with timestamps to store the results. The directory is created :param in results_path. Else predictions are stored directly in results_path: :param cell_buffer: None or int, distance in order to transform the cell contour giving an approximate :param representation of all points within a given distance of the this geometric object: :param all_pixels: bool, use for cell type prediction, add input with all pixels :param time_verbose: if True, print the time to predict each cell :param **kwargs:

Returns: a dict with key being a tuple with 2 string (the id of the cinac recording and the id of the

classifier used) and value:

numpy array of n_cells containing the predictions (1d for cell activity, and 2d for cell type (n_cells, n_cell_types),.

deepcinac.cinac_predictor.predict_cell_type_from_model(cinac_recording, cell, model, n_frames, n_segments_to_use_for_prediction=2, overlap_value=0, pixels_around=0, all_pixels=False, use_data_augmentation=False, buffer=None, verbose=0)
Parameters
  • cinac_recording

  • cell

  • model

  • n_frames

  • n_segments_to_use_for_prediction

  • overlap_value

  • pixels_around

  • all_pixels

  • use_data_augmentation

  • buffer

  • verbose

Returns:

deepcinac.cinac_predictor.load_data_for_prediction(cinac_recording, cell, sliding_window_len, overlap_value, augmentation_functions, n_frames)

Create data that will be used to predict neural activity. The data will be representing by instances of MoviePatchData that will contains information concerning this movie segment for a given cell to give to the neuronal network. :param cinac_recording: instance of CinacRecording, contains the movie frames and the ROIs :param cell: integer, the cell index :param sliding_window_len: integer, length in frames of the window used by the neuronal network. Predictions will be :param made for each frame of this segment: :param overlap_value: float value between 0 and 1, representing by how much 2 movie segments will overlap. :param 0.5 is equivalent to a 50% overlap. It allows the network to avoid edge effect in order to get a full temporal: :param vision of all transient. A good default value is 0.5: :param augmentation_functions: list of function that takes an image (np array) as input and return a copy of the image :param transformed.: :param n_frames: number of frames in the movie

Returns: a list of MoviePatchData instance and an integer representing the index of the first frame of the patch

deepcinac.cinac_predictor.load_data_for_cell_type_prediction(cinac_recording, cell, sliding_window_len, overlap_value, augmentation_functions, n_frames, n_windows_len_to_keep_by_cell=2)

Create data that will be used to predict cell type. The data will be representing by instances of MoviePatchData that will contains information concerning this movie segment for a given cell to give to the neuronal network. :param cinac_recording: instance of CinacRecording, contains the movie frames and the ROIs :param cell: integer, the cell index :param sliding_window_len: integer, length in frames of the window used by the neuronal network. Predictions will be :param made for each frame of this segment: :param overlap_value: float value between 0 and 1, representing by how much 2 movie segments will overlap. :param 0.5 is equivalent to a 50% overlap. It allows the network to avoid edge effect in order to get a full temporal: :param vision of all transient. A good default value is 0.5: :param augmentation_functions: list of function that takes an image (np array) as input and return a copy of the image :param transformed.: :param n_frames: number of frames in the movie :param n_windows_len_to_keep_by_cell: how many segment of window_len frames should be used to predict cell_type, :param so the length of the returned list will be the same as n_windows_len_to_keep_by_cell:

Returns: a list of MoviePatchData instance and an integer representing the index of the first frame of the patch

deepcinac.cinac_predictor.predict_transient_from_model(cinac_recording, cell, model, n_frames, overlap_value=0.8, pixels_around=0, use_data_augmentation=False, buffer=None, time_verbose=True)
Parameters
  • cinac_recording

  • cell

  • model

  • n_frames

  • overlap_value

  • pixels_around

  • use_data_augmentation

  • buffer

  • time_verbose – if True, print the time to predict the cell

Returns:

deepcinac.cinac_predictor.activity_predictions_from_cinac_files(cinac_dir_name, results_path, json_file_name, weights_file_name, classifier_id=None, output_file_formats='npy')

Evaluate activity prediction on cinac file with ground truth. Load all cinac files from a folder and compare the predictions from the classifier to the activity in the cinac file and then display the metrics and can save the plot of the distribution of the probability by cell type. :param cinac_dir_name: Directory in which to find the .cinac file to use :param results_path: Directory in which to save the plots, can be None if save_activity_distribution is False :param json_file_name: .json file containing the model to use to classify the cell types :param weights_file_name: .h5 file containing the weights of the network to classify the cell types :param save_activity_distribution: (bool) if True, a figure representing the distribution of the predictions score :param for activity will be save in results_path:

Returns:

deepcinac.cinac_predictor.evaluate_cell_type_predictions(cinac_dir_name, cell_type_yaml_file, results_path, json_file_name, weights_file_name, save_cell_type_distribution, all_pixels=False)

Evaluation the cell type prediction on cinac file with ground truth. Load all cinac files from a folder and compare the predictions from the classifier to the cell type in the cinac file and then display the metrics and can save the plot of the distribution of the probability by cell type. :param cinac_dir_name: Directory in which to find the .cinac file to use :param cell_type_yaml_file: ymal file containing the configuration to use by the classifier :param (cell type: :param number of classes): :param results_path: Directory in which to save the plots, can be None if save_cell_type_distribution is False :param json_file_name: .json file containing the model to use to classify the cell types :param weights_file_name: .h5 file containing the weights of the network to classify the cell types :param save_cell_type_distribution: (bool) if True, a figure representing the distribution of the predictions score :param of each cell type will be save in results_path: :param all_pixels: if True, add an input with all pixels in the frame

Returns: