Skip to content

Allow exploration of parameters without cartesian product (extend BoxSearch) #176

Closed

Description

@caglorithm

Exploration via BoxSearch of a entirely self-populated list of parameters could easily be implemented by skipping the pypet.cartesian_product() in line 135 and extending the ParameterSpace class to allow for lists of parameters (without a box or point constraint.

Activity

  1. added
    enhancement
    on Aug 2, 2021
  2. self-assigned this
    on Aug 2, 2021
  3. jajcayn commented on Nov 22, 2021

    @jajcayn
    Collaborator

    Since I will be needing this feature soon-ish, I can work on this:)

    Suggestion:

    1. allow one more kind in ParameterSpace called sequence (or something else)
    2. implement method of ParameterSpace called get_parametrization() (or similar) with the following pseudo-code:
    def get_parametrization(self):
        if self.kind in ["point", "bound"]:
            # just return as dict
            return self.parameters
        elif self.kind == "grid":
            # cartesian product
            return pypet.cartesian_product(self.parameters)
        elif self.kind == "sequence":
            # return as sequence
            return self._inflate_to_sequence(self.parameters)
    
    @staticmethod
    def _inflate_to_sequence(dict):
        """
        example: {"a": [1, 2], "b": [3, 4, 5]} -> {"a": [1, 2, None, None, None], "b": [None, None, 3, 4, 5]}, i.e.
        inserts `None` when the param should not change
        """
        return {k: [None]*sum([len(tmp) for tmp in list(dict.values())[:i]]) + v + [None]*sum([len(tmp) for tmp in list(dict.values())[i+1:]])  for i, (k, v) in enumerate(dict.items())}
    1. change behaviour in exploration.py, i.e. pypet.cartesian_product(self.exploreParameters) -> self.parameterSpace.get_parametrization()

    2. sanitize dictionary in getModelFromTraj function of BoxSearch, something like

    # contains None
    runParams = self.getParametersFromTraj(traj)
    # removes keys with None values
    runParams = {k: v for k, v in runParams.items() if v is not None}
    # values cannot be None
    model.params.update(runParams)
    1. profit

    @caglorithm what do you say? reasonable? Or do you have another approach in mind? The advantage of get_parametrization is that we might add more kinds later, e.g. drawing from random distributions etc...

  4. caglorithm commented on Nov 23, 2021

    @caglorithm
    MemberAuthor

    I like this idea! I had multiple instances where I needed an arbitrary trajectory (that's what pypet calls it, btw) instead of a box. Is this what you mean? If so, I haven't understood how _inflate_to_sequence will work. Could you make an example of a trajectory and how pypet would have to be called?

  5. jajcayn commented on Nov 23, 2021

    @jajcayn
    Collaborator

    Exactly, that is the idea to have just a particular sequence of parameters you want to run, but not over a grid...

    the idea is to define parameters over which the exploration would be done, but NOT on a grid, e.g.

    {"a": [1, 2], "b": [12, 13], "c": [0, -1, -2]}

    would run just 7 simulations, first 2 with a changing in {1, 2} (other parameters left at default), next 2 simulation with b changing in {12, 13} (other parameters left as default), and final 3 simulations would change c in {0, -1, -2}, again, with other parameters left untouched...

    So the definition of the parameter space would be the same as in the case of the grid, just it wouldn't run all the combinations...

    _inflate_to_sequence would just make all lists of parameters same length (i.e. the sum of list lengths of all parameters ) and fill them with None at the appropriate place... in the case of the example above, it would make:

    {"a": [1, 2, None, None, None, None, None], "b": [None, None, 12, 13, None, None, None], "c": [None, None, None, None, 0, -1, -2]}

    Where None would mean that that particular parameter in a specific run is not subject to change...

    No changes w.r.t pypet are needed, the only difference is described in my post in 4, hence when we get the parameters of a particular run with self.getParametersFromTraj(traj), we need to remove None, so the parameter stays as default.

    Note that if you need to, e.g. run two simulations, first with a=1.0, b=12.0 and second with a=2.0, b=13.0, more changes are required... The proposed sequence only supports one changing parameter per simulation... to allow pairs or even more parameters to change, I think the easiest solution is to define a complete dictionary yourself and just pass it to pypet as such, without calling any cartesian product, e.g. {"a": [1.0, 2.0], "b": [12.0, 13.0]} would run just two simulations. in this case, the lengths of parameters lists would have to be the same

  6. linked a pull request that will close this issueMore explore options #191on Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions