示例#1
0
    def phy2alf_conversion(session_path, ks2_path, alf_path, probe_label):
        try:
            # Find spikeglx meta data files associated with the session and probe
            files = spikeglx.glob_ephys_files(session_path, ext='meta')
            ap_files = [(ef.get("ap"), ef.get("label")) for ef in files
                        if "ap" in ef.keys()]
            meta_file = next(ap[0] for ap in ap_files if ap[1] == probe_label)

            # The .cbin file doesn't always still exist on server so point to it from meta
            ap_file = meta_file.with_suffix('.cbin')

            # Convert to alf format
            spikes.ks2_to_alf(
                ks2_path,
                bin_path=meta_file.parent,
                out_path=alf_path,
                bin_file=None,
                ampfactor=SpikeSorting_KS2_Matlab._sample2v(ap_file))

            # Sync the probes
            out_files, _ = spikes.sync_spike_sorting(ap_file=ap_file,
                                                     out_path=alf_path)

            return 0, out_files, None

        except BaseException as err:
            _logger.error(
                f'{session_path} and {probe_label} errored with message: {err}'
            )

            return -1, None, err
    def _run(self, overwrite=False):
        """
        Multiple steps. For each probe:
        - Runs ks2 (skips if it already ran)
        - synchronize the spike sorting
        - output the probe description files
        :param overwrite:
        :return: list of files to be registered on database
        """
        efiles = spikeglx.glob_ephys_files(self.session_path)
        ap_files = [(ef.get('ap'), ef.get('label')) for ef in efiles
                    if 'ap' in ef.keys()]
        out_files = []
        for ap_file, label in ap_files:
            try:
                ks2_dir = self._run_ks2(
                    ap_file)  # runs ks2, skips if it already ran
                probe_out_path = self.session_path.joinpath('alf', label)
                probe_out_path.mkdir(parents=True, exist_ok=True)
                spikes.ks2_to_alf(ks2_dir,
                                  bin_path=ap_file.parent,
                                  out_path=probe_out_path,
                                  bin_file=ap_file,
                                  ampfactor=self._sample2v(ap_file))
                out, _ = spikes.sync_spike_sorting(ap_file=ap_file,
                                                   out_path=probe_out_path)
                out_files.extend(out)
            except BaseException as err:
                _logger.error(err)
                self.status = -1
                continue

        probe_files = spikes.probes_description(self.session_path,
                                                one=self.one)
        return out_files + probe_files
示例#3
0
def sorting_sync_and_alf(session_path, overwrite=False):
    """
    Multiple steps. For each probe:
    - Runs ks2 (skips if it already ran)
    - synchronize the spike sorting
    - output the probe description files
    :param overwrite:
    :return: list of files to be registered on database
    """
    efiles = spikeglx.glob_ephys_files(session_path)
    ap_files = [(ef.get("ap"), ef.get("label")) for ef in efiles
                if "ap" in ef.keys()]
    out_files = []
    for ap_file, label in ap_files:
        ks2_dir = session_path.joinpath("spike_sorters", "ks2_matlab", label)
        probe_out_path = session_path.joinpath("alf", label)
        probe_out_path.mkdir(parents=True, exist_ok=True)
        spikes.ks2_to_alf(
            ks2_dir,
            bin_path=ap_file.parent,
            out_path=probe_out_path,
            bin_file=ap_file,
            ampfactor=_sample2v(ap_file),
        )
        out, _ = spikes.sync_spike_sorting(ap_file=ap_file,
                                           out_path=probe_out_path)
        out_files.extend(out)
        # convert ks2_output into tar file and also register
        # Make this in case spike sorting is in old raw_ephys_data folders, for new
        # sessions it should already exist
        tar_dir = session_path.joinpath('spike_sorters', 'ks2_matlab', label)
        tar_dir.mkdir(parents=True, exist_ok=True)
        out = spikes.ks2_to_tar(ks2_dir, tar_dir)
        out_files.extend(out)
示例#4
0
 def _run(self, overwrite=False):
     """
     Multiple steps. For each probe:
     - Runs ks2 (skips if it already ran)
     - synchronize the spike sorting
     - output the probe description files
     :param overwrite:
     :return: list of files to be registered on database
     """
     efiles = spikeglx.glob_ephys_files(self.session_path)
     ap_files = [(ef.get("ap"), ef.get("label")) for ef in efiles
                 if "ap" in ef.keys()]
     out_files = []
     for ap_file, label in ap_files:
         try:
             ks2_dir = self._run_ks2(
                 ap_file)  # runs ks2, skips if it already ran
             probe_out_path = self.session_path.joinpath("alf", label)
             probe_out_path.mkdir(parents=True, exist_ok=True)
             spikes.ks2_to_alf(
                 ks2_dir,
                 bin_path=ap_file.parent,
                 out_path=probe_out_path,
                 bin_file=ap_file,
                 ampfactor=self._sample2v(ap_file),
             )
             out, _ = spikes.sync_spike_sorting(ap_file=ap_file,
                                                out_path=probe_out_path)
             out_files.extend(out)
             # convert ks2_output into tar file and also register
             # Make this in case spike sorting is in old raw_ephys_data folders, for new
             # sessions it should already exist
             tar_dir = self.session_path.joinpath('spike_sorters',
                                                  'ks2_matlab', label)
             tar_dir.mkdir(parents=True, exist_ok=True)
             out = spikes.ks2_to_tar(ks2_dir, tar_dir)
             out_files.extend(out)
         except BaseException:
             _logger.error(traceback.format_exc())
             self.status = -1
             continue
     probe_files = spikes.probes_description(self.session_path,
                                             one=self.one)
     return out_files + probe_files