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

Object oriented approach to dj_decoder_conversion.py #1049

Open
CBroz1 opened this issue Jul 30, 2024 · 0 comments
Open

Object oriented approach to dj_decoder_conversion.py #1049

CBroz1 opened this issue Jul 30, 2024 · 0 comments
Assignees
Labels
decoding enhancement New feature or request

Comments

@CBroz1
Copy link
Member

CBroz1 commented Jul 30, 2024

At some point, I put together the attached object-oriented revision to the decoder class/dict conversion funcs.

I thought I inadvertently overwrote it, but I found a copy. Attaching here for future inclusion after pytests are in place

Script
import copy

from non_local_detector import continuous_state_transitions as cst
from non_local_detector import discrete_state_transitions as dst
from non_local_detector import initial_conditions as ic
from non_local_detector.environment import Environment
from non_local_detector.observation_models import ObservationModel
from track_linearization import make_track_graph


class ParamsConverter:
    def _convert_dict_to_class(self, d: dict, class_conversion: dict) -> object:
        class_name = d.pop("class_name")
        if class_name not in class_conversion:
            raise ValueError(f"Invalid class name: {class_name}")
        return class_conversion[class_name](**d)

    def _convert_env_dict(self, env_params: dict) -> Environment:
        track_graph = env_params.get("track_graph")
        if track_graph is not None:
            env_params["track_graph"] = make_track_graph(**track_graph)
        return Environment(**env_params)

    def _to_dict(self, transition: object) -> dict:
        return {"class_name": type(transition).__name__, **vars(transition)}

    def _convert_transitions(self, transitions):
        if isinstance(transitions, list):
            return [self._convert_transitions(t) for t in transitions]
        elif isinstance(transitions, dict):
            return {
                key: self._convert_transitions(value)
                for key, value in transitions.items()
            }
        elif isinstance(transitions, object):
            return self._to_dict(transitions)
        else:
            return transitions

    def convert_params(self, params):
        if isinstance(params, list):
            return [self.convert_params(p) for p in params]
        elif isinstance(params, dict):
            return {
                key: self.convert_params(value) for key, value in params.items()
            }
        elif isinstance(params, object):
            return self._convert_dict_to_class(params, self.class_conversion)
        else:
            return params

    def as_classes(self):
        params = copy.deepcopy(self.params)
        params["continuous_transition_types"] = self._convert_transitions(
            params["continuous_transition_types"]
        )
        params["environments"] = self.convert_params(params["environments"])
        params["discrete_transition_type"] = self._convert_dict_to_class(
            params["discrete_transition_type"],
            self.discrete_transition_types,
        )
        params["continuous_initial_conditions_types"] = self.convert_params(
            params["continuous_initial_conditions_types"]
        )
        if params["observation_models"] is not None:
            params["observation_models"] = [
                ObservationModel(**obs) for obs in params["observation_models"]
            ]
        return params

It's also the case that SortedSpikesDecodingV1 and ClusterlessDecodingV1 have essentially the same methods, but referring to one another. A single class could be initialized with 'this' and 'other' table to collapse across these

@CBroz1 CBroz1 added enhancement New feature or request decoding labels Jul 30, 2024
@CBroz1 CBroz1 self-assigned this Jul 30, 2024
@CBroz1 CBroz1 mentioned this issue Jul 31, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
decoding enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant