Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HeroTypes in Representation; DataFrame in _types #157

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
fa342a9
added MultiIndex DF support
mk2510 Aug 18, 2020
59a9f8c
beginning with tests
henrifroese Aug 19, 2020
19c52de
implemented correct sparse support
mk2510 Aug 19, 2020
66e566c
Merge branch 'master_upstream' into change_representation_to_multicolumn
mk2510 Aug 21, 2020
41f55a8
added back list() and rm .tolist()
mk2510 Aug 21, 2020
217611a
rm .tolist() and added list()
mk2510 Aug 21, 2020
6a3b56d
Adopted the test to the new dataframes
mk2510 Aug 21, 2020
b8ff561
wrong format
mk2510 Aug 21, 2020
e3af2f9
Address most review comments.
henrifroese Aug 21, 2020
77ad80e
Add more unittests for representation
henrifroese Aug 21, 2020
f7eb7c3
- Update _types.py with DocumentTermDF
henrifroese Aug 22, 2020
4937a4f
Fix DocumentTermDF example DataFrame column names
henrifroese Aug 22, 2020
e2768b5
implemented the suggested changes
mk2510 Sep 4, 2020
b09f624
fixed messy docstring
mk2510 Sep 4, 2020
508c361
fix black issues
mk2510 Sep 4, 2020
75e955f
fix formatting
mk2510 Sep 4, 2020
9ca244d
begin switch away from multiindex
henrifroese Sep 4, 2020
4ebc266
Merge remote-tracking branch 'origin/change_representation_to_multico…
henrifroese Sep 4, 2020
559a7bd
Finish switch from DocumentTermDF to MatrixDF
henrifroese Sep 4, 2020
75a999c
changed Dataframe name from MatrixDF to DataFrame
mk2510 Sep 4, 2020
a38a32b
merge master
henrifroese Sep 12, 2020
c7b0ece
apply stash
henrifroese Sep 12, 2020
4aeec2a
finish switch to DataFrame type
henrifroese Sep 12, 2020
a304413
Merge remote-tracking branch 'origin/Hero_Types_in_Representation' in…
henrifroese Sep 12, 2020
8d49bff
merge remote
henrifroese Sep 12, 2020
85076b8
Remove check for nlevels
henrifroese Sep 12, 2020
91ed11a
incorporate suggested changes
henrifroese Sep 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
beginning with tests
  • Loading branch information
henrifroese committed Aug 19, 2020
commit 59a9f8c0df70d8136780b3160bc1d2ca59f48b26
147 changes: 72 additions & 75 deletions tests/test_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,84 @@ def _tfidf(term, corpus, document_index):
[["Test", "Test", "TEST", "!"], ["Test", "?", ".", "."]], index=[5, 7]
)

s_tokenized_output_index = [0,1]
s_tokenized_output_index = [0, 1]

s_tokenized_output_index_noncontinous = [5, 7]


def _get_multiindex_for_tokenized_output(first_level_name):
return pd.MultiIndex.from_product(
[[first_level_name], ["!", ".", "?", "TEST", "Test"]]
)

s_tokenized_output_index_noncontinous = [5,7]

test_cases_vectorization = [
# format: [function_name, function, correct output for tokenized input above, dtype of output]
["count", representation.count, [1, 1, 2, 2, 1, 1], "int"],
# format: [function_name, function, correct output for tokenized input above]
[
"count",
representation.count,
pd.DataFrame(
[[1, 0, 0, 1, 2], [0, 2, 1, 0, 1]],
index=s_tokenized_output_index,
columns=_get_multiindex_for_tokenized_output("count"),
).astype("Sparse"),
],
[
"term_frequency",
representation.term_frequency,
[0.125, 0.125, 0.250, 0.250, 0.125, 0.125],
"float",
pd.DataFrame(
[[0.125, 0.0, 0.0, 0.125, 0.250], [0.0, 0.25, 0.125, 0.0, 0.125]],
index=s_tokenized_output_index,
columns=_get_multiindex_for_tokenized_output("term_frequency"),
).astype("Sparse"),
],
[
"tfidf",
representation.tfidf,
[_tfidf(x[1], s_tokenized, x[0]) for x in s_tokenized_output_index],
"float",
pd.DataFrame(
[
[
_tfidf(x, s_tokenized, 0) # Testing the tfidf formula here
for x in ["!", ".", "?", "TEST", "Test"]
],
[_tfidf(x, s_tokenized, 0) for x in ["!", ".", "?", "TEST", "Test"]],
],
index=s_tokenized_output_index,
columns=_get_multiindex_for_tokenized_output("tfidf"),
).astype("Sparse"),
],
]


test_cases_vectorization_min_df = [
# format: [function_name, function, correct output for tokenized input above, dtype of output]
["count", representation.count, [2, 1], "int"],
["term_frequency", representation.term_frequency, [0.666667, 0.333333], "float",],
["tfidf", representation.tfidf, [2.0, 1.0], "float",],
# format: [function_name, function, correct output for tokenized input above]
[
"count",
representation.count,
pd.DataFrame(
[2, 1],
index=s_tokenized_output_index,
columns=pd.MultiIndex.from_tuples([("count", "Test")]),
).astype("Sparse"),
],
[
"term_frequency",
representation.term_frequency,
pd.DataFrame(
[0.666667, 0.333333],
index=s_tokenized_output_index,
columns=pd.MultiIndex.from_tuples([("term_frequency", "Test")]),
).astype("Sparse"),
],
[
"tfidf",
representation.tfidf,
pd.DataFrame(
[2.0, 1.0],
index=s_tokenized_output_index,
columns=pd.MultiIndex.from_tuples([("tfidf", "Test")]),
).astype("Sparse"),
],
]


Expand All @@ -91,62 +143,23 @@ class AbstractRepresentationTest(PandasTestCase):
"""

@parameterized.expand(test_cases_vectorization)
def test_vectorization_simple(
self, name, test_function, correct_output_values, int_or_float
):
if int_or_float == "int":
s_true = pd.Series(
correct_output_values, index=s_tokenized_output_index, dtype="int"
).astype(pd.SparseDtype(np.int64, 0))
else:
s_true = pd.Series(
correct_output_values, index=s_tokenized_output_index, dtype="float"
).astype(pd.SparseDtype("float", np.nan))
def test_vectorization_simple(self, name, test_function, correct_output):
s_true = correct_output
result_s = test_function(s_tokenized)

pd.testing.assert_series_equal(s_true, result_s)
pd.testing.assert_series_equal(s_true, result_s, check_less_precise=True)

@parameterized.expand(test_cases_vectorization)
def test_vectorization_noncontinuous_index_kept(
self, name, test_function, correct_output_values, int_or_float
self, name, test_function, correct_output=None
):
if int_or_float == "int":
s_true = pd.Series(
correct_output_values,
index=s_tokenized_output_noncontinuous_index,
dtype="int",
).astype(pd.SparseDtype(np.int64, 0))
else:
s_true = pd.Series(
correct_output_values,
index=s_tokenized_output_noncontinuous_index,
dtype="float",
).astype(pd.SparseDtype("float", np.nan))

result_s = test_function(s_tokenized_with_noncontinuous_index)

pd.testing.assert_series_equal(s_true, result_s)
pd.testing.assert_series_equal(s_tokenized_output_index_noncontinous, result_s)

@parameterized.expand(test_cases_vectorization_min_df)
def test_vectorization_min_df(
self, name, test_function, correct_output_values, int_or_float
):
if int_or_float == "int":
s_true = pd.Series(
correct_output_values,
index=s_tokenized_output_min_df_index,
dtype="int",
).astype(pd.SparseDtype(np.int64, 0))
else:
s_true = pd.Series(
correct_output_values,
index=s_tokenized_output_min_df_index,
dtype="float",
).astype(pd.SparseDtype("float", np.nan))

def test_vectorization_min_df(self, name, test_function, correct_output):
s_true = correct_output
result_s = test_function(s_tokenized, min_df=2)

pd.testing.assert_series_equal(s_true, result_s)
pd.testing.assert_series_equal(s_true, result_s, check_less_precise=True)

@parameterized.expand(test_cases_vectorization)
def test_vectorization_not_tokenized_yet_warning(self, name, test_function, *args):
Expand All @@ -159,19 +172,3 @@ def test_vectorization_arguments_to_sklearn(self, name, test_function, *args):
test_function(s_not_tokenized, max_features=1, min_df=1, max_df=1.0)
except TypeError:
self.fail("Sklearn arguments not handled correctly.")

"""
Individual / special tests.
"""

def test_tfidf_formula(self):
s = pd.Series(["Hi Bye", "Test Bye Bye"])
s = preprocessing.tokenize(s)
s_true_index = pd.MultiIndex.from_tuples(
[(0, "Bye"), (0, "Hi"), (1, "Bye"), (1, "Test")],
)
s_true = pd.Series(
[_tfidf(x[1], s, x[0]) for x in s_true_index], index=s_true_index
).astype("Sparse")

self.assertEqual(representation.tfidf(s), s_true)
8 changes: 4 additions & 4 deletions texthero/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ def count(
>>> import pandas as pd
>>> s = pd.Series(["Sentence one", "Sentence two"]).pipe(hero.tokenize)
>>> hero.count(s)
count
Sentence one two
count
Sentence one two
0 1 1 0
1 1 0 1

# FIXME columns pandas doctest
See Also
--------
Document Term DataFrame: TODO add tutorial link
Expand Down Expand Up @@ -375,7 +375,7 @@ def pca(
values = list(s)

return pd.Series(pca.fit_transform(values).tolist(), index=s.index)

# FIXME: merge master again

def nmf(
s: Union[pd.Series, pd.DataFrame], n_components=2, random_state=None
Expand Down