예제 #1
0
def sort(algorithm: str,
         recording_path: str,
         sorting_out: str = None,
         params: dict = None,
         container: str = 'default',
         git_annex_mode=True,
         use_singularity: bool = False,
         job_timeout: float = 3600) -> str:

    from spikeforest2 import sorters
    HITHER_USE_SINGULARITY = os.getenv('HITHER_USE_SINGULARITY')
    if HITHER_USE_SINGULARITY is None:
        HITHER_USE_SINGULARITY = False
    print('HITHER_USE_SINGULARITY: ' + HITHER_USE_SINGULARITY)
    if not hasattr(sorters, algorithm):
        raise Exception('Sorter not found: {}'.format(algorithm))
    sorter = getattr(sorters, algorithm)
    if algorithm in [
            'kilosort2', 'kilosort', 'ironclust', 'tridesclous', 'jrclust'
    ]:
        gpu = True
    else:
        gpu = False
    if not sorting_out:
        sorting_out = hither.File()
    if not recording_path.startswith(
            'sha1dir://') or not recording_path.startswith('sha1://'):
        if os.path.isfile(recording_path):
            recording_path = ka.store_file(recording_path)
        elif os.path.isdir(recording_path):
            recording_path = ka.store_dir(recording_path,
                                          git_annex_mode=git_annex_mode)
    if params is None:
        params = dict()
    params_hither = dict(gpu=gpu, container=container)
    if job_timeout is not None:
        params_hither['job_timeout'] = job_timeout
    with hither.config(**params_hither):
        result = sorter.run(recording_path=recording_path,
                            sorting_out=sorting_out,
                            **params)
    print('SORTING')
    print('==============================================')
    return ka.store_file(result.outputs.sorting_out._path,
                         basename='firings.mda')


# def set_params(sorter, params_file):
#     params = {}
#     names_float = ['detection_thresh']
#     with open(params_file, 'r') as myfile:
#         for line in myfile:
#             name, var = line.partition("=")[::2]
#             name = name.strip()

#             params[name.strip()] = var
#     sorter.set_params(**params)
def patch_recording_geom(recording, geom_fname):
    print(f'PATCHING geom for recording: {recording["name"]}')
    geom_info = ka.get_file_info(geom_fname)
    x = recording['directory']
    y = ka.store_dir(x).replace('sha1dir://', 'sha1://')
    obj = ka.load_object(y)
    obj['files']['geom.csv'] = dict(size=geom_info['size'],
                                    sha1=geom_info['sha1'])
    x2 = ka.store_object(obj)
    recording['directory'] = 'sha1dir://' + ka.get_file_hash(x2) + '.patched'
예제 #3
0
def register_study(*,
                   path_from,
                   path_to,
                   studySetName,
                   studyName,
                   to='default_readwrite'):
    list_rec = [
        str(f) for f in os.listdir(path_from)
        if os.path.isdir(os.path.join(path_from, f))
    ]
    print('# files: {}'.format(len(list_rec)))
    study_obj = dict(name=studyName, studySetName=studySetName, recordings=[])
    mkdir_(path_to)
    for rec1 in list_rec:
        print(f'Uploading {rec1}')
        path_rec1 = os.path.join(path_from, rec1)
        register_groundtruth(recdir=path_rec1,
                             output_fname=os.path.join(
                                 path_to, rec1 + '.firings_true.json'),
                             label=rec1)
        rec = MdaRecordingExtractor(recording_directory=path_rec1)
        sorting = MdaSortingExtractor(firings_file=path_rec1 +
                                      '/firings_true.mda',
                                      samplerate=rec.get_sampling_frequency())
        recording_obj = dict(
            name=rec1,
            studyName=studyName,
            studySetName=studySetName,
            directory=ka.store_dir(path_rec1),
            firingsTrue=ka.store_file(os.path.join(path_to, rec1 +
                                                   '.firings_true.json'),
                                      basename='firings_true.json'),
            sampleRateHz=rec.get_sampling_frequency(),
            numChannels=len(rec.get_channel_ids()),
            durationSec=rec.get_num_frames() / rec.get_sampling_frequency(),
            numTrueUnits=len(sorting.get_unit_ids()),
            spikeSign=-1  # TODO: get this from params.json
        )
        study_obj['recordings'].append(recording_obj)
        # update .json files
        register_recording(recdir=path_rec1,
                           output_fname=os.path.join(path_to, rec1 + '.json'),
                           label=rec1)
    study_obj['self_reference'] = ka.store_object(study_obj)
    with open(os.path.join(path_to, studyName + '.json'), 'w') as f:
        json.dump(study_obj, f, indent=4)
    return study_obj
예제 #4
0
    if os.path.isdir(os.path.join(path_from, f))
]

print('# files: {}'.format(len(list_rec)))
study_obj = dict(name=study_name, studySetName=studyset_name, recordings=[])
for rec1 in list_rec:
    print(f'Uploading {rec1}')
    path_rec1 = os.path.join(path_from, rec1)
    rec = MdaRecordingExtractor(recording_directory=path_rec1)
    sorting = MdaSortingExtractor(firings_file=path_rec1 + '/firings_true.mda',
                                  samplerate=rec.get_sampling_frequency())
    recording_obj = dict(
        name=rec1,
        studyName=study_name,
        studySetName=studyset_name,
        directory=ka.store_dir(path_rec1),
        firingsTrue=ka.store_file(os.path.join(path_to,
                                               rec1 + '.firings_true.json'),
                                  basename='firings_true.json'),
        sampleRateHz=rec.get_sampling_frequency(),
        numChannels=len(rec.get_channel_ids()),
        durationSec=rec.get_num_frames() / rec.get_sampling_frequency(),
        numTrueUnits=len(sorting.get_unit_ids()),
        spikeSign=-1  # TODO: get this from params.json
    )
    study_obj['recordings'].append(recording_obj)

study_obj['self_reference'] = ka.store_object(study_obj)
with open(os.path.join(path_to, study_name + '.json'), 'w') as f:
    json.dump(study_obj, f, indent=4)