Skip to content

Commit

Permalink
Add ID search functionality in data manager
Browse files Browse the repository at this point in the history
  • Loading branch information
WarmCyan committed Aug 21, 2023
1 parent 4acdbd3 commit ae8b400
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
41 changes: 31 additions & 10 deletions icat/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ def __init__(

self.search_box = v.TextField(
v_model="",
dense=True,
append_icon="mdi-magnify",
label="Search (use 'id:X' to search index)",
clearable=True,
clear_icon="mdi-close",
style_="padding-top: 7px",
# dense=True,
color="success",
# class_="success--text",
# append_icon="mdi-magnify",
label="Search (use 'ID:X' to search index)",
# clearable=True,
# clear_icon="mdi-close",
# style_="padding-top: 7px",
)
self.search_box.on_event("keyup", self._handle_ipv_search_changed)
self.search_box.on_event("click:clear", self._handle_ipv_search_cleared)
Expand All @@ -113,6 +115,7 @@ def __init__(

self.search_add_new_tooltip = v.Tooltip(
top=True,
open_delay=500,
v_slots=[
{
"name": "activator",
Expand All @@ -124,6 +127,7 @@ def __init__(
)
self.search_add_sel_tooltip = v.Tooltip(
top=True,
open_delay=500,
v_slots=[
{
"name": "activator",
Expand Down Expand Up @@ -423,7 +427,7 @@ def _apply_filters(self):
df = self._current_tab_filter(
df, self.current_data_tab, self.sample_indices, self.selected_indices
)
df = DataManager._search_box_filter(df, self.search_value, self.text_col)
df = self._search_box_filter(df, self.search_value, self.text_col)
df = self._prediction_range_filter(df)
self.filtered_df = df

Expand Down Expand Up @@ -518,20 +522,37 @@ def _set_page_contents(self, options):
)
self.table.items = rows

@staticmethod
def _search_box_filter(
self,
df: pd.DataFrame,
pattern: str,
column: str,
) -> pd.DataFrame:
"""This function searches for a given string in code:`pattern` and applies it to the code:`column` within the
code:`df`.
if the search is "id:" or "ID:", we directly search the index column instead
If the search is "ID:", we directly search the index column instead.
"""
# TODO: possibly move to a utils.py
if not pattern:
return df

# check for an index search
if pattern.startswith("ID:"):
requested_index = str(pattern[3:])
if requested_index.isnumeric():
self.search_box.label = "Search (index search mode)"
if int(requested_index) in df.index:
self.search_box.success = True
self.search_box.error = False
return df.iloc[[int(requested_index)]]
else:
self.search_box.success = False
self.search_box.error = True
return pd.DataFrame()

self.search_box.label = "Search (use 'ID:X' to search index)"
self.search_box.success = False
self.search_box.error = False
return df[df[column].str.contains(pattern, case=False)]

# TODO: either make this static and add prediction col,
Expand Down
2 changes: 1 addition & 1 deletion icat/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def _template(self):
<v-btn x-small class="purple darken-1" @click.stop="addToExampleAnchor(item.id)">
example
</v-btn>
<v-tooltip bottom>
<v-tooltip bottom open-delay=500>
<template v-slot:activator="{ on, attrs }">
<v-btn x-small v-if="!item.in_sample" @click.stop="addToSample(item.id)" v-bind="attrs" v-on="on">
sample
Expand Down
26 changes: 26 additions & 0 deletions notebooks/archive/Example3-Model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@
"model.view"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aee0b807-acd3-4771-b8bd-1d7e00613ba2",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import ipyvuetify as v\n",
"\n",
"pn.Row(v.TextField(label=\"hiello\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0d0a404-aced-4f12-8650-0099a9d0e629",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"v.TextField(label=\"hiello\")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_datamanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,12 @@ def catch_change(sample_indices):
dummy_data_manager.set_random_sample()
assert len(returns) == 1
assert returns[0] == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]


def test_id_search_shows_only_id_row(dummy_data_manager):
"""Using a search string like ID:5 should only return a row with that index."""
dummy_data_manager.search_value = "ID:5"
assert len(dummy_data_manager.filtered_df) == 1
assert (
dummy_data_manager.filtered_df.iloc[0].text == "Hey kid, you can't skate here!"
)

0 comments on commit ae8b400

Please sign in to comment.