import os
import pandas as pd
from allensdk.config.manifest import Manifest
from allensdk.core import json_utilities
from allensdk.core.mouse_connectivity_cache import MouseConnectivityCache
from ..api.queries.mouse_connectivity_api_prerelease \
import MouseConnectivityApiPrerelease
[docs]class MouseConnectivityCachePrerelease(MouseConnectivityCache):
"""Extends MouseConnectivityCache to use prereleased data from lims.
Attributes
----------
resolution: int
Resolution of grid data to be downloaded when accessing projection volume,
the annotation volume, and the annotation volume. Must be one of (10, 25,
50, 100). Default is 25.
api: MouseConnectivityApiPrerelease instance
Used internally to make API queries.
Parameters
----------
resolution: int
Resolution of grid data to be downloaded when accessing projection volume,
the annotation volume, and the annotation volume. Must be one of (10, 25,
50, 100). Default is 25.
ccf_version: string
Desired version of the Common Coordinate Framework. This affects the annotation
volume (get_annotation_volume) and structure masks (get_structure_mask).
Must be one of (MouseConnectivityApi.CCF_2015, MouseConnectivityApi.CCF_2016).
Default: MouseConnectivityApi.CCF_2016
cache: boolean
Whether the class should save results of API queries to locations specified
in the manifest file. Queries for files (as opposed to metadata) must have a
file location. If caching is disabled, those locations must be specified
in the function call (e.g. get_projection_density(file_name='file.nrrd')).
manifest_file: string
File name of the manifest to be read. Default is "mouse_connectivity_manifest.json".
"""
EXPERIMENTS_PRERELEASE_KEY = 'EXPERIMENTS_PRERELEASE'
STORAGE_DIRECTORIES_PRERELEASE_KEY = 'STORAGE_DIRECTORIES_PRERELEASE'
# allows user to pass 'male', 'female' instead of only 'm', 'f'
_GENDER_DICT=dict(male='m', female='f')
def __init__(self,
resolution=None,
cache=True,
manifest_file='mouse_connectivity_manifest_prerelease.json',
ccf_version=None,
version=None,
cache_storage_directories=True,
storage_directories_file_name=None):
super(MouseConnectivityCachePrerelease, self).__init__(
resolution=resolution, cache=cache, manifest_file=manifest_file,
ccf_version=ccf_version, version=version)
file_name = self.get_cache_path(storage_directories_file_name,
self.STORAGE_DIRECTORIES_PRERELEASE_KEY)
self.api = MouseConnectivityApiPrerelease(
file_name, cache_storage_directories=cache_storage_directories)
[docs] def get_experiments(self,
dataframe=False,
file_name=None,
cre=None,
injection_structure_ids=None,
age=None,
gender=None,
workflow_state=None,
workflows=None,
project_code=None):
"""Read a list of experiments.
If caching is enabled, this will save the whole (unfiltered) list of
experiments to a file.
Parameters
----------
dataframe: boolean
Return the list of experiments as a Pandas DataFrame. If False,
return a list of dictionaries. Default False.
file_name: string
File name to save/read the structures table. If file_name is None,
the file_name will be pulled out of the manifest. If caching
is disabled, no file will be saved. Default is None.
"""
file_name = self.get_cache_path(file_name,
self.EXPERIMENTS_PRERELEASE_KEY)
if os.path.exists(file_name):
experiments = json_utilities.read(file_name)
else:
experiments = self.api.get_experiments()
if self.cache:
Manifest.safe_make_parent_dirs(file_name)
json_utilities.write(file_name, experiments)
# filter the read/downloaded list of experiments
experiments = self.filter_experiments(experiments,
cre,
injection_structure_ids,
age,
gender,
workflow_state,
workflows,
project_code)
if dataframe:
experiments = pd.DataFrame(experiments)
experiments.set_index(['id'], inplace=True, drop=False)
return experiments
[docs] def filter_experiments(self,
experiments,
cre=None,
injection_structure_ids=None,
age=None,
gender=None,
workflow_state=None,
workflows=None,
project_code=None):
"""
Take a list of experiments and filter them by cre status and injection structure.
Parameters
----------
cre: boolean or list
If True, return only cre-positive experiments. If False, return only
cre-negative experiments. If None, return all experients. If list, return
all experiments with cre line names in the supplied list. Default None.
injection_structure_ids: list
Only return experiments that were injected in the structures provided here.
If None, return all experiments. Default None.
age : list
Only return experiments with specimens with ages provided here.
If None, returna all experiments. Default None.
"""
experiments = super(MouseConnectivityCachePrerelease, self).filter_experiments(
experiments, cre=cre, injection_structure_ids=injection_structure_ids)
# all kwargs == None base case
conditions = [lambda d: True]
if age is not None:
age = [a.lower() for a in age]
conditions.append(lambda d: d['age'].lower() in age)
if gender is not None:
# TODO: pass a string instead of an iterable?
gender = [self._GENDER_DICT.get(g.lower(), g.lower()) for g in gender]
conditions.append(lambda d: d['gender'].lower() in gender)
if workflow_state is not None:
#workflow_state = map(str.lower, workflow_state)
workflow_state = [ws.lower() for ws in workflow_state]
conditions.append(lambda d: d['workflow_state'].lower() in workflow_state)
if workflows is not None:
workflows = [w.lower() for w in workflows]
conditions.append(lambda d: any([w.lower() in workflows
for w in d['workflows']]))
if project_code is not None:
project_code = [pc.lower() for pc in project_code]
conditions.append(lambda d: d['project_code'].lower() in project_code)
return [e for e in experiments if all(f(e) for f in conditions)]
[docs] def add_manifest_paths(self, manifest_builder):
"""
Construct a manifest for this Cache class and save it in a file.
Parameters
----------
file_name: string
File location to save the manifest.
"""
manifest_builder = super(MouseConnectivityCachePrerelease, self)\
.add_manifest_paths(manifest_builder)
manifest_builder.add_path(self.EXPERIMENTS_PRERELEASE_KEY,
'experiments_prerelease.json',
parent_key='BASEDIR',
typename='file')
manifest_builder.add_path(self.STORAGE_DIRECTORIES_PRERELEASE_KEY,
'storage_directories_prerelease.json',
parent_key='BASEDIR',
typename='file')
return manifest_builder