Esempio n. 1
0
 def convolve(self, timeseries, win_len=None, kernel=None):
     if kernel is None:
         kernel = np.ones((np.int(np.round(win_len)), 1, 1, 1))
     else:
         kernel = kernel * np.ones((np.int(np.round(win_len)), 1, 1, 1))
     return Timeseries(convolve(timeseries.data, kernel, mode='same'), timeseries.dimension_labels,
                       timeseries.time_start, timeseries.time_step, timeseries.time_unit)
Esempio n. 2
0
 def test_timeseries_1D_definition(self):
     with pytest.raises(ValueError):
         Timeseries(data=self.data_1D,
                    dimension_labels={},
                    time_start=0,
                    time_step=0.01,
                    time_unit="ms")
Esempio n. 3
0
    def test_plot_sim_results(self):
        lsa_hypothesis = HypothesisBuilder(
            config=self.config).build_lsa_hypothesis()
        mc = ModelConfigurationBuilder().build_model_from_E_hypothesis(
            lsa_hypothesis, numpy.array([1]))
        model = build_EpileptorDP2D(mc)

        # TODO: this figure_name is constructed inside plot method, so it can change
        figure_name = "Simulated_TAVG"
        file_name = os.path.join(self.config.out.FOLDER_FIGURES,
                                 figure_name + ".png")
        assert not os.path.exists(file_name)

        data_3D = numpy.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9], [0, 1, 2]],
                               [[3, 4, 5], [6, 7, 8], [9, 0, 1], [2, 3, 4]],
                               [[5, 6, 7], [8, 9, 0], [1, 2, 3], [4, 5, 6]]])

        self.plotter.plot_simulated_timeseries(
            Timeseries(
                data_3D, {
                    TimeseriesDimensions.SPACE.value: ["r1", "r2", "r3", "r4"],
                    TimeseriesDimensions.VARIABLES.value: ["x1", "x2", "z"]
                }, 0, 1), model, [0])

        assert os.path.exists(file_name)
Esempio n. 4
0
 def decimate_by_filtering(self, timeseries, decim_ratio):
     if decim_ratio > 1:
         decim_data, decim_time, decim_dt, decim_n_times = decimate_signals(timeseries.squeezed,
                                                                            timeseries.time_line, decim_ratio)
         return Timeseries(decim_data, timeseries.dimension_labels,
                           decim_time[0], decim_dt, timeseries.time_unit)
     else:
         return timeseries
Esempio n. 5
0
def read_edf_to_Timeseries(path,
                           sensors,
                           rois_selection=None,
                           label_strip_fun=None,
                           time_units="ms"):
    data, times, rois, rois_inds, rois_lbls = \
        read_edf(path, sensors, rois_selection, label_strip_fun, time_units)

    return Timeseries(data, {TimeseriesDimensions.SPACE.value: rois_lbls},
                      times[0], np.mean(np.diff(times)), time_units)
Esempio n. 6
0
 def test_timeseries_3D(self):
     ts_3D = Timeseries(self.data_3D,
                        dimension_labels={
                            TimeseriesDimensions.SPACE.value: [],
                            TimeseriesDimensions.VARIABLES.value: []
                        },
                        time_start=self.time_start,
                        time_step=self.time_step,
                        time_unit=self.time_unit)
     assert ts_3D.data.ndim == 4
     assert ts_3D.data.shape[3] == 1
Esempio n. 7
0
 def compute_seeg(self, source_timeseries, sensors, sum_mode="lin"):
     if sum_mode == "exp":
         seeg_fun = lambda source, gain_matrix: np.log(np.exp(source.squeezed).dot(gain_matrix.T))
     else:
         seeg_fun = lambda source, gain_matrix: source.squeezed.dot(gain_matrix.T)
     seeg = []
     for sensor in ensure_list(sensors):
         seeg.append(Timeseries(seeg_fun(source_timeseries, sensor.gain_matrix),
                                {TimeseriesDimensions.SPACE.value: sensor.labels,
                                 TimeseriesDimensions.VARIABLES.value:
                                     PossibleVariables.SEEG.value + str(sensor.labels)},
                                source_timeseries.time_start, source_timeseries.time_step,
                                source_timeseries.time_unit))
     return seeg
Esempio n. 8
0
 def test_timeseries_4D(self):
     ts_4D = Timeseries(self.data_4D,
                        dimension_labels={
                            TimeseriesDimensions.SPACE.value:
                            ["r1", "r2", "r3", "r4"],
                            TimeseriesDimensions.VARIABLES.value: [
                                PossibleVariables.X1.value,
                                PossibleVariables.X2.value, "sv3"
                            ]
                        },
                        time_start=self.time_start,
                        time_step=self.time_step,
                        time_unit=self.time_unit)
     assert ts_4D.data.shape == (3, 4, 3, 4)
     assert ts_4D.x1.data.shape == (3, 4, 1, 4)
Esempio n. 9
0
    def read_timeseries(self, path):
        """
        :param path: Path towards a valid TimeSeries H5 file
        :return: Timeseries data and time in 2 numpy arrays
        """
        self.logger.info("Starting to read TimeSeries from: %s" % path)
        h5_file = h5py.File(path, 'r', libver='latest')

        data = h5_file['/data'][()]
        time = h5_file['/time'][()]
        labels = ensure_list(h5_file['/labels'][()])
        variables = ensure_list(h5_file['/variables'][()])
        time_unit = h5_file.attrs["time_unit"]
        self.logger.info("First Channel sv sum: " + str(numpy.sum(data[:, 0])))
        self.logger.info("Successfully read Timeseries!")  #: %s" % data)
        h5_file.close()

        return Timeseries(
            data, {
                TimeseriesDimensions.SPACE.value: labels,
                TimeseriesDimensions.VARIABLES.value: variables
            }, time[0], np.mean(np.diff(time)), time_unit)
Esempio n. 10
0
 def normalize(self, timeseries, normalization=None):
     return Timeseries(normalize_signals(timeseries.data, normalization), timeseries.dimension_labels,
                       timeseries.time_start, timeseries.time_step, timeseries.time_unit)
Esempio n. 11
0
                        sys.stdout.flush()
                        curr_block += 1.0
            except Exception, error_message:
                status = False
                self.logger.warning(
                    "Something went wrong with this simulation...:" + "\n" +
                    str(error_message))
                return None, None, status

            tavg_time = numpy.array(tavg_time).flatten().astype('f')
            tavg_data = numpy.swapaxes(tavg_data, 1, 2).astype('f')
            # Variables of interest in a dictionary:
            sim_output = Timeseries(
                tavg_data, {
                    TimeseriesDimensions.SPACE.value:
                    self.connectivity.region_labels,
                    TimeseriesDimensions.VARIABLES.value: self.get_vois()
                }, tavg_time[0],
                numpy.diff(tavg_time).mean(), "ms")
            return sim_output, status

    def configure_model(self, **kwargs):
        self.model = model_build_dict[self.model._ui_name](
            self.model_configuration, **kwargs)

    def configure_initial_conditions(self, initial_conditions=None):

        if isinstance(initial_conditions, numpy.ndarray):
            self.simTVB.initial_conditions = initial_conditions

        else:
Esempio n. 12
0
            for subvector in vector:
                subresult.append(tuple(subvector))
            result.append(tuple(subresult))

    return result


if __name__ == "__main__":
    data, total_time, nr_of_steps, start_time = read_ts(
        "/WORK/Episense/trunk/demo-data/Head_TREC/epHH/ts.h5")
    conn = H5Reader().read_connectivity(
        "/WORK/Episense/trunk/demo-data/Head_TREC/Connectivity.h5")
    signal = Timeseries(
        data,
        OrderedDict({
            TimeseriesDimensions.SPACE.value:
            list(conn.region_labels),
            TimeseriesDimensions.VARIABLES.value:
            [PossibleVariables.X1.value, PossibleVariables.X2.value, "c"]
        }), start_time, total_time / float(nr_of_steps))

    timeline = signal.time_line

    sv = signal.get_state_variable("x1")

    subspace = signal.get_subspace_by_labels(list(conn.region_labels)[:3])
    timewindow = signal.get_time_window(10, 100)
    timewindowUnits = signal.get_time_window_by_units(timeline[10],
                                                      timeline[100])

    print signal.lfp.data.shape
    print signal.lfp.dimension_labels
Esempio n. 13
0
    def test_timeseries_data_access(self):
        ts = Timeseries(self.data_3D,
                        dimension_labels={
                            TimeseriesDimensions.SPACE.value:
                            ["r1", "r2", "r3", "r4"],
                            TimeseriesDimensions.VARIABLES.value:
                            ["sv1", "sv2", "sv3"]
                        },
                        time_start=self.time_start,
                        time_step=self.time_step,
                        time_unit=self.time_unit)
        assert isinstance(ts.r1, Timeseries)
        assert ts.r1.data.shape == (3, 1, 3, 1)

        assert isinstance(ts.sv1, Timeseries)
        assert ts.sv1.data.shape == (3, 4, 1, 1)

        with pytest.raises(AttributeError):
            ts.r9

        with pytest.raises(AttributeError):
            ts.sv0

        assert ts[:, :, :, :].shape == ts.data.shape
        assert ts[1:, :, :, :].shape == ts.data[1:, :, :, :].shape
        assert ts[1:2, :, :, :].shape == ts.data[1:2, :, :, :].shape
        assert ts[1, :, :, :].shape == ts.data[1, :, :, :].shape

        assert ts[:, 1:, :, :].shape == ts.data[:, 1:, :, :].shape
        assert ts[:, :1, :, :].shape == ts.data[:, :1, :, :].shape
        assert ts[:, 1:3, :, :].shape == ts.data[:, 1:3, :, :].shape
        assert ts[:, 1, :, :].shape == ts.data[:, 1, :, :].shape

        assert ts[:, "r2":, :, :].shape == ts.data[:, 1:, :, :].shape
        assert ts[:, :"r2", :, :].shape == ts.data[:, :1, :, :].shape
        assert ts[:, "r2", :, :].shape == ts.data[:, 1, :, :].shape
        assert ts[:, "r2":"r4", :, :].shape == ts.data[:, 1:3, :, :].shape

        assert ts[1:2, "r2":"r4", :, :].shape == ts.data[1:2, 1:3, :, :].shape
        assert ts[1, "r2":"r4", :, :].shape == ts.data[1, 1:3, :, :].shape

        assert ts[:, :, 1:, :].shape == ts.data[:, :, 1:, :].shape
        assert ts[:, :, :1, :].shape == ts.data[:, :, :1, :].shape
        assert ts[:, :, 0:2, :].shape == ts.data[:, :, 0:2, :].shape
        assert ts[:, :, 2, :].shape == ts.data[:, :, 2, :].shape

        assert ts[:, :, "sv2":, :].shape == ts.data[:, :, 1:, :].shape
        assert ts[:, :, :"sv2", :].shape == ts.data[:, :, :1, :].shape
        assert ts[:, :, "sv1":"sv3", :].shape == ts.data[:, :, 0:2, :].shape
        assert ts[:, :, "sv3", :].shape == ts.data[:, :, 2, :].shape

        assert ts[1:2, :, "sv2":, :].shape == ts.data[1:2, :, 1:, :].shape
        assert ts[1:2, :, :"sv2", :].shape == ts.data[1:2, :, :1, :].shape
        assert ts[1:2, :, "sv1":"sv3", :].shape == ts.data[1:2, :,
                                                           0:2, :].shape
        assert ts[1:2, :, "sv3", :].shape == ts.data[1:2, :, 2, :].shape
        assert ts[2, :, "sv3", :].shape == ts.data[2, :, 2, :].shape

        assert ts[2, 0:3, "sv3", :].shape == ts.data[2, 0:3, 2, :].shape
        assert ts[2, "r1":"r4", "sv3", :].shape == ts.data[2, 0:3, 2, :].shape
        assert ts[0:2, "r1":"r4", "sv3", :].shape == ts.data[0:2, 0:3,
                                                             2, :].shape
        assert ts[0:2, :"r2", "sv3", :].shape == ts.data[0:2, :1, 2, :].shape
        assert ts[0:2, "r2":, "sv3", :].shape == ts.data[0:2, 1:, 2, :].shape
        assert ts[0:2, "r1", "sv3", :].shape == ts.data[0:2, 0, 2, :].shape

        assert all(ts[0:2, "r1", "sv3", :] == ts.data[0:2, 0, 2, :])
        assert ts[0:2, "r1":"r3", "sv3", :].all() == ts.data[0:2, 0:2,
                                                             2, :].all()
        assert ts[0:2,
                  "r1":"r3", :"sv2", :].all() == ts.data[0:2,
                                                         0:2, :1, :].all()
        assert ts[2, "r1":"r3", :"sv2", :].all() == ts.data[2,
                                                            0:2, :1, :].all()
        assert ts[2, "r3", "sv2", :].all() == ts.data[2, 2, 1, :].all()
        assert ts[2, "r3", "sv2", 0] == ts.data[2, 2, 1, 0]

        with pytest.raises(ValueError):
            ts[:, :, "sv0", :]

        with pytest.raises(ValueError):
            ts[0, "r1":"r5", :, :]

        with pytest.raises(IndexError):
            ts[0, :, 10, :]

        with pytest.raises(AttributeError):
            ts.lfp
Esempio n. 14
0
def samples_to_timeseries(samples,
                          model_data,
                          target_data=None,
                          regions_labels=[]):
    samples = ensure_list(samples)

    if isinstance(target_data, Timeseries):
        time = target_data.time_line
        n_target_data = target_data.number_of_labels
        target_data_labels = target_data.space_labels
    else:
        time = model_data.get("time", False)
        n_target_data = samples[0]["fit_target_data"]
        target_data_labels = generate_region_labels(n_target_data, [], ". ",
                                                    False)

    if time is not False:
        time_start = time[0]
        time_step = np.diff(time).mean()
    else:
        time_start = 0
        time_step = 1

    if not isinstance(target_data, Timeseries):
        target_data = Timeseries(
            target_data, {
                TimeseriesDimensions.SPACE.value: target_data_labels,
                TimeseriesDimensions.VARIABLES.value: ["target_data"]
            },
            time_start=time_start,
            time_step=time_step)

    (n_times, n_regions, n_samples) = samples[0]["x1"].T.shape
    active_regions = model_data.get("active_regions", range(n_regions))
    regions_labels = generate_region_labels(
        np.maximum(n_regions, len(regions_labels)), regions_labels, ". ",
        False)
    if len(regions_labels) > len(active_regions):
        regions_labels = regions_labels[active_regions]

    for sample in ensure_list(samples):
        for x in ["x1", "z", "dX1t", "dZt"]:
            try:
                sample[x] = Timeseries(
                    np.expand_dims(sample[x].T, 2), {
                        TimeseriesDimensions.SPACE.value: regions_labels,
                        TimeseriesDimensions.VARIABLES.value: [x]
                    },
                    time_start=time_start,
                    time_step=time_step,
                    time_unit=target_data.time_unit)
            except:
                pass

        sample["fit_target_data"] = Timeseries(
            np.expand_dims(sample["fit_target_data"].T, 2), {
                TimeseriesDimensions.SPACE.value: target_data_labels,
                TimeseriesDimensions.VARIABLES.value: ["fit_target_data"]
            },
            time_start=time_start,
            time_step=time_step)

    return samples, target_data
Esempio n. 15
0
    def test_timeseries_2D(self):
        ts_from_2D = Timeseries(self.data_2D,
                                dimension_labels={
                                    TimeseriesDimensions.SPACE.value:
                                    ["r1", "r2", "r3"]
                                },
                                time_start=self.time_start,
                                time_step=self.time_step,
                                time_unit=self.time_unit)
        assert ts_from_2D.data.ndim == 4
        assert ts_from_2D.data.shape[2] == ts_from_2D.data.shape[3] == 1
        assert ts_from_2D.data.shape == (3, 3, 1, 1)

        assert ts_from_2D.end_time == 0.02
        assert all(ts_from_2D.time_line == numpy.array([0, 0.01, 0.02]))

        with pytest.raises(KeyError):
            ts_from_2D.get_state_variable("")

        ts_r2r3 = ts_from_2D.get_subspace_by_labels(["r2", "r3"])
        assert ts_r2r3.data.ndim == 4
        assert ts_r2r3.data.shape == (3, 2, 1, 1)

        ts_r2 = ts_from_2D.get_subspace_by_labels(["r2"])
        assert ts_r2.data.ndim == 4
        assert ts_r2.data.shape == (3, 1, 1, 1)
        assert ts_r2.dimension_labels[TimeseriesDimensions.SPACE.value] == [
            "r2"
        ]
        assert ts_r2.get_subspace_by_labels([
            "r2"
        ]).dimension_labels[TimeseriesDimensions.SPACE.value] == ["r2"]

        with pytest.raises(ValueError):
            ts_r2.get_subspace_by_labels(["r1"])

        ts_r2r3_idx = ts_from_2D.get_subspace_by_index([1, 2])
        assert ts_r2r3_idx.data.ndim == 4
        assert ts_r2r3_idx.data.shape == (3, 2, 1, 1)

        ts_r2_idx = ts_r2r3_idx.get_subspace_by_index([0])
        assert ts_r2_idx.data.ndim == 4
        assert ts_r2_idx.data.shape == (3, 1, 1, 1)
        assert ts_r2_idx.dimension_labels[
            TimeseriesDimensions.SPACE.value] == ["r2"]
        assert ts_r2_idx.get_subspace_by_index(
            [0]).dimension_labels[TimeseriesDimensions.SPACE.value] == ["r2"]

        with pytest.raises(IndexError):
            ts_r2_idx.get_subspace_by_index([2])

        assert ts_r2r3_idx.data.all() == ts_r2r3.data.all()
        assert all(ts_r2_idx.data == ts_r2.data)

        ts_time_window = ts_from_2D.get_time_window(1, 2)
        assert ts_time_window.data.ndim == 4
        assert ts_time_window.data.shape == (1, 3, 1, 1)
        assert ts_time_window.dimension_labels[
            TimeseriesDimensions.SPACE.value] == ["r1", "r2", "r3"]
        assert ts_time_window.time_start == 0.01

        ts_time_window_units = ts_from_2D.get_time_window_by_units(0.01, 0.02)
        assert ts_time_window_units.data.ndim == 4
        assert ts_time_window_units.data.shape == (1, 3, 1, 1)
        assert ts_time_window_units.dimension_labels[
            TimeseriesDimensions.SPACE.value] == ["r1", "r2", "r3"]
        assert ts_time_window_units.time_start == 0.01

        with pytest.raises(IndexError):
            ts_from_2D.get_time_window(2, 4)

        with pytest.raises(ValueError):
            ts_from_2D.get_time_window_by_units(0, 0.025)

        with pytest.raises(AttributeError):
            ts_from_2D.lfp
Esempio n. 16
0
 def decimate(self, timeseries, decim_ratio):
     if decim_ratio > 1:
         return Timeseries(timeseries.data[0:timeseries.time_length:decim_ratio], timeseries.dimension_labels,
                           timeseries.time_start, decim_ratio*timeseries.time_step, timeseries.time_unit)
     else:
         return timeseries
Esempio n. 17
0
 def square(self, timeseries):
     return Timeseries(timeseries.data ** 2, timeseries.dimension_labels,
                       timeseries.time_start, timeseries.time_step, timeseries.time_unit)
Esempio n. 18
0
 def log(self, timeseries):
     return Timeseries(np.log(timeseries.data), timeseries.dimension_labels,
                       timeseries.time_start, timeseries.time_step, timeseries.time_unit)
Esempio n. 19
0
 def filter(self, timeseries, lowcut=None, highcut=None, mode='bandpass', order=3):
     return Timeseries(filter_data(timeseries.data, timeseries.sampling_frequency, lowcut, highcut, mode, order),
                      timeseries.dimension_labels, timeseries.time_start, timeseries.time_step, timeseries.time_unit)
Esempio n. 20
0
 def hilbert_envelope(self, timeseries):
     return Timeseries(np.abs(hilbert(timeseries.data, axis=0)), timeseries.dimension_labels,
                       timeseries.time_start, timeseries.time_step, timeseries.time_unit)
Esempio n. 21
0
 def detrend(self, timeseries, type='linear'):
     return Timeseries(detrend(timeseries.data, axis=0, type=type), timeseries.dimension_labels,
                       timeseries.time_start, timeseries.time_step, timeseries.time_unit)