Ejemplo n.º 1
0
    def load_session(self, session_id, kind="eeg", extension="edf"):
        """
        Load in the all runs for the session.

        Parameters
        ----------
        session_id :
        kind :
        extension :

        Returns
        -------
        list[mne.io.BaseRaw]
            A list of objects with raw EEG data for a single session

        """
        # query for the run ids in the session based on factors
        runs_in_session = self.query.get(
            target="run",
            subject=self.subject_id,
            session=self.session_id,
            return_type="id",
            datatype=kind,
            extension=extension,
        )

        # load in all the mne_raw datasets
        rawlist = []
        for run_id in runs_in_session:
            loader = BidsLoader(bids_root=self.bids_root, bids_basename=None)
            rawlist.append(loader.load_dataset())
        return rawlist
Ejemplo n.º 2
0
    def test_baseio(self):
        test_subjectid = "0001"
        session_id = "seizure"
        kind = "eeg"
        run_id = "01"
        task = "monitor"
        bids_basename = make_bids_basename(
            subject=test_subjectid,
            session=session_id,
            acquisition=kind,
            run=run_id,
            task=task,
            # suffix=kind + ".fif",
        )
        ext = "fif"
        bids_root = os.path.join(os.getcwd(), "./data/bids_layout/")

        # instantiate a loader/writer
        loader = BidsLoader(
            bids_root=bids_root,
            bids_basename=bids_basename,
            kind=kind,
            datatype=ext,
        )
        participant_dict = loader.load_participants_json()
        participant_df = loader.load_participants_tsv()
        scans_df = loader.load_scans_tsv()
        sidecar = loader.load_sidecar_json()
        chans_df = loader.load_channels_tsv()

        # check basic funcs
        print(loader.chanstsv_fpath)
        print(loader.datafile_fpath)
        print(loader.eventstsv_fpath)
        print(loader.rel_chanstsv_fpath)
        print(loader.rel_datafile_fpath)
        print(loader.rel_eventstsv_fpath)
        print(loader.rel_participantsjson_fpath)
        print(loader.rel_participantstsv_fpath)
        print(loader.rel_scanstsv_fpath)
        print(loader.rel_sidecarjson_fpath)

        with tempfile.TemporaryDirectory() as bids_root:
            writer = BidsWriter(
                bids_root=bids_root,
                bids_basename=bids_basename,
                kind=kind,
                datatype=ext,
            )
            with pytest.raises(Exception):
                writer.write_channels_tsv(chans_df)
            with pytest.raises(Exception):
                writer.write_electrode_coords()
            with pytest.raises(Exception):
                writer.write_scans_tsv(scans_df)
            with pytest.raises(Exception):
                writer.write_sidecar_json(sidecar)

            writer.write_participants_json(participant_dict)
            writer.write_participants_tsv(participant_df)
Ejemplo n.º 3
0
    def _loader(self, runid):
        """
        Create a BidsLoader for a specific runid.

        Parameters
        ----------
        runid : str
            Identifier for the desired recording

        """
        # instantiate a loader/writer
        self.loader = BidsLoader(bids_root=self.bids_root, bids_basename=None)
Ejemplo n.º 4
0
    def load_run(self, session_id, run_id, kind="eeg"):
        """
        Load in the raw data for a specific run.

        Parameters
        ----------
        session_id :
        run_id :
        kind :

        Returns
        -------
        mne.IO.BaseRaw
            An object containing the raw EEG data

        """
        loader = BidsLoader(bids_root=self.bids_root, bids_basename=None)
        raw = loader.load_dataset()
        return raw
Ejemplo n.º 5
0
    def __init__(self, bids_root, bids_fname: str, verbose: bool = True):
        super(BidsRun, self).__init__(bids_root=bids_root)

        # ensures just base path
        self.bids_fname = os.path.basename(bids_fname)

        # what is the modality -- meg, eeg, ieeg to read
        self.bids_basename = "_".join(bids_fname.split("_")[:-1])
        self.kind = bids_fname.split("_")[-1].split(".")[0]

        # extract BIDS parameters from the bids filename to be loaded/modified
        # gets: subjectid, sessionid, acquisition type, task name, runid, file extension
        params = _parse_bids_filename(self.bids_fname, verbose=verbose)
        self.subject_id, self.session_id = params["sub"], params["ses"]
        self.acquisition, self.task, self.run = (
            params["acq"],
            params["task"],
            params["run"],
        )
        _, self.ext = _parse_ext(self.bids_fname)

        self.result_fpath = None

        # instantiate a loader/writer
        self.loader = BidsLoader(
            bids_root=self.bids_root,
            bids_basename=self.bids_basename,
            kind=self.kind,
            datatype=self.ext,
        )
        self.writer = BidsWriter(
            bids_root=self.bids_root,
            bids_basename=self.bids_basename,
            kind=self.kind,
            datatype=self.ext,
        )

        if not os.path.exists(self.loader.datafile_fpath):
            raise RuntimeError(
                f"Bids dataset run does not exist at {self.loader.datafile_fpath}. "
                f"Please first create the dataset in BIDS format."
            )
Ejemplo n.º 6
0
    def __init__(self, bids_root, subject_id: str, verbose):
        super(BidsPatient, self).__init__(bids_root=bids_root)

        # what is the modality -- meg, eeg, ieeg to read
        self.bids_basename = make_bids_basename(subject=subject_id)

        # extract BIDS parameters from the bids filename to be loaded/modified
        self.subject_id = subject_id

        # instantiate a loader/writer
        self.loader = BidsLoader(
            bids_root=self.bids_root, bids_basename=self.bids_basename,
        )
        self.writer = BidsWriter(
            bids_root=self.bids_root, bids_basename=self.bids_basename,
        )

        # run to cache and search to get all datasets available for this patient
        self._get_all_datasets()

        # run BIDS compatability checks
        self._bids_compatible_check()
Ejemplo n.º 7
0
    def preprocess_edf_files(self, filelist: list = None, line_noise=60, kind="eeg"):
        """
        Convert .edf files to .fif and .json file pair.

        Parameters
        ----------
        filelist : list[os.PathLike]
            The list of all files to be converted into .fif format. Default is all edf files available
        line_noise : int
            The line noise to be eliminated. Typically 60 Hz in America

        """
        if filelist is None:
            filelist = self._find_non_processed_files()
        for i, fpath in enumerate(filelist):
            if not os.path.exists(fpath):
                raise OSError(f"{fpath} doesn't exist. Please pass in valid filepaths.")
            if line_noise != 50:
                loader = BidsLoader(bids_root=self.bids_root, bids_basename=None)
                sidecardict = loader.load_sidecar_json()
                sidecardict["PowerLineFrequency"] = line_noise

                writer = BidsWriter(bids_root=self.bids_root, bids_basename=None)
                writer.write_sidecar_json(sidecardict)