def test_get_object_provenance(self): """correctly deduce object provenance""" result = get_object_provenance("abncd") self.assertEqual(result, "str") from cogent3 import DNA got = get_object_provenance(DNA) self.assertEqual(got, "cogent3.core.moltype.MolType") from cogent3.evolve.models import HKY85 sm = HKY85() got = get_object_provenance(sm) self.assertEqual( got, "cogent3.evolve.substitution_model." "TimeReversibleNucleotide")
def load_from_json(filename, classes): """Loads objects from json files. Parameters ---------- filename: name of the json file classes: A series of the Cogent3 types, for example: (Alignment, ArrayAlignment) """ assert all( (isinstance(klass, type) for klass in classes) ), "classes should be a series of Cogent3 types, for example: (Alignment, ArrayAlignment)" with open_(filename) as f: content = json.loads(f.read()) try: _, data, completed = load_record_from_json(content) if not completed: raise TypeError("json file is a record for type NotCompleted.") except (KeyError, TypeError): data = content type_ = data.get("type", None) if type_ is None: raise TypeError("json does not contain 'type' key") valid_types = {get_object_provenance(klass) for klass in classes} if type_ not in valid_types: raise TypeError( f"Invalid data type: {type_} is not one of {valid_types}") return deserialise_object(data)
def to_rich_dict(self): data = self.array.tolist() return { "type": get_object_provenance(self.template), "array": data, "names": self.template.names, "version": __version__, }
def to_rich_dict(self, for_pickle=False): data = self._serialisable.copy() if not for_pickle: for key, value in data.items(): type_ = get_object_provenance(value) if type_.startswith("cogent3"): try: value = value.to_rich_dict(for_pickle=False) except AttributeError: pass finally: data[key] = value if "predicates" in data and data["predicates"]: data["predicates"] = [str(p) for p in data["predicates"]] data["type"] = get_object_provenance(self) data["version"] = __version__ return data
def to_rich_dict(self): """returns detailed info on object, used by to_json""" data = self._serialisable.copy() for key in ("model", "tree"): del data[key] tree = self.tree.to_rich_dict() edge_attr = tree["edge_attributes"] for edge in edge_attr: if edge == "root": continue try: edge_attr[edge]["length"] = self.get_param_value("length", edge=edge) except KeyError: # probably discrete-time model edge_attr[edge]["length"] = None model = self._model.to_rich_dict(for_pickle=False) aln_defn = self.defn_for["alignment"] if len(aln_defn.index) == 1: alignment = self.get_param_value("alignment").to_rich_dict() mprobs = self.get_motif_probs().to_dict() else: alignment = { a["locus"]: a["value"] for a in aln_defn.get_param_rules() } mprobs = self.get_motif_probs() for k in alignment: alignment[k] = alignment[k].to_rich_dict() mprobs[k] = mprobs[k].to_dict() DLC = self.all_psubs_DLC() try: unique_Q = self.all_rate_matrices_unique() except Exception: # there's a mix of assertions # for "storage", make this indeterminate in those cases unique_Q = None data = dict( model=model, tree=tree, alignment=alignment, likelihood_construction=data, param_rules=self.get_param_rules(), lnL=self.get_log_likelihood(), nfp=self.get_num_free_params(), motif_probs=mprobs, DLC=DLC, unique_Q=unique_Q, type=get_object_provenance(self), name=self.get_name(), version=__version__, ) return data
def to_rich_dict(self): """returns dicts for contained spans [dict(), ..]""" spans = [s.to_rich_dict() for s in self.spans] data = self._serialisable.copy() data.pop("locations") data["spans"] = spans data["type"] = get_object_provenance(self) data["version"] = __version__ return data
def to_rich_dict(self): """returns components for to_json""" return { "type": get_object_provenance(self), "not_completed_construction": dict( args=self._persistent[0], kwargs=self._persistent[1] ), "version": __version__, }
def to_rich_dict(self, for_pickle=False): data = {"motifset": tuple(self), "gap": self.gap, "moltype": self.moltype} if not for_pickle: data["type"] = get_object_provenance(self) data["moltype"] = self.moltype.label data["version"] = __version__ if hasattr(self, "_gc"): data["genetic_code"] = self._gc.name return data
def to_rich_dict(self, for_pickle=False): data = { "data": ["".join(e) for e in self.sub_enumerations], "gap": self.gap, "moltype": self.moltype, } if not for_pickle: data["type"] = get_object_provenance(self) data["moltype"] = self.moltype.label data["version"] = __version__ return data
def to_rich_dict(self): # because dicts with tuples as keys cannot be json'ed, we convert to # a list of tuples dists = self.to_dict() json_safe = [(k[0], k[1], dists[k]) for k in dists] return dict( dists=json_safe, invalid=self._invalid, type=get_object_provenance(self), version=__version__, )
def test_to_rich_dict(self): """Sequence to_dict works""" r = self.SEQ("AAGGCC", name="seq1") got = r.to_rich_dict() expect = { "name": "seq1", "seq": "AAGGCC", "moltype": r.moltype.label, "info": None, "type": get_object_provenance(r), "version": __version__, } self.assertEqual(got, expect)
def test_to_json(self): """to_json roundtrip recreates to_dict""" r = self.SEQ("AAGGCC", name="seq1") got = json.loads(r.to_json()) expect = { "name": "seq1", "seq": "AAGGCC", "moltype": r.moltype.label, "info": None, "type": get_object_provenance(r), "version": __version__, } self.assertEqual(got, expect)
def to_rich_dict(self): """returns {'name': name, 'seq': sequence, 'moltype': moltype.label}""" data = self._serialisable.copy() # the first constructor argument will be the instance recreating # so we pop out the two possible keys data.pop("parent", None) data.pop("seq", None) if "original" in data: data.pop("original") # convert the map to coordinates data["map"] = data.pop("map").to_rich_dict() data = dict(annotation_construction=data) data["type"] = get_object_provenance(self) data["version"] = __version__ return data
def to_rich_dict(self): """returns the rich dict on values""" result = { "type": get_object_provenance(self), "result_construction": self._construction_kwargs, "version": __version__, } items = [] for key, val in self.items(): try: val = val.to_rich_dict() except AttributeError: pass items.append([key, val]) result["items"] = items return result
def to_rich_dict(self, for_pickle=False): data = self._serialisable.copy() if not for_pickle: # we rely on reconstruction from label data = dict(type=get_object_provenance(self), moltype=self.label) data["version"] = __version__ return data
def to_rich_dict(self): attribs = self._serialisable.copy() attribs["type"] = get_object_provenance(self) attribs["version"] = __version__ return attribs
def to_rich_dict(self): data = self.__getstate__() data["type"] = get_object_provenance(self) data["version"] = __version__ return data