def _generate_lattice_from_parameters(interaction_matrix: np.ndarray): # make a graph from the interaction matrix. # This should be replaced by from_adjacency_matrix of retworkx. shape = interaction_matrix.shape if len(shape) != 2 or shape[0] != shape[1]: raise ValueError( f"Invalid shape of `interaction_matrix`, {shape}, is given." "It must be a square matrix.") graph = PyGraph(multigraph=False) graph.add_nodes_from(range(shape[0])) for source_index in range(shape[0]): for target_index in range(source_index, shape[0]): weight = interaction_matrix[source_index, target_index] if not weight == 0.0: graph.add_edge(source_index, target_index, weight) return Lattice(graph)
def from_json(cls, s: str) -> 'Topology': """Initializes from a JSON string.""" # load dict from JSON string data = orjson.loads(s) # validate type _type = data.pop("type") if _type != cls.__name__: raise TypeError(f"cannot deserialize from type `{_type}`") # initialize an empty graph graph = PyGraph() # process atoms for atom in data["atoms"]: graph.add_node(Atom.from_json(orjson.dumps(atom))) # process bonds for bond in data["bonds"]: bond = Bond.from_json(orjson.dumps(bond)) graph.add_edge(bond.indices[0], bond.indices[1], edge=bond) # return instance return cls(graph)