-
Notifications
You must be signed in to change notification settings - Fork 190
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
extractor_dict_iterator for solving path detection in object kwargs
#3089
extractor_dict_iterator for solving path detection in object kwargs
#3089
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@h-mayorquin I love this! just some renaming suggestions
@h-mayorquin this should cloe #3014 right? |
…ator' into recording_dict_iterator
@h-mayorquin can you make the previous test function in |
Yes, done. |
This looks good to me. Much much cleaner than the old presentation! @samuelgarcia what do you think? |
Excellent guess and analysis of the histical reason! Thanks a lot for this effort. |
elif isinstance(dict_list_or_value, list): | ||
for i, v in enumerate(dict_list_or_value): | ||
yield from _extractor_dict_iterator( | ||
v, access_path + (i,), name=name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really smart to have the path both with key and index for list!!
@@ -183,6 +184,75 @@ def is_dict_extractor(d: dict) -> bool: | |||
return is_extractor | |||
|
|||
|
|||
extractor_dict_element = namedtuple(typename="extractor_dict_element", field_names=["value", "name", "access_path"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very good idea!
Extractor dicts (outputs of
BaseExtractor.to_dict()
) are nested structures where some arguments are paths.For solving the problem in #3013 we need a way of filtering by the arguments that correspond to real paths (e.g. Path(argument).exists() is True) and only modify those.
The current approach is to use the
recursive_path_modifier
:https://github.com/alejoe91/spikeinterface/blob/fa2ca6d7c804fc8ab3df93dbd463d9576bc7723b/src/spikeinterface/core/core_tools.py#L186
I guess at the beginning the purpose of the function was both iterating overt the dict and also modifying the values.
But then, some other functions appeared like
get_path_list
that userecursive_path_modifier
to iterate over the extractor and accumulate path-like values.This PR creates two functions that can accomplish the same as
recursive_path_modifier
but are decoupled:extractor_dict_iterator
that generates an iterator over all elements with three attributes: value, name and access path.set_value_in_extractor_dict
function that uses the access path of the elements from the iterator to modify the value is also introduced.With this logic, functions like
find_recording_folders
that only use the first part can avoid the part ofrecursive_path_modifier
that modifies the values. Avoiding computational waste and simplifying intent. Moreover,, the problem in #3014 of having to check for path existence before or after a modification can be easily stated as a single line where elements are filtered instead of modifying the logic ofrecursive_path_modifier
. In general it enables working with extractor dicts in the folowing way:Note that this does not even need to be about paths. Any type of filter, reduce or concistency check and modification can be implemented with this.
I also modified the tests of
make_relative_paths
andmake_absolute_paths
to work on real paths so we can directly test the type of problem causes #3013