Esempio n. 1
0
    def _run(self, recording: se.RecordingExtractor, output_folder: Path):
        dataset_dir = output_folder / 'ironclust_dataset'
        source_dir = Path(__file__).parent

        samplerate = recording.get_sampling_frequency()

        num_channels = recording.get_num_channels()
        num_timepoints = recording.get_num_frames()
        duration_minutes = num_timepoints / samplerate / 60
        if self.verbose:
            print(
                'Num. channels = {}, Num. timepoints = {}, duration = {} minutes'
                .format(num_channels, num_timepoints, duration_minutes))

        if self.verbose:
            print('Creating argfile.txt...')
        txt = ''
        for key0, val0 in self.params.items():
            txt += '{}={}\n'.format(key0, val0)
        txt += 'samplerate={}\n'.format(samplerate)
        with (dataset_dir / 'argfile.txt').open('w') as f:
            f.write(txt)

        tmpdir = output_folder / 'tmp'
        os.makedirs(str(tmpdir), exist_ok=True)
        if self.verbose:
            print(
                'Running ironclust in {tmpdir}...'.format(tmpdir=str(tmpdir)))

        shell_cmd = '''
            #!/bin/bash
            cd {tmpdir}
            exec /run_irc {dataset_dir} {tmpdir} {dataset_dir}/argfile.txt
        '''.format(tmpdir=str(tmpdir), dataset_dir=str(dataset_dir))

        shell_script = ShellScript(shell_cmd, redirect_output_to_stdout=True)
        shell_script.start()

        retcode = shell_script.wait()

        if retcode != 0:
            raise Exception('ironclust returned a non-zero exit code')

        result_fname = str(tmpdir / 'firings.mda')
        if not os.path.exists(result_fname):
            raise Exception('Result file does not exist: ' + result_fname)

        samplerate_fname = str(tmpdir / 'samplerate.txt')
        with open(samplerate_fname, 'w') as f:
            f.write('{}'.format(samplerate))
Esempio n. 2
0
    def _run(self, recording, output_folder):
        source_dir = Path(Path(__file__).parent)
        p = self.params

        recording_folder=str((output_folder / 'recording').absolute())
        samplerate = recording.get_sampling_frequency()
        txt = ''
        for key0, val0 in p.items():
            txt += '{}={}\n'.format(key0, val0)
        txt += 'samplerate={}\n'.format(samplerate)
        params_file = output_folder / 'argfile.txt'
        with params_file.open('w') as f:
            f.write(txt)

        if os.getenv('JRCLUST_BINARY_PATH', None):
            shell_cmd = f'''
            #!/bin/bash
            exec $JRCLUST_BINARY_PATH {str(recording_folder)} {str(output_folder)} {str(params_file)}
            '''
        else:
            # copy m files
            shutil.copy(str(source_dir / 'matlab' / 'jrclust_binary.m'), str(output_folder))
            shutil.copy(str(source_dir / 'matlab' / 'writemda.m'), str(output_folder))
            matlab_script = f'''
            addpath(genpath('{self.jrclust_path}'));
            jrclust_binary('{str(recording_folder)}', '{str(output_folder)}', '{str(params_file)}')
            '''
            ShellScript(matlab_script).write(str(output_folder / 'jrclust_script.m'))
            if "win" in sys.platform:
                shell_cmd = f'''
                            cd {str(output_folder)}
                            matlab -nosplash -nodisplay -wait -r jrclust_script
                        '''
            else:
                shell_cmd = f'''
                            #!/bin/bash
                            cd "{str(output_folder)}"
                            matlab -nosplash -nodisplay -r jrclust_script
                        '''
        shell_cmd = ShellScript(shell_cmd, redirect_output_to_stdout=True)
        shell_cmd.start()

        retcode = shell_cmd.wait()

        if retcode != 0:
            raise Exception('jrclust returned a non-zero exit code')
Esempio n. 3
0
    def _run(self, recording, output_folder):
        source_dir = Path(Path(__file__).parent)
        p = self.params

        dat_file = str((output_folder / 'recording' / 'raw.mda').absolute())

        if os.getenv('WAVECLUS_BINARY_PATH', None):
            shell_cmd = f'''
            #!/bin/bash
            exec $WAVECLUS_BINARY_PATH {str(output_folder)} {dat_file} {str(output_folder)}/firings.mda {recording.get_sampling_frequency()}
            '''
        else:
            # copy m files
            shutil.copy(str(source_dir / 'matlab' / 'waveclus_binary.m'),
                        str(output_folder))
            shutil.copy(str(source_dir / 'matlab' / 'readmda.m'),
                        str(output_folder))
            shutil.copy(str(source_dir / 'matlab' / 'writemda.m'),
                        str(output_folder))
            shutil.copy(str(source_dir / 'matlab' / 'set_parameters_spf.m'),
                        str(output_folder))
            matlab_script = f'''
            addpath(genpath('{self.waveclus_path}'));
            waveclus_binary('{str(output_folder)}', '{dat_file}', '{str(output_folder)}/firings.mda', {recording.get_sampling_frequency()})
            '''
            ShellScript(matlab_script).write(
                str(output_folder / 'waveclus_script.m'))
            if "win" in sys.platform:
                shell_cmd = f'''
                            cd {str(output_folder)}
                            matlab -nosplash -nodisplay -wait -r waveclus_script
                        '''
            else:
                shell_cmd = f'''
                            #!/bin/bash
                            cd "{str(output_folder)}"
                            matlab -nosplash -nodisplay -r waveclus_script
                        '''
        shell_cmd = ShellScript(shell_cmd, redirect_output_to_stdout=True)
        shell_cmd.start()

        retcode = shell_cmd.wait()

        if retcode != 0:
            raise Exception('waveclus returned a non-zero exit code')
Esempio n. 4
0
    def _run(self, recording: se.RecordingExtractor, output_folder: Path):
        dataset_dir = output_folder / 'ironclust_dataset'
        source_dir = Path(__file__).parent

        samplerate = recording.get_sampling_frequency()

        num_channels = recording.get_num_channels()
        num_timepoints = recording.get_num_frames()
        duration_minutes = num_timepoints / samplerate / 60
        if self.verbose:
            print(
                'Num. channels = {}, Num. timepoints = {}, duration = {} minutes'
                .format(num_channels, num_timepoints, duration_minutes))

        if self.verbose:
            print('Creating argfile.txt...')
        txt = ''
        for key0, val0 in self.params.items():
            txt += '{}={}\n'.format(key0, val0)
        txt += 'samplerate={}\n'.format(samplerate)
        with (dataset_dir / 'argfile.txt').open('w') as f:
            f.write(txt)

        tmpdir = output_folder / 'tmp'
        os.makedirs(str(tmpdir), exist_ok=True)
        if self.verbose:
            print(
                'Running ironclust in {tmpdir}...'.format(tmpdir=str(tmpdir)))

        if os.getenv('IRONCLUST_BINARY_PATH', None):
            shell_cmd = f'''
            #!/bin/bash
            cd {tmpdir}
            exec $IRONCLUST_BINARY_PATH {dataset_dir} {tmpdir} {dataset_dir}/argfile.txt
            '''
        else:
            matlab_script = f'''
            try
                addpath(genpath('{self.ironclust_path}'));
                irc2('{dataset_dir}', '{str(tmpdir)}', '{dataset_dir}/argfile.txt')
            catch
                fprintf('----------------------------------------');
                fprintf(lasterr());
                quit(1);
            end
            quit(0);
            '''
            ShellScript(matlab_script).write(
                str(output_folder / 'ironclust_script.m'))

            if "win" in sys.platform:
                shell_cmd = f'''
                            cd {str(output_folder)}
                            matlab -nosplash -wait -batch ironclust_script
                        '''
            else:
                shell_cmd = f'''
                            #!/bin/bash
                            cd "{str(output_folder)}"
                            matlab -nosplash -nodisplay -r ironclust_script
                        '''
        shell_script = ShellScript(shell_cmd, redirect_output_to_stdout=True)
        shell_script.start()

        retcode = shell_script.wait()

        if retcode != 0:
            raise Exception('ironclust returned a non-zero exit code')

        result_fname = str(tmpdir / 'firings.mda')
        if not os.path.exists(result_fname):
            raise Exception('Result file does not exist: ' + result_fname)

        samplerate_fname = str(tmpdir / 'samplerate.txt')
        with open(samplerate_fname, 'w') as f:
            f.write('{}'.format(samplerate))
    def _run(self, recording, output_folder):
        source_dir = Path(Path(__file__).parent)
        p = self.params

        dat_file = str((output_folder / 'recording.dat').absolute())
        if p['car']:
            use_car = 1
        else:
            use_car = 0

        # prepare electrode positions
        if self.grouping_property == 'group' and 'group' in recording.get_shared_channel_property_names(
        ):
            groups = recording.get_channel_groups()
        else:
            groups = [1] * recording.get_num_channels()
        if 'location' in recording.get_shared_channel_property_names():
            positions = np.array(recording.get_channel_locations())
            if positions.shape[1] != 2:
                raise RuntimeError(
                    "3D 'location' are not supported. Set 2D locations instead"
                )
        else:
            print(
                "'location' information is not found. Using linear configuration"
            )
            positions = np.array(
                [[0, i_ch] for i_ch in range(recording.get_num_channels())])
        xcoords = [positions[i, 0] for i in range(positions.shape[0])]
        ycoords = [positions[i, 1] for i in range(positions.shape[0])]
        kcoords = groups

        params0 = dict(nchan=recording.get_num_channels(),
                       sample_rate=recording.get_sampling_frequency(),
                       freq_min=p["freq_min"],
                       projection_threshold=p["projection_threshold"],
                       minFR=p["minFR"],
                       sigmaMask=p["sigmaMask"],
                       preclust_threshold=p["preclust_threshold"],
                       kilo_thresh=p["detect_threshold"],
                       use_car=use_car,
                       nPCs=p["nPCs"],
                       xcoords=xcoords,
                       ycoords=ycoords,
                       kcoords=kcoords)
        params_path = str(output_folder / 'params.json')
        with open(params_path, 'w') as f:
            json.dump(params0, f)

        if os.getenv('KILOSORT2_BINARY_PATH', None):
            shell_cmd = f'''
            #!/bin/bash
            exec $KILOSORT2_BINARY_PATH {dat_file} {str(output_folder)} {params_path}
            '''
        else:
            # copy m files
            shutil.copy(str(source_dir / 'matlab' / 'kilosort2_master.m'),
                        str(output_folder))
            shutil.copy(str(source_dir / 'matlab' / 'kilosort2_config.m'),
                        str(output_folder))
            shutil.copy(str(source_dir / 'matlab' / 'kilosort2_channelmap.m'),
                        str(output_folder))
            shutil.copy(str(source_dir / 'matlab' / 'writeNPY.m'),
                        str(output_folder))
            shutil.copy(str(source_dir / 'matlab' / 'constructNPYheader.m'),
                        str(output_folder))
            matlab_script = f'''
            try
                addpath(genpath('{self.kilosort2_path}'));
                kilosort2_master('{dat_file}', '{str(output_folder)}', '{params_path}')
            catch
                fprintf('----------------------------------------');
                fprintf(lasterr());
                quit(1);
            end
            quit(0);
            '''
            ShellScript(matlab_script).write(
                str(output_folder / 'kilosort2_script.m'))
            if "win" in sys.platform:
                shell_cmd = f'''
                            cd {str(output_folder)}
                            matlab -nosplash -nodisplay -wait -r kilosort2_script
                        '''
            else:
                shell_cmd = f'''
                            #!/bin/bash
                            cd "{str(output_folder)}"
                            matlab -nosplash -nodisplay -r kilosort2_script
                        '''
        shell_cmd = ShellScript(shell_cmd, redirect_output_to_stdout=True)
        shell_cmd.start()

        retcode = shell_cmd.wait()

        if retcode != 0:
            raise Exception('kilosort2 returned a non-zero exit code')