Skip to content

Commit

Permalink
use lazy by default
Browse files Browse the repository at this point in the history
  • Loading branch information
brimoor committed Dec 4, 2024
1 parent acf9ffe commit 947c04a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
4 changes: 2 additions & 2 deletions fiftyone/brain/internal/core/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,12 @@ def get_embeddings(

return embeddings, sample_ids, label_ids

def reload(self, lazy=False):
def reload(self):
sample_ids, label_ids = self._parse_data(self._dataset, self.config)
self._sample_ids = sample_ids
self._label_ids = label_ids

super().reload(lazy=lazy)
super().reload()

def cleanup(self):
if self._index is None:
Expand Down
4 changes: 2 additions & 2 deletions fiftyone/brain/internal/core/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def get_embeddings(

return embeddings, sample_ids, label_ids

def reload(self, lazy=False):
def reload(self):
if self.config.embeddings_field is not None:
embeddings, sample_ids, label_ids = self._parse_data(
self._dataset, self.config
Expand All @@ -336,7 +336,7 @@ def reload(self, lazy=False):
self._curr_ids_to_inds = None
self._neighbors_helper = None

super().reload(lazy=lazy)
super().reload()

def cleanup(self):
pass
Expand Down
44 changes: 13 additions & 31 deletions fiftyone/brain/similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ def __init__(self, samples, config, brain_key, backend=None):

self._model = None
self._last_view = None
self._last_lazy = None
self._last_views = []
self._curr_view = None
self._curr_view_allow_missing = None
Expand All @@ -339,19 +338,19 @@ def __init__(self, samples, config, brain_key, backend=None):
self._curr_keep_inds = None
self._curr_missing_size = None

self.use_view(samples, lazy=True)
self.use_view(samples)

def __enter__(self):
self._last_views.append((self._last_view, self._last_lazy))
self._last_views.append(self._last_view)
return self

def __exit__(self, *args):
try:
last_view, last_lazy = self._last_views.pop()
last_view = self._last_views.pop()
except:
last_view, last_lazy = self._samples, True
last_view = self._samples

self.use_view(last_view, lazy=last_lazy)
self.use_view(last_view)

@property
def config(self):
Expand Down Expand Up @@ -548,7 +547,6 @@ def use_view(
samples,
allow_missing=True,
warn_missing=False,
lazy=False,
):
"""Restricts the index to the provided view.
Expand Down Expand Up @@ -589,26 +587,18 @@ def use_view(
warn_missing (False): whether to log a warning if the provided
collection contains data points that this index does not
contain
lazy (False): whether to lazily reinitialze the index for the
provided view only when necessary
Returns:
self
"""
self._last_view = self._curr_view
self._last_lazy = lazy

self._curr_view = samples
self._curr_view_allow_missing = allow_missing
self._curr_view_warn_missing = warn_missing

if lazy:
self._curr_sample_ids = None
self._curr_label_ids = None
self._curr_keep_inds = None
self._curr_missing_size = None
else:
self._apply_view()
self._curr_sample_ids = None
self._curr_label_ids = None
self._curr_keep_inds = None
self._curr_missing_size = None

return self

Expand Down Expand Up @@ -636,30 +626,22 @@ def _apply_view_if_necessary(self):
if self._curr_sample_ids is None:
self._apply_view()

def clear_view(self, lazy=False):
def clear_view(self):
"""Clears the view set by :meth:`use_view`, if any.
Subsequent operations will be performed on the full index.
Args:
lazy (False): whether to lazily reinitialze the index only when
necessary
"""
self.use_view(self._samples, lazy=lazy)
self.use_view(self._samples)

def reload(self, lazy=False):
def reload(self):
"""Reloads the index for the current view.
Subclasses may override this method, but by default this method simply
passes the current :meth:`view` back into :meth:`use_view`, which
updates the index's current ID set based on any changes to the view
since the index was last loaded.
Args:
lazy (False): whether to lazily reinitialze the index only when
necessary
"""
self.use_view(self._curr_view, lazy=lazy)
self.use_view(self._curr_view)

def cleanup(self):
"""Deletes the similarity index from the backend."""
Expand Down

0 comments on commit 947c04a

Please sign in to comment.