Skip to content

Commit

Permalink
linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sibre28 committed Aug 19, 2024
1 parent 3a4233a commit 897e17a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
12 changes: 0 additions & 12 deletions src/safeds/ml/metrics/_regression_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,6 @@ def mean_directional_accuracy(predicted: Column | TabularDataset | TimeSeriesDat
if expected.row_count == 0:
return 1.0

# For TimeSeries Predictions, where the output is a list of values.
# Expected results are internally converted to a column containing multiple Columns for each prediction window
# Currently only used in fit_by_exhaustive_search, where prediction metrics have to be calculated internally.
if isinstance(expected.get_value(0), Column):
sum_of_mean_directional_accuracy = 0.0
for i in range(0, expected.row_count):
predicted_row_as_col: Column = Column("predicted", predicted[i])
expected_row_as_col = expected.get_value(i)
sum_of_mean_directional_accuracy += RegressionMetrics.mean_directional_accuracy(predicted_row_as_col, expected_row_as_col)
return sum_of_mean_directional_accuracy / expected.row_count


# Calculate the differences between the target values
predicted_directions = predicted._series.diff().sign()
expected_directions = expected._series.diff().sign()
Expand Down
60 changes: 54 additions & 6 deletions tests/safeds/ml/nn/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
class TestClassificationModel:
class TestFit:
def test_should_return_input_size(self, device: Device) -> None:
configure_test_with_device(device)
model = NeuralNetworkClassifier(
InputConversionTable(),
[ForwardLayer(neuron_count=1)],
).fit(
Table.from_dict({"a": [1], "b": [2]}).to_tabular_dataset("a"),
)
device.type # noqa: B018
assert model.input_size == 1

def test_should_raise_if_epoch_size_out_of_bounds(self, device: Device) -> None:
Expand Down Expand Up @@ -402,7 +402,6 @@ def test_should_assert_that_is_fitted_is_set_correctly_and_check_return_type_for

fitted_model = model.fit_by_exhaustive_search(train_table, optimization_metric=metric, positive_class=positive_class, epoch_size=2)

#device.type # noqa: B018
assert fitted_model.is_fitted
assert isinstance(fitted_model, NeuralNetworkClassifier)

Expand Down Expand Up @@ -780,13 +779,13 @@ def test_should_be_pickleable(self, device: Device) -> None:
class TestRegressionModel:
class TestFit:
def test_should_return_input_size(self, device: Device) -> None:
configure_test_with_device(device)
model = NeuralNetworkRegressor(
InputConversionTable(),
[ForwardLayer(neuron_count=1)],
).fit(
Table.from_dict({"a": [1], "b": [2]}).to_tabular_dataset("a"),
)
device.type # noqa: B018
assert model.input_size == 1

def test_should_raise_if_epoch_size_out_of_bounds(self, device: Device) -> None:
Expand Down Expand Up @@ -971,14 +970,14 @@ def callback_was_called(self) -> bool:

class TestFitByExhaustiveSearch:
def test_should_return_input_size(self, device: Device) -> None:
configure_test_with_device(device)
model = NeuralNetworkRegressor(
InputConversionTable(),
[ForwardLayer(neuron_count=Choice(2, 4)), ForwardLayer(1)],
).fit_by_exhaustive_search(
Table.from_dict({"a": [1, 2, 3, 4], "b": [1.0, 2.0, 3.0, 4.0]}).to_tabular_dataset("b"),
"mean_squared_error",
)
device.type # noqa: B018
assert model.input_size == 1

def test_should_raise_if_epoch_size_out_of_bounds_when_fitting_by_exhaustive_search(
Expand Down Expand Up @@ -1098,13 +1097,62 @@ def test_should_assert_that_is_fitted_is_set_correctly_and_check_return_type_for
)
model = NeuralNetworkRegressor(
InputConversionTimeSeries(),
[ForwardLayer(neuron_count=Choice(128,256)), GRULayer(128), LSTMLayer(neuron_count=1)],
[ForwardLayer(neuron_count=Choice(128, 256)), GRULayer(128), LSTMLayer(neuron_count=1)],
)
assert not model.is_fitted

fitted_model = model.fit_by_exhaustive_search(train_table, optimization_metric=metric, epoch_size=2)

assert fitted_model.is_fitted
assert isinstance(fitted_model, NeuralNetworkRegressor)

@pytest.mark.parametrize(
"metric",
[
"mean_squared_error",
"mean_absolute_error",
"median_absolute_deviation",
"coefficient_of_determination",
],
ids=[
"mean_squared_error",
"mean_absolute_error",
"median_absolute_deviation",
"coefficient_of_determination",
],
)
def test_should_assert_that_is_fitted_is_set_correctly_and_check_return_type_for_rnns_continuous(
self,
metric: Literal[
"mean_squared_error",
"mean_absolute_error",
"median_absolute_deviation",
"coefficient_of_determination",
],
device: Device,
) -> None:
configure_test_with_device(device)

# Create a DataFrame
_inflation_path = "_datas/US_Inflation_rates.csv"
table = Table.from_csv_file(path=resolve_resource_path(_inflation_path))
rs = RangeScaler(column_names="value")
_, table = rs.fit_and_transform(table)
train_table = table.to_time_series_dataset(
"value",
window_size=7,
forecast_horizon=12,
continuous=True,
extra_names=["date"],
)
model = NeuralNetworkRegressor(
InputConversionTimeSeries(),
[ForwardLayer(neuron_count=Choice(128, 256)), LSTMLayer(neuron_count=12)],
)
assert not model.is_fitted

fitted_model = model.fit_by_exhaustive_search(train_table, optimization_metric=metric, epoch_size=2)

device.type # noqa: B018
assert fitted_model.is_fitted
assert isinstance(fitted_model, NeuralNetworkRegressor)

Expand Down

0 comments on commit 897e17a

Please sign in to comment.