def default_array(self, array_size): array = numpy.empty(array_size, dtype=self.dtype) if self.value_type == Enum: array.fill(self.default_value.index) return EnumArray(array, self.possible_values) array.fill(self.default_value) return array
def value_from_person(self, array, role, default=0): """ Get the value of ``array`` for the person with the unique role ``role``. ``array`` must have the dimension of the number of persons in the simulation If such a person does not exist, return ``default`` instead The result is a vector which dimension is the number of entities """ self.check_role_validity(role) if role.max != 1: raise Exception( 'You can only use value_from_person with a role that is unique in {}. Role {} is not unique.' .format(self.key, role.key)) self.simulation.persons.check_array_compatible_with_entity(array) members_map = self.ordered_members_map result = self.filled_array(default, dtype=array.dtype) if isinstance(array, EnumArray): result = EnumArray(result, array.possible_values) role_filter = self.members.has_role(role) entity_filter = self.any(role_filter) result[entity_filter] = array[members_map][role_filter[members_map]] return result
def encode( cls, array: typing.Union[EnumArray, numpy.ndarray[int], numpy.ndarray[str], numpy.ndarray[Enum], ], ) -> EnumArray: """ Encode a string numpy array, an enum item numpy array, or an int numpy array into an :any:`EnumArray`. See :any:`EnumArray.decode` for decoding. :param ndarray array: Array of string identifiers, or of enum items, to encode. :returns: An :any:`EnumArray` encoding the input array values. :rtype: :any:`EnumArray` For instance: >>> string_identifier_array = asarray(['free_lodger', 'owner']) >>> encoded_array = HousingOccupancyStatus.encode(string_identifier_array) >>> encoded_array[0] 2 # Encoded value >>> free_lodger = HousingOccupancyStatus.free_lodger >>> owner = HousingOccupancyStatus.owner >>> enum_item_array = asarray([free_lodger, owner]) >>> encoded_array = HousingOccupancyStatus.encode(enum_item_array) >>> encoded_array[0] 2 # Encoded value """ if type(array) is EnumArray: return array if array.dtype.kind in {'U', 'S'}: # String array array = numpy.select( [array == item.name for item in cls], [item.index for item in cls], ).astype(config.ENUM_ARRAY_DTYPE) elif array.dtype.kind == 'O': # Enum items arrays # Ensure we are comparing the comparable. The problem this fixes: # On entering this method "cls" will generally come from # variable.possible_values, while the array values may come from directly # importing a module containing an Enum class. However, variables (and # hence their possible_values) are loaded by a call to load_module, which # gives them a different identity from the ones imported in the usual way. # So, instead of relying on the "cls" passed in, we use only its name to # check that the values in the array, if non-empty, are of the right type. if len(array) > 0 and cls.__name__ is array[0].__class__.__name__: cls = array[0].__class__ array = numpy.select( [array == item for item in cls], [item.index for item in cls], ).astype(config.ENUM_ARRAY_DTYPE) return EnumArray(array, cls)
def default_array(self): """ Return a new array of the appropriate length for the entity, filled with the variable default values. """ array_size = self.entity.count array = np.empty(array_size, dtype=self.variable.dtype) if self.variable.value_type == Enum: array.fill(self.variable.default_value.index) return EnumArray(array, self.variable.possible_values) array.fill(self.variable.default_value) return array
def _decode_file(self, file): enum = self._enums.get(file) if enum is not None: return EnumArray(np.load(file), enum) else: return np.load(file)