|
3 | 3 | import gc |
4 | 4 | import logging |
5 | 5 | import re |
6 | | -import os |
7 | 6 | from pathlib import Path |
8 | 7 | from collections import defaultdict |
9 | 8 |
|
@@ -410,191 +409,6 @@ def load_channel_locations(eid, probe=None, one=None, aligned=False, brain_atlas |
410 | 409 | return channels |
411 | 410 |
|
412 | 411 |
|
413 | | -def load_spike_sorting_fast(eid, one=None, probe=None, dataset_types=None, spike_sorter=None, revision=None, |
414 | | - brain_regions=None, nested=True, collection=None, return_collection=False): |
415 | | - """ |
416 | | - From an eid, loads spikes and clusters for all probes |
417 | | - The following set of dataset types are loaded: |
418 | | - 'clusters.channels', |
419 | | - 'clusters.depths', |
420 | | - 'clusters.metrics', |
421 | | - 'spikes.clusters', |
422 | | - 'spikes.times', |
423 | | - 'probes.description' |
424 | | - :param eid: experiment UUID or pathlib.Path of the local session |
425 | | - :param one: an instance of OneAlyx |
426 | | - :param probe: name of probe to load in, if not given all probes for session will be loaded |
427 | | - :param dataset_types: additional spikes/clusters objects to add to the standard default list |
428 | | - :param spike_sorter: name of the spike sorting you want to load (None for default) |
429 | | - :param collection: name of the spike sorting collection to load - exclusive with spike sorter name ex: "alf/probe00" |
430 | | - :param brain_regions: iblatlas.regions.BrainRegions object - will label acronyms if provided |
431 | | - :param nested: if a single probe is required, do not output a dictionary with the probe name as key |
432 | | - :param return_collection: (False) if True, will return the collection used to load |
433 | | - :return: spikes, clusters, channels (dict of bunch, 1 bunch per probe) |
434 | | - """ |
435 | | - _logger.warning('Deprecation warning: brainbox.io.one.load_spike_sorting_fast will be removed in future versions.' |
436 | | - 'Use brainbox.io.one.SpikeSortingLoader instead') |
437 | | - if collection is None: |
438 | | - collection = _collection_filter_from_args(probe, spike_sorter) |
439 | | - _logger.debug(f"load spike sorting with collection filter {collection}") |
440 | | - kwargs = dict(eid=eid, one=one, collection=collection, revision=revision, dataset_types=dataset_types, |
441 | | - brain_regions=brain_regions) |
442 | | - spikes, clusters, channels = _load_spike_sorting(**kwargs, return_channels=True) |
443 | | - clusters = merge_clusters_channels(clusters, channels, keys_to_add_extra=None) |
444 | | - if nested is False and len(spikes.keys()) == 1: |
445 | | - k = list(spikes.keys())[0] |
446 | | - channels = channels[k] |
447 | | - clusters = clusters[k] |
448 | | - spikes = spikes[k] |
449 | | - if return_collection: |
450 | | - return spikes, clusters, channels, collection |
451 | | - else: |
452 | | - return spikes, clusters, channels |
453 | | - |
454 | | - |
455 | | -def load_spike_sorting(eid, one=None, probe=None, dataset_types=None, spike_sorter=None, revision=None, |
456 | | - brain_regions=None, return_collection=False): |
457 | | - """ |
458 | | - From an eid, loads spikes and clusters for all probes |
459 | | - The following set of dataset types are loaded: |
460 | | - 'clusters.channels', |
461 | | - 'clusters.depths', |
462 | | - 'clusters.metrics', |
463 | | - 'spikes.clusters', |
464 | | - 'spikes.times', |
465 | | - 'probes.description' |
466 | | - :param eid: experiment UUID or pathlib.Path of the local session |
467 | | - :param one: an instance of OneAlyx |
468 | | - :param probe: name of probe to load in, if not given all probes for session will be loaded |
469 | | - :param dataset_types: additional spikes/clusters objects to add to the standard default list |
470 | | - :param spike_sorter: name of the spike sorting you want to load (None for default) |
471 | | - :param brain_regions: iblatlas.regions.BrainRegions object - will label acronyms if provided |
472 | | - :param return_collection:(bool - False) if True, returns the collection for loading the data |
473 | | - :return: spikes, clusters (dict of bunch, 1 bunch per probe) |
474 | | - """ |
475 | | - _logger.warning('Deprecation warning: brainbox.io.one.load_spike_sorting will be removed in future versions.' |
476 | | - 'Use brainbox.io.one.SpikeSortingLoader instead') |
477 | | - collection = _collection_filter_from_args(probe, spike_sorter) |
478 | | - _logger.debug(f"load spike sorting with collection filter {collection}") |
479 | | - spikes, clusters = _load_spike_sorting(eid=eid, one=one, collection=collection, revision=revision, |
480 | | - return_channels=False, dataset_types=dataset_types, |
481 | | - brain_regions=brain_regions) |
482 | | - if return_collection: |
483 | | - return spikes, clusters, collection |
484 | | - else: |
485 | | - return spikes, clusters |
486 | | - |
487 | | - |
488 | | -def load_spike_sorting_with_channel(eid, one=None, probe=None, aligned=False, dataset_types=None, |
489 | | - spike_sorter=None, brain_atlas=None, nested=True, return_collection=False): |
490 | | - """ |
491 | | - For a given eid, get spikes, clusters and channels information, and merges clusters |
492 | | - and channels information before returning all three variables. |
493 | | -
|
494 | | - Parameters |
495 | | - ---------- |
496 | | - eid : [str, UUID, Path, dict] |
497 | | - Experiment session identifier; may be a UUID, URL, experiment reference string |
498 | | - details dict or Path |
499 | | - one : one.api.OneAlyx |
500 | | - An instance of ONE (shouldn't be in 'local' mode) |
501 | | - probe : [str, list of str] |
502 | | - The probe label(s), e.g. 'probe01' |
503 | | - aligned : bool |
504 | | - Whether to get the latest user aligned channel when not resolved or use histology track |
505 | | - dataset_types : list of str |
506 | | - Optional additional spikes/clusters objects to add to the standard default list |
507 | | - spike_sorter : str |
508 | | - Name of the spike sorting you want to load (None for default which is pykilosort if it's |
509 | | - available otherwise the default MATLAB kilosort) |
510 | | - brain_atlas : iblatlas.atlas.BrainAtlas |
511 | | - Brain atlas object (default: Allen atlas) |
512 | | - return_collection: bool |
513 | | - Returns an extra argument with the collection chosen |
514 | | -
|
515 | | - Returns |
516 | | - ------- |
517 | | - spikes : dict of one.alf.io.AlfBunch |
518 | | - A dict with probe labels as keys, contains bunch(es) of spike data for the provided |
519 | | - session and spike sorter, with keys ('clusters', 'times') |
520 | | - clusters : dict of one.alf.io.AlfBunch |
521 | | - A dict with probe labels as keys, contains bunch(es) of cluster data, with keys |
522 | | - ('channels', 'depths', 'metrics') |
523 | | - channels : dict of one.alf.io.AlfBunch |
524 | | - A dict with probe labels as keys, contains channel locations with keys ('acronym', |
525 | | - 'atlas_id', 'x', 'y', 'z'). Atlas IDs non-lateralized. |
526 | | - """ |
527 | | - # --- Get spikes and clusters data |
528 | | - _logger.warning('Deprecation warning: brainbox.io.one.load_spike_sorting will be removed in future versions.' |
529 | | - 'Use brainbox.io.one.SpikeSortingLoader instead') |
530 | | - one = one or ONE() |
531 | | - brain_atlas = brain_atlas or AllenAtlas() |
532 | | - spikes, clusters, collection = load_spike_sorting( |
533 | | - eid, one=one, probe=probe, dataset_types=dataset_types, spike_sorter=spike_sorter, return_collection=True) |
534 | | - # -- Get brain regions and assign to clusters |
535 | | - channels = load_channel_locations(eid, one=one, probe=probe, aligned=aligned, |
536 | | - brain_atlas=brain_atlas) |
537 | | - clusters = merge_clusters_channels(clusters, channels, keys_to_add_extra=None) |
538 | | - if nested is False and len(spikes.keys()) == 1: |
539 | | - k = list(spikes.keys())[0] |
540 | | - channels = channels[k] |
541 | | - clusters = clusters[k] |
542 | | - spikes = spikes[k] |
543 | | - if return_collection: |
544 | | - return spikes, clusters, channels, collection |
545 | | - else: |
546 | | - return spikes, clusters, channels |
547 | | - |
548 | | - |
549 | | -def load_ephys_session(eid, one=None): |
550 | | - """ |
551 | | - From an eid, hits the Alyx database and downloads a standard default set of dataset types |
552 | | - From a local session Path (pathlib.Path), loads a standard default set of dataset types |
553 | | - to perform analysis: |
554 | | - 'clusters.channels', |
555 | | - 'clusters.depths', |
556 | | - 'clusters.metrics', |
557 | | - 'spikes.clusters', |
558 | | - 'spikes.times', |
559 | | - 'probes.description' |
560 | | -
|
561 | | - Parameters |
562 | | - ---------- |
563 | | - eid : [str, UUID, Path, dict] |
564 | | - Experiment session identifier; may be a UUID, URL, experiment reference string |
565 | | - details dict or Path |
566 | | - one : oneibl.one.OneAlyx, optional |
567 | | - ONE object to use for loading. Will generate internal one if not used, by default None |
568 | | -
|
569 | | - Returns |
570 | | - ------- |
571 | | - spikes : dict of one.alf.io.AlfBunch |
572 | | - A dict with probe labels as keys, contains bunch(es) of spike data for the provided |
573 | | - session and spike sorter, with keys ('clusters', 'times') |
574 | | - clusters : dict of one.alf.io.AlfBunch |
575 | | - A dict with probe labels as keys, contains bunch(es) of cluster data, with keys |
576 | | - ('channels', 'depths', 'metrics') |
577 | | - trials : one.alf.io.AlfBunch of numpy.ndarray |
578 | | - The session trials data |
579 | | - """ |
580 | | - assert one |
581 | | - spikes, clusters = load_spike_sorting(eid, one=one) |
582 | | - trials = one.load_object(eid, 'trials') |
583 | | - return spikes, clusters, trials |
584 | | - |
585 | | - |
586 | | -def _remove_old_clusters(session_path, probe): |
587 | | - # gets clusters and spikes from a local session folder |
588 | | - probe_path = session_path.joinpath('alf', probe) |
589 | | - |
590 | | - # look for clusters.metrics.csv file, if it exists delete as we now have .pqt file instead |
591 | | - cluster_file = probe_path.joinpath('clusters.metrics.csv') |
592 | | - |
593 | | - if cluster_file.exists(): |
594 | | - os.remove(cluster_file) |
595 | | - _logger.info('Deleting old clusters.metrics.csv file') |
596 | | - |
597 | | - |
598 | 412 | def merge_clusters_channels(dic_clus, channels, keys_to_add_extra=None): |
599 | 413 | """ |
600 | 414 | Takes (default and any extra) values in given keys from channels and assign them to clusters. |
|
0 commit comments