Skip to content

Commit

Permalink
refactor: use Table.__init__ where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed May 6, 2023
1 parent d89e840 commit ad86a2f
Show file tree
Hide file tree
Showing 72 changed files with 291 additions and 313 deletions.
18 changes: 9 additions & 9 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,9 +1070,9 @@ def transform_table(self, transformer: TableTransformer) -> Table:
>>> from safeds.data.tabular.transformation import OneHotEncoder
>>> from safeds.data.tabular.containers import Table
>>> transformer = OneHotEncoder()
>>> table = Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]})
>>> transformer = transformer.fit(table, None)
>>> table.transform_table(transformer)
>>> table = Table({"col1": [1, 2, 1], "col2": [1, 2, 4]})
>>> fitted_transformer = transformer.fit(table, None)
>>> table.transform_table(fitted_transformer)
col1_1 col1_2 col2_1 col2_2 col2_4
0 1.0 0.0 1.0 0.0 0.0
1 0.0 1.0 0.0 1.0 0.0
Expand Down Expand Up @@ -1104,15 +1104,15 @@ def inverse_transform_table(self, transformer: InvertibleTableTransformer) -> Ta
>>> from safeds.data.tabular.transformation import OneHotEncoder
>>> from safeds.data.tabular.containers import Table
>>> transformer = OneHotEncoder()
>>> table = Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]})
>>> transformer = transformer.fit(table, None)
>>> transformed_table = transformer.transform(table)
>>> transformed_table.inverse_transform_table(transformer)
>>> table = Table({"col1": [1, 2, 1], "col2": [1, 2, 4]})
>>> fitted_transformer = transformer.fit(table, None)
>>> transformed_table = fitted_transformer.transform(table)
>>> transformed_table.inverse_transform_table(fitted_transformer)
col1 col2
0 1 1
1 2 2
2 1 4
>>> transformer.inverse_transform(transformed_table)
>>> fitted_transformer.inverse_transform(transformed_table)
col1 col2
0 1 1
1 2 2
Expand Down Expand Up @@ -1331,7 +1331,7 @@ def to_html(self) -> str:
Examples
--------
>>> from safeds.data.tabular.containers import Table
>>> table = Table.from_dict({"a": [1, 2, 3], "b": [4, 5, 6]})
>>> table = Table({"a": [1, 2, 3], "b": [4, 5, 6]})
>>> html = table.to_html()
"""
return self._data.to_html(max_rows=self._data.shape[0], max_cols=self._data.shape[1])
Expand Down
18 changes: 18 additions & 0 deletions src/safeds/data/tabular/transformation/_table_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ def fit_and_transform(self, table: Table, column_names: list[str] | None = None)
class InvertibleTableTransformer(TableTransformer):
"""A `TableTransformer` that can also undo the learned transformation after it has been applied."""

@abstractmethod
def fit(self, table: Table, column_names: list[str] | None) -> InvertibleTableTransformer:
"""
Learn a transformation for a set of columns in a table.
Parameters
----------
table : Table
The table used to fit the transformer.
column_names : Optional[list[str]]
The list of columns from the table used to fit the transformer. If `None`, all columns are used.
Returns
-------
fitted_transformer : InvertibleTableTransformer
The fitted transformer.
"""

@abstractmethod
def inverse_transform(self, transformed_table: Table) -> Table:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@


def test_should_match_snapshot() -> None:
table = Table.from_dict({"A": [1, 2, 3]})
table = Table({"A": [1, 2, 3]})
table.get_column("A").plot_boxplot()
current = table.get_column("A").plot_boxplot()
snapshot = Image.from_png_file(resolve_resource_path("./image/snapshot_boxplot.png"))
assert snapshot._image.tobytes() == current._image.tobytes()


def test_should_raise_if_column_contains_non_numerical_values() -> None:
table = Table.from_dict({"A": [1, 2, "A"]})
table = Table({"A": [1, 2, "A"]})
with pytest.raises(NonNumericColumnError):
table.get_column("A").plot_boxplot()
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@


def test_should_match_snapshot_numeric() -> None:
table = Table.from_dict({"A": [1, 2, 3]})
table = Table({"A": [1, 2, 3]})
current = table.get_column("A").plot_histogram()
snapshot = Image.from_png_file(resolve_resource_path("./image/snapshot_histogram_numeric.png"))
assert snapshot._image.tobytes() == current._image.tobytes()


def test_should_match_snapshot_str() -> None:
table = Table.from_dict({"A": ["A", "B", "Apple"]})
table = Table({"A": ["A", "B", "Apple"]})
current = table.get_column("A").plot_histogram()
snapshot = Image.from_png_file(resolve_resource_path("./image/snapshot_histogram_str.png"))
assert snapshot._image.tobytes() == current._image.tobytes()
12 changes: 6 additions & 6 deletions tests/safeds/data/tabular/containers/_table/test_add_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
("table1", "column", "expected"),
[
(
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Column("col3", ["a", "b", "c"]),
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4], "col3": ["a", "b", "c"]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4], "col3": ["a", "b", "c"]}),
),
(
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Column("col3", [0, -1, -2]),
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4], "col3": [0, -1, -2]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4], "col3": [0, -1, -2]}),
),
],
ids=["String", "Integer"],
Expand All @@ -25,12 +25,12 @@ def test_should_add_column(table1: Table, column: Column, expected: Table) -> No


def test_should_raise_error_if_column_name_exists() -> None:
table1 = Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]})
table1 = Table({"col1": [1, 2, 1], "col2": [1, 2, 4]})
with pytest.raises(DuplicateColumnNameError):
table1.add_column(Column("col1", ["a", "b", "c"]))


def test_should_raise_error_if_column_size_invalid() -> None:
table1 = Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]})
table1 = Table({"col1": [1, 2, 1], "col2": [1, 2, 4]})
with pytest.raises(ColumnSizeError):
table1.add_column(Column("col3", ["a", "b", "c", "d"]))
14 changes: 7 additions & 7 deletions tests/safeds/data/tabular/containers/_table/test_add_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
("table1", "columns", "expected"),
[
(
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
[Column("col3", [0, -1, -2]), Column("col4", ["a", "b", "c"])],
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4], "col3": [0, -1, -2], "col4": ["a", "b", "c"]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4], "col3": [0, -1, -2], "col4": ["a", "b", "c"]}),
),
],
ids=["add 2 columns"],
Expand All @@ -23,9 +23,9 @@ def test_should_add_columns(table1: Table, columns: list[Column], expected: Tabl
("table1", "table2", "expected"),
[
(
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Table.from_dict({"col3": [0, -1, -2], "col4": ["a", "b", "c"]}),
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4], "col3": [0, -1, -2], "col4": ["a", "b", "c"]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Table({"col3": [0, -1, -2], "col4": ["a", "b", "c"]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4], "col3": [0, -1, -2], "col4": ["a", "b", "c"]}),
),
],
ids=["add a table with 2 columns"],
Expand All @@ -39,7 +39,7 @@ def test_should_add_columns_from_table(table1: Table, table2: Table, expected: T
("table", "columns"),
[
(
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
[Column("col3", ["a", "b", "c", "d"]), Column("col4", ["e", "f", "g", "h"])],
),
],
Expand All @@ -54,7 +54,7 @@ def test_should_raise_error_if_column_size_invalid(table: Table, columns: list[C
("table", "columns"),
[
(
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
[Column("col2", ["a", "b", "c"]), Column("col3", [2, 3, 4])],
),
],
Expand Down
4 changes: 2 additions & 2 deletions tests/safeds/data/tabular/containers/_table/test_add_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@pytest.mark.parametrize(
("table", "row"),
[
(Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]}), Row({"col1": 5, "col2": 6})),
(Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}), Row({"col1": 5, "col2": 6})),
],
ids=["added row"],
)
Expand All @@ -19,7 +19,7 @@ def test_should_add_row(table: Table, row: Row) -> None:


def test_should_raise_error_if_row_schema_invalid() -> None:
table1 = Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]})
table1 = Table({"col1": [1, 2, 1], "col2": [1, 2, 4]})
row = Row({"col1": 5, "col2": "Hallo"})
with raises(SchemaMismatchError):
table1.add_row(row)
12 changes: 6 additions & 6 deletions tests/safeds/data/tabular/containers/_table/test_add_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
("table1", "rows", "table2"),
[
(
Table.from_dict({"col1": ["a", "b", "c"], "col2": [1, 2, 4]}),
Table({"col1": ["a", "b", "c"], "col2": [1, 2, 4]}),
[Row({"col1": "d", "col2": 6}), Row({"col1": "e", "col2": 8})],
Table.from_dict({"col1": ["a", "b", "c", "d", "e"], "col2": [1, 2, 4, 6, 8]}),
Table({"col1": ["a", "b", "c", "d", "e"], "col2": [1, 2, 4, 6, 8]}),
),
],
ids=["Rows with string and integer values"],
Expand All @@ -23,9 +23,9 @@ def test_should_add_rows(table1: Table, rows: list[Row], table2: Table) -> None:
("table1", "table2", "expected"),
[
(
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Table.from_dict({"col1": [5, 7], "col2": [6, 8]}),
Table.from_dict({"col1": [1, 2, 1, 5, 7], "col2": [1, 2, 4, 6, 8]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Table({"col1": [5, 7], "col2": [6, 8]}),
Table({"col1": [1, 2, 1, 5, 7], "col2": [1, 2, 4, 6, 8]}),
),
],
ids=["Rows from table"],
Expand All @@ -36,7 +36,7 @@ def test_should_add_rows_from_table(table1: Table, table2: Table, expected: Tabl


def test_should_raise_error_if_row_schema_invalid() -> None:
table1 = Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]})
table1 = Table({"col1": [1, 2, 1], "col2": [1, 2, 4]})
row = [Row({"col1": 2, "col2": 4}), Row({"col1": 5, "col2": "Hallo"})]
with pytest.raises(SchemaMismatchError):
table1.add_rows(row)
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
@pytest.mark.parametrize(
("table", "expected"),
[
(Table.from_dict({"col1": [1], "col2": [1]}), ["col1", "col2"]),
(Table.from_dict({}), []),
(Table({"col1": [1], "col2": [1]}), ["col1", "col2"]),
(Table(), []),
],
ids=["Integer", "empty"],
)
Expand Down
6 changes: 3 additions & 3 deletions tests/safeds/data/tabular/containers/_table/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@pytest.mark.parametrize(
"table",
[
Table.from_dict({}),
Table.from_dict({"a": [1, 2], "b": [3, 4]}),
Table(),
Table({"a": [1, 2], "b": [3, 4]}),
],
ids=[
"empty",
Expand All @@ -22,6 +22,6 @@ def test_should_restore_table_from_exchange_object(table: Table) -> None:


def test_should_raise_error_if_allow_copy_is_false() -> None:
table = Table.from_dict({})
table = Table()
with pytest.raises(NotImplementedError, match="`allow_copy` must be True"):
table.__dataframe__(allow_copy=False)
16 changes: 8 additions & 8 deletions tests/safeds/data/tabular/containers/_table/test_eq.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
@pytest.mark.parametrize(
("table1", "table2", "expected"),
[
(Table.from_dict({}), Table.from_dict({}), True),
(Table.from_dict({"col1": [1]}), Table.from_dict({"col1": [1]}), True),
(Table.from_dict({"col1": [1]}), Table.from_dict({"col2": [1]}), False),
(Table.from_dict({"col1": [1, 2, 3]}), Table.from_dict({"col1": [1, 1, 3]}), False),
(Table.from_dict({"col1": [1, 2, 3]}), Table.from_dict({"col1": ["1", "2", "3"]}), False),
(Table(), Table(), True),
(Table({"col1": [1]}), Table({"col1": [1]}), True),
(Table({"col1": [1]}), Table({"col2": [1]}), False),
(Table({"col1": [1, 2, 3]}), Table({"col1": [1, 1, 3]}), False),
(Table({"col1": [1, 2, 3]}), Table({"col1": ["1", "2", "3"]}), False),
],
ids=[
"empty Table",
Expand All @@ -27,7 +27,7 @@ def test_should_return_whether_two_tables_are_equal(table1: Table, table2: Table

@pytest.mark.parametrize(
"table",
[Table.from_dict({}), Table.from_dict({"col1": [1]})],
[Table(), Table({"col1": [1]})],
ids=[
"empty",
"non-empty",
Expand All @@ -40,8 +40,8 @@ def test_should_return_true_if_objects_are_identical(table: Table) -> None:
@pytest.mark.parametrize(
("table", "other"),
[
(Table.from_dict({"col1": [1]}), None),
(Table.from_dict({"col1": [1]}), Row()),
(Table({"col1": [1]}), None),
(Table({"col1": [1]}), Row()),
],
ids=[
"Table vs. None",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
("table1", "filter_column", "filter_value", "table2"),
[
(
Table.from_dict({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
Table({"col1": [1, 2, 1], "col2": [1, 2, 4]}),
"col1",
1,
Table.from_dict({"col1": [1, 1], "col2": [1, 4]}),
Table({"col1": [1, 1], "col2": [1, 4]}),
),
(
Table.from_dict({"col1": [3, 2, 4], "col2": [1, 2, 4]}),
Table({"col1": [3, 2, 4], "col2": [1, 2, 4]}),
"col1",
1,
Table._from_pandas_dataframe(pd.DataFrame(), Schema({"col1": Integer(), "col2": Integer()})),
Expand All @@ -29,6 +29,6 @@ def test_should_filter_rows(table1: Table, filter_column: String, filter_value:

# noinspection PyTypeChecker
def test_should_raise_error_if_column_type_invalid() -> None:
table = Table.from_dict({"col1": [1, 2, 3], "col2": [1, 1, 4]})
table = Table({"col1": [1, 2, 3], "col2": [1, 1, 4]})
with pytest.raises(TypeError):
table.filter_rows(table.get_column("col1")._data > table.get_column("col2")._data)
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
Column("A", [1, 4]),
Column("B", [2, 5]),
],
Table.from_dict(
Table(
{
"A": [1, 4],
"B": [2, 5],
},
),
),
([], Table.from_dict({})),
([], Table()),
],
ids=["2 Columns", "empty"],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[
(
resolve_resource_path("./dummy_excel_file.xlsx"),
Table.from_dict(
Table(
{
"A": [1],
"B": [2],
Expand All @@ -20,7 +20,7 @@
),
(
Path(resolve_resource_path("./dummy_excel_file.xlsx")),
Table.from_dict(
Table(
{
"A": [1],
"B": [2],
Expand Down
4 changes: 2 additions & 2 deletions tests/safeds/data/tabular/containers/_table/test_from_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
[
(
[],
Table.from_dict({}),
Table({}),
),
(
[
Row({"A": 1, "B": 4, "C": "d"}),
Row({"A": 2, "B": 5, "C": "e"}),
Row({"A": 3, "B": 6, "C": "f"}),
],
Table.from_dict(
Table(
{
"A": [1, 2, 3],
"B": [4, 5, 6],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@pytest.mark.parametrize(
("table1", "expected"),
[
(Table.from_dict({"col1": ["col1_1"], "col2": ["col2_1"]}), Column("col1", ["col1_1"])),
(Table({"col1": ["col1_1"], "col2": ["col2_1"]}), Column("col1", ["col1_1"])),
],
ids=["First column"],
)
Expand All @@ -15,6 +15,6 @@ def test_should_get_column(table1: Table, expected: Column) -> None:


def test_should_raise_error_if_column_name_unknown() -> None:
table = Table.from_dict({"col1": ["col1_1"], "col2": ["col2_1"]})
table = Table({"col1": ["col1_1"], "col2": ["col2_1"]})
with pytest.raises(UnknownColumnNameError):
table.get_column("col3")
Loading

0 comments on commit ad86a2f

Please sign in to comment.