Skip to content

Commit

Permalink
csort range py3 compatible (#32)
Browse files Browse the repository at this point in the history
* Changed xrange to range for #8

* Modified csort due to range changing from list to type for #8

* Use assertItemsEqual py2 and assertCountEqual py3
Due to a change in unittest in as identified by @jneuff in
#8 (comment)

Semantically they appear to be the same and this fixes the related failing
tests on Python 3.5 as described in the gist in
#8 (comment)

Adds a basic method to determine python version for now, only committing so that
the new deeper unitest.assertEqual issue that now presents itself can be
addressed.
  • Loading branch information
earthgecko authored and MaxBenChrist committed Nov 3, 2016
1 parent 47c1ff0 commit 89590c0
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
30 changes: 23 additions & 7 deletions tests/feature_extraction/test_feature_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from unittest import TestCase
from tsfresh.feature_extraction.feature_calculators import *
from tsfresh.feature_extraction.feature_calculators import _get_length_sequences_where
from sys import version_info


class FeatureCalculationTestCase(TestCase):
Expand Down Expand Up @@ -279,7 +280,10 @@ def test_mass_quantile(self):
expected_index = ["TEST__index_mass_quantile__q_0.5"]
res = index_mass_quantile(x, c, param)
self.assertIsInstance(res, pd.Series)
self.assertItemsEqual(list(res.index), expected_index)
if int(version_info[0]) == 2:
self.assertItemsEqual(list(res.index), expected_index)
else:
self.assertCountEqual(list(res.index), expected_index)
self.assertAlmostEqual(res["TEST__index_mass_quantile__q_0.5"], 0.5, places=1)

x = [0] * 1000 + [1]
Expand All @@ -288,7 +292,10 @@ def test_mass_quantile(self):
expected_index = ["TEST__index_mass_quantile__q_0.5", "TEST__index_mass_quantile__q_0.99"]
res = index_mass_quantile(x, c, param)
self.assertIsInstance(res, pd.Series)
self.assertItemsEqual(list(res.index), expected_index)
if int(version_info[0]) == 2:
self.assertItemsEqual(list(res.index), expected_index)
else:
self.assertCountEqual(list(res.index), expected_index)
self.assertAlmostEqual(res["TEST__index_mass_quantile__q_0.5"], 1, places=1)
self.assertAlmostEqual(res["TEST__index_mass_quantile__q_0.99"], 1, places=1)

Expand All @@ -299,7 +306,11 @@ def test_mass_quantile(self):
"TEST__index_mass_quantile__q_0.9"]
res = index_mass_quantile(x, c, param)
self.assertIsInstance(res, pd.Series)
self.assertItemsEqual(list(res.index), expected_index)

if int(version_info[0]) == 2:
self.assertItemsEqual(list(res.index), expected_index)
else:
self.assertCountEqual(list(res.index), expected_index)
self.assertAlmostEqual(res["TEST__index_mass_quantile__q_0.3"], 0.25, places=1)
self.assertAlmostEqual(res["TEST__index_mass_quantile__q_0.6"], 0.375, places=1)
self.assertAlmostEqual(res["TEST__index_mass_quantile__q_0.9"], 0.75, places=1)
Expand Down Expand Up @@ -331,7 +342,10 @@ def test_ar_coefficient(self):
expected_index = ["TEST__ar_coefficient__k_1__coeff_0", "TEST__ar_coefficient__k_1__coeff_1"]

self.assertIsInstance(res, pd.Series)
self.assertItemsEqual(list(res.index), expected_index)
if int(version_info[0]) == 2:
self.assertItemsEqual(list(res.index), expected_index)
else:
self.assertCountEqual(list(res.index), expected_index)
self.assertAlmostEqual(res["TEST__ar_coefficient__k_1__coeff_0"], 1, places=2)
self.assertAlmostEqual(res["TEST__ar_coefficient__k_1__coeff_1"], 2.5, places=2)

Expand All @@ -350,10 +364,12 @@ def test_ar_coefficient(self):
"TEST__ar_coefficient__k_2__coeff_0", "TEST__ar_coefficient__k_2__coeff_1",
"TEST__ar_coefficient__k_2__coeff_2", "TEST__ar_coefficient__k_2__coeff_3"]


print(res.sort_index())
self.assertIsInstance(res, pd.Series)
self.assertItemsEqual(list(res.index), expected_index)
if int(version_info[0]) == 2:
self.assertItemsEqual(list(res.index), expected_index)
else:
self.assertCountEqual(list(res.index), expected_index)
self.assertAlmostEqual(res["TEST__ar_coefficient__k_2__coeff_0"], 1, places=2)
self.assertAlmostEqual(res["TEST__ar_coefficient__k_2__coeff_1"], 3.5, places=2)
self.assertAlmostEqual(res["TEST__ar_coefficient__k_2__coeff_2"], -2, places=2)
Expand Down Expand Up @@ -428,4 +444,4 @@ def test_range_count(self):
self.assertEqualPandasSeriesWrapper(range_count, list(range(10)), 9, min=0, max=9)
self.assertEqualPandasSeriesWrapper(range_count, list(range(10)), 10, min=0, max=10)
self.assertEqualPandasSeriesWrapper(range_count, list(range(0, -10, -1)), 9, min=-10, max=0)
self.assertEqualPandasSeriesWrapper(range_count, [np.NaN, np.PINF, np.NINF] + list(range(10)), 10, min=0, max=10)
self.assertEqualPandasSeriesWrapper(range_count, [np.NaN, np.PINF, np.NINF] + list(range(10)), 10, min=0, max=10)
15 changes: 11 additions & 4 deletions tests/feature_extraction/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import numpy as np

from tsfresh.feature_extraction.settings import FeatureExtractionSettings
from sys import version_info


class TestSettingsObject(TestCase):

Expand Down Expand Up @@ -36,13 +38,18 @@ def test_from_columns(self):

cset = fset.from_columns(feature_names)

self.assertItemsEqual(list(cset.kind_to_calculation_settings_mapping[tsn].keys()),
["sum_values", "median", "length", "quantile", "number_peaks", "ar_coefficient",
"value_count"])
if int(version_info[0]) == 2:
self.assertItemsEqual(list(cset.kind_to_calculation_settings_mapping[tsn].keys()),
["sum_values", "median", "length", "quantile", "number_peaks", "ar_coefficient",
"value_count"])
else:
self.assertCountEqual(list(cset.kind_to_calculation_settings_mapping[tsn].keys()),
["sum_values", "median", "length", "quantile", "number_peaks", "ar_coefficient",
"value_count"])

self.assertEqual(cset.kind_to_calculation_settings_mapping[tsn]["sum_values"], None)
self.assertEqual(cset.kind_to_calculation_settings_mapping[tsn]["ar_coefficient"],
[{"k": 20, "coeff": 4}, {"k": -1, "coeff": 10}])

self.assertEqual(cset.kind_to_calculation_settings_mapping[tsn]["value_count"],
[{"value": np.PINF}, {"value": np.NINF}, {"value": np.NaN}])
[{"value": np.PINF}, {"value": np.NINF}, {"value": np.NaN}])
6 changes: 5 additions & 1 deletion tests/feature_extraction/test_ts_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from tests.fixtures import DataTestCase
from tsfresh.feature_extraction.extraction import extract_features
from tsfresh.feature_extraction.settings import FeatureExtractionSettings
from sys import version_info


class FeatureExtractorTestCase(DataTestCase):
Expand Down Expand Up @@ -48,7 +49,10 @@ def test_calculate_ts_features_after_randomisation(self):
extracted_features_from_random = extract_features(df_random, self.settings,
"id", "sort", "kind", "val").sort_index()

self.assertItemsEqual(extracted_features.columns, extracted_features_from_random.columns)
if int(version_info[0]) == 2:
self.assertItemsEqual(extracted_features.columns, extracted_features_from_random.columns)
else:
self.assertCountEqual(extracted_features.columns, extracted_features_from_random.columns)

for col in extracted_features:
self.assertIsNone(np.testing.assert_array_almost_equal(extracted_features[col],
Expand Down
6 changes: 5 additions & 1 deletion tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ def create_test_data_sample_with_target(self):
:return: target y which is the mean of each sample's timeseries
"""
cid = np.repeat(range(50), 3)
csort = range(3) * 50
# The csort was changed for Python 3 support
# https://github.com/blue-yonder/tsfresh/issues/8
# range has changed type in py3 from list to class
# csort = range(3) * 50
csort = list(range(3)) * 50
cval = [1, 2, 3] * 30 + [4, 5, 6] * 20
df = pd.DataFrame({'id': cid, 'kind': 'a', 'sort': csort, 'val': cval})
y = pd.Series([2] * 30 + [5] * 20)
Expand Down
25 changes: 24 additions & 1 deletion tests/transformers/test_feature_augmenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ def test_fit_and_transform(self):

# Fit should do nothing
returned_df = augmenter.fit()
# Changed for py3 as unittest.assertEqual has changed a little
# TODO in progress
# Tried patterns, back at the original for now so can commit the
# assertItemsEqual assertCountEqual fix
# self.assertEqual(returned_df, augmenter)
#> self.assertEqual(list(X_transformed.columns), ["feature_1", "a__length", "b__length"])
#E AssertionError: Lists differ: ['feature_1', 'b__length', 'a__length'] != ['feature_1', 'a__length', 'b__length']
#E
#E First differing element 1:
#E 'b__length'
#E 'a__length'
#E
#E - ['feature_1', 'b__length', 'a__length']
#E + ['feature_1', 'a__length', 'b__length']

# self.assertEqual(sorted(returned_df), sorted((augmenter))
#> self.assertEqual(sorted(returned_df), sorted(augmenter))
#E TypeError: 'FeatureAugmenter' object is not iterable

# self.assertEqual(sorted(returned_df), sorted(list(augmenter)))
#> self.assertEqual(sorted(returned_df), sorted(list(augmenter)))
#E TypeError: 'FeatureAugmenter' object is not iterable

self.assertEqual(returned_df, augmenter)

self.assertRaises(RuntimeError, augmenter.transform, None)
Expand Down Expand Up @@ -75,4 +98,4 @@ def test_add_features_to_only_a_part(self):
for index, row in X_transformed.iterrows():
print((index, row))
self.assertFalse(np.isnan(row["a__length"]))
self.assertFalse(np.isnan(row["b__length"]))
self.assertFalse(np.isnan(row["b__length"]))

0 comments on commit 89590c0

Please sign in to comment.