Skip to content

Commit

Permalink
perf: remove unneeded copy operations
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Nov 16, 2023
1 parent 9a19389 commit 8f2c1b3
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 52 deletions.
24 changes: 4 additions & 20 deletions src/safeds/data/tabular/containers/_tagged_table.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import copy
from typing import TYPE_CHECKING

from safeds.data.tabular.containers import Column, Row, Table
Expand Down Expand Up @@ -193,21 +192,6 @@ def target(self) -> Column:
"""
return self._target

# ------------------------------------------------------------------------------------------------------------------
# Helpers
# ------------------------------------------------------------------------------------------------------------------

def _copy(self) -> TaggedTable:
"""
Return a copy of this tagged table.
Returns
-------
table : TaggedTable
The copy of this tagged table.
"""
return copy.deepcopy(self)

# ------------------------------------------------------------------------------------------------------------------
# Specific methods from TaggedTable class:
# ------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -268,7 +252,7 @@ def add_columns_as_features(self, columns: list[Column] | Table) -> TaggedTable:
super().add_columns(columns),
target_name=self.target.name,
feature_names=self.features.column_names
+ [col.name for col in (columns.to_columns() if isinstance(columns, Table) else columns)],
+ [col.name for col in (columns.to_columns() if isinstance(columns, Table) else columns)],
)

# ------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -702,8 +686,8 @@ def replace_column(self, old_column_name: str, new_columns: list[Column]) -> Tag
self.features.column_names
if old_column_name not in self.features.column_names
else self.features.column_names[: self.features.column_names.index(old_column_name)]
+ [col.name for col in new_columns]
+ self.features.column_names[self.features.column_names.index(old_column_name) + 1 :]
+ [col.name for col in new_columns]
+ self.features.column_names[self.features.column_names.index(old_column_name) + 1:]
),
)

Expand Down Expand Up @@ -763,7 +747,7 @@ def slice_rows(
def sort_columns(
self,
comparator: Callable[[Column, Column], int] = lambda col1, col2: (col1.name > col2.name)
- (col1.name < col2.name),
- (col1.name < col2.name),
) -> TaggedTable:
"""
Sort the columns of a `TaggedTable` with the given comparator and return a new `TaggedTable`.
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/data/tabular/transformation/_discretizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def transform(self, table: Table) -> Table:
if not table.get_column(column).type.is_numeric():
raise NonNumericColumnError(f"{column} is of type {table.get_column(column).type}.")

data = table._data.copy()
data = table._data.reset_index(drop=True)
data.columns = table.column_names
data[self._column_names] = self._wrapped_transformer.transform(data[self._column_names])
return Table._from_pandas_dataframe(data)
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/data/tabular/transformation/_imputer.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def transform(self, table: Table) -> Table:
if table.number_of_rows == 0:
raise ValueError("The Imputer cannot transform the table because it contains 0 rows")

data = table._data.copy()
data = table._data.reset_index(drop=True)
data[self._column_names] = pd.DataFrame(
self._wrapped_transformer.transform(data[self._column_names]),
columns=self._column_names,
Expand Down
4 changes: 2 additions & 2 deletions src/safeds/data/tabular/transformation/_label_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def transform(self, table: Table) -> Table:
if table.number_of_rows == 0:
raise ValueError("The LabelEncoder cannot transform the table because it contains 0 rows")

data = table._data.copy()
data = table._data.reset_index(drop=True)
data.columns = table.column_names
data[self._column_names] = self._wrapped_transformer.transform(data[self._column_names])
return Table._from_pandas_dataframe(data)
Expand Down Expand Up @@ -171,7 +171,7 @@ def inverse_transform(self, transformed_table: Table) -> Table:
),
)

data = transformed_table._data.copy()
data = transformed_table._data.reset_index(drop=True)
data.columns = transformed_table.column_names
data[self._column_names] = self._wrapped_transformer.inverse_transform(data[self._column_names])
return Table._from_pandas_dataframe(data)
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/data/tabular/transformation/_one_hot_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def fit(self, table: Table, column_names: list[str] | None) -> OneHotEncoder:
stacklevel=2,
)

data = table._data.copy()
data = table._data.reset_index(drop=True)
data.columns = table.column_names

result = OneHotEncoder()
Expand Down
4 changes: 2 additions & 2 deletions src/safeds/data/tabular/transformation/_range_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def transform(self, table: Table) -> Table:
),
)

data = table._data.copy()
data = table._data.reset_index(drop=True)
data.columns = table.column_names
data[self._column_names] = self._wrapped_transformer.transform(data[self._column_names])
return Table._from_pandas_dataframe(data)
Expand Down Expand Up @@ -213,7 +213,7 @@ def inverse_transform(self, transformed_table: Table) -> Table:
),
)

data = transformed_table._data.copy()
data = transformed_table._data.reset_index(drop=True)
data.columns = transformed_table.column_names
data[self._column_names] = self._wrapped_transformer.inverse_transform(data[self._column_names])
return Table._from_pandas_dataframe(data)
Expand Down
4 changes: 2 additions & 2 deletions src/safeds/data/tabular/transformation/_standard_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def transform(self, table: Table) -> Table:
),
)

data = table._data.copy()
data = table._data.reset_index(drop=True)
data.columns = table.column_names
data[self._column_names] = self._wrapped_transformer.transform(data[self._column_names])
return Table._from_pandas_dataframe(data)
Expand Down Expand Up @@ -195,7 +195,7 @@ def inverse_transform(self, transformed_table: Table) -> Table:
),
)

data = transformed_table._data.copy()
data = transformed_table._data.reset_index(drop=True)
data.columns = transformed_table.column_names
data[self._column_names] = self._wrapped_transformer.inverse_transform(data[self._column_names])
return Table._from_pandas_dataframe(data)
Expand Down
2 changes: 1 addition & 1 deletion src/safeds/ml/classical/_util_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def predict(model: Any, dataset: Table, feature_names: list[str] | None, target_
dataset_df = dataset.keep_only_columns(feature_names)._data
dataset_df.columns = feature_names

result_set = dataset._data.copy(deep=True)
result_set = dataset._data.reset_index(drop=True)
result_set.columns = dataset.column_names

try:
Expand Down

This file was deleted.

0 comments on commit 8f2c1b3

Please sign in to comment.