Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for tags #67

Merged
merged 11 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion autointent/modules/prediction/_adaptive.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ def dump(self, path: str) -> None:
dump_dir = Path(path)

metadata = AdaptivePredictorDumpMetadata(
r=self._r, tags=[t.model_dump() for t in self.tags] if self.tags else None, n_classes=self.n_classes
r=self._r,
tags=[t.model_dump() for t in self.tags] if self.tags else None, # type: ignore[misc]
n_classes=self.n_classes,
)

with (dump_dir / self.metadata_dict_name).open("w") as file:
Expand Down
2 changes: 1 addition & 1 deletion autointent/modules/prediction/_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def dump(self, path: str) -> None:

dump_dir = Path(path)
metadata_json = self.metadata
metadata_json["tags"] = [tag.model_dump() for tag in metadata_json["tags"]] if metadata_json["tags"] else None
metadata_json["tags"] = [tag.model_dump() for tag in metadata_json["tags"]] if metadata_json["tags"] else None # type: ignore[misc]

with (dump_dir / self.metadata_dict_name).open("w") as file:
json.dump(metadata_json, file, indent=4)
Expand Down
10 changes: 6 additions & 4 deletions autointent/modules/prediction/_tunable.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def dump(self, path: str) -> None:

dump_dir = Path(path)
metadata_json = self.metadata
metadata_json["tags"] = [tag.model_dump() for tag in metadata_json["tags"]] if metadata_json["tags"] else None
metadata_json["tags"] = [tag.model_dump() for tag in metadata_json["tags"]] if metadata_json["tags"] else None # type: ignore[misc]

with (dump_dir / self.metadata_dict_name).open("w") as file:
json.dump(metadata_json, file, indent=4)
Expand All @@ -138,12 +138,14 @@ def load(self, path: str) -> None:
dump_dir = Path(path)

with (dump_dir / self.metadata_dict_name).open() as file:
metadata: TunablePredictorDumpMetadata = json.load(file)
metadata = json.load(file)

self.metadata = metadata
metadata["tags"] = [Tag(**tag) for tag in metadata["tags"]] if metadata["tags"] else None

self.metadata: TunablePredictorDumpMetadata = metadata
self.thresh = np.array(metadata["thresh"])
self.multilabel = metadata["multilabel"]
self.tags = [Tag(**t) for t in metadata["tags"]] if metadata["tags"] else None
self.tags = metadata["tags"]
self.n_classes = metadata["n_classes"]


Expand Down
2 changes: 1 addition & 1 deletion tests/modules/prediction/test_argmax.py
voorhs marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def test_multiclass(multiclass_fit_data):
predictor = ArgmaxPredictor()
predictor.fit(*multiclass_fit_data)
scores = np.array([[0.1, 0.9, 0, 0.1], [0.8, 0, 0.2, 0.5], [0, 0.3, 0.7, 0.1]])
scores = np.array([[0.1, 0.9, 0, 0.1], [0.8, 0, 0.1, 0.1], [0, 0.2, 0.7, 0.1]])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

еще пропустил 0.1, 0.9, 0, 0.1

predictions = predictor.predict(scores)
np.testing.assert_array_equal(predictions, np.array([1, 0, 2]))

Expand Down
2 changes: 1 addition & 1 deletion tests/modules/prediction/test_jinoos.py
Samoed marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def detect_oos(scores: npt.NDArray[Any], labels: npt.NDArray[Any], thresh: float
def test_predict_returns_correct_indices(multiclass_fit_data):
predictor = JinoosPredictor()
predictor.fit(*multiclass_fit_data)
scores = np.array([[0.1, 0.9, 0, 0.1], [0.8, 0, 0.2, 0.5], [0, 0.3, 0.7, 0.1]])
scores = np.array([[0.1, 0.9, 0, 0.1], [0.8, 0, 0.1, 0.1], [0, 0.2, 0.7, 0.1]])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и тут


# inference
predictions = predictor.predict(scores)
Expand Down
10 changes: 5 additions & 5 deletions tests/modules/prediction/test_threshold.py
Samoed marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ def test_multiclass(multiclass_fit_data):
def test_multilabel(multilabel_fit_data):
predictor = ThresholdPredictor(thresh=0.5)
predictor.fit(*multilabel_fit_data)
scores = np.array([[0.1, 0.9, 0, 0.1], [0.8, 0, 0.2, 0.5], [0, 0.3, 0.7, 0.1]])
scores = np.array([[0.1, 0.9, 0, 0.1], [0.8, 0, 0.1, 0.1], [0, 0.2, 0.7, 0.1]])
Samoed marked this conversation as resolved.
Show resolved Hide resolved
predictions = predictor.predict(scores)
np.testing.assert_array_equal(predictions, np.array([[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 1, 0]]))
np.testing.assert_array_equal(predictions, np.array([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]]))


def test_multiclass_list(multiclass_fit_data):
predictor = ThresholdPredictor(np.array([0.5, 0.5, 0.8, 0.5]))
predictor.fit(*multiclass_fit_data)
scores = np.array([[0.1, 0.9, 0, 0.1], [0.8, 0, 0.2, 0.5], [0, 0.3, 0.7, 0.1]])
scores = np.array([[0.1, 0.9, 0, 0.1], [0.8, 0, 0.1, 0.1], [0, 0.2, 0.7, 0.1]])
Samoed marked this conversation as resolved.
Show resolved Hide resolved
predictions = predictor.predict(scores)
np.testing.assert_array_equal(predictions, np.array([1, 0, -1]))


def test_multilabel_list(multilabel_fit_data):
predictor = ThresholdPredictor(np.array([0.5, 0.5, 0.8, 0.5]))
predictor.fit(*multilabel_fit_data)
scores = np.array([[0.1, 0.9, 0, 0.1], [0.8, 0, 0.2, 0.5], [0, 0.3, 0.7, 0.1]])
scores = np.array([[0.1, 0.9, 0, 0.1], [0.8, 0, 0.1, 0.1], [0, 0.2, 0.7, 0.1]])
Samoed marked this conversation as resolved.
Show resolved Hide resolved
predictions = predictor.predict(scores)
np.testing.assert_array_equal(predictions, np.array([[0, 1, 0, 0], [1, 0, 0, 1], [0, 0, 0, 0]]))
np.testing.assert_array_equal(predictions, np.array([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0]]))


def test_fails_on_wrong_n_classes_predict(multiclass_fit_data):
Expand Down
2 changes: 1 addition & 1 deletion tests/modules/prediction/test_tunable.py
Samoed marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_multiclass(multiclass_fit_data):
def test_multilabel(multilabel_fit_data):
predictor = TunablePredictor()
predictor.fit(*multilabel_fit_data)
scores = np.array([[0.2, 0.9, 0, 0.5], [0.8, 0, 0.6, 0.5], [0, 0.4, 0.7, 0.5]])
scores = np.array([[0.1, 0.9, 0, 0.1], [0.8, 0, 0.1, 0.1], [0, 0.2, 0.7, 0.1]])
Samoed marked this conversation as resolved.
Show resolved Hide resolved
predictions = predictor.predict(scores)
desired = np.array([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]])

Expand Down
Loading