code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
new_event = Event(new_index, self.user_data)
new_event.time = self.time
new_event.step_size = self.step_size
return new_event | def split(self, new_index) | Create a new event which is a copy of this one but with a new index. | 4.065165 | 3.067066 | 1.325425 |
if hasattr(event, 'time'):
event.step_size = self.manager.step_size()
event.time = self.manager.clock() + self.manager.step_size()
for priority_bucket in self.listeners:
for listener in sorted(priority_bucket, key=lambda x: x.__name__):
liste... | def emit(self, event) | Notifies all listeners to this channel that an event has occurred.
Parameters
----------
event : Event
The event to be emitted. | 5.227738 | 5.62066 | 0.930093 |
self.clock = builder.time.clock()
self.step_size = builder.time.step_size() | def setup(self, builder) | Performs this components simulation setup.
Parameters
----------
builder : vivarium.framework.engine.Builder
Object giving access to core framework functionality. | 5.541889 | 6.399233 | 0.866024 |
self._event_types[name].listeners[priority].append(listener) | def register_listener(self, name, listener, priority=5) | Registers a new listener to the named event.
Parameters
----------
name : str
The name of the event.
listener : Callable
The consumer of the named event.
priority : int
Number in range(10) used to assign the ordering in which listeners process... | 10.876117 | 12.270814 | 0.88634 |
return self._event_manager.get_emitter(name) | def get_emitter(self, name: str) -> Callable[[Event], Event] | Gets and emitter for a named event.
Parameters
----------
name :
The name of the event he requested emitter will emit.
Users may provide their own named events by requesting an emitter with this function,
but should do so with caution as it makes time much mo... | 7.873981 | 15.97967 | 0.49275 |
self._event_manager.register_listener(name, listener, priority) | def register_listener(self, name: str, listener: Callable[[Event], None], priority: int=5) -> None | Registers a callable as a listener to a events with the given name.
The listening callable will be called with a named ``Event`` as it's only argument any time the
event emitter is invoked from somewhere in the simulation.
The framework creates the following events and emits them at different ... | 6.38831 | 6.463611 | 0.98835 |
super().setup(builder)
self.clock = builder.time.clock()
self.excess_mortality_rate = builder.value.register_rate_producer(
f'{self.state_id}.excess_mortality_rate',
source=self.risk_deleted_excess_mortality_rate
)
self.excess_mortality_rate_paf ... | def setup(self, builder) | Performs this component's simulation setup.
Parameters
----------
builder : `engine.Builder`
Interface to several simulation tools. | 6.401775 | 6.429315 | 0.995717 |
pop = self.manager.get_population(True).loc[index]
if self._query:
pop = pop.query(self._query)
if query:
pop = pop.query(query)
if not self._columns:
return pop
else:
if omit_missing_columns:
columns = l... | def get(self, index: pd.Index, query: str='', omit_missing_columns: bool=False) -> pd.DataFrame | For the rows in ``index`` get the columns from the simulation's population which this view is configured.
The result may be further filtered by the view's query.
Parameters
----------
index :
Index of the population to get.
query :
Conditions used to filt... | 2.744781 | 2.727167 | 1.006459 |
if not pop.empty:
if isinstance(pop, pd.Series):
if pop.name in self._columns:
affected_columns = [pop.name]
elif len(self._columns) == 1:
affected_columns = self._columns
else:
rais... | def update(self, pop: Union[pd.DataFrame, pd.Series]) | Update the simulation's state to match ``pop``
Parameters
----------
pop :
The data which should be copied into the simulation's state. If ``pop`` is a DataFrame only those columns
included in the view's columns will be used. If ``pop`` is a Series it must have a nam... | 3.612108 | 3.395793 | 1.063701 |
if 'tracked' not in columns:
query_with_track = query + 'and tracked == True' if query else 'tracked == True'
return PopulationView(self, columns, query_with_track)
return PopulationView(self, columns, query) | def get_view(self, columns: Sequence[str], query: str=None) -> PopulationView | Return a configured PopulationView
Notes
-----
Client code should only need this (and only through the version exposed as
``population_view`` on the builder during setup) if it uses dynamically
generated column names that aren't known at definition time. Otherwise
compon... | 3.882117 | 4.322356 | 0.898148 |
return self._population_manager.get_view(columns, query) | def get_view(self, columns: Sequence[str], query: str = None) -> PopulationView | Get a time-varying view of the population state table.
The requested population view can be used to view the current state or to update the state
with new values.
Parameters
----------
columns :
A subset of the state table columns that will be available in the retur... | 6.915161 | 11.495852 | 0.601535 |
return self._population_manager.get_simulant_creator() | def get_simulant_creator(self) -> Callable[[int, Union[Mapping[str, Any], None]], pd.Index] | Grabs a reference to the function that creates new simulants (adds rows to the state table).
Returns
-------
Callable
The simulant creator function. The creator function takes the number of simulants to be
created as it's first argument and a dict or other mapping of popul... | 8.796659 | 12.212546 | 0.720297 |
self._population_manager.register_simulant_initializer(initializer, creates_columns, requires_columns) | def initializes_simulants(self, initializer: Callable[[SimulantData], None],
creates_columns: Sequence[str]=(),
requires_columns: Sequence[str]=()) | Marks a callable as a source of initial state information for new simulants.
Parameters
----------
initializer :
A callable that adds or updates initial state information about new simulants.
creates_columns :
A list of the state table columns that the given init... | 6.36413 | 5.5131 | 1.154365 |
# decodifica y pasa a minúsculas
if decode:
title = unidecode(title)
title = title.lower()
# remueve caracteres no permitidos
filtered_title = re.sub(r'[^a-z0-9- ]+', '', title)
# remueve stop words y espacios y une palabras sólo con un "-"
normalized_title = '-'.join([word fo... | def title_to_name(title, decode=True, max_len=None, use_complete_words=True) | Convierte un título en un nombre normalizado para generar urls. | 3.312383 | 3.124032 | 1.060291 |
try:
result = urlparse(uri_string)
has_elements = all([result.scheme, result.netloc, result.path])
is_http = result.scheme == "http" or result.scheme == "https"
return True if has_elements and is_http else False
except Exception:
return False | def validate_url(uri_string) | Valida si un string es una URI válida. | 2.389983 | 2.360968 | 1.01229 |
if directory and not os.path.exists(directory):
os.makedirs(directory) | def ensure_dir_exists(directory) | Se asegura de que un directorio exista. | 2.545976 | 2.61631 | 0.973117 |
for key in keys:
if isinstance(dicc, dict) and key in dicc:
dicc = dicc[key]
elif (isinstance(dicc, list)
and isinstance(key, int)
and key < len(dicc)):
dicc = dicc[key]
else:
return default_value
return dicc | def traverse_dict(dicc, keys, default_value=None) | Recorre un diccionario siguiendo una lista de claves, y devuelve
default_value en caso de que alguna de ellas no exista.
Args:
dicc (dict): Diccionario a ser recorrido.
keys (list): Lista de claves a ser recorrida. Puede contener
índices de listas y claves de diccionarios mezcladas.... | 1.715685 | 1.898308 | 0.903797 |
if isinstance(list_of_dicts, list) and len(list_of_dicts) == 0:
return False
is_not_list_msg = .format(list_of_dicts)
assert isinstance(list_of_dicts, list), is_not_list_msg
not_all_dicts_msg = .format(list_of_dicts)
assert all([isinstance(d, dict) for d in list_of_dicts]), not_all_d... | def is_list_of_matching_dicts(list_of_dicts, expected_keys=None) | Comprueba que una lista esté compuesta únicamente por diccionarios,
que comparten exactamente las mismas claves.
Args:
list_of_dicts (list): Lista de diccionarios a comparar.
expected_keys (set): Conjunto de las claves que cada diccionario debe
tener. Si no se incluye, se asume que ... | 2.654031 | 2.411828 | 1.100423 |
value = cell.value
# stripea espacios en strings
if isinstance(value, string_types):
value = value.strip()
# convierte a texto ISO 8601 las fechas
if isinstance(value, (datetime)):
value = value.isoformat()
return value | def parse_value(cell) | Extrae el valor de una celda de Excel como texto. | 6.84697 | 5.593485 | 1.224097 |
headers = []
value_rows = []
for row_i, row in enumerate(worksheet.iter_rows()):
# lee los headers y el tamaño máximo de la hoja en columnas en fila 1
if row_i == 0:
for header_cell in row:
if header_cell.value:
headers.append(parse_valu... | def sheet_to_table(worksheet) | Transforma una hoja de libro de Excel en una lista de diccionarios.
Args:
worksheet (Workbook.worksheet): Hoja de cálculo de un archivo XLSX
según los lee `openpyxl`
Returns:
list_of_dicts: Lista de diccionarios, con tantos elementos como
registros incluya la hoja, y co... | 3.851147 | 3.753789 | 1.025936 |
return [value.strip() for value in string.split(sep)
if (not filter_empty or value)] | def string_to_list(string, sep=",", filter_empty=False) | Transforma una string con elementos separados por `sep` en una lista. | 3.707558 | 3.743107 | 0.990503 |
result = other_dict.copy()
for k, v in one_dict.items():
if v is None:
v = 0
if isinstance(v, dict):
result[k] = add_dicts(v, other_dict.get(k, {}))
else:
other_value = result.get(k, 0)
if other_value is None:
other_va... | def add_dicts(one_dict, other_dict) | Suma clave a clave los dos diccionarios. Si algún valor es un
diccionario, llama recursivamente a la función. Ambos diccionarios deben
tener exactamente las mismas claves, y los valores asociados deben ser
sumables, o diccionarios.
Args:
one_dict (dict)
other_dict (dict)
Returns:
... | 1.908468 | 2.186397 | 0.872883 |
intervals = {
'Y': 365,
'M': 30,
'W': 7,
'D': 1,
'H': 0,
'S': 0
}
if date_str.find('R/P') != 0: # Periodicity mal formada
return 0
date_str = date_str.strip('R/P')
days = 0
index = 0
for interval in intervals:
value_end... | def parse_repeating_time_interval_to_days(date_str) | Parsea un string con un intervalo de tiempo con repetición especificado
por la norma ISO 8601 en una cantidad de días que representa ese intervalo.
Devuelve 0 en caso de que el intervalo sea inválido. | 4.042652 | 3.483401 | 1.160548 |
with open(os.path.join(ABSOLUTE_SCHEMA_DIR,
"accrualPeriodicity.json"), "r") as f:
freqs_map = {freq["id"]: freq["description"]
for freq in json.load(f)}
return freqs_map[date_str] | def parse_repeating_time_interval_to_str(date_str) | Devuelve descripción humana de un intervalo de repetición.
TODO: Por ahora sólo interpreta una lista fija de intervalos. Debería poder
parsear cualquier caso. | 5.128776 | 4.938097 | 1.038614 |
if isinstance(wb, string_types):
# FIXME: importar o borrar segun corresponda
wb = load_workbook(wb, read_only=True, data_only=True)
for sheetname in wb.sheetnames:
if sheetname.lower() == name.lower():
return sheetname
raise Exception("No existe la hoja {}".format... | def find_ws_name(wb, name) | Busca una hoja en un workbook sin importar mayúsculas/minúsculas. | 3.933946 | 3.121079 | 1.260444 |
dataset_is_equal = True
dataset_diff = []
# Campos a comparar. Si es un campo anidado escribirlo como lista
if not fields_dataset:
fields_dataset = [
'title',
['publisher', 'name']
]
for field_dataset in fields_dataset:
if isinstance(field_datas... | def datasets_equal(dataset, other, fields_dataset=None,
fields_distribution=None, return_diff=False) | Función de igualdad de dos datasets: se consideran iguales si
los valores de los campos 'title', 'publisher.name',
'accrualPeriodicity' e 'issued' son iguales en ambos.
Args:
dataset (dict): un dataset, generado por la lectura de un catálogo
other (dict): idem anterior
Returns:
... | 1.901198 | 1.874734 | 1.014116 |
for dataset in catalog.get("dataset", []):
for distribution_index, distribution in enumerate(
dataset.get("distribution", [])):
if "identifier" not in distribution:
distribution["identifier"] = "{}_{}".format(
dataset["identifier"], distr... | def generate_distribution_ids(catalog) | Genera identificadores para las distribuciones que no los tienen.
Los identificadores de distribuciones se generan concatenando el id del
dataset al que pertenecen con el índice posicional de la distribución en el
dataset: distribution_identifier = "{dataset_identifier}_{index}". | 3.036587 | 2.39891 | 1.265819 |
try:
label = catalog.get_theme(identifier=theme)['label']
except BaseException:
try:
label = catalog.get_theme(label=theme)['label']
except BaseException:
raise ce.ThemeNonExistentError(theme)
label = re.sub(r'[^\wá-úÁ-ÚñÑ .-]+', '',
l... | def _get_theme_label(catalog, theme) | Intenta conseguir el theme por id o por label. | 3.808314 | 3.318364 | 1.147648 |
if isinstance(catalogs, list):
for catalog in catalogs:
try:
make_catalog_backup(
catalog,
local_catalogs_dir=local_catalogs_dir,
include_metadata=include_metadata,
include_metadata_xlsx=include... | def make_catalogs_backup(catalogs, local_catalogs_dir="",
include_metadata=True, include_data=True,
include_metadata_xlsx=False, use_short_path=False) | Realiza una copia local de los datos y metadatos de un catálogo.
Args:
catalogs (list or dict): Lista de catálogos (elementos que pueden
ser interpretados por DataJson como catálogos) o diccionario
donde las keys se interpretan como los catalog_identifier:
{
... | 1.539614 | 1.527021 | 1.008247 |
catalog = pydatajson.DataJson(catalog)
catalog_identifier = catalog_id if catalog_id else catalog["identifier"]
if include_metadata:
logger.info(
"Descargando catálogo {}".format(
catalog_identifier.ljust(30)))
# catálogo en json
catalog_path = get... | def make_catalog_backup(catalog, catalog_id=None, local_catalogs_dir="",
include_metadata=True, include_data=True,
include_datasets=None,
include_distribution_formats=['CSV', 'XLS'],
include_metadata_xlsx=True, use_short_pat... | Realiza una copia local de los datos y metadatos de un catálogo.
Args:
catalog (dict or str): Representación externa/interna de un catálogo.
Una representación _externa_ es un path local o una URL remota a un
archivo con la metadata de un catálogo, en formato JSON o XLSX. La
... | 2.454504 | 2.422487 | 1.013217 |
if use_short_path:
catalog_path = os.path.join(catalogs_dir, "catalog", catalog_id)
distribution_dir = os.path.join(catalog_path, dataset_id)
else:
catalog_path = os.path.join(catalogs_dir, "catalog", catalog_id)
dataset_path = os.path.join(catalog_path, "dataset", dataset_i... | def get_distribution_dir(catalog_id, dataset_id, distribution_id,
catalogs_dir=CATALOGS_DIR, use_short_path=False) | Genera el path estándar de un catálogo en un filesystem. | 1.710369 | 1.677616 | 1.019523 |
if use_short_path:
distribution_dir = get_distribution_dir(
catalog_id, dataset_id, distribution_id, catalogs_dir,
use_short_path=True)
distribution_file_path = os.path.join(
distribution_dir, distribution_file_name)
else:
distribution_dir = get_d... | def get_distribution_path(catalog_id, dataset_id, distribution_id,
distribution_file_name, catalogs_dir=CATALOGS_DIR,
use_short_path=False) | Genera el path estándar de un catálogo en un filesystem. | 1.552751 | 1.54069 | 1.007829 |
base_path = os.path.join(catalogs_dir, "catalog", catalog_id)
if fmt == "json":
return os.path.join(base_path, "data.json")
elif fmt == "xlsx":
return os.path.join(base_path, "catalog.xlsx")
else:
raise NotImplementedError("El formato {} no está implementado.".format(
... | def get_catalog_path(catalog_id, catalogs_dir=CATALOGS_DIR, fmt="json") | Genera el path estándar de un catálogo en un filesystem. | 2.494748 | 2.12277 | 1.175232 |
include_data = bool(int(include_data))
make_catalogs_backup(catalogs.split(
","), include_data=include_data, use_short_path=use_short_path) | def main(catalogs, include_data=True, use_short_path=True) | Permite hacer backups de uno o más catálogos por línea de comandos.
Args:
catalogs (str): Lista de catálogos separados por coma (URLs o paths
locales) para hacer backups. | 4.205976 | 6.553994 | 0.641742 |
# Si se paso una ruta, guardarla
if isinstance(catalog, string_types):
catalog_path_or_url = catalog
else:
catalog_path_or_url = None
catalog = read_catalog(catalog)
validation = validate_catalog(catalog)
# Solo necesito indicadores para un catalogo
indicators = generat... | def generate_readme(catalog, export_path=None) | Genera una descripción textual en formato Markdown sobre los
metadatos generales de un catálogo (título, editor, fecha de
publicación, et cetera), junto con:
- estado de los metadatos a nivel catálogo,
- estado global de los metadatos,
- cantidad de datasets federados y no federados,
... | 2.533538 | 2.434985 | 1.040474 |
self.config = builder.configuration
self.with_common_random_numbers = bool(self.config.randomness.key_columns)
self.register = builder.randomness.register_simulants
if (self.with_common_random_numbers
and not ['entrance_time', 'age'] == self.config.randomness.ke... | def setup(self, builder: Builder) | Performs this component's simulation setup.
The ``setup`` method is automatically called by the simulation
framework. The framework passes in a ``builder`` object which
provides access to a variety of framework subsystems and metadata.
Parameters
----------
builder :
... | 5.913236 | 5.764662 | 1.025773 |
age_start = self.config.population.age_start
age_end = self.config.population.age_end
if age_start == age_end:
age_window = pop_data.creation_window / pd.Timedelta(days=365)
else:
age_window = age_end - age_start
age_draw = self.age_randomness.g... | def on_initialize_simulants(self, pop_data: SimulantData) | Called by the simulation whenever new simulants are added.
This component is responsible for creating and filling four columns
in the population state table:
'age' :
The age of the simulant in fractional years.
'sex' :
The sex of the simulant. One of {'Male', 'F... | 3.232608 | 2.802257 | 1.153573 |
population = self.population_view.get(event.index, query="alive == 'alive'")
population['age'] += event.step_size / pd.Timedelta(days=365)
self.population_view.update(population) | def age_simulants(self, event: Event) | Updates simulant age on every time step.
Parameters
----------
event :
An event object emitted by the simulation containing an index
representing the simulants affected by the event and timing
information. | 8.215494 | 8.711199 | 0.943096 |
assert isinstance(catalogs, string_types + (dict, list))
# Si se pasa un único catálogo, genero una lista que lo contenga
if isinstance(catalogs, string_types + (dict,)):
catalogs = [catalogs]
indicators_list = []
# Cuenta la cantidad de campos usados/recomendados a nivel global
fi... | def generate_catalogs_indicators(catalogs, central_catalog=None,
identifier_search=False,
validator=None) | Genera una lista de diccionarios con varios indicadores sobre
los catálogos provistos, tales como la cantidad de datasets válidos,
días desde su última fecha actualizada, entre otros.
Args:
catalogs (str o list): uno o más catalogos sobre los que se quiera
obtener indicadores
ce... | 4.173791 | 4.029204 | 1.035885 |
result = {}
# Obtengo summary para los indicadores del estado de los metadatos
result.update(_generate_status_indicators(catalog, validator=validator))
# Genero los indicadores relacionados con fechas, y los agrego
result.update(
_generate_date_indicators(catalog, only_numeric=only_n... | def _generate_indicators(catalog, validator=None, only_numeric=False) | Genera los indicadores de un catálogo individual.
Args:
catalog (dict): diccionario de un data.json parseado
Returns:
dict: diccionario con los indicadores del catálogo provisto | 2.99279 | 3.014523 | 0.992791 |
result = {
'datasets_federados_cant': None,
'datasets_federados_pct': None,
'datasets_no_federados_cant': None,
'datasets_federados_eliminados_cant': None,
'distribuciones_federadas_cant': None,
'datasets_federados_eliminados': [],
'datasets_no_federados'... | def _federation_indicators(catalog, central_catalog,
identifier_search=False) | Cuenta la cantidad de datasets incluídos tanto en la lista
'catalogs' como en el catálogo central, y genera indicadores a partir
de esa información.
Args:
catalog (dict): catálogo ya parseado
central_catalog (str o dict): ruta a catálogo central, o un dict
con el catálogo ya par... | 1.992385 | 1.941607 | 1.026152 |
# Los porcentuales no se pueden sumar, tienen que ser recalculados
percentages = {
'datasets_meta_ok_pct':
(network_indicators.get('datasets_meta_ok_cant'),
network_indicators.get('datasets_meta_error_cant')),
'datasets_actualizados_pct':
(network_indic... | def _network_indicator_percentages(fields, network_indicators) | Encapsula el cálculo de indicadores de porcentaje (de errores,
de campos recomendados/optativos utilizados, de datasets actualizados)
sobre la red de nodos entera.
Args:
fields (dict): Diccionario con claves 'recomendado', 'optativo',
'total_recomendado', 'total_optativo', cada uno con ... | 3.06048 | 2.477833 | 1.235144 |
result = {
'datasets_cant': None,
'distribuciones_cant': None,
'datasets_meta_ok_cant': None,
'datasets_meta_error_cant': None,
'datasets_meta_ok_pct': None,
'datasets_con_datos_cant': None,
'datasets_sin_datos_cant': None,
'datasets_con_datos_pct... | def _generate_status_indicators(catalog, validator=None) | Genera indicadores básicos sobre el estado de un catálogo
Args:
catalog (dict): diccionario de un data.json parseado
Returns:
dict: indicadores básicos sobre el catálogo, tal como la cantidad
de datasets, distribuciones y número de errores | 2.150381 | 2.063199 | 1.042256 |
result = {
'datasets_desactualizados_cant': None,
'datasets_actualizados_cant': None,
'datasets_actualizados_pct': None,
'catalogo_ultima_actualizacion_dias': None
}
if not only_numeric:
result.update({
'datasets_frecuencia_cant': {}
})
t... | def _generate_date_indicators(catalog, tolerance=0.2, only_numeric=False) | Genera indicadores relacionados a las fechas de publicación
y actualización del catálogo pasado por parámetro. La evaluación de si
un catálogo se encuentra actualizado o no tiene un porcentaje de
tolerancia hasta que se lo considere como tal, dado por el parámetro
tolerance.
Args:
catalog (... | 2.908193 | 2.80839 | 1.035537 |
# el "date_field" se busca primero a nivel catálogo, luego a nivel
# de cada dataset, y nos quedamos con el que sea más reciente
date_modified = catalog.get(date_field, None)
dias_ultima_actualizacion = None
# "date_field" a nivel de catálogo puede no ser obligatorio,
# si no está pasamos
... | def _days_from_last_update(catalog, date_field="modified") | Calcula días desde la última actualización del catálogo.
Args:
catalog (dict): Un catálogo.
date_field (str): Campo de metadatos a utilizar para considerar
los días desde la última actualización del catálogo.
Returns:
int or None: Cantidad de días desde la última actualizac... | 3.513278 | 3.41575 | 1.028553 |
catalog = readers.read_catalog(catalog)
# Archivo .json con el uso de cada campo. Lo cargamos a un dict
catalog_fields_path = os.path.join(CATALOG_FIELDS_PATH,
'fields.json')
with open(catalog_fields_path) as f:
catalog_fields = json.load(f)
# A... | def _count_required_and_optional_fields(catalog) | Cuenta los campos obligatorios/recomendados/requeridos usados en
'catalog', junto con la cantidad máxima de dichos campos.
Args:
catalog (str o dict): path a un catálogo, o un dict de python que
contenga a un catálogo ya leído
Returns:
dict: diccionario con las claves 'recomend... | 6.022356 | 6.084118 | 0.989849 |
key_count = {
'recomendado': 0,
'optativo': 0,
'requerido': 0,
'total_optativo': 0,
'total_recomendado': 0,
'total_requerido': 0
}
for k, v in fields.items():
# Si la clave es un diccionario se implementa recursivamente el
# mismo algori... | def _count_fields_recursive(dataset, fields) | Cuenta la información de campos optativos/recomendados/requeridos
desde 'fields', y cuenta la ocurrencia de los mismos en 'dataset'.
Args:
dataset (dict): diccionario con claves a ser verificadas.
fields (dict): diccionario con los campos a verificar en dataset
como claves, y 'optat... | 4.536685 | 3.890953 | 1.165957 |
if len(index) > 0:
random_state = np.random.RandomState(seed=get_hash(key))
# Generate a random number for every simulant.
#
# NOTE: We generate a full set of random numbers for the population
# even when we may only need a few. This ensures consistency in outcomes
... | def random(key: str, index: Index, index_map: IndexMap=None) -> pd.Series | Produces an indexed `pandas.Series` of uniformly distributed random numbers.
The index passed in typically corresponds to a subset of rows in a
`pandas.DataFrame` for which a probabilistic draw needs to be made.
Parameters
----------
key :
A string used to create a seed for the random numb... | 6.382491 | 6.21008 | 1.027763 |
# 4294967295 == 2**32 - 1 which is the maximum allowable seed for a `numpy.random.RandomState`.
return int(hashlib.sha1(key.encode('utf8')).hexdigest(), 16) % 4294967295 | def get_hash(key: str) -> int | Gets a hash of the provided key.
Parameters
----------
key :
A string used to create a seed for the random number generator.
Returns
-------
int
A hash of the provided key. | 3.559613 | 3.3279 | 1.069627 |
# Convert p to normalized probabilities broadcasted over index.
p = _set_residual_probability(_normalize_shape(p, index)) if p is not None else np.ones((len(index), len(choices)))
p = p/p.sum(axis=1, keepdims=True)
draw = random(key, index, index_map)
p_bins = np.cumsum(p, axis=1)
# Use t... | def choice(key: str, index: Index, choices: Array, p: Array=None, index_map: IndexMap=None) -> pd.Series | Decides between a weighted or unweighted set of choices.
Given a a set of choices with or without corresponding weights,
returns an indexed set of decisions from those choices. This is
simply a vectorized way to make decisions with some book-keeping.
Parameters
----------
key :
A strin... | 5.243553 | 5.349186 | 0.980252 |
residual_mask = p == RESIDUAL_CHOICE
if residual_mask.any(): # I.E. if we have any placeholders.
if np.any(np.sum(residual_mask, axis=1) - 1):
raise RandomnessError(
'More than one residual choice supplied for a single set of weights. Weights: {}.'.format(p))
p... | def _set_residual_probability(p: np.ndarray) -> np.ndarray | Turns any use of `RESIDUAL_CHOICE` into a residual probability.
Parameters
----------
p :
Array where each row is a set of probability weights and potentially
a `RESIDUAL_CHOICE` placeholder.
Returns
-------
np.ndarray
Array where each row is a set of normalized probabi... | 4.669529 | 4.122971 | 1.132564 |
if population.empty:
return population
index = population if isinstance(population, pd.Index) else population.index
draw = random(key, index, index_map)
mask = np.array(draw < probability)
return population[mask] | def filter_for_probability(key: str, population: Union[pd.DataFrame, pd.Series, Index],
probability: Array, index_map: IndexMap=None) -> Union[pd.DataFrame, pd.Series, Index] | Decide an event outcome for each individual in a population from probabilities.
Given a population or its index and an array of associated probabilities
for some event to happen, we create and return the sub-population for whom
the event occurred.
Parameters
----------
key :
A string u... | 4.665567 | 5.63611 | 0.827799 |
if not self._map.index.intersection(new_keys).empty:
raise KeyError("Non-unique keys in index.")
mapping_update = self.hash_(new_keys)
if self._map.empty:
self._map = mapping_update.drop_duplicates()
else:
self._map = self._map.append(mapping... | def update(self, new_keys: Index) | Adds the new keys to the mapping.
Parameters
----------
new_keys :
The new index to hash. | 2.83241 | 2.711204 | 1.044705 |
key_frame = keys.to_frame()
new_map = pd.Series(0, index=keys)
salt = self.convert_to_ten_digit_int(pd.Series(salt, index=keys))
for i, column_name in enumerate(key_frame.columns):
column = self.convert_to_ten_digit_int(key_frame[column_name])
primes = ... | def hash_(self, keys: Index, salt: int = 0) -> pd.Series | Hashes the given index into an integer index in the range [0, self.stride]
Parameters
----------
keys :
The new index to hash.
salt :
An integer used to perturb the hash in a deterministic way. Useful
in dealing with collisions.
Returns
... | 5.342183 | 5.363026 | 0.996114 |
if isinstance(column.iloc[0], datetime.datetime):
column = self.clip_to_seconds(column.astype(int))
elif np.issubdtype(column.iloc[0], np.integer):
if not len(column >= 0) == len(column):
raise RandomnessError("Values in integer columns must be greater th... | def convert_to_ten_digit_int(self, column: pd.Series) -> pd.Series | Converts a column of datetimes, integers, or floats into a column of 10 digit integers.
Parameters
----------
column :
A series of datetimes, integers, or floats.
Returns
-------
pd.Series
A series of ten digit integers based on the input data.
... | 4.740309 | 4.112103 | 1.15277 |
return (m // (10 ** n)) % 10 | def digit(m: Union[int, pd.Series], n: int) -> Union[int, pd.Series] | Returns the nth digit of each number in m. | 4.851006 | 4.603423 | 1.053782 |
return m // pd.Timedelta(1, unit='s').value | def clip_to_seconds(m: Union[int, pd.Series]) -> Union[int, pd.Series] | Clips UTC datetime in nanoseconds to seconds. | 10.397306 | 7.855128 | 1.323633 |
return (m * 111_111) % self.TEN_DIGIT_MODULUS | def spread(self, m: Union[int, pd.Series]) -> Union[int, pd.Series] | Spreads out integer values to give smaller values more weight. | 18.104872 | 16.697575 | 1.084282 |
out = m % 1 * self.TEN_DIGIT_MODULUS // 1
if isinstance(out, pd.Series):
return out.astype(int)
return int(out) | def shift(self, m: Union[float, pd.Series]) -> Union[int, pd.Series] | Shifts floats so that the first 10 decimal digits are significant. | 7.534901 | 6.325756 | 1.191146 |
if self._for_initialization:
raise RandomnessError('Initialization streams cannot be copied.')
elif self._manager:
return self._manager.get_randomness_stream('_'.join([self.key, key]))
else:
return RandomnessStream(self.key, self.clock, self.seed, sel... | def copy_with_additional_key(self, key: Any) -> 'RandomnessStream' | Creates a copy of this stream that combines this streams key with a new one.
Parameters
----------
key :
The additional key to describe the new stream with.
Returns
-------
RandomnessStream
A new RandomnessStream with a combined key. | 6.329175 | 6.913142 | 0.915528 |
return '_'.join([self.key, str(self.clock()), str(additional_key), str(self.seed)]) | def _key(self, additional_key: Any=None) -> str | Construct a hashable key from this object's state.
Parameters
----------
additional_key :
Any additional information used to seed random number generation.
Returns
-------
str
A key to seed random number generation. | 8.983864 | 11.21521 | 0.801043 |
if self._for_initialization:
draw = random(self._key(additional_key), pd.Index(range(len(index))), self.index_map)
draw.index = index
else:
draw = random(self._key(additional_key), index, self.index_map)
return draw | def get_draw(self, index: Index, additional_key: Any=None) -> pd.Series | Get an indexed sequence of floats pulled from a uniform distribution over [0.0, 1.0)
Parameters
----------
index :
An index whose length is the number of random draws made
and which indexes the returned `pandas.Series`.
additional_key :
Any additional... | 5.966606 | 5.534732 | 1.07803 |
return self.filter_for_probability(population, rate_to_probability(rate), additional_key) | def filter_for_rate(self, population: Union[pd.DataFrame, pd.Series, Index],
rate: Array, additional_key: Any=None) -> Index | Decide an event outcome for each individual in a population from rates.
Given a population or its index and an array of associated rates for
some event to happen, we create and return the sub-population for whom
the event occurred.
Parameters
----------
population :
... | 5.596454 | 9.140864 | 0.612246 |
return filter_for_probability(self._key(additional_key), population, probability, self.index_map) | def filter_for_probability(self, population: Union[pd.DataFrame, pd.Series, Index],
probability: Array, additional_key: Any=None) -> Index | Decide an event outcome for each individual in a population from probabilities.
Given a population or its index and an array of associated probabilities
for some event to happen, we create and return the sub-population for whom
the event occurred.
Parameters
----------
... | 9.490037 | 19.666372 | 0.482551 |
return choice(self._key(additional_key), index, choices, p, self.index_map) | def choice(self, index: Index, choices: Array, p: Array=None, additional_key: Any=None) -> pd.Series | Decides between a weighted or unweighted set of choices.
Given a a set of choices with or without corresponding weights,
returns an indexed set of decisions from those choices. This is
simply a vectorized way to make decisions with some book-keeping.
Parameters
----------
... | 10.571656 | 18.609495 | 0.568079 |
if decision_point in self._decision_points:
raise RandomnessError(f"Two separate places are attempting to create "
f"the same randomness stream for {decision_point}")
stream = RandomnessStream(key=decision_point, clock=self._clock, seed=self._seed,
... | def get_randomness_stream(self, decision_point: str, for_initialization: bool=False) -> RandomnessStream | Provides a new source of random numbers for the given decision point.
Parameters
----------
decision_point :
A unique identifier for a stream of random numbers. Typically represents
a decision that needs to be made each time step like 'moves_left' or
'gets_d... | 4.509183 | 4.064664 | 1.109362 |
if not all(k in simulants.columns for k in self._key_columns):
raise RandomnessError("The simulants dataframe does not have all specified key_columns.")
self._key_mapping.update(simulants.set_index(self._key_columns).index) | def register_simulants(self, simulants: pd.DataFrame) | Adds new simulants to the randomness mapping.
Parameters
----------
simulants :
A table with state data representing the new simulants. Each simulant should
pass through this function exactly once.
Raises
------
RandomnessError :
If ... | 5.100999 | 3.517918 | 1.450005 |
return self._randomness_manager.get_randomness_stream(decision_point, for_initialization) | def get_stream(self, decision_point: str, for_initialization: bool = False) -> RandomnessStream | Provides a new source of random numbers for the given decision point.
``vivarium`` provides a framework for Common Random Numbers which allows for variance reduction
when modeling counter-factual scenarios. Users interested in causal analysis and comparisons
between simulation scenarios should ... | 4.037845 | 5.832022 | 0.692358 |
group_ids = set()
for group in Group.objects.all():
if group.properties.student_visible:
group_ids.add(group.id)
return Group.objects.filter(id__in=group_ids) | def student_visible(self) | Return a list of groups that are student-visible. | 3.405801 | 2.598932 | 1.310462 |
if value in self.empty_values:
return ""
value = force_text(value).strip()
return value | def to_python(self, value) | Returns a Unicode object. | 5.502266 | 4.846674 | 1.135266 |
'''
Resolve the time in seconds of a configuration value.
'''
if value is None or isinstance(value,(int,long)):
return value
if NUMBER_TIME.match(value):
return long(value)
simple = SIMPLE_TIME.match(value)
if SIMPLE_TIME.match(value):
multiplier = long( simple.groups()[0] )
constant = S... | def _resolve_time(value) | Resolve the time in seconds of a configuration value. | 5.21225 | 4.158798 | 1.253307 |
'''
Return the time in seconds of a step. If a begin and end timestamp,
return the time in seconds between them after adjusting for what buckets
they alias to. If t1 and t0 resolve to the same bucket,
'''
if t0!=None and t1!=None:
tb0 = self.to_bucket( t0 )
tb1 = self.to_bucket( t1, ... | def step_size(self, t0=None, t1=None) | Return the time in seconds of a step. If a begin and end timestamp,
return the time in seconds between them after adjusting for what buckets
they alias to. If t1 and t0 resolve to the same bucket, | 7.297741 | 3.043603 | 2.397731 |
'''
Calculate the buckets within a starting and ending timestamp.
'''
start_bucket = self.to_bucket(start)
end_bucket = self.to_bucket(end)
return range(start_bucket, end_bucket+1) | def buckets(self, start, end) | Calculate the buckets within a starting and ending timestamp. | 3.893544 | 2.6369 | 1.476561 |
'''
Return the ttl given the number of steps, None if steps is not defined
or we're otherwise unable to calculate one. If relative_time is defined,
then return a ttl that is the number of seconds from now that the
record should be expired.
'''
if steps:
if relative_time:
rtime ... | def ttl(self, steps, relative_time=None) | Return the ttl given the number of steps, None if steps is not defined
or we're otherwise unable to calculate one. If relative_time is defined,
then return a ttl that is the number of seconds from now that the
record should be expired. | 6.203485 | 3.128148 | 1.983117 |
'''
Return the time in seconds for each step. Requires that we know a time
relative to which we should calculate to account for variable length
intervals (e.g. February)
'''
tb0 = self.to_bucket( t0 )
if t1:
tb1 = self.to_bucket( t1, steps=1 ) # NOTE: "end" of second bucket
else:
... | def step_size(self, t0, t1=None) | Return the time in seconds for each step. Requires that we know a time
relative to which we should calculate to account for variable length
intervals (e.g. February) | 8.419893 | 4.301598 | 1.957387 |
'''
Calculate the bucket from a timestamp.
'''
dt = datetime.utcfromtimestamp( timestamp )
if steps!=0:
if self._step == 'daily':
dt = dt + timedelta(days=steps)
elif self._step == 'weekly':
dt = dt + timedelta(weeks=steps)
elif self._step == 'monthly':
dt ... | def to_bucket(self, timestamp, steps=0) | Calculate the bucket from a timestamp. | 2.777652 | 2.661372 | 1.043692 |
'''
Calculate the timestamp given a bucket.
'''
# NOTE: this is due to a bug somewhere in strptime that does not process
# the week number of '%Y%U' correctly. That bug could be very specific to
# the combination of python and ubuntu that I was testing.
bucket = str(bucket)
if self._step... | def from_bucket(self, bucket, native=False) | Calculate the timestamp given a bucket. | 6.464039 | 5.750118 | 1.124158 |
'''
Calculate the buckets within a starting and ending timestamp.
'''
rval = [ self.to_bucket(start) ]
step = 1
# In theory there's already been a check that end>start
# TODO: Not a fan of an unbound while loop here
while True:
bucket = self.to_bucket(start, step)
bucket_tim... | def buckets(self, start, end) | Calculate the buckets within a starting and ending timestamp. | 6.364378 | 5.406761 | 1.177115 |
'''
Normalize a timestamp according to the interval configuration. Optionally
can be used to calculate the timestamp N steps away.
'''
# So far, the only commonality with RelativeTime
return self.from_bucket( self.to_bucket(timestamp, steps) ) | def normalize(self, timestamp, steps=0) | Normalize a timestamp according to the interval configuration. Optionally
can be used to calculate the timestamp N steps away. | 17.909288 | 7.081137 | 2.529154 |
'''
Return the ttl given the number of steps, None if steps is not defined
or we're otherwise unable to calculate one. If relative_time is defined,
then return a ttl that is the number of seconds from now that the
record should be expired.
'''
if steps:
# Approximate the ttl based on n... | def ttl(self, steps, relative_time=None) | Return the ttl given the number of steps, None if steps is not defined
or we're otherwise unable to calculate one. If relative_time is defined,
then return a ttl that is the number of seconds from now that the
record should be expired. | 5.679403 | 3.80062 | 1.494336 |
'''
Perform a bulk insert. The format of the inserts must be:
{
timestamp : {
name: [ values ],
...
},
...
}
If the timestamp should be auto-generated, then "timestamp" should be None.
Backends can implement this in any number of ways, the default being to
... | def bulk_insert(self, inserts, intervals=0, **kwargs) | Perform a bulk insert. The format of the inserts must be:
{
timestamp : {
name: [ values ],
...
},
...
}
If the timestamp should be auto-generated, then "timestamp" should be None.
Backends can implement this in any number of ways, the default being to
perform a ... | 6.474836 | 2.111728 | 3.066132 |
'''
Insert a value for the timeseries "name". For each interval in the
configuration, will insert the value into a bucket for the interval
"timestamp". If time is not supplied, will default to time.time(), else it
should be a floating point value.
If "intervals" is less than 0, inserts the valu... | def insert(self, name, value, timestamp=None, intervals=0, **kwargs) | Insert a value for the timeseries "name". For each interval in the
configuration, will insert the value into a bucket for the interval
"timestamp". If time is not supplied, will default to time.time(), else it
should be a floating point value.
If "intervals" is less than 0, inserts the value into times... | 7.834605 | 2.802938 | 2.795141 |
'''
Support for batch insert. Default implementation is non-optimized and
is a simple loop over values.
'''
for timestamp,names in inserts.iteritems():
for name,values in names.iteritems():
for value in values:
self._insert( name, value, timestamp, intervals, **kwargs ) | def _batch_insert(self, inserts, intervals, **kwargs) | Support for batch insert. Default implementation is non-optimized and
is a simple loop over values. | 7.091985 | 3.234308 | 2.192737 |
'''
Helper for the subclasses to generate a list of timestamps.
'''
rval = [timestamp]
if intervals<0:
while intervals<0:
rval.append( config['i_calc'].normalize(timestamp, intervals) )
intervals += 1
elif intervals>0:
while intervals>0:
rval.append( config['i... | def _normalize_timestamps(self, timestamp, intervals, config) | Helper for the subclasses to generate a list of timestamps. | 5.201459 | 3.70645 | 1.403353 |
'''
Returns a generator that iterates over all the intervals and returns
data for various timestamps, in the form:
( unix_timestamp, data )
This will check for all timestamp buckets that might exist between
the first and last timestamp in a series. Each timestamp bucket will
be fetched s... | def iterate(self, name, interval, **kwargs) | Returns a generator that iterates over all the intervals and returns
data for various timestamps, in the form:
( unix_timestamp, data )
This will check for all timestamp buckets that might exist between
the first and last timestamp in a series. Each timestamp bucket will
be fetched separately to... | 7.608671 | 2.555367 | 2.977526 |
'''
Join a list of results. Supports both get and series.
'''
rval = OrderedDict()
i_keys = set()
for res in results:
i_keys.update( res.keys() )
for i_key in sorted(i_keys):
if coarse:
rval[i_key] = join( [res.get(i_key) for res in results] )
else:
rval[i_k... | def _join_results(self, results, coarse, join) | Join a list of results. Supports both get and series. | 2.448816 | 1.989759 | 1.23071 |
'''
Process transforms on the data.
'''
if isinstance(transform, (list,tuple,set)):
return { t : self._transform(data,t,step_size) for t in transform }
elif isinstance(transform, dict):
return { tn : self._transform(data,tf,step_size) for tn,tf in transform.items() }
return self._tra... | def _process_transform(self, data, transform, step_size) | Process transforms on the data. | 3.055201 | 2.692502 | 1.134707 |
'''
Transform the data. If the transform is not supported by this series,
returns the data unaltered.
'''
if transform=='mean':
total = sum( k*v for k,v in data.items() )
count = sum( data.values() )
data = float(total)/float(count) if count>0 else 0
elif transform=='count':
... | def _transform(self, data, transform, step_size) | Transform the data. If the transform is not supported by this series,
returns the data unaltered. | 2.704439 | 2.263041 | 1.195047 |
'''
Condense by adding together all of the lists.
'''
rval = {}
for resolution,histogram in data.items():
for value,count in histogram.items():
rval[ value ] = count + rval.get(value,0)
return rval | def _condense(self, data) | Condense by adding together all of the lists. | 8.72328 | 5.47251 | 1.594018 |
'''
Join multiple rows worth of data into a single result.
'''
rval = {}
for row in rows:
if row:
for value,count in row.items():
rval[ value ] = count + rval.get(value,0)
return rval | def _join(self, rows) | Join multiple rows worth of data into a single result. | 6.849771 | 4.761981 | 1.438429 |
'''
Join multiple rows worth of data into a single result.
'''
rval = 0
for row in rows:
if row: rval += row
return rval | def _join(self, rows) | Join multiple rows worth of data into a single result. | 10.523625 | 5.715749 | 1.841163 |
'''
Transform the data. If the transform is not supported by this series,
returns the data unaltered.
'''
if callable(transform):
data = transform(data, step_size)
return data | def _transform(self, data, transform, step_size) | Transform the data. If the transform is not supported by this series,
returns the data unaltered. | 6.63206 | 2.932346 | 2.26169 |
'''
Condense by returning the last real value of the gauge.
'''
if data:
data = filter(None,data.values())
if data:
return data[-1]
return None | def _condense(self, data) | Condense by returning the last real value of the gauge. | 11.060218 | 4.329454 | 2.554645 |
'''
Transform the data. If the transform is not supported by this series,
returns the data unaltered.
'''
if transform=='mean':
total = sum( data )
count = len( data )
data = float(total)/float(count) if count>0 else 0
elif transform=='count':
data = len(data)
elif tr... | def _transform(self, data, transform, step_size) | Transform the data. If the transform is not supported by this series,
returns the data unaltered. | 3.05352 | 2.427206 | 1.258039 |
'''
Condense by or-ing all of the sets.
'''
if data:
return reduce(operator.ior, data.values())
return set() | def _condense(self, data) | Condense by or-ing all of the sets. | 10.897528 | 4.715469 | 2.311017 |
r
from math import sqrt
n = len(x)
mean = sum(x) / float(n)
std = sqrt(sum((a - mean)**2 for a in x) / float(n - 1))
return std | def stdev(x) | r"""Calculate standard deviation of data x[]:
std = sqrt(\sum_i (x_i - mean)^2 \over n-1)
https://wiki.python.org/moin/NumericAndScientificRecipes | 2.797665 | 2.785158 | 1.00449 |
students_grp, _ = Group.objects.get_or_create(name="Students")
sg_prop = students_grp.properties
sg_prop.student_visible = True
sg_prop.save()
students_grp.save()
for gr in [settings.SENIOR_GRADUATION_YEAR + y for y in range(0, 4)]:
users = User.obj... | def handle(self, **options) | Create "Class of 20[16-19]" groups | 3.070882 | 2.878486 | 1.066839 |
do_run = options["run"]
if do_run:
if not options["confirm"]:
self.ask("===== WARNING! =====\n\n"
"This script will DESTROY data! Ensure that you have a properly backed-up copy of your database before proceeding.\n\n"
"===== ... | def handle(self, *args, **options) | EIGHTH:
EighthBlock: filtered
EighthSignup: absences removed
EighthActivity: keep
ANNOUNCEMENTS:
AnnouncementRequest: filtered
USERS:
User: graduated students deleted | 4.653563 | 4.574543 | 1.017274 |
user = auth.authenticate(username=userid, password=password)
if user is None or (user and not user.is_active):
raise exceptions.AuthenticationFailed("Invalid username/password.")
return (user, None) | def authenticate_credentials(self, userid, password, request=None) | Authenticate the userid and password. | 3.110249 | 3.249691 | 0.957091 |
remote_addr = (request.META["HTTP_X_FORWARDED_FOR"] if "HTTP_X_FORWARDED_FOR" in request.META else request.META.get("REMOTE_ADDR", ""))
return remote_addr in settings.INTERNAL_IPS | def check_internal_ip(request) | request is an AsgiRequest | 2.360949 | 2.329848 | 1.013349 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.