From ec011e47d8a595ac6aa1c40d911b1b3da8cf5bd4 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Mon, 27 Mar 2023 20:36:15 +0200 Subject: [PATCH] feat: add `_file` suffix to methods interacting with files (#103) ### Summary of Changes In `Table`: * Rename `to_csv` to `to_csv_file` * Rename `to_json` to `to_json_file` * Rename `from_csv` to `from_csv_file` * Rename `from_json` to `from_json_file` * Rename `path_to_file` parameters to `path` --------- Co-authored-by: lars-reimann --- docs/tutorials/data_processing.ipynb | 2 +- docs/tutorials/data_visualization.ipynb | 2 +- src/safeds/data/tabular/containers/_table.py | 20 +++++++------- ...d_csv.csv => test_table_from_csv_file.csv} | 0 ...on.json => test_table_from_json_file.json} | 0 .../containers/_row/test_has_column.py | 4 +-- .../containers/_table/test_column_drop.py | 4 +-- .../_table/test_drop_duplicate_rows.py | 2 +- .../containers/_table/test_from_columns.py | 2 +- .../containers/_table/test_from_csv_file.py | 18 +++++++++++++ .../containers/_table/test_from_json_file.py | 20 ++++++++++++++ .../containers/_table/test_from_rows.py | 2 +- .../tabular/containers/_table/test_get_row.py | 6 ++--- .../containers/_table/test_has_column.py | 4 +-- .../_table/test_keep_only_columns.py | 4 +-- .../containers/_table/test_read_csv.py | 16 ------------ .../containers/_table/test_read_json.py | 16 ------------ .../tabular/containers/_table/test_rename.py | 8 ++++-- .../containers/_table/test_replace_column.py | 6 ++--- .../_table/test_table_add_column.py | 6 ++--- .../containers/_table/test_to_columns.py | 2 +- .../containers/_table/test_to_csv_file.py | 15 +++++++++++ .../containers/_table/test_to_json_file.py | 15 +++++++++++ .../tabular/containers/_table/test_to_rows.py | 2 +- .../_table/test_transform_column.py | 6 ++--- .../containers/_table/test_write_and_read.py | 26 ------------------- .../containers/_tagged_table/test_features.py | 2 +- .../containers/_tagged_table/test_target.py | 2 +- .../_table_schema/test_get_column_type.py | 2 +- .../typing/_table_schema/test_has_column.py | 4 +-- .../typing/_table_schema/test_table_equals.py | 4 +-- .../ml/classification/test_ada_boost.py | 4 +-- .../ml/classification/test_decision_tree.py | 4 +-- .../classification/test_gradient_boosting.py | 4 +-- .../test_k_nearest_neighbors.py | 4 +-- .../test_logistic_regression.py | 4 +-- .../ml/classification/test_random_forest.py | 4 +-- tests/safeds/ml/regression/test_ada_boost.py | 4 +-- .../ml/regression/test_decision_tree.py | 4 +-- .../safeds/ml/regression/test_elastic_net.py | 6 +++-- .../ml/regression/test_gradient_boosting.py | 4 +-- .../ml/regression/test_k_nearest_neighbors.py | 4 +-- .../ml/regression/test_lasso_regression.py | 6 +++-- .../ml/regression/test_linear_regression.py | 6 +++-- .../ml/regression/test_random_forest.py | 4 +-- .../ml/regression/test_ridge_regression.py | 6 +++-- 46 files changed, 156 insertions(+), 134 deletions(-) rename tests/resources/{test_table_read_csv.csv => test_table_from_csv_file.csv} (100%) rename tests/resources/{test_table_read_json.json => test_table_from_json_file.json} (100%) create mode 100644 tests/safeds/data/tabular/containers/_table/test_from_csv_file.py create mode 100644 tests/safeds/data/tabular/containers/_table/test_from_json_file.py delete mode 100644 tests/safeds/data/tabular/containers/_table/test_read_csv.py delete mode 100644 tests/safeds/data/tabular/containers/_table/test_read_json.py create mode 100644 tests/safeds/data/tabular/containers/_table/test_to_csv_file.py create mode 100644 tests/safeds/data/tabular/containers/_table/test_to_json_file.py delete mode 100644 tests/safeds/data/tabular/containers/_table/test_write_and_read.py diff --git a/docs/tutorials/data_processing.ipynb b/docs/tutorials/data_processing.ipynb index 1aa7d4a66..0c8627fb4 100644 --- a/docs/tutorials/data_processing.ipynb +++ b/docs/tutorials/data_processing.ipynb @@ -29,7 +29,7 @@ "source": [ "from safeds.data.tabular.containers import Table\n", "\n", - "titanic = Table.from_csv(\"data/titanic.csv\")" + "titanic = Table.from_csv_file(\"data/titanic.csv\")" ], "metadata": { "collapsed": false diff --git a/docs/tutorials/data_visualization.ipynb b/docs/tutorials/data_visualization.ipynb index b9a4e102f..b223b2f47 100644 --- a/docs/tutorials/data_visualization.ipynb +++ b/docs/tutorials/data_visualization.ipynb @@ -22,7 +22,7 @@ "source": [ "from safeds.data.tabular.containers import Table\n", "\n", - "titanic = Table.from_csv(\"data/titanic.csv\")" + "titanic = Table.from_csv_file(\"data/titanic.csv\")" ], "metadata": { "collapsed": false, diff --git a/src/safeds/data/tabular/containers/_table.py b/src/safeds/data/tabular/containers/_table.py index fbfeeb962..2ff97c833 100644 --- a/src/safeds/data/tabular/containers/_table.py +++ b/src/safeds/data/tabular/containers/_table.py @@ -52,7 +52,7 @@ class Table: # ------------------------------------------------------------------------------------------------------------------ @staticmethod - def from_csv(path: str) -> Table: + def from_csv_file(path: str) -> Table: """ Read data from a CSV file into a table. @@ -82,7 +82,7 @@ def from_csv(path: str) -> Table: raise ValueError(f'Could not read file from "{path}" as CSV') from exception @staticmethod - def from_json(path: str) -> Table: + def from_json_file(path: str) -> Table: """ Read data from a JSON file into a table. @@ -1023,7 +1023,7 @@ def scatterplot(self, x_column_name: str, y_column_name: str) -> None: # Conversion # ------------------------------------------------------------------------------------------------------------------ - def to_csv(self, path_to_file: str) -> None: + def to_csv_file(self, path: str) -> None: """ Write the data from the table into a CSV file. If the file and/or the directories do not exist they will be created. @@ -1031,15 +1031,15 @@ def to_csv(self, path_to_file: str) -> None: Parameters ---------- - path_to_file : str + path : str The path to the output file. """ - Path(os.path.dirname(path_to_file)).mkdir(parents=True, exist_ok=True) + Path(os.path.dirname(path)).mkdir(parents=True, exist_ok=True) data_to_csv = self._data.copy() data_to_csv.columns = self._schema.get_column_names() - data_to_csv.to_csv(path_to_file, index=False) + data_to_csv.to_csv(path, index=False) - def to_json(self, path_to_file: str) -> None: + def to_json_file(self, path: str) -> None: """ Write the data from the table into a JSON file. If the file and/or the directories do not exist, they will be created. @@ -1047,13 +1047,13 @@ def to_json(self, path_to_file: str) -> None: Parameters ---------- - path_to_file : str + path : str The path to the output file. """ - Path(os.path.dirname(path_to_file)).mkdir(parents=True, exist_ok=True) + Path(os.path.dirname(path)).mkdir(parents=True, exist_ok=True) data_to_json = self._data.copy() data_to_json.columns = self._schema.get_column_names() - data_to_json.to_json(path_to_file) + data_to_json.to_json(path) def to_columns(self) -> list[Column]: """ diff --git a/tests/resources/test_table_read_csv.csv b/tests/resources/test_table_from_csv_file.csv similarity index 100% rename from tests/resources/test_table_read_csv.csv rename to tests/resources/test_table_from_csv_file.csv diff --git a/tests/resources/test_table_read_json.json b/tests/resources/test_table_from_json_file.json similarity index 100% rename from tests/resources/test_table_read_json.json rename to tests/resources/test_table_from_json_file.json diff --git a/tests/safeds/data/tabular/containers/_row/test_has_column.py b/tests/safeds/data/tabular/containers/_row/test_has_column.py index 8cf6d7645..a18246328 100644 --- a/tests/safeds/data/tabular/containers/_row/test_has_column.py +++ b/tests/safeds/data/tabular/containers/_row/test_has_column.py @@ -3,12 +3,12 @@ def test_has_column_positive() -> None: - table = Table.from_csv(resolve_resource_path("test_table_has_column.csv")) + table = Table.from_csv_file(resolve_resource_path("test_table_has_column.csv")) row = table.to_rows()[0] assert row.has_column("A") def test_has_column_negative() -> None: - table = Table.from_csv(resolve_resource_path("test_table_has_column.csv")) + table = Table.from_csv_file(resolve_resource_path("test_table_has_column.csv")) row = table.to_rows()[0] assert not row.has_column("C") diff --git a/tests/safeds/data/tabular/containers/_table/test_column_drop.py b/tests/safeds/data/tabular/containers/_table/test_column_drop.py index b4084a012..290a5f055 100644 --- a/tests/safeds/data/tabular/containers/_table/test_column_drop.py +++ b/tests/safeds/data/tabular/containers/_table/test_column_drop.py @@ -5,7 +5,7 @@ def test_table_column_drop() -> None: - table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv")) + table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) transformed_table = table.drop_columns(["A"]) assert transformed_table.schema.has_column( "B" @@ -13,6 +13,6 @@ def test_table_column_drop() -> None: def test_table_column_drop_warning() -> None: - table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv")) + table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) with pytest.raises(UnknownColumnNameError): table.drop_columns(["C"]) diff --git a/tests/safeds/data/tabular/containers/_table/test_drop_duplicate_rows.py b/tests/safeds/data/tabular/containers/_table/test_drop_duplicate_rows.py index 4c2fc8c09..fdfdea451 100644 --- a/tests/safeds/data/tabular/containers/_table/test_drop_duplicate_rows.py +++ b/tests/safeds/data/tabular/containers/_table/test_drop_duplicate_rows.py @@ -13,6 +13,6 @@ ) def test_drop_duplicate_rows(path: str) -> None: expected_table = Table(pd.DataFrame(data={"A": [1, 4], "B": [2, 5]})) - table = Table.from_csv(resolve_resource_path(path)) + table = Table.from_csv_file(resolve_resource_path(path)) result_table = table.drop_duplicate_rows() assert expected_table == result_table diff --git a/tests/safeds/data/tabular/containers/_table/test_from_columns.py b/tests/safeds/data/tabular/containers/_table/test_from_columns.py index 7354712bb..c908504e6 100644 --- a/tests/safeds/data/tabular/containers/_table/test_from_columns.py +++ b/tests/safeds/data/tabular/containers/_table/test_from_columns.py @@ -6,7 +6,7 @@ def test_from_columns() -> None: - table_expected = Table.from_csv(resolve_resource_path("test_column_table.csv")) + table_expected = Table.from_csv_file(resolve_resource_path("test_column_table.csv")) columns_table: list[Column] = [ Column(pd.Series([1, 4]), "A"), Column(pd.Series([2, 5]), "B"), diff --git a/tests/safeds/data/tabular/containers/_table/test_from_csv_file.py b/tests/safeds/data/tabular/containers/_table/test_from_csv_file.py new file mode 100644 index 000000000..0150e334e --- /dev/null +++ b/tests/safeds/data/tabular/containers/_table/test_from_csv_file.py @@ -0,0 +1,18 @@ +import pytest +from safeds.data.tabular.containers import Table +from tests.fixtures import resolve_resource_path + + +def test_from_csv_file_valid() -> None: + table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) + assert ( + table.get_column("A").get_value(0) == 1 + and table.get_column("B").get_value(0) == 2 + ) + + +def test_from_csv_file_invalid() -> None: + with pytest.raises(FileNotFoundError): + Table.from_csv_file( + resolve_resource_path("test_table_from_csv_file_invalid.csv") + ) diff --git a/tests/safeds/data/tabular/containers/_table/test_from_json_file.py b/tests/safeds/data/tabular/containers/_table/test_from_json_file.py new file mode 100644 index 000000000..adf3cdf41 --- /dev/null +++ b/tests/safeds/data/tabular/containers/_table/test_from_json_file.py @@ -0,0 +1,20 @@ +import pytest +from safeds.data.tabular.containers import Table +from tests.fixtures import resolve_resource_path + + +def test_from_json_file_valid() -> None: + table = Table.from_json_file( + resolve_resource_path("test_table_from_json_file.json") + ) + assert ( + table.get_column("A").get_value(0) == 1 + and table.get_column("B").get_value(0) == 2 + ) + + +def test_from_json_file_invalid() -> None: + with pytest.raises(FileNotFoundError): + Table.from_json_file( + resolve_resource_path("test_table_from_json_file_invalid.json") + ) diff --git a/tests/safeds/data/tabular/containers/_table/test_from_rows.py b/tests/safeds/data/tabular/containers/_table/test_from_rows.py index c38d87259..3e5ebac15 100644 --- a/tests/safeds/data/tabular/containers/_table/test_from_rows.py +++ b/tests/safeds/data/tabular/containers/_table/test_from_rows.py @@ -5,7 +5,7 @@ def test_from_rows() -> None: - table_expected = Table.from_csv(resolve_resource_path("test_row_table.csv")) + table_expected = Table.from_csv_file(resolve_resource_path("test_row_table.csv")) rows_is: list[Row] = table_expected.to_rows() table_is: Table = Table.from_rows(rows_is) diff --git a/tests/safeds/data/tabular/containers/_table/test_get_row.py b/tests/safeds/data/tabular/containers/_table/test_get_row.py index 3f3afe7b7..ab457753c 100644 --- a/tests/safeds/data/tabular/containers/_table/test_get_row.py +++ b/tests/safeds/data/tabular/containers/_table/test_get_row.py @@ -5,18 +5,18 @@ def test_get_row() -> None: - table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv")) + table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) val = table.get_row(0) assert val.get_value("A") == 1 and val.get_value("B") == 2 def test_get_row_negative_index() -> None: - table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv")) + table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) with pytest.raises(IndexOutOfBoundsError): table.get_row(-1) def test_get_row_out_of_bounds_index() -> None: - table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv")) + table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) with pytest.raises(IndexOutOfBoundsError): table.get_row(5) diff --git a/tests/safeds/data/tabular/containers/_table/test_has_column.py b/tests/safeds/data/tabular/containers/_table/test_has_column.py index 6495d13b4..99b224b15 100644 --- a/tests/safeds/data/tabular/containers/_table/test_has_column.py +++ b/tests/safeds/data/tabular/containers/_table/test_has_column.py @@ -3,10 +3,10 @@ def test_has_column_positive() -> None: - table = Table.from_csv(resolve_resource_path("test_table_has_column.csv")) + table = Table.from_csv_file(resolve_resource_path("test_table_has_column.csv")) assert table.has_column("A") def test_has_column_negative() -> None: - table = Table.from_csv(resolve_resource_path("test_table_has_column.csv")) + table = Table.from_csv_file(resolve_resource_path("test_table_has_column.csv")) assert not table.has_column("C") diff --git a/tests/safeds/data/tabular/containers/_table/test_keep_only_columns.py b/tests/safeds/data/tabular/containers/_table/test_keep_only_columns.py index 89211e5a8..edc3034d4 100644 --- a/tests/safeds/data/tabular/containers/_table/test_keep_only_columns.py +++ b/tests/safeds/data/tabular/containers/_table/test_keep_only_columns.py @@ -5,7 +5,7 @@ def test_keep_columns() -> None: - table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv")) + table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) transformed_table = table.keep_only_columns(["A"]) assert transformed_table.schema.has_column( "A" @@ -13,6 +13,6 @@ def test_keep_columns() -> None: def test_keep_columns_warning() -> None: - table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv")) + table = Table.from_csv_file(resolve_resource_path("test_table_from_csv_file.csv")) with pytest.raises(UnknownColumnNameError): table.keep_only_columns(["C"]) diff --git a/tests/safeds/data/tabular/containers/_table/test_read_csv.py b/tests/safeds/data/tabular/containers/_table/test_read_csv.py deleted file mode 100644 index 2aff09461..000000000 --- a/tests/safeds/data/tabular/containers/_table/test_read_csv.py +++ /dev/null @@ -1,16 +0,0 @@ -import pytest -from safeds.data.tabular.containers import Table -from tests.fixtures import resolve_resource_path - - -def test_read_csv_valid() -> None: - table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv")) - assert ( - table.get_column("A").get_value(0) == 1 - and table.get_column("B").get_value(0) == 2 - ) - - -def test_read_csv_invalid() -> None: - with pytest.raises(FileNotFoundError): - Table.from_csv(resolve_resource_path("test_table_read_csv_invalid.csv")) diff --git a/tests/safeds/data/tabular/containers/_table/test_read_json.py b/tests/safeds/data/tabular/containers/_table/test_read_json.py deleted file mode 100644 index 3fe4a129a..000000000 --- a/tests/safeds/data/tabular/containers/_table/test_read_json.py +++ /dev/null @@ -1,16 +0,0 @@ -import pytest -from safeds.data.tabular.containers import Table -from tests.fixtures import resolve_resource_path - - -def test_read_json_valid() -> None: - table = Table.from_json(resolve_resource_path("test_table_read_json.json")) - assert ( - table.get_column("A").get_value(0) == 1 - and table.get_column("B").get_value(0) == 2 - ) - - -def test_read_json_invalid() -> None: - with pytest.raises(FileNotFoundError): - Table.from_json(resolve_resource_path("test_table_read_json_invalid.json")) diff --git a/tests/safeds/data/tabular/containers/_table/test_rename.py b/tests/safeds/data/tabular/containers/_table/test_rename.py index 55d75e3c5..d3e8ac4ce 100644 --- a/tests/safeds/data/tabular/containers/_table/test_rename.py +++ b/tests/safeds/data/tabular/containers/_table/test_rename.py @@ -11,7 +11,9 @@ def test_rename_valid( name_from: str, name_to: str, column_one: str, column_two: str ) -> None: - table: Table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv")) + table: Table = Table.from_csv_file( + resolve_resource_path("test_table_from_csv_file.csv") + ) renamed_table = table.rename_column(name_from, name_to) assert renamed_table.schema.has_column(column_one) assert renamed_table.schema.has_column(column_two) @@ -27,6 +29,8 @@ def test_rename_valid( ], ) def test_rename_invalid(name_from: str, name_to: str, error: Exception) -> None: - table: Table = Table.from_csv(resolve_resource_path("test_table_read_csv.csv")) + table: Table = Table.from_csv_file( + resolve_resource_path("test_table_from_csv_file.csv") + ) with pytest.raises(error): table.rename_column(name_from, name_to) diff --git a/tests/safeds/data/tabular/containers/_table/test_replace_column.py b/tests/safeds/data/tabular/containers/_table/test_replace_column.py index 2f77b3bb0..3be3ba1d8 100644 --- a/tests/safeds/data/tabular/containers/_table/test_replace_column.py +++ b/tests/safeds/data/tabular/containers/_table/test_replace_column.py @@ -17,10 +17,10 @@ ], ) def test_replace_valid(column_name: str, path: str) -> None: - input_table: Table = Table.from_csv( + input_table: Table = Table.from_csv_file( resolve_resource_path("test_table_replace_column_input.csv") ) - expected: Table = Table.from_csv(resolve_resource_path(path)) + expected: Table = Table.from_csv_file(resolve_resource_path(path)) column = Column(pd.Series(["d", "e", "f"]), column_name) @@ -43,7 +43,7 @@ def test_replace_invalid( column_name: str, error: type[Exception], ) -> None: - input_table: Table = Table.from_csv( + input_table: Table = Table.from_csv_file( resolve_resource_path("test_table_replace_column_input.csv") ) column = Column(pd.Series(column_values), column_name) diff --git a/tests/safeds/data/tabular/containers/_table/test_table_add_column.py b/tests/safeds/data/tabular/containers/_table/test_table_add_column.py index 65b8ee84d..7b593d88b 100644 --- a/tests/safeds/data/tabular/containers/_table/test_table_add_column.py +++ b/tests/safeds/data/tabular/containers/_table/test_table_add_column.py @@ -6,10 +6,10 @@ def test_table_add_column_valid() -> None: - input_table = Table.from_csv( + input_table = Table.from_csv_file( resolve_resource_path("test_table_add_column_valid_input.csv") ) - expected = Table.from_csv( + expected = Table.from_csv_file( resolve_resource_path("test_table_add_column_valid_output.csv") ) column = Column(pd.Series(["a", "b", "c"]), "C") @@ -28,7 +28,7 @@ def test_table_add_column_valid() -> None: def test_table_add_column_( column_values: list[str], column_name: str, error: type[Exception] ) -> None: - input_table = Table.from_csv( + input_table = Table.from_csv_file( resolve_resource_path("test_table_add_column_valid_input.csv") ) column = Column(pd.Series(column_values), column_name) diff --git a/tests/safeds/data/tabular/containers/_table/test_to_columns.py b/tests/safeds/data/tabular/containers/_table/test_to_columns.py index 0fa61757a..4a25e3902 100644 --- a/tests/safeds/data/tabular/containers/_table/test_to_columns.py +++ b/tests/safeds/data/tabular/containers/_table/test_to_columns.py @@ -9,7 +9,7 @@ [([1, 4], "A", 0), ([2, 5], "B", 1)], ) def test_to_columns(values: list[int], name: str, index: int) -> None: - table = Table.from_csv(resolve_resource_path("test_column_table.csv")) + table = Table.from_csv_file(resolve_resource_path("test_column_table.csv")) columns_list: list[Column] = table.to_columns() column_expected: Column = Column(pd.Series(values, name=name), name) diff --git a/tests/safeds/data/tabular/containers/_table/test_to_csv_file.py b/tests/safeds/data/tabular/containers/_table/test_to_csv_file.py new file mode 100644 index 000000000..8e7185d6f --- /dev/null +++ b/tests/safeds/data/tabular/containers/_table/test_to_csv_file.py @@ -0,0 +1,15 @@ +from tempfile import NamedTemporaryFile + +import pandas as pd +from safeds.data.tabular.containers import Table + + +def test_to_csv_file() -> None: + table = Table(pd.DataFrame(data={"col1": ["col1_1"], "col2": ["col2_1"]})) + with NamedTemporaryFile() as tmp_table_file: + tmp_table_file.close() + with open(tmp_table_file.name, "w", encoding="utf-8") as tmp_file: + table.to_csv_file(tmp_file.name) + with open(tmp_table_file.name, "r", encoding="utf-8") as tmp_file: + table_r = Table.from_csv_file(tmp_file.name) + assert table == table_r diff --git a/tests/safeds/data/tabular/containers/_table/test_to_json_file.py b/tests/safeds/data/tabular/containers/_table/test_to_json_file.py new file mode 100644 index 000000000..cdc92db3f --- /dev/null +++ b/tests/safeds/data/tabular/containers/_table/test_to_json_file.py @@ -0,0 +1,15 @@ +from tempfile import NamedTemporaryFile + +import pandas as pd +from safeds.data.tabular.containers import Table + + +def test_to_json_file() -> None: + table = Table(pd.DataFrame(data={"col1": ["col1_1"], "col2": ["col2_1"]})) + with NamedTemporaryFile() as tmp_table_file: + tmp_table_file.close() + with open(tmp_table_file.name, "w", encoding="utf-8") as tmp_file: + table.to_json_file(tmp_file.name) + with open(tmp_table_file.name, "r", encoding="utf-8") as tmp_file: + table_r = Table.from_json_file(tmp_file.name) + assert table == table_r diff --git a/tests/safeds/data/tabular/containers/_table/test_to_rows.py b/tests/safeds/data/tabular/containers/_table/test_to_rows.py index bc5767bf8..e623dc969 100644 --- a/tests/safeds/data/tabular/containers/_table/test_to_rows.py +++ b/tests/safeds/data/tabular/containers/_table/test_to_rows.py @@ -5,7 +5,7 @@ def test_to_rows() -> None: - table = Table.from_csv(resolve_resource_path("test_row_table.csv")) + table = Table.from_csv_file(resolve_resource_path("test_row_table.csv")) expected_schema: TableSchema = TableSchema( { "A": IntColumnType(), diff --git a/tests/safeds/data/tabular/containers/_table/test_transform_column.py b/tests/safeds/data/tabular/containers/_table/test_transform_column.py index 6597545d0..227fc8acd 100644 --- a/tests/safeds/data/tabular/containers/_table/test_transform_column.py +++ b/tests/safeds/data/tabular/containers/_table/test_transform_column.py @@ -5,7 +5,7 @@ def test_transform_column_valid() -> None: - input_table: Table = Table.from_csv( + input_table: Table = Table.from_csv_file( resolve_resource_path("test_table_transform_column.csv") ) @@ -13,13 +13,13 @@ def test_transform_column_valid() -> None: "A", lambda row: row.get_value("A") * 2 ) - assert result == Table.from_csv( + assert result == Table.from_csv_file( resolve_resource_path("test_table_transform_column_output.csv") ) def test_transform_column_invalid() -> None: - input_table: Table = Table.from_csv( + input_table: Table = Table.from_csv_file( resolve_resource_path("test_table_transform_column.csv") ) diff --git a/tests/safeds/data/tabular/containers/_table/test_write_and_read.py b/tests/safeds/data/tabular/containers/_table/test_write_and_read.py deleted file mode 100644 index cfecce09a..000000000 --- a/tests/safeds/data/tabular/containers/_table/test_write_and_read.py +++ /dev/null @@ -1,26 +0,0 @@ -from tempfile import NamedTemporaryFile - -import pandas as pd -from safeds.data.tabular.containers import Table - - -def test_write_and_read_json_valid() -> None: - table = Table(pd.DataFrame(data={"col1": ["col1_1"], "col2": ["col2_1"]})) - with NamedTemporaryFile() as tmp_table_file: - tmp_table_file.close() - with open(tmp_table_file.name, "w", encoding="utf-8") as tmp_file: - table.to_json(tmp_file.name) - with open(tmp_table_file.name, "r", encoding="utf-8") as tmp_file: - table_r = Table.from_json(tmp_file.name) - assert table == table_r - - -def test_write_and_read_csv_valid() -> None: - table = Table(pd.DataFrame(data={"col1": ["col1_1"], "col2": ["col2_1"]})) - with NamedTemporaryFile() as tmp_table_file: - tmp_table_file.close() - with open(tmp_table_file.name, "w", encoding="utf-8") as tmp_file: - table.to_csv(tmp_file.name) - with open(tmp_table_file.name, "r", encoding="utf-8") as tmp_file: - table_r = Table.from_csv(tmp_file.name) - assert table == table_r diff --git a/tests/safeds/data/tabular/containers/_tagged_table/test_features.py b/tests/safeds/data/tabular/containers/_tagged_table/test_features.py index 597f5d833..6110227c0 100644 --- a/tests/safeds/data/tabular/containers/_tagged_table/test_features.py +++ b/tests/safeds/data/tabular/containers/_tagged_table/test_features.py @@ -3,7 +3,7 @@ def test_tagged_table_features() -> None: - table = Table.from_csv(resolve_resource_path("test_tagged_table.csv")) + table = Table.from_csv_file(resolve_resource_path("test_tagged_table.csv")) tagged_table = TaggedTable(table, "T") assert "T" not in tagged_table.features._data assert tagged_table.features.schema.has_column("A") diff --git a/tests/safeds/data/tabular/containers/_tagged_table/test_target.py b/tests/safeds/data/tabular/containers/_tagged_table/test_target.py index 3e7ec9665..9fc6bec26 100644 --- a/tests/safeds/data/tabular/containers/_tagged_table/test_target.py +++ b/tests/safeds/data/tabular/containers/_tagged_table/test_target.py @@ -3,7 +3,7 @@ def test_tagged_table_target() -> None: - table = Table.from_csv(resolve_resource_path("test_tagged_table.csv")) + table = Table.from_csv_file(resolve_resource_path("test_tagged_table.csv")) tagged_table = TaggedTable(table, "T") assert tagged_table.target._data[0] == 0 assert tagged_table.target._data[1] == 1 diff --git a/tests/safeds/data/tabular/typing/_table_schema/test_get_column_type.py b/tests/safeds/data/tabular/typing/_table_schema/test_get_column_type.py index 0e36ee7ef..110f0432d 100644 --- a/tests/safeds/data/tabular/typing/_table_schema/test_get_column_type.py +++ b/tests/safeds/data/tabular/typing/_table_schema/test_get_column_type.py @@ -5,6 +5,6 @@ def test_get_type_of_column() -> None: - table = Table.from_json(resolve_resource_path("test_schema_table.json")) + table = Table.from_json_file(resolve_resource_path("test_schema_table.json")) table_column_type = table.schema.get_type_of_column("A") assert table_column_type == ColumnType.from_numpy_dtype(np.dtype("int64")) diff --git a/tests/safeds/data/tabular/typing/_table_schema/test_has_column.py b/tests/safeds/data/tabular/typing/_table_schema/test_has_column.py index 9cd5b63bb..5a3329d94 100644 --- a/tests/safeds/data/tabular/typing/_table_schema/test_has_column.py +++ b/tests/safeds/data/tabular/typing/_table_schema/test_has_column.py @@ -3,12 +3,12 @@ def test_has_column_true() -> None: - table = Table.from_json(resolve_resource_path("test_schema_table.json")) + table = Table.from_json_file(resolve_resource_path("test_schema_table.json")) assert table.schema.has_column("A") def test_has_column_false() -> None: - table = Table.from_json(resolve_resource_path("test_schema_table.json")) + table = Table.from_json_file(resolve_resource_path("test_schema_table.json")) assert not table.schema.has_column("C") diff --git a/tests/safeds/data/tabular/typing/_table_schema/test_table_equals.py b/tests/safeds/data/tabular/typing/_table_schema/test_table_equals.py index 6f711e993..715e03567 100644 --- a/tests/safeds/data/tabular/typing/_table_schema/test_table_equals.py +++ b/tests/safeds/data/tabular/typing/_table_schema/test_table_equals.py @@ -4,7 +4,7 @@ def test_table_equals_valid() -> None: - table = Table.from_json(resolve_resource_path("test_schema_table.json")) + table = Table.from_json_file(resolve_resource_path("test_schema_table.json")) schema_expected = TableSchema( { "A": IntColumnType(), @@ -16,7 +16,7 @@ def test_table_equals_valid() -> None: def test_table_equals_invalid() -> None: - table = Table.from_json(resolve_resource_path("test_schema_table.json")) + table = Table.from_json_file(resolve_resource_path("test_schema_table.json")) schema_not_expected = TableSchema( { "A": FloatColumnType(), diff --git a/tests/safeds/ml/classification/test_ada_boost.py b/tests/safeds/ml/classification/test_ada_boost.py index 41b04c9f7..89b7ca936 100644 --- a/tests/safeds/ml/classification/test_ada_boost.py +++ b/tests/safeds/ml/classification/test_ada_boost.py @@ -12,13 +12,13 @@ def classifier() -> Classifier: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_ada_boost.csv")) + table = Table.from_csv_file(resolve_resource_path("test_ada_boost.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_ada_boost_invalid.csv")) + table = Table.from_csv_file(resolve_resource_path("test_ada_boost_invalid.csv")) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/classification/test_decision_tree.py b/tests/safeds/ml/classification/test_decision_tree.py index 68f0fad22..4080824d5 100644 --- a/tests/safeds/ml/classification/test_decision_tree.py +++ b/tests/safeds/ml/classification/test_decision_tree.py @@ -12,13 +12,13 @@ def classifier() -> Classifier: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_decision_tree.csv")) + table = Table.from_csv_file(resolve_resource_path("test_decision_tree.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_decision_tree_invalid.csv")) + table = Table.from_csv_file(resolve_resource_path("test_decision_tree_invalid.csv")) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/classification/test_gradient_boosting.py b/tests/safeds/ml/classification/test_gradient_boosting.py index d1bf2446d..0b0e035c1 100644 --- a/tests/safeds/ml/classification/test_gradient_boosting.py +++ b/tests/safeds/ml/classification/test_gradient_boosting.py @@ -12,7 +12,7 @@ def classifier() -> Classifier: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv( + table = Table.from_csv_file( resolve_resource_path("test_gradient_boosting_classification.csv") ) return TaggedTable(table, "T") @@ -20,7 +20,7 @@ def valid_data() -> TaggedTable: @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv( + table = Table.from_csv_file( resolve_resource_path("test_gradient_boosting_classification_invalid.csv") ) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/classification/test_k_nearest_neighbors.py b/tests/safeds/ml/classification/test_k_nearest_neighbors.py index 93359a9c6..4717842a8 100644 --- a/tests/safeds/ml/classification/test_k_nearest_neighbors.py +++ b/tests/safeds/ml/classification/test_k_nearest_neighbors.py @@ -12,13 +12,13 @@ def classifier() -> Classifier: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_k_nearest_neighbors.csv")) + table = Table.from_csv_file(resolve_resource_path("test_k_nearest_neighbors.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv( + table = Table.from_csv_file( resolve_resource_path("test_k_nearest_neighbors_invalid.csv") ) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/classification/test_logistic_regression.py b/tests/safeds/ml/classification/test_logistic_regression.py index fd62ad8ea..cae355ca3 100644 --- a/tests/safeds/ml/classification/test_logistic_regression.py +++ b/tests/safeds/ml/classification/test_logistic_regression.py @@ -12,13 +12,13 @@ def classifier() -> Classifier: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_logistic_regression.csv")) + table = Table.from_csv_file(resolve_resource_path("test_logistic_regression.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv( + table = Table.from_csv_file( resolve_resource_path("test_logistic_regression_invalid.csv") ) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/classification/test_random_forest.py b/tests/safeds/ml/classification/test_random_forest.py index 181e2a577..d966fc0c5 100644 --- a/tests/safeds/ml/classification/test_random_forest.py +++ b/tests/safeds/ml/classification/test_random_forest.py @@ -12,13 +12,13 @@ def classifier() -> Classifier: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_random_forest.csv")) + table = Table.from_csv_file(resolve_resource_path("test_random_forest.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_random_forest_invalid.csv")) + table = Table.from_csv_file(resolve_resource_path("test_random_forest_invalid.csv")) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/regression/test_ada_boost.py b/tests/safeds/ml/regression/test_ada_boost.py index d067ece5a..001419dcd 100644 --- a/tests/safeds/ml/regression/test_ada_boost.py +++ b/tests/safeds/ml/regression/test_ada_boost.py @@ -12,13 +12,13 @@ def regressor() -> Regressor: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_ada_boost.csv")) + table = Table.from_csv_file(resolve_resource_path("test_ada_boost.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_ada_boost_invalid.csv")) + table = Table.from_csv_file(resolve_resource_path("test_ada_boost_invalid.csv")) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/regression/test_decision_tree.py b/tests/safeds/ml/regression/test_decision_tree.py index 51dee8f84..a1362d45e 100644 --- a/tests/safeds/ml/regression/test_decision_tree.py +++ b/tests/safeds/ml/regression/test_decision_tree.py @@ -12,13 +12,13 @@ def regressor() -> Regressor: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_decision_tree.csv")) + table = Table.from_csv_file(resolve_resource_path("test_decision_tree.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_decision_tree_invalid.csv")) + table = Table.from_csv_file(resolve_resource_path("test_decision_tree_invalid.csv")) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/regression/test_elastic_net.py b/tests/safeds/ml/regression/test_elastic_net.py index 1f317fe76..4b64d85ac 100644 --- a/tests/safeds/ml/regression/test_elastic_net.py +++ b/tests/safeds/ml/regression/test_elastic_net.py @@ -12,13 +12,15 @@ def regressor() -> Regressor: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_elastic_net_regression.csv")) + table = Table.from_csv_file( + resolve_resource_path("test_elastic_net_regression.csv") + ) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv( + table = Table.from_csv_file( resolve_resource_path("test_elastic_net_regression_invalid.csv") ) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/regression/test_gradient_boosting.py b/tests/safeds/ml/regression/test_gradient_boosting.py index 40816d73a..55f151f2b 100644 --- a/tests/safeds/ml/regression/test_gradient_boosting.py +++ b/tests/safeds/ml/regression/test_gradient_boosting.py @@ -12,7 +12,7 @@ def regressor() -> Regressor: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv( + table = Table.from_csv_file( resolve_resource_path("test_gradient_boosting_regression.csv") ) return TaggedTable(table, "T") @@ -20,7 +20,7 @@ def valid_data() -> TaggedTable: @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv( + table = Table.from_csv_file( resolve_resource_path("test_gradient_boosting_regression_invalid.csv") ) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/regression/test_k_nearest_neighbors.py b/tests/safeds/ml/regression/test_k_nearest_neighbors.py index 0e14ed4d9..e86195710 100644 --- a/tests/safeds/ml/regression/test_k_nearest_neighbors.py +++ b/tests/safeds/ml/regression/test_k_nearest_neighbors.py @@ -12,13 +12,13 @@ def regressor() -> Regressor: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_k_nearest_neighbors.csv")) + table = Table.from_csv_file(resolve_resource_path("test_k_nearest_neighbors.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv( + table = Table.from_csv_file( resolve_resource_path("test_k_nearest_neighbors_invalid.csv") ) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/regression/test_lasso_regression.py b/tests/safeds/ml/regression/test_lasso_regression.py index 4de72b8f2..fea67b0c2 100644 --- a/tests/safeds/ml/regression/test_lasso_regression.py +++ b/tests/safeds/ml/regression/test_lasso_regression.py @@ -12,13 +12,15 @@ def regressor() -> Regressor: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_lasso_regression.csv")) + table = Table.from_csv_file(resolve_resource_path("test_lasso_regression.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_lasso_regression_invalid.csv")) + table = Table.from_csv_file( + resolve_resource_path("test_lasso_regression_invalid.csv") + ) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/regression/test_linear_regression.py b/tests/safeds/ml/regression/test_linear_regression.py index 2084a1ac4..afe24ee6e 100644 --- a/tests/safeds/ml/regression/test_linear_regression.py +++ b/tests/safeds/ml/regression/test_linear_regression.py @@ -12,13 +12,15 @@ def regressor() -> Regressor: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_linear_regression.csv")) + table = Table.from_csv_file(resolve_resource_path("test_linear_regression.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_linear_regression_invalid.csv")) + table = Table.from_csv_file( + resolve_resource_path("test_linear_regression_invalid.csv") + ) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/regression/test_random_forest.py b/tests/safeds/ml/regression/test_random_forest.py index 0d47b7681..d7b05c4ef 100644 --- a/tests/safeds/ml/regression/test_random_forest.py +++ b/tests/safeds/ml/regression/test_random_forest.py @@ -12,13 +12,13 @@ def regressor() -> Regressor: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_random_forest.csv")) + table = Table.from_csv_file(resolve_resource_path("test_random_forest.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_random_forest_invalid.csv")) + table = Table.from_csv_file(resolve_resource_path("test_random_forest_invalid.csv")) return TaggedTable(table, "T") diff --git a/tests/safeds/ml/regression/test_ridge_regression.py b/tests/safeds/ml/regression/test_ridge_regression.py index 0ec6f6978..d3ec9f3e6 100644 --- a/tests/safeds/ml/regression/test_ridge_regression.py +++ b/tests/safeds/ml/regression/test_ridge_regression.py @@ -12,13 +12,15 @@ def regressor() -> Regressor: @pytest.fixture() def valid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_ridge_regression.csv")) + table = Table.from_csv_file(resolve_resource_path("test_ridge_regression.csv")) return TaggedTable(table, "T") @pytest.fixture() def invalid_data() -> TaggedTable: - table = Table.from_csv(resolve_resource_path("test_ridge_regression_invalid.csv")) + table = Table.from_csv_file( + resolve_resource_path("test_ridge_regression_invalid.csv") + ) return TaggedTable(table, "T")