Ejemplo n.º 1
0
 def __setstate__(self, state):
     state = dict(state)
     if "interactions" in state and isinstance(state["interactions"], str):
         state["interactions"] = self.loads_interactions(
             state["interactions"]).interactions
     if "identifier" in state and isinstance(state["identifier"], str):
         state["identifier"] = Identifier.loads(state["identifier"])
     if "_identifier" in state:
         try:
             state["_identifier"] = Identifier.loads(state["_identifier"])
         except:  # noqa
             pass
     if "lineage" in state:
         try:
             state["lineage"] = [
                 tuple(t[:1]) + (Identifier.loads(t[1]), ) +
                 (tuple(t[2:]) if len(t) > 2 else ())
                 for t in state["lineage"]
             ]
         except:  # noqa
             pass
     if "logdir" in state:
         try:
             state["logdir"] = (
                 pathlib.Path.home() /
                 f"robustnessgym/datasets/{str(state['identifier'])}")
         except:  # noqa
             state["logdir"] = (
                 pathlib.Path.home() /
                 f"robustnessgym/datasets/{str(state['_identifier'])}")
     super(Dataset, self).__setstate__(state)
Ejemplo n.º 2
0
    def loads(cls, s: str):
        tape = InteractionTape()
        history = json.loads(s)
        history = {
            tuple(json.loads(json_tuple)): idx
            for json_tuple, idx in history.items()
        }
        tape.history = {(Identifier.loads(identifier), json_columns): idx
                        for (identifier, json_columns), idx in history.items()}

        return tape
Ejemplo n.º 3
0
    def __setstate__(self, state):
        """Set the current state of the slice."""
        # Check that the state contains all keys
        self._assert_state_keys(state)

        # Load the lineage
        self.lineage = [
            tuple(t[:1]) + (Identifier.loads(t[1]), ) +
            (tuple(t[2:]) if len(t) > 2 else ()) for t in state["lineage"]
        ]

        # Load the category
        self.category = state["category"]

        # Set the dataset state: pick out state keys that correspond to the Dataset
        super(Slice, self).__setstate__({
            k: v
            for k, v in state.items() if k in super(Slice, self)._state_keys()
        })
Ejemplo n.º 4
0
    def __setstate__(self, state):
        """Set the current state of the dataset."""
        # Check that the state contains all keys
        Dataset._assert_state_keys(state)

        # Load the interactions
        self.interactions = self.loads_interactions(
            state["interactions"]).interactions

        # Load the identifier
        self._identifier = (Identifier.loads(state["_identifier"])
                            if state["_identifier"] else None)

        # Load the dataset
        self._dataset = state["_dataset"]

        # Set the dataset format
        self._dataset_fmt = state["_dataset_fmt"]

        # Update the logging directory
        self.logdir = Dataset.logdir / str(self.identifier)
 def test_loads(self):
     # Dump the identifier to a json string and load it back
     s = self.identifier.dumps()
     identifier = Identifier.loads(s)
     self.assertEqual(identifier, self.identifier)