def __init__(self, universe, name, units, data=None, dtype=None, element_shape=None): api.validate_type(universe, Universe, "universe") self._universe = universe api.validate_label(name, "name") self._name = name api.validate_units(units, "units") self._units = units if data is None: if dtype is None: dtype = N.float64 if dtype not in self._allowed_dtypes: raise ValueError("dtype must be " + " or ".join(str(t) for t in self._allowed_dtypes)) if element_shape is None: element_shape = () array_shape = (self._number_of_values(),) + element_shape self._data = N.empty(array_shape, dtype) else: api.validate_array(data, None, self._allowed_dtypes, "data") if dtype is not None and data.dtype != dtype: raise ValueError("elements of data array do not have " "the requested type") if data.shape[0] != self._number_of_values(): raise ValueError("data array has incorrect shape") if element_shape is not None and data.shape[1:] != element_shape: raise ValueError("elements of data array do not have " "the requested shape") self._data = data
def __init__(self, args): descriptor, nsites = args api.validate_type(descriptor, AtomDescriptor, "atom descriptor") self.descriptor = descriptor api.validate_type(nsites, int, "number of sites") if nsites < 1: raise ValueError("number of sites must be >= 1") self.nsites = nsites
def __init__(self, universe, indices, selection_type): api.validate_type(universe, Universe, "universe") api.validate_array(indices, None, [N.uint8, N.uint16, N.uint32, N.uint64], "indices") api.validate_value(selection_type, self._allowed_types, "selection_type") self._universe = universe self._indices = indices self._type = selection_type
def __init__(self, universe, name, strings, label_type): api.validate_type(universe, Universe, "universe") api.validate_label(name, "name") api.validate_sequence(strings, str, "strings") api.validate_value(label_type, self._allowed_types, "label_type") self._universe = universe self._name = name self._string_array = N.array(list('\0'.join(strings) + '\0'), dtype='S') self._type = label_type
def __new__(cls, name=""): try: return cls._instances[name] except KeyError: ob = super(AtomDescriptor, cls).__new__(cls) api.validate_type(name, str, "name") cls._validate_name(name) ob._name = name cls._instances[name] = ob return ob
def _validate_bond_atom(self, atom_label): api.validate_type(atom_label, str, "atom label") obj = self for item in atom_label.split("."): if isinstance(obj, Atom): raise ValueError("invalid atom reference " "%s (refers to child object of an atom)" % atom_label) try: obj = obj.attrs[item] except KeyError: raise ValueError("invalid atom reference %s " "(child %s not found)" % (atom_label, item))
def __init__(self, universe, positions, cell_parameters): api.validate_type(universe, Universe, "universe") api.validate_array(positions, (universe.number_of_sites, 3), self._allowed_dtypes, "positions") api.validate_array(cell_parameters, universe._cell_parameter_array_shapes [universe.cell_shape], [positions.dtype], "cell_parameters") self._universe = universe self._positions = positions self._cell_parameters = cell_parameters
def __init__(self, label, descriptor, nsites=1): self.label = label api.validate_type(descriptor, AtomDescriptor, "atom descriptor") api.validate_type(nsites, int, "number of sites") if nsites < 1: raise ValueError("number of sites must be >= 1") self._descriptor = descriptor self.nsites = nsites self._parent = None
def __init__(self, universe, name, strings): api.validate_type(universe, Universe, "universe") self._universe = universe api.validate_label(name, "name") self._name = name api.validate_sequence(strings, str, "strings") if len(strings) != self._number_of_values(): raise ValueError("incorrect number of strings") for s in strings: api.validate_ascii_string(s, "label") self._strings = ImmutableTuple(strings)
def __init__(self, universe, name, units, data): api.validate_type(universe, Universe, "universe") self._universe = universe api.validate_label(name, "name") self._name = name api.validate_units(units, "units") self._units = units api.validate_array(data, None, self._allowed_dtypes, "data") if data.shape[0] != self._number_of_values(): raise ValueError("data array has incorrect shape") self._data = data
def __init__(self, universe, name, units, data, property_type): api.validate_type(universe, Universe, "universe") api.validate_label(name, "name") api.validate_units(units, "units") api.validate_array(data, None, self._allowed_dtypes, "data") api.validate_value(property_type, self._allowed_types, "property_type") self._universe = universe self._name = name self._units = units self._data = data self._type = property_type
def __init__(self, args): cell_shape, molecules, symmetry_transformations, convention = args api.validate_type(cell_shape, str, "cell_shape") api.validate_value(cell_shape, list(self._cell_parameter_array_shapes.keys()), "cell_shape") api.validate_sequence( molecules, ImmutableTuple, "molecules", ( (lambda p: len(p) == 3, "must have length 3"), ( lambda p: isinstance(p[0], Fragment) and isascii(p[1]) and isinstance(p[2], int), "elements must be (fragment, label, count) " "triples", ), ), ) api.validate_sequence( symmetry_transformations, ImmutableTuple, "symmetry_transformations", ( (lambda p: len(p) == 2, "must have length 2"), ( lambda p: hasattr(p[0], "shape") and p[0].shape == (3, 3) and p[0].dtype == N.float64, "rotation matrix must be float64 " "and have shape (3,3)", ), ( lambda p: hasattr(p[1], "shape") and p[1].shape == (3,) and p[0].dtype == N.float64, "translation vector must be float64 " "and have shape (3,)", ), ), ) if cell_shape == "infinite" and len(symmetry_transformations) > 0: raise ValueError("Symmetry transformations are allowed " "only in periodic universes") api.validate_label(convention, "Universe.convention") self._cell_shape = cell_shape self._molecules = ImmutableTuple( ImmutableTuple((FragmentRef(label, fragment), count)) for fragment, label, count in molecules ) self._symmetry_transformations = symmetry_transformations self._convention = convention self._fragments = ImmutableTuple(ImmutableTuple((f, l)) for f, l, c in molecules) self._molecule_counts = ImmutableTuple(c for f, l, c in molecules) self._atom_counts = ImmutableTuple(f.number_of_atoms for f, l, c in molecules) self._site_counts = ImmutableTuple(f.number_of_sites for f, l, c in molecules) self._bond_counts = ImmutableTuple(f.number_of_bonds for f, l, c in molecules)
def store(self, path, data): """ :param path: a HDF5 path, relative to the group used by the HDF5Store :type path: str :param data: a Mosaic data item :type data: :class:`mosaic.api.MosaicDataItem` """ api.validate_type(path, str, "path") if path[0] == "/": raise ValueError("absolute paths not allowed") api.validate_type(data, api.MosaicDataItem, "data") handler = self.storage_handler[type(data)] if handler is None: raise TypeError("Storage of %s not yet implemented" % str(type(data))) handler(self, path, data) self._register_data_item(path, data)
def __init__(self, universe, positions, cell_parameters): api.validate_type(universe, Universe, "universe") cell_param_shape = universe.cell_parameter_array_shape nsites = universe.number_of_sites # Allow cell_parameters=None for universes that don't # need cell parameters. if cell_parameters is None and cell_param_shape == (0,): cell_parameters = IN.zeros(cell_param_shape, positions.dtype) # At this point, positions and cell_parameters must be arrays # of the required shapes. api.validate_array(positions, (nsites, 3), self._allowed_dtypes, "positions") api.validate_array(cell_parameters, cell_param_shape, self._allowed_dtypes, "cell_parameters") if positions.dtype != cell_parameters.dtype: raise ValueError("positions and cell parameters must have" " the same element type") self._universe = universe self._positions = positions self._cell_parameters = cell_parameters
def __init__(self, universe, positions=None, cell_parameters=None, dtype=None): api.validate_type(universe, Universe, "universe") cell_param_shape = universe.cell_parameter_array_shape nsites = universe.number_of_sites if positions is None and cell_parameters is None: if dtype is None: dtype = N.float64 if dtype not in self._allowed_dtypes: raise ValueError("dtype must be " + " or ".join(str(t) for t in self._allowed_dtypes)) positions = N.empty((nsites, 3), dtype) cell_parameters = N.empty(cell_param_shape, dtype) else: # Allow cell_parameters=None for universes that don't # need cell parameters. if positions is not None and cell_parameters is None \ and cell_param_shape == (0,): cell_parameters = N.empty(cell_param_shape, positions.dtype) # Require both positions and cell parameters, or neither if positions is None or cell_parameters is None: raise ValueError("configuration requires both " "positions and cell parameters") # At this point, positions and cell_parameters must be arrays # of the required shapes. api.validate_array(positions, (nsites, 3), self._allowed_dtypes, "positions") api.validate_array(cell_parameters, cell_param_shape, self._allowed_dtypes, "cell_parameters") if positions.dtype != cell_parameters.dtype: raise ValueError("positions and cell parameters must have" " the same element type") # If an explicit dtype is given, check arrays for conformance if dtype is not None: if positions.dtype != dtype: raise ValueError("arrays don't have the requested" " element type " + str(dtype)) self._universe = universe self._positions = positions self._cell_parameters = cell_parameters
def store(self, xml_id, data): """ :param xml_id: the id of the XML element representing the data item :type xml_id: str :param data: a Mosaic data item :type data: :class:`mosaic.api.MosaicDataItem` """ api.validate_type(xml_id, str, "xml_id") api.validate_type(data, api.MosaicDataItem, "data") xml_id = xml_id if xml_id in self._xml_ids: raise ValueError("XML ID %s has already been used" % xml_id) handler = self.storage_handler[type(data)] if handler is None: raise TypeError("Storage of %s not yet implemented" % str(type(data))) handler(self, xml_id, data) self._register_data_item(xml_id, data) self._xml_ids.add(xml_id)
def __init__(self, args): species, fragments, atoms, bonds = args api.validate_label(species, "species") self.species = species labels = set() api.validate_sequence(fragments, ImmutableTuple, "fragments", ((lambda p: len(p) == 2, "must have length 2"),)) for label, fragment in fragments: api.validate_label(label, "fragment label") if label in labels: raise ValueError("label %s occurs more than once" % label) labels.add(label) api.validate_type(fragment, Fragment, "fragment template") self.fragments = fragments api.validate_sequence(atoms, ImmutableTuple, "atoms", ((lambda p: len(p) == 2, "must have length 2"),)) for label, atom in atoms: api.validate_label(label, "atom label") if label in labels: raise ValueError("label %s occurs more than once" % label) labels.add(label) api.validate_type(atom, Atom, "atom") self.atoms = atoms self.attrs = ImmutableDict(IT.chain(fragments, atoms)) api.validate_sequence(bonds, tuple, "bonds", ((lambda p: len(p) == 3, "must have length 3"),)) for a1, a2, order in bonds: self._validate_bond_atom(a1) self._validate_bond_atom(a2) if a1.split(".")[0] == a2.split(".")[0]: raise ValueError( "bond between %s and %s must be defined " "in fragment %s" % (a1, a2, a1.split(".")[0]) ) api.validate_value(order, api.MosaicFragment._bond_orders, "bond order") self.bonds = bonds
def cell_shape(self, new_cell_shape): api.validate_type(new_cell_shape, str, "cell_shape") api.validate_value(new_cell_shape, self._cell_parameter_array_shapes.keys(), "cell_shape") self._cell_shape = new_cell_shape
def __init__(self, universe, indices): api.validate_type(universe, Universe, "universe") self._universe = universe indices = N.array(indices, uint_for_max_value(max(indices))) api.validate_indices(indices, self._max_index(), "indices") self._indices = indices