Skip to content

Commit

Permalink
Add tests for dropdown change event fire and add_anchor function to m…
Browse files Browse the repository at this point in the history
…odel
  • Loading branch information
WarmCyan committed Aug 10, 2023
1 parent 19f2a1f commit 6ce4bf0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
* Useful imports to top level module.
* Activity indicators to anchors in anchor list table
* Activity indicators to anchors in anchor list table.
* `add_anchor` function directly to model.

### Fixed
* Text fields not applying changes when user clicks away instead of hitting enter

* Text fields not applying changes when user clicks away instead of hitting enter.
* Similarity function anchor dropdown change not triggering featurization.



Expand Down
24 changes: 24 additions & 0 deletions icat/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@


class Model:
"""The interactive machine learning model - a basic binary classifier with tools
for viewing and interacting with the data and features.
Args:
data (pd.DataFrame): The data to explore with.
text_col (str): The name of the text column in the passed data.
similarity_functions (list[Callable]): A set of additional functions that can
be used for similarity-based features. Any function passed should take a
dataframe (representing all of the currently active data,) and an anchor
instance. It should return a pandas series with the output similarity values,
using the same index as that in the passed dataframe.
"""

def __init__(
self,
data: pd.DataFrame,
Expand Down Expand Up @@ -322,3 +335,14 @@ def predict(
)[:, 1]

return predictions

def add_anchor(self, anchor: Anchor):
"""Add the passed anchor to this model's anchor list.
Args:
anchor (Anchor): The Anchor to add to the list.
Note:
See ``AnchorList.add_anchor`` for more details.
"""
self.anchor_list.add_anchor(anchor)
27 changes: 26 additions & 1 deletion tests/test_anchors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
import pandas as pd
import pytest

from icat.anchors import Anchor, DictionaryAnchor, TFIDFAnchor
from icat.anchors import Anchor, DictionaryAnchor, SimilarityFunctionAnchor, TFIDFAnchor
from icat.model import Model


Expand Down Expand Up @@ -316,3 +317,27 @@ def test_similarity_anchor_gets_short_from_text_entry(fun_df):
assert anchor.reference_texts == ["kid"]
assert anchor.reference_short == ["kid"]
assert len(anchor._chips_container.children) == 1


@pytest.mark.integration
def test_changing_similarity_function_fires_change_event(fun_df):
"""Changing the selected similarity function should fire an event."""
returns = []

def catch_change(name, key, value):
nonlocal returns
returns.append((name, key, value))

def simple_sim(data, anchor):
return pd.Series(np.ones(len(data)), index=data.index)

model = Model(fun_df, "text", similarity_functions=[simple_sim])
anchor = SimilarityFunctionAnchor()
anchor.on_anchor_changed(catch_change)
model.add_anchor(anchor)

assert anchor.sim_function_options.items == ["simple_sim"]
anchor.sim_function_options.v_model = "simple_sim"
anchor.sim_function_options.fire_event("change", "simple_sim")

assert returns[0][2] == "simple_sim"

0 comments on commit 6ce4bf0

Please sign in to comment.