-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Added bdi.materialize_mapping() and basic value mappers
- Loading branch information
Showing
4 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import pandas as pd | ||
|
||
|
||
class ValueMapper: | ||
""" | ||
A ValueMapper represents objects that transform the values in a input | ||
column to the values from a new output column. | ||
""" | ||
|
||
def map(self, input_column: pd.Series) -> pd.Series: | ||
""" | ||
Every concrete ValueMapper should implement this method, which takes a | ||
pandas Series as input and returns a new pandas Series with transformed | ||
values. | ||
""" | ||
pass | ||
|
||
|
||
class IdentityValueMapper(ValueMapper): | ||
""" | ||
A column mapper that maps each value in input column into itself. | ||
""" | ||
|
||
def map(self, input_column: pd.Series) -> pd.Series: | ||
""" | ||
Simply copies the values in input_column to the output column. | ||
""" | ||
return input_column.copy() | ||
|
||
|
||
class FunctionValueMapper(ValueMapper): | ||
""" | ||
A column mapper that transforms each value in the input column using the | ||
provided custom function. | ||
""" | ||
|
||
def __init__(self, function): | ||
self.function = function | ||
|
||
def map(self, input_column: pd.Series) -> pd.Series: | ||
""" | ||
Applies the given function to each value in input_column to generate | ||
the output column. | ||
""" | ||
return input_column.map(self.function) | ||
|
||
|
||
class DictionaryMapper(ValueMapper): | ||
""" | ||
A column mapper that transforms each value in the input column using the | ||
values stored in the provided dictionary. | ||
""" | ||
|
||
def __init__(self, dictionary: dict): | ||
self.dictionary = dictionary | ||
|
||
def map(self, input_column: pd.Series) -> pd.Series: | ||
""" | ||
Transforms the values in the input_column to the values specified in | ||
the dictionary provided using the object constructor. | ||
""" | ||
return input_column.map(self.dictionary) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pandas as pd | ||
from bdikit.mapping_algorithms.value_mapping import ( | ||
FunctionValueMapper, | ||
DictionaryMapper, | ||
IdentityValueMapper, | ||
) | ||
|
||
|
||
def test_identity_mapper(): | ||
# given | ||
str_column = pd.Series(data=["a", "b", "c", "d", "e"], name="column_str") | ||
identity_mapper = IdentityValueMapper() | ||
|
||
# when | ||
mapped_column = identity_mapper.map(str_column) | ||
|
||
# then | ||
assert mapped_column.eq(["a", "b", "c", "d", "e"]).all() | ||
|
||
|
||
def test_dictionary_mapper(): | ||
# given | ||
str_column = pd.Series(data=["a", "b", "c", "d", "e"], name="column_str") | ||
dict_mapper = DictionaryMapper(dictionary={"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}) | ||
|
||
# when | ||
mapped_column = dict_mapper.map(str_column) | ||
|
||
# then | ||
assert mapped_column.eq([1, 2, 3, 4, 5]).all() | ||
|
||
|
||
def test_custom_function_mapper(): | ||
# given | ||
str_column = pd.Series(data=["a", "b", "c", "d", "e"], name="column_str") | ||
fn_mapper = FunctionValueMapper(function=lambda x: x + x) | ||
|
||
# when | ||
mapped_column = fn_mapper.map(str_column) | ||
|
||
# then | ||
assert mapped_column.eq(["aa", "bb", "cc", "dd", "ee"]).all() |