예제 #1
0
    def _execute(self, data, n=None):
        """ Execute learned transformation on *data*.
        
        Projects the given data to the axis of the most significant
        eigenvectors and returns the data in this lower-dimensional subspace.
        """
        # 'INITIALIZATION'
        if self.retained_channels == None:
            self.retained_channels = data.shape[1]
        if n is None:
            n = self.retained_channels
        if self.channel_names is None:
            self.channel_names = data.channel_names
        if len(self.channel_names) < self.retained_channels:
            self.retained_channels = len(self.channel_names)
            self._log(
                "To many channels chosen for the retained channels! Replaced by maximum number.",
                level=logging.CRITICAL)
        if not (self.output_dim == self.retained_channels):
            # overwrite internal output_dim variable, since it is set wrong
            self._output_dim = self.retained_channels

        # 'Real' Processing
        #projected_data = super(PCANodeWrapper, self)._execute(data, n)
        x = data.view(numpy.ndarray)
        projected_data = mult(x - self.avg, self.v[:, :self.retained_channels])

        if self.new_channels is None:
            self.new_channel_names = [
                "pca%03d" % i for i in range(projected_data.shape[1])
            ]
        return TimeSeries(projected_data, self.new_channel_names,
                          data.sampling_frequency, data.start_time,
                          data.end_time, data.name, data.marker_name)
예제 #2
0
    def test_fda(self):
        """
        Tests that FDA produces the expected transformation matrix on the data
        """
        fda_node = FDAFilterNode(retained_channels=2)
        for i, data in enumerate(self.data):
            fda_node.train(data, self.classes[i])
        fda_node.stop_training()

        self.assert_(
            numpy.allclose(
                fda_node.filters,
                numpy.array([[1.56207903, -1.15805762],
                             [-2.32599494, -0.79980837]])),
            "FDA transformation matrix is wrong!")

        transformed_data = []
        for i, data in enumerate(self.data):
            ts = TimeSeries(input_array=data,
                            channel_names=[("test_channel_%s" % j)
                                           for j in range(2)],
                            sampling_frequency=10,
                            start_time=0,
                            end_time=1)
            transformed_data.append(fda_node.execute(ts).view(numpy.ndarray))
        self.assert_(
            numpy.allclose(transformed_data[0:2], [
                numpy.array([[-0.45549484, -1.20700466]]),
                numpy.array([[-1.52271298, 0.16829469]])
            ]), "FDA-transformed data does not match expectation!")
예제 #3
0
    def generate_normalized_test_data(self,
                                      channels,
                                      time_points,
                                      function,
                                      sampling_frequency,
                                      initial_phase=0.0):
        """
        A method which generates a normalized (mu = 0, sigma =1) signal for testing, with
        the specified number of "channels" which are all generated using the given function
        """

        #Generate an empty ndarray
        data = numpy.zeros((time_points, channels))

        #Compute the values for all channels
        for channel_index in range(channels):
            for time_index in range(time_points):
                data[time_index, channel_index] = function(
                    2.0 * numpy.pi * (channel_index + 1) *
                    (time_index / sampling_frequency + initial_phase))
            current_channel = data[:, channel_index]
            current_channel = (current_channel - pylab.mean(current_channel)
                               ) / pylab.std(current_channel)
            data[:, channel_index] = current_channel

        #Generate a time series build out of the data
        test_data = TimeSeries(
            input_array=data,
            channel_names=[("test_channel_%s" % i) for i in range(channels)],
            sampling_frequency=sampling_frequency,
            start_time=initial_phase,
            end_time=float(time_points) / sampling_frequency + initial_phase)

        return test_data
예제 #4
0
 def setUp(self):
     """ Define basic needed FeatureVector instances """
     self.x = FeatureVector([[0, 1, 2, 3, 4, 5]],
                            ["a", "b", "ab", "cb", "c4", "abc"])
     self.y = FeatureVector(
         [[0, 1, 2, 3, 4, 5]],
         ["a_7ms", "b_7ms", "ab_7ms", "cb_7ms", "c4_7ms", "abc_7ms"])
     self.tx = TimeSeries([[0, 1, 2, 3, 4, 5]],
                          channel_names=["a", "b", "ab", "cb", "c4", "abc"],
                          sampling_frequency=1)
     self.ty = TimeSeries([[0, 1, 2, 3, 4, 5]],
                          channel_names=[
                              "a_7ms", "b_7ms", "ab_7ms", "cb_7ms",
                              "c4_7ms", "abc_7ms"
                          ],
                          sampling_frequency=1)
예제 #5
0
    def _execute(self, data):
        """ Apply the learned spatial filters to the given data point """
        if self.channel_names is None:
            self.channel_names = data.channel_names

        if self.retained_channels in [None, 'None']:
            self.retained_channels = len(self.channel_names)

        if len(self.channel_names) < self.retained_channels:
            self.retained_channels = len(self.channel_names)
            self._log(
                "To many channels chosen for the retained channels! "
                "Replaced by maximum number.",
                level=logging.CRITICAL)
        data_array = data.view(numpy.ndarray)
        # Project the data using the learned spatial filters
        projected_data = numpy.dot(data_array,
                                   self.filters[:, :self.retained_channels])

        if self.xDAWN_channel_names is None:
            self.xDAWN_channel_names = [
                "xDAWN%03d" % i for i in range(self.retained_channels)
            ]

        return TimeSeries(projected_data, self.xDAWN_channel_names,
                          data.sampling_frequency, data.start_time,
                          data.end_time, data.name, data.marker_name)
예제 #6
0
 def setUp(self):
     self.test_data = numpy.zeros((128, 3))
     self.test_data[:,1] = numpy.ones(128)
     self.test_data[:,2] = numpy.random.random(128)
     
     self.test_time_series = TimeSeries(self.test_data, ["A","B", "C"], 64,
                                        start_time = 0, end_time = 2000)
예제 #7
0
    def test_specgram_band_pass(self):
        time_points = 10000
        sampling_frequency = 100.0
        data = numpy.zeros((time_points,1))
        for time_index in range(time_points):
            data[time_index,0] = numpy.sin(2.0 * numpy.pi * 5.0 * (time_index / sampling_frequency))
            data[time_index,0] += numpy.sin(2.0 * numpy.pi * 15.0 * (time_index / sampling_frequency))
            data[time_index,0] += numpy.sin(2.0 * numpy.pi * 25.0 * (time_index / sampling_frequency))
            data[time_index,0] += numpy.sin(2.0 * numpy.pi * 35.0 * (time_index / sampling_frequency))
            data[time_index,0] += numpy.sin(2.0 * numpy.pi * 45.0 * (time_index / sampling_frequency))

        pass_band=(20.,30.)

        #Generate a time series build out of the data
        from pySPACE.resources.data_types.time_series import TimeSeries
        test_data = TimeSeries(input_array = data,
                               channel_names = ["test_channel_1"],
                               sampling_frequency = sampling_frequency,
                               start_time = 0,
                               end_time = float(time_points) / sampling_frequency)

        lpf_node = filtering.FFTBandPassFilterNode(pass_band=pass_band)
        filtered_time_series = lpf_node.execute(test_data)

        lpf_node_fir = filtering.FIRFilterNode(pass_band=pass_band)
        filtered_time_series_fir = lpf_node_fir.execute(test_data)

        lpf_node_fir2 = filtering.FIRFilterNode(pass_band=pass_band,window='hann')
        filtered_time_series_fir2 = lpf_node_fir2.execute(test_data)

        lpf_node_iir = filtering.IIRFilterNode(pass_band=pass_band,stop_band_rifle=90)
        filtered_time_series_iir = lpf_node_iir.execute(test_data)
예제 #8
0
    def _execute(self, data):
        # Determine the new list of channel_names
        self.selected_channel_names = [
            channel_name + "-" + self.dual[channel_name]
            for channel_name in self.dual_list
            if channel_name in data.channel_names
            and self.dual[channel_name] in data.channel_names
        ]
        # Initialize the new array
        difference_data = numpy.zeros(
            (len(data), len(self.selected_channel_names)))
        current_index = 0

        # Do the same check as in the determination of the channel names...
        for channel_name in self.dual_list:
            if channel_name in data.channel_names and self.dual[
                    channel_name] in data.channel_names:
                first_channel_index = data.channel_names.index(channel_name)
                second_channel_index = data.channel_names.index(
                    self.dual[channel_name])
                # ...and build the difference of the corresponding channels.
                difference_data[:,
                                current_index] = data[:,
                                                      first_channel_index] - data[:,
                                                                                  second_channel_index]
                current_index += 1
        # Create new TimeSeries object
        difference_time_series = TimeSeries(difference_data,
                                            self.selected_channel_names,
                                            data.sampling_frequency,
                                            data.start_time, data.end_time,
                                            data.name, data.marker_name)
        return difference_time_series
예제 #9
0
    def test_specgram_low_pass(self):
        time_points = 10000
        sampling_frequency = 100.0
        data = numpy.zeros((time_points, 1))
        for time_index in range(time_points):
            data[time_index, 0] = numpy.sin(2.0 * numpy.pi * 5.0 *
                                            (time_index / sampling_frequency))
            data[time_index, 0] += numpy.sin(2.0 * numpy.pi * 15.0 *
                                             (time_index / sampling_frequency))
            data[time_index, 0] += numpy.sin(2.0 * numpy.pi * 25.0 *
                                             (time_index / sampling_frequency))
            data[time_index, 0] += numpy.sin(2.0 * numpy.pi * 35.0 *
                                             (time_index / sampling_frequency))
            data[time_index, 0] += numpy.sin(2.0 * numpy.pi * 45.0 *
                                             (time_index / sampling_frequency))

        #Generate a time series build out of the data
        from pySPACE.resources.data_types.time_series import TimeSeries
        test_data = TimeSeries(input_array=data,
                               channel_names=["test_channel_1"],
                               sampling_frequency=sampling_frequency,
                               start_time=0,
                               end_time=float(time_points) /
                               sampling_frequency)

        lpf_node = filtering.SimpleLowPassFilterNode(cutoff_frequency=20.0)
        filtered_time_series = lpf_node.execute(test_data)

        lpf_node_fir = filtering.FIRFilterNode([20.0])
        filtered_time_series_fir = lpf_node_fir.execute(test_data)
예제 #10
0
파일: fda.py 프로젝트: pyspace/test
    def _execute(self, data):
        """ Execute learned transformation on *data*."""
        # We must have computed the projection matrix
        assert (self.filters != None)

        if self.retained_channels == None:
            self.retained_channels = data.shape[1]
        if self.channel_names is None:
            self.channel_names = data.channel_names

        if len(self.channel_names) < self.retained_channels:
            self.retained_channels = len(self.channel_names)
            self._log(
                "To many channels chosen for the retained channels! Replaced by maximum number.",
                level=logging.CRITICAL)
        # Project the data using the learned FDA
        projected_data = numpy.dot(data,
                                   self.filters[:, :self.retained_channels])
        if self.new_channel_names is None:
            self.new_channel_names = [
                "fda%03d" % i for i in range(self.retained_channels)
            ]

        return TimeSeries(projected_data, self.new_channel_names,
                          data.sampling_frequency, data.start_time,
                          data.end_time, data.name, data.marker_name)
예제 #11
0
    def _execute(self, data):
        """ Project the data onto the selected channels. """
        projected_data = data[:, self.selected_indices]

        return TimeSeries(projected_data, self.selected_channels,
                          data.sampling_frequency, data.start_time,
                          data.end_time, data.name, data.marker_name)
예제 #12
0
    def setUp(self):
        samples = 5000
        ranges = [-3.5, 3.5, -3.5, 3.5]

        numpy.random.seed(0)
        true_data = numpy.zeros((samples, 2))
        true_data[:, 0] = numpy.random.normal(loc=0.0,
                                              scale=1.0,
                                              size=(samples, ))
        true_data[:, 1] = numpy.random.normal(loc=0.0,
                                              scale=0.5,
                                              size=(samples, ))
        self.classes = [-1 if x < 0 else 1 for x in true_data[:, 1]]

        mixed_data = numpy.zeros((samples, 2))
        for i in range(samples):
            mixed_data[i,
                       0] = 0.6 * true_data[i, 0] + 0.4 * true_data[i, 1] + 1.0
            mixed_data[i,
                       1] = 0.4 * true_data[i, 0] - 0.6 * true_data[i, 1] + 1.5

        self.data = numpy.zeros(mixed_data.shape)
        self.data[:, 0] = mixed_data[:, 0] - numpy.mean(mixed_data[:, 1])
        self.data[:, 1] = mixed_data[:, 1] - numpy.mean(mixed_data[:, 1])

        self.data = [
            TimeSeries(data,
                       channel_names=[("test_channel_%s" % j)
                                      for j in range(2)],
                       sampling_frequency=10) for data in self.data
        ]
 def setUp(self):
     self.time_series = test_ts_generator.generate_test_data(
         channels=8,
         time_points=1000,
         function=test_sine,
         sampling_frequency=100.0)
     self.x1 = TimeSeries([[1, 2, 3], [6, 5, 3]], ['a', 'b', 'c'], 120)
예제 #14
0
    def get_data(self, run_nr, split_nr, train_test):
        """ Return the train or test data for the given split in the given run.
        
        **Parameters**
          
          :run_nr: The number of the run whose data should be loaded.
          
          :split_nr: The number of the split whose data should be loaded.
          
          :train_test: "train" if the training data should be loaded.
                       "test" if the test data should be loaded.
    
        """
        # Do lazy loading of the time series objects.
        filepath = self.data_directory + os.path.sep + self.file_path
        data = scipy.io.loadmat(filepath)
        signal = data['Signal']
        flashing = data['Flashing']
        stimulus_code = data['StimulusCode']
        stimulus_type = data['StimulusType']
        target_char = data['TargetChar']

        window = 240
        channels = 64
        epochs = signal.shape[0]
        data_collection = []

        responses = numpy.zeros((12, 15, window, channels))
        for epoch in range(epochs):
            counter = 0
            rowcolcnt = numpy.ones(12)
            for n in range(1, signal.shape[1]):
                if (flashing[epoch, n] == 0 and flashing[epoch, n - 1] == 1):
                    rowcol = stimulus_code[epoch, n - 1]
                    responses[rowcol - 1, rowcolcnt[rowcol - 1] -
                              1, :, :] = signal[epoch,
                                                n - 24:n + window - 24, :]
                    rowcolcnt[rowcol - 1] = rowcolcnt[rowcol - 1] + 1

            avgresp = numpy.mean(responses, 1)

            targets = stimulus_code[epoch, :] * stimulus_type[epoch, :]
            target_rowcol = []
            for value in targets:
                if value not in target_rowcol:
                    target_rowcol.append(value)

            target_rowcol.sort()

            for i in range(avgresp.shape[0]):
                temp = avgresp[i, :, :]
                data = TimeSeries(input_array=temp,
                                  channel_names=range(64),
                                  sampling_frequency=window)
                if i == target_rowcol[1] - 1 or i == target_rowcol[2] - 1:
                    data_collection.append((data, "Target"))
                else:
                    data_collection.append((data, "Standard"))

        return data_collection
 def setUp(self):
     # initiate the two channels
     self.channel_names = ['a', 'b']
     array = []
     # fill in the data points according to a pre set equation
     for counter in range(100):
         array.append([4 * counter + 1, 4.36 * counter - 23.4])
     self.initial_data = TimeSeries(array, self.channel_names, 100)
예제 #16
0
파일: test_csp.py 프로젝트: pyspace/test
    def test_csp(self):
        """
        Tests that CSP produces the expected transformation matrix on the data
        """
        csp_node = CSPNode(retained_channels=2)
        for i in range(self.data.shape[0]):
            ts = TimeSeries(input_array=self.data[i:i + 1, :],
                            channel_names=[("test_channel_%s" % j)
                                           for j in range(2)],
                            sampling_frequency=10,
                            start_time=0,
                            end_time=1)
            csp_node.train(ts, self.classes[i])
        csp_node.stop_training()

        self.assert_(
            numpy.allclose(
                csp_node.filters,
                numpy.array([[-0.75319083, -0.35237094], [1., -1.]])),
            "CSP transformation matrix is wrong! Got:%s, expected:%s" %
            (str(csp_node.filters),
             str(numpy.array([[-0.75319083, -0.35237094], [1., -1.]]))))

        transformed_data = numpy.zeros(self.data.shape)
        for i in range(self.data.shape[0]):
            ts = TimeSeries(input_array=self.data[i:i + 1, :],
                            channel_names=[("test_channel_%s" % j)
                                           for j in range(2)],
                            sampling_frequency=10,
                            start_time=0,
                            end_time=1)
            transformed_data[i, :] = csp_node.execute(ts)

        self.assert_(
            numpy.allclose(
                transformed_data[0:2, :],
                numpy.array([[0.14525655, -0.83028934],
                             [0.68796176, -0.23672793]])),
            "CSP-transformed data (%s) does not match expectation (%s)!" %
            (str(transformed_data[0:2, :]),
             str(
                 numpy.array([[0.14525655, -0.83028934],
                              [0.68796176, -0.23672793]]))))
예제 #17
0
    def testInheritAndAddStuff(self):
        """test inheritance of meta data from other objects"""
        # Inherit
        self.assertEqual(self.x5.tag, self.x2.tag)
        self.assertEqual(self.x5.key, self.x2.key)

        self.assertEqual(self.f3.tag, self.x2.tag)
        self.assertEqual(self.f3.key, self.x2.key)

        #Inherit

        #suppress warning of BaseData type and cast data back to numpy
        hist_x6 = self.x6.history[0].view(numpy.ndarray)
        data_x5 = self.x5.view(numpy.ndarray)

        # history
        self.assertEqual((hist_x6 == data_x5).all(), True)
        self.assertEqual(self.x6.history[0].key, self.x5.key)
        self.assertEqual(self.x6.history[0].tag, self.x5.tag)
        self.assertEqual(self.x6.history[0].specs['node_specs'],
                         self.some_nice_dict)

        hist_f3 = self.f3.history[0].view(numpy.ndarray)

        self.assertEqual((hist_f3 == data_x5).all(), True)
        self.assertEqual(self.f3.history[0].key, self.x5.key)
        self.assertEqual(self.f3.history[0].tag, self.x5.tag)

        #if key (and tag) were already set, these original values
        #have to be kept
        #
        self.assertEqual(self.x6.key, self.x6_key)
        self.assertEqual(self.x6.tag, self.x2.tag)

        self.x6.inherit_meta_from(self.f3)  #should not change tag and key

        self.assertEqual(self.x6.key, self.x6_key)
        self.assertEqual(self.x6.tag, self.x2.tag)

        #testing multiple histories
        x7 = TimeSeries([1, 2, 3, 4, 5, 6], ['a', 'b', 'c', 'd', 'e', 'f'],
                        12,
                        marker_name='S4')
        x7.add_to_history(self.x1)
        x7.add_to_history(self.x2)
        x7.add_to_history(self.x3)
        x7.add_to_history(self.x4)
        x7.add_to_history(self.x5)
        x7.add_to_history(self.x6)
        x7.add_to_history(self.x1)

        self.assertEqual(len(x7.history), 7)
        self.assertEqual(x7.history[0].key, x7.history[6].key)
        self.assertEqual(x7.history[5].history, [])
예제 #18
0
    def _execute(self, data):
        # First check if all channels actually appear in the data

        # Determine the indices of the channels that are the basis for the
        # average reference.
        if not self.inverse:
            if self.avg_channels == None:
                self.avg_channels = data.channel_names
            channel_indices = [
                data.channel_names.index(channel_name)
                for channel_name in self.avg_channels
            ]
        else:
            channel_indices = [
                data.channel_names.index(channel_name)
                for channel_name in data.channel_names
                if channel_name not in self.avg_channels
            ]

        not_found_channels = \
            [channel_name for channel_name in self.avg_channels
                     if channel_name not in data.channel_names]
        if not not_found_channels == []:
            warnings.warn(
                "Couldn't find selected channel(s): %s. Ignoring." %
                not_found_channels, Warning)

        if self.old_ref is None:
            self.old_ref = 'avg'

        # Compute the actual data of the reference channel. This is the sum of all
        # channels divided by (the number of channels +1).
        ref_chen = -numpy.sum(data[:, channel_indices], axis=1) / (
            data.shape[1] + 1)
        ref_chen = numpy.atleast_2d(ref_chen).T
        # Reference all electrodes against average
        avg_referenced_data = data + ref_chen

        # Add average as new channel to the signal if enabled
        if self.keep_average:
            avg_referenced_data = numpy.hstack((avg_referenced_data, ref_chen))
            channel_names = data.channel_names + [self.old_ref]


#        for i in range(channels_names.shape[0]):
#            print 'channel %d: %s\n', i, channel_names[i]

# Create new time series object and return it
        result_time_series = TimeSeries(avg_referenced_data, channel_names,
                                        data.sampling_frequency,
                                        data.start_time, data.end_time,
                                        data.name, data.marker_name)

        return result_time_series
예제 #19
0
    def _execute(self, data):
        """ Identify feature names with channel names """
        assert (type(data) == FeatureVector), \
               "Feature2MonoTimeSeries requires FeatureVector inputs " \
               "not %s" % type(data)

        data_array = numpy.atleast_2d(data.view(numpy.ndarray))

        new_data = TimeSeries(data_array,
                              channel_names=data.feature_names,
                              sampling_frequency=1.0)
        return new_data
예제 #20
0
 def _execute(self, data):
     if self.retained_channels is None:
         self.retained_channels = len(data.channel_names)
     if self.channel_names is None:
         self.channel_names = data.channel_names
     if self.filters is None:
         return TimeSeries(data[:, :self.retained_channels],
                           data.channel_names[:self.retained_channels],
                           data.sampling_frequency, data.start_time,
                           data.end_time, data.name, data.marker_name)
     else:
         data_array = data.view(numpy.ndarray)
         projected_data = numpy.dot(
             data_array, self.filters[:, :self.retained_channels])
         if self.filter_channel_names is None:
             filter_channel_names = None
         else:
             filter_channel_names = self.filter_channel_names[:self.
                                                              retained_channels]
         return TimeSeries(projected_data, filter_channel_names,
                           data.sampling_frequency, data.start_time,
                           data.end_time, data.name, data.marker_name)
예제 #21
0
 def setUp(self):
     # set up the channels
     self.channel_names = ['Target', 'Standard']
     self.points = []
     # fill in the data points according to a given equation
     for counter in range(100):
         self.points.append((2 * counter, 13 * counter))
     initial_data = TimeSeries(self.points, self.channel_names, 100)
     # since the node was built for online analysis and splitting,
     # we must fool it by giving it the input under the form of a node
     # and not just a e.g. TimeSeries object
     self.input_node = ExternalGeneratorSourceNode()
     self.input_node.set_generator(initial_data)
예제 #22
0
 def setUp(self):
     ts_t_1 = TimeSeries([[1.5, -1], [1.5, -1], [1.5, -1], [1.5, -1]],
                         channel_names=["C3", "C4"],
                         sampling_frequency=0.5,
                         start_time=0.0,
                         end_time=3.0)
     ts_s_1 = TimeSeries([[-1, 1.5], [-1, 1.5], [-1, 1.5], [-1, 1.5]],
                         channel_names=["C3", "C4"],
                         sampling_frequency=0.5,
                         start_time=0.0,
                         end_time=3.0)
     ts_t_2 = TimeSeries([[0, 0], [0, 0], [0, 0], [0, 0]],
                         channel_names=["C3", "C4"],
                         sampling_frequency=0.5,
                         start_time=0.0,
                         end_time=3.0)
     ts_s_2 = TimeSeries([[0, 0], [0, 0], [0, 0], [0, 0]],
                         channel_names=["C3", "C4"],
                         sampling_frequency=0.5,
                         start_time=0.0,
                         end_time=3.0)
     self.target = [ts_t_1, ts_t_2]
     self.standard = [ts_s_1, ts_s_2]
예제 #23
0
    def project_data(self, data):
        """ Project the data set on to the channels that will be retained """
        # Note: We have to create a new array since otherwise the removed
        #       channels remains in memory
        projected_data = numpy.array(
            data.view(numpy.ndarray)[:, self.retained_channel_indices])

        # Create new TimeSeries object
        projected_time_series = TimeSeries(projected_data,
                                           self.selected_channel_names,
                                           data.sampling_frequency,
                                           data.start_time, data.end_time,
                                           data.name, data.marker_name)

        return projected_time_series
예제 #24
0
def convert_feature_vector_to_time_series(feature_vector, sample_data):
    """ Parse the feature name and reconstruct a time series object holding the equivalent data 
    
    In a feature vector object, a feature is determined by the feature
    name and the feature value. When dealing with time domain features, the
    feature name is a concatenation of the (pseudo-) channel
    name and the time within an epoch in seconds. A typical feature name
    reads, e.g., "TD_F7_0.960sec".
    """

    # channel name is what comes after the first underscore
    feat_channel_names = [
        chnames.split('_')[1] for chnames in feature_vector.feature_names
    ]
    # time is what comes after the second underscore
    feat_times = [
        int(
            float(
                (chnames.split('_')[2])[:-3]) * sample_data.sampling_frequency)
        for chnames in feature_vector.feature_names
    ]

    # generate new time series object based on the exemplary "sample_data"
    # all filled with zeros instead of data
    new_data = TimeSeries(pylab.zeros(sample_data.shape),
                          channel_names=sample_data.channel_names,
                          sampling_frequency=sample_data.sampling_frequency,
                          start_time=sample_data.start_time,
                          end_time=sample_data.end_time,
                          name=sample_data.name,
                          marker_name=sample_data.marker_name)

    # try to find the correct place (channel name and time)
    # to insert the feature values
    for i in range(len(feature_vector)):
        try:
            new_data[feat_times[i],
                     new_data.channel_names.index(feat_channel_names[i])] = \
                feature_vector[i]
        except ValueError:
            import warnings
            warnings.warn("\n\nFeatureVis can't find equivalent to Feature " +
                          feature_vector.feature_names[i] +
                          " in the time series.\n")

    return new_data
예제 #25
0
파일: csp.py 프로젝트: pyspace/test
    def _execute(self, data):
        """ Apply the learned spatial filters to the given data point. """
        # We must have computed the common spatial patterns, before
        # we can project the data onto the CSP subspace
        assert (self.filters != None)

        # If retained_channels not specified, retain all
        if self.retained_channels in [None, 'None']:
            self.retained_channels = self.number_of_channels

        if self.channel_names is None:
            self.channel_names = data.channel_names

        if len(self.channel_names) < self.retained_channels:
            self.retained_channels = len(self.channel_names)
            self._log(
                "To many channels chosen for the retained channels! Replaced by maximum number.",
                level=logging.CRITICAL)

        if self.new_order is None:
            # We resort everything according to importance
            self.new_order = []
            for i in range(self.number_of_channels / 2):
                self.new_order.append(i)
                self.new_order.append(self.number_of_channels - 1 - i)

        if self.spatio_temporal:
            orig_shape = data.shape
            data = data.reshape(1, data.shape[0] * data.shape[1])
        # Project the data using the learned CSP
        projected_data = numpy.dot(data,
                                   self.filters[:, :self.retained_channels])
        if self.spatio_temporal:
            projected_data = data.reshape(orig_shape[0], orig_shape[1])

        if self.new_channel_names is None:
            self.new_channel_names = ["csp%03d" % i for i in self.new_order]

        return TimeSeries(projected_data,
                          self.new_channel_names[:self.retained_channels],
                          data.sampling_frequency, data.start_time,
                          data.end_time, data.name, data.marker_name)
예제 #26
0
    def request_data_for_testing(self):
        """
        Returns the data that can be used for testing of subsequent nodes

        .. todo:: to document
        """

        # If we haven't read the data for testing yet
        if self.data_for_testing is None:
            self.time_series = [(TimeSeries(input_array=numpy.ones((2, 2)) * i,
                                            channel_names=["X", "Y"],
                                            sampling_frequency=2),
                                 random.choice(["A", "B"])) for i in range(23)]
            # Create a generator that emits the windows
            test_data_generator = ((sample, label) \
                                     for (sample, label) in self.time_series)

            self.data_for_testing = MemoizeGenerator(test_data_generator,
                                                     caching=True)
        # Return a fresh copy of the generator
        return self.data_for_testing.fresh()
예제 #27
0
    def generate_test_data_simple(self,
                                  channels,
                                  time_points,
                                  function,
                                  sampling_frequency,
                                  initial_phase=0.0):
        """
        A method which generates a signal by using function for testing, with
        the specified number of "channels" which are all generated using
        the given function.
        
        **Keyword arguments**

            :channels:  number of channels
            :time_points: number of time points
            :function: the function used for sample generation
            :sampling_frequency: the frequency which is used for sampling,
                                 e.g. the signal corresponds to a time frame
                                 of time_points/sampling frequency
          
        """

        #Generate an empty ndarray
        data = numpy.zeros((time_points, channels))

        #Compute the values for all channels
        for channel_index in range(channels):
            for time_index in range(time_points):
                data[time_index, channel_index] = function(time_index /
                                                           sampling_frequency +
                                                           initial_phase)

        #Generate a time series build out of the data
        test_data = TimeSeries(
            input_array=data,
            channel_names=[("test_channel_%s" % i) for i in range(channels)],
            sampling_frequency=sampling_frequency,
            start_time=initial_phase,
            end_time=float(time_points) / sampling_frequency + initial_phase)
        return test_data
    def generate_random_data(self):
        """ Method that is invoked by train and test data generation functions"""
        # invokes the given generating functions
        generated_data = []

        for i in range(self.num_instances):
            choice = self.class_choice_function()
            label = None

            if choice < self.choice_threshold:
                input_array = self.generating_function_class_0(i)
                label = self.class_labels[0]
            else:
                input_array = self.generating_function_class_1(i)
                label = self.class_labels[1]

            generated_data.append(
                (TimeSeries(input_array=input_array,
                            channel_names=self.channel_names,
                            sampling_frequency=self.sampling_frequency),
                 label))
        return generated_data
예제 #29
0
파일: base.py 프로젝트: jhuebotter/pyspace
    def _prepare_FV(self, data):
        """ Convert FeatureVector into TimeSeries and use it for plotting.

        .. note:: This function is not yet working as it should be.
                  Work in progress.
                  Commit due to LRP-Demo (DLR Review)
        """
        # visualization of transformation or history data times visualization
        if self.current_trafo_TS is None:
            transformation_list = self.get_previous_transformations(data)
            transformation_list.reverse()  #first element is previous node

            for elem in transformation_list:
                if self.use_FN and elem[3] == "feature normalization":
                    # visualize Feature normalization scaling as feature vector
                    FN_FV = FeatureVector(numpy.atleast_2d(elem[0]),
                                          feature_names=elem[2])
                    self.current_trafo_TS = type_conversion.FeatureVector2TimeSeriesNode(
                    )._execute(FN_FV)
                    self.current_trafo_TS.reorder(
                        sorted(self.current_trafo_TS.channel_names))
                    break

                # visualize spatial filter as times series,
                # where the time axis is the number of channel or virtual
                # channel name
                if self.use_SF and elem[3] == "spatial filter":
                    new_channel_names = elem[2]
                    SF_trafo = elem[0]
                    self.current_trafo_TS = TimeSeries(
                        SF_trafo.T,
                        channel_names=new_channel_names,
                        sampling_frequency=1)
                    self.current_trafo_TS.reorder(
                        sorted(self.current_trafo_TS.channel_names))
                    break

        return self.current_trafo_TS
예제 #30
0
    def next(self, debug=False):
        """Return next labeled window when used in iterator context."""
        while len(self.cur_extract_windows) == 0:
            # fetch the next block from data_client
            if debug:
                print "reading next block"
            self._readnextblock()
            self._extract_windows_cur_block()
            if debug:
                print "  buffermarkers", self.buffermarkers
                print "  current block", self.samplebuf.get()[self.prebuflen][
                    1, :]
                # print "  current extracted windows ", self.cur_extract_windows

        (windef_name, current_window, class_, start_time, end_time, markers_cur_win) = \
            self.cur_extract_windows.pop(0)

        # TODO: Replace this by a decorator or something similar
        current_window = numpy.atleast_2d(current_window.transpose())
        current_window = TimeSeries(
                input_array=current_window,
                channel_names=self.data_client.channelNames,
                sampling_frequency=self.data_client.dSamplingInterval,
                start_time = start_time,
                end_time = end_time,
                name = "Window extracted @ %d ms, length %d ms, class %s" % \
                    (start_time, end_time - start_time, class_),
                marker_name = markers_cur_win
        )

        current_window.generate_meta()
        current_window.specs[
            'sampling_frequency'] = self.data_client.dSamplingInterval
        current_window.specs['wdef_name'] = windef_name
        self.nwindow += 1

        # return (ndsamplewin, ndmarkerwin)
        return (current_window, class_)