def test_hdf(self, tmpdir):
        from cmlreaders.readers.readers import EventReader
        from unittest.mock import patch

        efile = osp.join(osp.dirname(__file__), "data",
                         "R1111M_FR1_0_events.json")
        filename = str(tmpdir.join("test.h5"))

        events = EventReader.fromfile(efile,
                                      subject="R1111M",
                                      experiment="FR1")
        ev = events[events.eegoffset > 0].sample(n=5)

        rel_start, rel_stop = 0, 10
        get_eeg = partial(self.make_eeg, ev, rel_start, rel_stop)

        reader = self.reader

        with patch.object(reader, "load_eeg", return_value=get_eeg()):
            eeg = reader.load_eeg(events=ev, rel_start=0, rel_stop=10)

        ts = eeg.to_ptsa()
        ts.to_hdf(filename)

        ts2 = TimeSeries.from_hdf(filename)
        assert_timeseries_equal(ts, ts2)
Exemplo n.º 2
0
    def test_eeg_absolute(self, subject, events_filename, expected_basenames):
        path = resource_filename("cmlreaders.test.data", events_filename)
        events = EventReader.fromfile(path)
        reader = EEGReader("eeg", subject)
        new_events = reader._eegfile_absolute(events)

        for eegfile in new_events[
                new_events["eegfile"].notnull()]["eegfile"].unique():
            assert eegfile in expected_basenames
Exemplo n.º 3
0
 def test_load_matlab(self, kind):
     if kind in ["all_events", "task_events"]:
         filename = "TJ001_events.mat"
     else:
         filename = "TJ001_math.mat"
     path = datafile(filename)
     df = EventReader.fromfile(path)
     assert df.columns[0] == "eegoffset"
     assert "experiment" in df.columns
     assert len(df)
Exemplo n.º 4
0
    def load(self, **kwargs):
        """Overrides the generic load method so as to accept keyword arguments
        to pass along to :meth:`as_timeseries`.

        """
        if "events" in kwargs:
            events = kwargs["events"]
        else:
            if self.session is None:
                raise ValueError(
                    "A session must be specified to load an entire session of "
                    "EEG data!")

            finder = PathFinder(subject=self.subject,
                                experiment=self.experiment,
                                session=self.session,
                                rootdir=self.rootdir)

            events_file = finder.find("task_events")
            all_events = EventReader.fromfile(events_file, self.subject,
                                              self.experiment, self.session)

            # Select only a single event with a valid eegfile just to get the
            # filename
            valid = all_events[(all_events["eegfile"].notnull())
                               & (all_events["eegfile"].str.len() > 0)]
            events = pd.DataFrame(valid.iloc[0]).T.reset_index(drop=True)

            # Set relative start and stop times if necessary. If they were
            # already specified, these will allow us to subset the session.
            if "rel_start" not in kwargs:
                kwargs["rel_start"] = 0
            if "rel_stop" not in kwargs:
                kwargs["rel_stop"] = -1

        if not len(events):
            raise ValueError("No events found! Hint: did filtering events "
                             "result in at least one?")
        elif len(events["subject"].unique()) > 1:
            raise ValueError("Loading multiple sessions of EEG data requires "
                             "using events from only a single subject.")

        if "rel_start" not in kwargs or "rel_stop" not in kwargs:
            raise exc.IncompatibleParametersError(
                "rel_start and rel_stop must be given with events")

        # info = EEGMetaReader.fromfile(path, subject=self.subject)
        # sample_rate = info["sample_rate"]
        # dtype = info["data_format"]

        self.scheme = kwargs.get("scheme", None)

        events = self._eegfile_absolute(events.copy())
        return self.as_timeseries(events, kwargs["rel_start"],
                                  kwargs["rel_stop"])
Exemplo n.º 5
0
def events():
    path = resource_filename('cmlreaders.test.data', 'all_events.json')
    return EventReader.fromfile(path, subject="R1389J")