def preprocess_X_y_FeaExtr(ts, X, y):
    new_X = []
    new_y = []

    winlength_seconds = 1
    overlap_seconds = 0
    print("Window size of " + str(winlength_seconds) +
          " seconds and overlap of" +
          str(float(overlap_seconds) * 100 / winlength_seconds) + "%")
    samples_per_sec = 5
    winlength = samples_per_sec * winlength_seconds
    current = winlength
    overlap = samples_per_sec * overlap_seconds

    transform = Transforms(window_length=winlength, window_overlap=overlap)
    print("Use number of mean crossings, spectral entropy as features...")
    feature_transforms = [
        transform.mean, transform.std, transform.prod, transform.minx,
        transform.maxx
    ]
    #                          transform.sumx]
    #                          transform.mean_crossings]
    #                          transform.spec_entropy,
    #                          transform.zero_crossings]
    #                          transform.interq,
    #                          transform.skewn,
    #                          transform.spec_energy,
    #                          transform.p25,
    #                          transform.p75,
    #                          transform.kurtosis]

    while True:
        windowed_raw = transform.slide(X)
        if len(windowed_raw) > 0:
            try:
                # TODO Save the timestamps into a windowed_ts instead
                windowed_features = [ts[transform.current_position][0]]
            except Exception as e:
                print(e)
                break
            for function in feature_transforms:
                windowed_features.extend(
                    (np.apply_along_axis(function, 0, windowed_raw).tolist()))
            new_X.append(windowed_features)

            windowed_raw_labels = transform.slide(y, update=False)
            most_freq_label = np.bincount(windowed_raw_labels).argmax()
            new_y.append(most_freq_label)

    # Convert lists to Numpy arrays
    new_X = np.array(new_X)
    # Store new timestamps
    # TODO This will not be necessary after the windowed_ts is generated
    ts = new_X[:, 0]
    # TODO Check why the features are converted to strings
    # Remove datetime from features
    new_X = new_X[:, 1:].astype(float)
    new_y = np.array(new_y)

    return ts, new_X, new_y
예제 #2
0
 def test_spec_entropy(self):
     """
     Test spectral entropy.
     """
     print('Test spectral entropy.')
     x_list = [[5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 2, 3, 4, 10, 5, 7, 8, 11],
               [7, 7, 31, 31, 47, 75, 87, 115, 116, 119, 119, 155, 177]]
     y_list = [0.9333118993685063, 1.8492364375192922, 1.941214222324683]
     t = Transforms(window_length=1, stride=0)
     for x, y in zip(x_list, y_list):
         result = t.spec_entropy(x)
         np.testing.assert_almost_equal(result, y)
예제 #3
0
 def test_spec_energy(self):
     """
     Test spectral energy.
     """
     print('Test spectral energy.')
     x_list = [[5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 2, 3, 4, 10, 5, 7, 8, 11],
               [7, 7, 31, 31, 47, 75, 87, 115, 116, 119, 119, 155, 177]]
     y_list = [2130, 5016, 1661139.9999999995]
     t = Transforms(window_length=1, stride=0)
     for x, y in zip(x_list, y_list):
         result = t.spec_energy(x)
         np.testing.assert_almost_equal(result, y)
def preprocess_X_y_Shape(ts, X, y):
    new_X = []
    new_y = []

    winlength_seconds = 10
    overlap_seconds = 9
    print("Window size of " + str(winlength_seconds) +
          " seconds and overlap of" +
          str(float(overlap_seconds) * 100 / winlength_seconds) + "%")
    samples_per_sec = 1
    winlength = samples_per_sec * winlength_seconds
    current = winlength
    overlap = samples_per_sec * overlap_seconds

    transform = Transforms(window_length=winlength, window_overlap=overlap)
    print("Use number of mean crossings, spectral entropy as features...")
    feature_transforms = [transform.raw]

    while True:
        windowed_raw = transform.slide(X)
        if len(windowed_raw) > 0:
            try:
                windowed_features = [ts[transform.current_position][0]]
            except Exception as e:
                print(e)
                break
            for function in feature_transforms:
                windowed_features.extend(
                    (np.apply_along_axis(function, 0, windowed_raw).tolist()))
            new_X.append(windowed_features)

            windowed_raw_labels = transform.slide(y, update=False)
            most_freq_label = np.bincount(windowed_raw_labels).argmax()
            new_y.append(most_freq_label)

    # Convert lists to Numpy arrays
    new_X = np.array(new_X)
    # Store new timestamps
    ts = new_X[:, 0]
    # Remove datetime from features
    new_X = new_X[:, 1:]
    new_y = np.array(new_y)

    dims = np.size(new_X[0][0])
    tmpX = np.ones((new_X.shape[0], new_X.shape[1], dims), dtype='float64')
    for i in range(0, new_X.shape[0]):
        for j in range(0, new_X.shape[1]):
            for k in range(0, dims):
                tmpX[i][j][k] = new_X[i][j][k]

    new_X = tmpX
    return ts, new_X, new_y
예제 #5
0
    def test_25_percentiles(self):
        """
        Test 25th percentiles.
        """
        print('Test 25th percentiles.')

        x_list = [[1, 2, 3, 4, 5, 6, 7], [5, 6, 3, 1, 3],
                  [10, -10, -90, -90.5, -100, -200]]
        y_list = [2.5, 3, -97.625]
        t = Transforms(window_length=1, stride=0)
        for x, y in zip(x_list, y_list):
            result = t.p25(x)
            self.assertEqual(result, y)

        winlength = 3
        x_list = [[1, 2, 3, 4, 5, 6, 7], [5, 6, 3, 1, 3],
                  [10, -10, -90, -90.5, -100, -200]]
        y_list = [[1.5, 2.5, 3.5, 4.5, 5.5], [4, 2, 2],
                  [-50, -90.25, -95.25, -150]]
        t = Transforms(window_length=winlength, stride=1)

        for x, y in zip(x_list, y_list):
            t.current_position = winlength
            final_result = []
            while True:
                windowed_raw = t.slide(np.array(x))
                if len(windowed_raw) > 0 and len(windowed_raw) == winlength:
                    result = t.p25(windowed_raw)
                    final_result.append(result)
                else:
                    break
            np.testing.assert_almost_equal(final_result, y)
예제 #6
0
    def test_zero_crossings(self):
        """
        Test zero crossings.
        """
        print('Test zero crossings.')
        x_list = [[-1, -1, -1], [0, 0, 0], [1, -4, 5, -10], [2, -3, 4, -5]]
        y_list = [0, 0, 3, 3]
        t = Transforms(window_length=1, stride=0)
        for x, y in zip(x_list, y_list):
            result = t.zero_crossings(x)
            self.assertEqual(result, y)

        winlength = 3
        x_list = [[-1, -1, -1, -1, -1, -1], [0, 0, 0, 0, 0, 0],
                  [1, -5, 2, -6, 9, -3, 2, -5, 2], [-1, 1, 2, 2, -3, 3]]
        y_list = [[0, 0, 0, 0], [0, 0, 0, 0], [2, 2, 2, 2, 2, 2, 2],
                  [1, 0, 1, 2]]
        t = Transforms(window_length=winlength, stride=1)

        for x, y in zip(x_list, y_list):
            t.current_position = winlength
            final_result = []
            while True:
                windowed_raw = t.slide(np.array(x))
                if len(windowed_raw) > 0 and len(windowed_raw) == winlength:
                    result = t.zero_crossings(windowed_raw)
                    final_result.append(result)
                else:
                    break
            self.assertEqual(final_result, y)
예제 #7
0
    def test_kurtosis(self):
        """
        Test kurtosis.
        """
        print('Test kurtosis.')
        x_list = [[5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 2, 3, 4, 10, 5, 7, 8, 11],
                  [7, 7, 31, 31, 47, 75, 87, 115, 116, 119, 119, 155, 177]]
        y_list = [1.7314285714285718, 2.128698649813272, 1.8298588697064042]
        t = Transforms(window_length=1, stride=0)
        for x, y in zip(x_list, y_list):
            result = t.kurtosis(x)
            self.assertEqual(result, y)

        winlength = 5
        x_list = [[1, 2, 3, 4, 5, 6, 7], [5, 6, 3, 1, 3],
                  [10, -90, -10, -90.5, -100, -200]]
        y_list = [[1.7, 1.7, 1.7], [1.7957064], [1.2987871, 2.5169668]]
        t = Transforms(window_length=winlength, stride=1)
        for x, y in zip(x_list, y_list):
            t.current_position = winlength
            final_result = []
            while True:
                windowed_raw = t.slide(np.array(x))
                if len(windowed_raw) > 0 and len(windowed_raw) == winlength:
                    result = t.kurtosis(windowed_raw)
                    final_result.append(result)
                else:
                    break
            np.testing.assert_almost_equal(final_result, y)
예제 #8
0
    def test_skewness(self):
        """
        Test skewness.
        """
        print('Test skewness.')
        x_list = [[5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 2, 3, 4, 10, 5, 7, 8, 11],
                  [7, 7, 31, 31, 47, 75, 87, 115, 116, 119, 119, 155, 177]]
        y_list = [0, 0.6486028516711329, 0.05039822936776113]
        t = Transforms(window_length=1, stride=0)
        for x, y in zip(x_list, y_list):
            result = t.skewn(x)
            np.testing.assert_almost_equal(result, y)

        winlength = 3
        x_list = [[1, 2, 3, 4, 5, 6, 7], [5, 6, 3, 1, 3],
                  [10, -90, -10, -90.5, -100, -200]]
        y_list = [[0, 0, 0, 0, 0], [-0.3818018, 0.2390631, -0.7071068],
                  [-0.5951701, 0.7070141, 0.6778576, -0.687648]]
        t = Transforms(window_length=winlength, stride=1)
        for x, y in zip(x_list, y_list):
            t.current_position = winlength
            final_result = []
            while True:
                windowed_raw = t.slide(np.array(x))
                if len(windowed_raw) > 0 and len(windowed_raw) == winlength:
                    result = t.skewn(windowed_raw)
                    final_result.append(result)
                else:
                    break
            np.testing.assert_almost_equal(final_result, y)
예제 #9
0
    def test_interquartiles(self):
        """
        Test interquartiles.
        """
        print('Test interquartiles.')
        x_list = [[5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 2, 3, 4, 10, 5, 7, 8, 11],
                  [7, 7, 31, 31, 47, 75, 87, 115, 116, 119, 119, 155, 177]]
        y_list = [2.5, 4.5, 88]
        t = Transforms(window_length=1, stride=0)
        for x, y in zip(x_list, y_list):
            result = t.interq(x)
            self.assertEqual(result, y)

        winlength = 3
        x_list = [[1, 2, 3, 4, 5, 6, 7], [5, 6, 3, 1, 3],
                  [10, -90, -10, -90.5, -100, -200]]
        y_list = [[1, 1, 1, 1, 1], [1.5, 2.5, 1], [50, 40.25, 45, 54.75]]
        t = Transforms(window_length=winlength, stride=1)
        for x, y in zip(x_list, y_list):
            t.current_position = winlength
            final_result = []
            while True:
                windowed_raw = t.slide(np.array(x))
                if len(windowed_raw) > 0 and len(windowed_raw) == winlength:
                    result = t.interq(windowed_raw)
                    final_result.append(result)
                else:
                    break
            np.testing.assert_almost_equal(final_result, y)
예제 #10
0
def preprocess_X_y(ts, X, y):
    new_X = []
    new_y = []

    winlength_seconds = 5
    overlap_seconds = 1
    print("Window size of " + str(winlength_seconds) +
          " seconds and overlap of " +
          str(float(overlap_seconds) / winlength_seconds) + "%")
    samples_per_sec = 50
    winlength = samples_per_sec * winlength_seconds
    current = winlength
    overlap = samples_per_sec * overlap_seconds

    transform = Transforms(window_length=winlength, window_overlap=overlap)
    print("Use number of mean crossings, spectral entropy as features...")
    feature_transforms = [
        transform.mean_crossings, transform.spec_entropy,
        transform.zero_crossings, transform.interq, transform.skewn,
        transform.spec_energy, transform.p25, transform.p75, transform.kurtosis
    ]

    while True:
        try:
            ts_window = ts[transform.current_position - 1]
        except Exception as e:
            print(e)
            break
        windowed_raw = transform.slide(X, update=False)
        windowed_raw_labels = transform.slide(y, update=True)
        if len(windowed_raw) > 0:
            windowed_features = [ts_window]
            for function in feature_transforms:
                windowed_features.extend(
                    (np.apply_along_axis(function, 0, windowed_raw).tolist()))
            new_X.append(windowed_features)

            most_freq_label = np.bincount(windowed_raw_labels).argmax()
            new_y.append(most_freq_label)

    # Convert lists to Numpy arrays
    new_X = np.array(new_X)
    # Remove datetime from features
    new_X = new_X[:, 1:]
    new_y = np.array(new_y)

    new_X = transform.feature_selection(new_X, new_y, 'tree')

    return new_X, new_y
예제 #11
0
def preprocess_X_y_2(ts, X, y):
    new_X = []
    new_y = []

    winlength_seconds = 10
    overlap_seconds = 5
    print("Window size of " + str(winlength_seconds) +
          " seconds and overlap of" +
          str(float(overlap_seconds) * 100 / winlength_seconds) + "%")
    samples_per_sec = 1
    winlength = samples_per_sec * winlength_seconds
    current = winlength
    overlap = samples_per_sec * overlap_seconds

    transform = Transforms(window_length=winlength, window_overlap=overlap)
    print("Use number of mean crossings, spectral entropy as features...")
    feature_transforms = [transform.mean]
    from IPython import embed
    embed()

    while True:
        windowed_raw = transform.slide(X)
        if len(windowed_raw) > 0:
            try:
                windowed_features = [ts[transform.current_position][0]]
            except Exception as e:
                print(e)
                break
            for function in feature_transforms:
                windowed_features.extend(
                    (np.apply_along_axis(function, 0, windowed_raw).tolist()))
            new_X.append(windowed_features)

            windowed_raw_labels = transform.slide(y, update=False)
            most_freq_label = np.bincount(windowed_raw_labels).argmax()
            new_y.append(most_freq_label)

    # Convert lists to Numpy arrays
    new_X = np.array(new_X)
    # Store new timestamps
    ts = new_X[:, 0]
    # Remove datetime from features
    new_X = new_X[:, 1:]
    new_y = np.array(new_y)

    return ts, new_X, new_y