code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
complexity = params.get('complexity', 10)
no_assert = params.get('no_assert', False)
show_closures = params.get('show_closures', False)
visitor = ComplexityVisitor.from_code(code, no_assert=no_assert)
blocks = visitor.blocks
if show_closures:
blocks ... | def run(path, code=None, params=None, ignore=None, select=None, **meta) | Check code with Radon.
:return list: List of errors. | 4.262563 | 4.070472 | 1.047191 |
p = Popen(command.split(), stdout=PIPE, stderr=PIPE)
(stdout, stderr) = p.communicate()
return (p.returncode, [line.strip() for line in stdout.splitlines()],
[line.strip() for line in stderr.splitlines()]) | def run(command) | Run a shell command.
:return str: Stdout | 1.942008 | 2.352074 | 0.825658 |
_, files_modified, _ = run("git diff-index --cached --name-only HEAD")
options = parse_options()
setup_logger(options)
if sys.version_info >= (3,):
candidates = [f.decode('utf-8') for f in files_modified]
else:
candidates = [str(f) for f in files_modified]
if candidates:
... | def git_hook(error=True) | Run pylama after git commit. | 4.325484 | 4.111821 | 1.051963 |
seen = set()
paths = []
if len(repo):
for rev in range(repo[node], len(repo)):
for file_ in repo[rev].files():
file_ = op.join(repo.root, file_)
if file_ in seen or not op.exists(file_):
continue
seen.add(file_)
... | def hg_hook(ui, repo, node=None, **kwargs) | Run pylama after mercurial commit. | 4.480775 | 4.234087 | 1.058262 |
hook = op.join(path, 'pre-commit')
with open(hook, 'w') as fd:
fd.write()
chmod(hook, 484) | def install_git(path) | Install hook in Git repository. | 5.617914 | 5.391912 | 1.041915 |
hook = op.join(path, 'hgrc')
if not op.isfile(hook):
open(hook, 'w+').close()
c = ConfigParser()
c.readfp(open(hook, 'r'))
if not c.has_section('hooks'):
c.add_section('hooks')
if not c.has_option('hooks', 'commit'):
c.set('hooks', 'commit', 'python:pylama.hooks.hg... | def install_hg(path) | Install hook in Mercurial repository. | 2.39806 | 2.371255 | 1.011304 |
git = op.join(path, '.git', 'hooks')
hg = op.join(path, '.hg')
if op.exists(git):
install_git(git)
LOGGER.warn('Git hook has been installed.')
elif op.exists(hg):
install_hg(hg)
LOGGER.warn('Mercurial hook has been installed.')
else:
LOGGER.error('VCS h... | def install_hook(path) | Auto definition of SCM and hook installation. | 3.265351 | 3.10269 | 1.052426 |
code = converter(code)
line_numbers = commented_out_code_line_numbers(code)
lines = code.split('\n')
result = []
for line_number in line_numbers:
line = lines[line_number - 1]
result.append(dict(
lnum=line_number,
... | def run(path, code=None, params=None, **meta) | Eradicate code checking.
:return list: List of errors. | 4.205597 | 4.342315 | 0.968515 |
passed = defaultdict(list)
for error in errors:
key = error.linter, error.number
if key in DUPLICATES:
if key in passed[error.lnum]:
continue
passed[error.lnum] = DUPLICATES[key]
yield error | def remove_duplicates(errors) | Filter duplicates from given error's list. | 5.492425 | 5.246902 | 1.046794 |
self.path.profile = self.path.gen.joinpath("profile")
if not self.path.profile.exists():
self.path.profile.mkdir()
self.python = hitchpylibrarytoolkit.project_build(
"strictyaml",
self.path,
self.given["python version"],
{"ru... | def set_up(self) | Set up your applications and the test environment. | 4.735573 | 4.596406 | 1.030277 |
forked_chunk = YAMLChunk(
deepcopy(self._ruamelparsed),
pointer=self.pointer,
label=self.label,
key_association=copy(self._key_association),
)
forked_chunk.contents[self.ruamelindex(strictindex)] = new_value.as_marked_up()
forked_c... | def fork(self, strictindex, new_value) | Return a chunk referring to the same location in a duplicated document.
Used when modifying a YAML chunk so that the modification can be validated
before changing it. | 8.088015 | 7.030761 | 1.150375 |
if self.is_mapping():
for key, value in self.contents.items():
self.key(key, key).pointer.make_child_of(chunk.pointer)
self.val(key).make_child_of(chunk)
elif self.is_sequence():
for index, item in enumerate(self.contents):
... | def make_child_of(self, chunk) | Link one YAML chunk to another.
Used when inserting a chunk of YAML into another chunk. | 3.038475 | 2.916924 | 1.041671 |
return YAMLChunk(
self._ruamelparsed,
pointer=pointer,
label=self._label,
strictparsed=self._strictparsed,
key_association=copy(self._key_association),
) | def _select(self, pointer) | Get a YAMLChunk referenced by a pointer. | 15.147156 | 9.996222 | 1.515288 |
return self._select(self._pointer.index(self.ruamelindex(strictindex))) | def index(self, strictindex) | Return a chunk in a sequence referenced by index. | 22.849211 | 20.001774 | 1.142359 |
return (
self.key_association.get(strictindex, strictindex)
if self.is_mapping()
else strictindex
) | def ruamelindex(self, strictindex) | Get the ruamel equivalent of a strict parsed index.
E.g. 0 -> 0, 1 -> 2, parsed-via-slugify -> Parsed via slugify | 8.838279 | 9.607499 | 0.919935 |
ruamelkey = self.ruamelindex(strictkey)
return self._select(self._pointer.val(ruamelkey, strictkey)) | def val(self, strictkey) | Return a chunk referencing a value in a mapping with the key 'key'. | 14.256032 | 12.533983 | 1.13739 |
return self._select(self._pointer.key(key, strictkey)) | def key(self, key, strictkey=None) | Return a chunk referencing a key in a mapping with the name 'key'. | 11.997929 | 10.050747 | 1.193735 |
return self._select(self._pointer.textslice(start, end)) | def textslice(self, start, end) | Return a chunk referencing a slice of a scalar text value. | 11.728487 | 9.046867 | 1.296414 |
for x in items:
if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
for sub_x in flatten(x):
yield sub_x
else:
yield x | def flatten(items) | Yield items from any nested iterable.
>>> list(flatten([[1, 2, 3], [[4, 5], 6, 7]]))
[1, 2, 3, 4, 5, 6, 7] | 1.829987 | 2.307895 | 0.792925 |
chunks = []
start = 0
end = 0
for item in text.split(","):
space_increment = 1 if item[0] == " " else 0
start += space_increment # Is there a space after the comma to ignore? ", "
end += len(item.lstrip()) + space_increment
chunks.append((start, end))
start ... | def comma_separated_positions(text) | Start and end positions of comma separated text items.
Commas and trailing spaces should not be included.
>>> comma_separated_positions("ABC, 2,3")
[(0, 3), (5, 6), (7, 8)] | 4.4954 | 4.776556 | 0.941138 |
if isinstance(data, dict):
if len(data) == 0:
raise exceptions.CannotBuildDocumentsFromEmptyDictOrList(
"Document must be built with non-empty dicts and lists"
)
return CommentedMap(
[
(ruamel_structure(key), ruamel_structure(v... | def ruamel_structure(data, validator=None) | Take dicts and lists and return a ruamel.yaml style
structure of CommentedMaps, CommentedSeqs and
data.
If a validator is presented and the type is unknown,
it is checked against the validator to see if it will
turn it back in to YAML. | 2.589052 | 2.532845 | 1.022191 |
settings = _personal_settings().data
settings["engine"]["rewrite"] = True
_storybook(settings["engine"]).with_params(
**{"python version": settings["params"]["python version"]}
).only_uninherited().shortcut(*keywords).play() | def rbdd(*keywords) | Run story matching keywords and rewrite story if code changed. | 33.171799 | 29.009739 | 1.143471 |
_storybook({"rewrite": False}).in_filename(filename).with_params(
**{"python version": "2.7.14"}
).filter(
lambda story: not story.info.get("fails_on_python_2")
).ordered_by_name().play()
_storybook({"rewrite": False}).with_params(
**{"python version": "3.7.0"}
).in_fil... | def regressfile(filename) | Run all stories in filename 'filename' in python 2 and 3. | 7.553854 | 6.556505 | 1.152116 |
lint()
doctests()
storybook = _storybook({}).only_uninherited()
storybook.with_params(**{"python version": "2.7.14"}).filter(
lambda story: not story.info.get("fails_on_python_2")
).ordered_by_name().play()
storybook.with_params(**{"python version": "3.7.0"}).ordered_by_name().play(... | def regression() | Run regression testing - lint and then run all tests. | 10.66464 | 10.214753 | 1.044043 |
hitchpylibrarytoolkit.docgen(
_storybook({}), DIR.project, DIR.key / "story", DIR.gen
) | def docgen() | Build documentation. | 133.620636 | 128.469681 | 1.040095 |
from commandlib import Command
Command(DIR.gen.joinpath("py{0}".format(version), "bin", "python"))(
DIR.gen.joinpath("state", "examplepythoncode.py")
).in_dir(DIR.gen.joinpath("state")).run() | def rerun(version="3.7.0") | Rerun last example code block with specified version of python. | 9.836322 | 9.4488 | 1.041013 |
if schema is None:
schema = Any()
return schema(YAMLChunk(schema.to_yaml(data), label=label)) | def as_document(data, schema=None, label=u"<unicode string>") | Translate dicts/lists and scalar (string/bool/float/int/etc.) values into a
YAML object which can be dumped out. | 13.147665 | 12.204609 | 1.07727 |
return generic_load(
yaml_string, schema=schema, label=label, allow_flow_style=allow_flow_style
) | def dirty_load(
yaml_string, schema=None, label=u"<unicode string>", allow_flow_style=False
) | Parse the first YAML document in a string
and produce corresponding YAML object.
If allow_flow_style is set to True, then flow style is allowed. | 2.848286 | 3.381554 | 0.842301 |
return generic_load(yaml_string, schema=schema, label=label) | def load(yaml_string, schema=None, label=u"<unicode string>") | Parse the first YAML document in a string
and produce corresponding YAML object. | 4.507564 | 5.41784 | 0.831986 |
if isinstance(self._value, CommentedMap):
mapping = OrderedDict()
for key, value in self._value.items():
mapping[key.data] = value.data
return mapping
elif isinstance(self._value, CommentedSeq):
return [item.data for item in self._... | def data(self) | Returns raw data representation of the document or document segment.
Mappings are rendered as ordered dicts, sequences as lists and scalar values
as whatever the validator returns (int, string, etc.).
If no validators are used, scalar values are always returned as strings. | 2.44505 | 2.302618 | 1.061857 |
dumped = dump(self.as_marked_up(), Dumper=StrictYAMLDumper, allow_unicode=True)
return dumped if sys.version_info[0] == 3 else dumped.decode("utf8") | def as_yaml(self) | Render the YAML node and subnodes as string. | 6.991371 | 6.384092 | 1.095124 |
if isinstance(self._value, CommentedMap):
raise TypeError("{0} is a mapping, has no text value.".format(repr(self)))
if isinstance(self._value, CommentedSeq):
raise TypeError("{0} is a sequence, has no text value.".format(repr(self)))
return self._text | def text(self) | Return string value of scalar, whatever value it was parsed as. | 3.19191 | 2.866465 | 1.113535 |
# In python2, ast.parse(text_string_with_encoding_pragma) raises
# SyntaxError: encoding declaration in Unicode string
ast_obj = ast.parse(src.encode('UTF-8'))
visitor = TopLevelImportVisitor()
visitor.visit(ast_obj)
line_offsets = get_line_offsets_by_line_no(src)
chunks = []
star... | def partition_source(src) | Partitions source into a list of `CodePartition`s for import
refactoring. | 3.194907 | 3.145165 | 1.015815 |
def _inner():
for partition in partitions:
if partition.code_type is CodeType.IMPORT:
import_obj = import_obj_from_str(partition.src)
if import_obj.has_multiple_imports:
for new_import_obj in import_obj.split_imports():
... | def separate_comma_imports(partitions) | Turns `import a, b` into `import a` and `import b` | 3.572402 | 3.443955 | 1.037296 |
parts = s.split('.')
for i in range(1, len(parts)):
yield '.'.join(parts[:i]) | def _module_to_base_modules(s) | return all module names that would be imported due to this
import-import | 2.913912 | 3.047305 | 0.956226 |
condlist = [input <= threshold for threshold in thresholds]
if len(condlist) == len(choices) - 1:
# If a choice is provided for input > highest threshold, last condition must be true to return it.
condlist += [True]
assert len(condlist) == len(choices), \
"apply_thresholds must ... | def apply_thresholds(input, thresholds, choices) | Return one of the choices depending on the input position compared to thresholds, for each input.
>>> apply_thresholds(np.array([4]), [5, 7], [10, 15, 20])
array([10])
>>> apply_thresholds(np.array([5]), [5, 7], [10, 15, 20])
array([10])
>>> apply_thresholds(np.array([6]), [5, 7], [10, 15, 20])
... | 5.12275 | 5.679704 | 0.90194 |
if not os.path.exists(file_path):
raise ValueError("{} doest not exist".format(file_path))
if os.path.isdir(file_path):
return ParameterNode(name, directory_path = file_path)
data = _load_yaml_file(file_path)
return _parse_child(name, data, file_path) | def load_parameter_file(file_path, name = '') | Load parameters from a YAML file (or a directory containing YAML files).
:returns: An instance of :any:`ParameterNode` or :any:`Scale` or :any:`Parameter`. | 3.368314 | 3.141247 | 1.072285 |
if period is not None:
if start is not None or stop is not None:
raise TypeError("Wrong input for 'update' method: use either 'update(period, value = value)' or 'update(start = start, stop = stop, value = value)'. You cannot both use 'period' and 'start' or 'stop'.")
... | def update(self, period = None, start = None, stop = None, value = None) | Change the value for a given period.
:param period: Period where the value is modified. If set, `start` and `stop` should be `None`.
:param start: Start of the period. Instance of `openfisca_core.periods.Instant`. If set, `period` should be `None`.
:param stop: Stop of the period. Instance of `... | 2.809549 | 2.77398 | 1.012822 |
for child_name, child in other.children.items():
self.add_child(child_name, child) | def merge(self, other) | Merges another ParameterNode into the current node.
In case of child name conflict, the other node child will replace the current node child. | 3.381519 | 2.938192 | 1.150884 |
if name in self.children:
raise ValueError("{} has already a child named {}".format(self.name, name))
if not (isinstance(child, ParameterNode) or isinstance(child, Parameter) or isinstance(child, Scale)):
raise TypeError("child must be of type ParameterNode, Parameter, o... | def add_child(self, name, child) | Add a new child to the node.
:param name: Name of the child that must be used to access that child. Should not contain anything that could interfere with the operator `.` (dot).
:param child: The new child, an instance of :any:`Scale` or :any:`Parameter` or :any:`ParameterNode`. | 2.790143 | 2.25925 | 1.234987 |
MESSAGE_PART_1 = "Cannot use fancy indexing on parameter node '{}', as"
MESSAGE_PART_3 = "To use fancy indexing on parameter node, its children must be homogenous."
MESSAGE_PART_4 = "See more at <https://openfisca.org/doc/coding-the-legislation/legislation_parameters#computing-a-paramet... | def check_node_vectorisable(node) | Check that a node can be casted to a vectorial node, in order to be able to use fancy indexing. | 2.672922 | 2.623215 | 1.018949 |
name = variable.__name__
if self.variables.get(name) is not None:
del self.variables[name]
self.load_variable(variable, update = False) | def replace_variable(self, variable) | Replaces an existing OpenFisca variable in the tax and benefit system by a new one.
The new variable must have the same name than the replaced one.
If no variable with the given name exists in the tax and benefit system, no error will be raised and the new variable will be simply added.
:para... | 4.886647 | 6.501171 | 0.751656 |
try:
file_name = path.splitext(path.basename(file_path))[0]
# As Python remembers loaded modules by name, in order to prevent collisions, we need to make sure that:
# - Files with the same name, but located in different directories, have a different module names. ... | def add_variables_from_file(self, file_path) | Adds all OpenFisca variables contained in a given file to the tax and benefit system. | 4.935579 | 4.493096 | 1.098481 |
py_files = glob.glob(path.join(directory, "*.py"))
for py_file in py_files:
self.add_variables_from_file(py_file)
subdirectories = glob.glob(path.join(directory, "*/"))
for subdirectory in subdirectories:
self.add_variables_from_directory(subdirectory) | def add_variables_from_directory(self, directory) | Recursively explores a directory, and adds all OpenFisca variables found there to the tax and benefit system. | 2.022274 | 2.07724 | 0.973539 |
# Load extension from installed pip package
try:
package = importlib.import_module(extension)
extension_directory = package.__path__[0]
except ImportError:
message = linesep.join([traceback.format_exc(),
'Error load... | def load_extension(self, extension) | Loads an extension to the tax and benefit system.
:param string extension: The extension to load. Can be an absolute path pointing to an extension directory, or the name of an OpenFisca extension installed as a pip package. | 5.253275 | 4.856853 | 1.081621 |
from openfisca_core.reforms import Reform
try:
reform_package, reform_name = reform_path.rsplit('.', 1)
except ValueError:
raise ValueError('`{}` does not seem to be a path pointing to a reform. A path looks like `some_country_package.reforms.some_reform.`'.forma... | def apply_reform(self, reform_path) | Generates a new tax and benefit system applying a reform to the tax and benefit system.
The current tax and benefit system is **not** mutated.
:param string reform_path: The reform to apply. Must respect the format *installed_package.sub_module.reform*
:returns: A reformed tax and benefit sys... | 3.299459 | 3.243202 | 1.017346 |
variables = self.variables
found = variables.get(variable_name)
if not found and check_existence:
raise VariableNotFound(variable_name, self)
return found | def get_variable(self, variable_name, check_existence = False) | Get a variable from the tax and benefit system.
:param variable_name: Name of the requested variable.
:param check_existence: If True, raise an error if the requested variable does not exist. | 3.099923 | 4.146153 | 0.747663 |
self.variables[variable_name] = get_neutralized_variable(self.get_variable(variable_name)) | def neutralize_variable(self, variable_name) | Neutralizes an OpenFisca variable existing in the tax and benefit system.
A neutralized variable always returns its default value when computed.
Trying to set inputs for a neutralized variable has no effect except raising a warning. | 4.18548 | 4.524904 | 0.924988 |
parameters = ParameterNode('', directory_path = path_to_yaml_dir)
if self.preprocess_parameters is not None:
parameters = self.preprocess_parameters(parameters)
self.parameters = parameters | def load_parameters(self, path_to_yaml_dir) | Loads the legislation parameter for a directory containing YAML parameters files.
:param path_to_yaml_dir: Absolute path towards the YAML parameter directory.
Example:
>>> self.load_parameters('/path/to/yaml/parameters/dir') | 5.1062 | 6.439757 | 0.792918 |
if isinstance(instant, periods.Period):
instant = instant.start
elif isinstance(instant, (str, int)):
instant = periods.instant(instant)
else:
assert isinstance(instant, periods.Instant), "Expected an Instant (e.g. Instant((2017, 1, 1)) ). Got: {}.".f... | def get_parameters_at_instant(self, instant) | Get the parameters of the legislation at a given instant
:param instant: string of the format 'YYYY-MM-DD' or `openfisca_core.periods.Instant` instance.
:returns: The parameters of the legislation at a given instant.
:rtype: :any:`ParameterNodeAtInstant` | 3.027206 | 2.730723 | 1.108573 |
# Handle reforms
if self.baseline:
return self.baseline.get_package_metadata()
fallback_metadata = {
'name': self.__class__.__name__,
'version': '',
'repository_url': '',
'location': '',
}
module = inspect... | def get_package_metadata(self) | Gets metatada relative to the country package the tax and benefit system is built from.
:returns: Country package metadata
:rtype: dict
Example:
>>> tax_benefit_system.get_package_metadata()
>>> {
>>> 'location': '/path/to/dir/containing/pack... | 3.065089 | 2.944355 | 1.041005 |
if not entity:
return self.variables
else:
return {
variable_name: variable
for variable_name, variable in self.variables.items()
# TODO - because entities are copied (see constructor) they can't be compared
... | def get_variables(self, entity = None) | Gets all variables contained in a tax and benefit system.
:param <Entity subclass> entity: If set, returns only the variable defined for the given entity.
:returns: A dictionnary, indexed by variable names.
:rtype: dict | 5.589043 | 5.688595 | 0.9825 |
input_dict = self.explicit_singular_entities(tax_benefit_system, input_dict)
if any(key in tax_benefit_system.entities_plural() for key in input_dict.keys()):
return self.build_from_entities(tax_benefit_system, input_dict)
else:
return self.build_from_variables(... | def build_from_dict(self, tax_benefit_system, input_dict) | Build a simulation from ``input_dict``
This method uses :any:`build_from_entities` if entities are fully specified, or :any:`build_from_variables` if not.
:param dict input_dict: A dict represeting the input of the simulation
:return: A :any:`Simulation` | 3.266781 | 3.124174 | 1.045646 |
input_dict = deepcopy(input_dict)
simulation = Simulation(tax_benefit_system, tax_benefit_system.instantiate_entities())
# Register variables so get_variable_entity can find them
for (variable_name, _variable) in tax_benefit_system.variables.items():
self.register_... | def build_from_entities(self, tax_benefit_system, input_dict) | Build a simulation from a Python dict ``input_dict`` fully specifying entities.
Examples:
>>> simulation_builder.build_from_entities({
'persons': {'Javier': { 'salary': {'2018-11': 2000}}},
'households': {'household': {'parents': ['Javier']}}
}) | 3.7659 | 3.708328 | 1.015525 |
count = _get_person_count(input_dict)
simulation = self.build_default_simulation(tax_benefit_system, count)
for variable, value in input_dict.items():
if not isinstance(value, dict):
if self.default_period is None:
raise SituationParsingEr... | def build_from_variables(self, tax_benefit_system, input_dict) | Build a simulation from a Python dict ``input_dict`` describing variables values without expliciting entities.
This method uses :any:`build_default_simulation` to infer an entity structure
Example:
>>> simulation_builder.build_from_variables(
{'salary': {'2016-10':... | 4.806546 | 4.471941 | 1.074823 |
simulation = Simulation(tax_benefit_system, tax_benefit_system.instantiate_entities())
for population in simulation.populations.values():
population.count = count
population.ids = np.array(range(count))
if not population.entity.is_person:
pop... | def build_default_simulation(self, tax_benefit_system, count = 1) | Build a simulation where:
- There are ``count`` persons
- There are ``count`` instances of each group entity, containing one person
- Every person has, in each entity, the first role | 6.747325 | 6.161342 | 1.095107 |
singular_keys = set(input_dict).intersection(tax_benefit_system.entities_by_singular())
if not singular_keys:
return input_dict
result = {
entity_id: entity_description
for (entity_id, entity_description) in input_dict.items()
if entity_... | def explicit_singular_entities(self, tax_benefit_system, input_dict) | Preprocess ``input_dict`` to explicit entities defined using the single-entity shortcut
Example:
>>> simulation_builder.explicit_singular_entities(
{'persons': {'Javier': {}, }, 'household': {'parents': ['Javier']}}
)
>>> {'persons': {'Javier': {}}, ... | 3.195752 | 3.348786 | 0.954302 |
check_type(instances_json, dict, [entity.plural])
entity_ids = list(map(str, instances_json.keys()))
self.persons_plural = entity.plural
self.entity_ids[self.persons_plural] = entity_ids
self.entity_counts[self.persons_plural] = len(entity_ids)
for instance_id, ... | def add_person_entity(self, entity, instances_json) | Add the simulation's instances of the persons entity as described in ``instances_json``. | 3.599334 | 3.414491 | 1.054135 |
check_type(instances_json, dict, [entity.plural])
entity_ids = list(map(str, instances_json.keys()))
self.entity_ids[entity.plural] = entity_ids
self.entity_counts[entity.plural] = len(entity_ids)
persons_count = len(persons_ids)
persons_to_allocate = set(perso... | def add_group_entity(self, persons_plural, persons_ids, entity, instances_json) | Add all instances of one of the model's entities as described in ``instances_json``. | 2.537916 | 2.530896 | 1.002774 |
combined_tax_scales = None
for child_name in node:
child = node[child_name]
if not isinstance(child, AbstractTaxScale):
log.info('Skipping {} with value {} because it is not a tax scale'.format(child_name, child))
continue
if combined_tax_scales is None:
... | def combine_tax_scales(node) | Combine all the MarginalRateTaxScales in the node into a single MarginalRateTaxScale. | 3.179197 | 2.74806 | 1.156888 |
# threshold : threshold of brut revenue
# net_threshold: threshold of net revenue
# theta : ordonnée à l'origine des segments des différents seuils dans une
# représentation du revenu imposable comme fonction linéaire par
# morceaux du revenu brut
... | def inverse(self) | Returns a new instance of MarginalRateTaxScale
Invert a taxscale:
Assume tax_scale composed of bracket which thresholds are expressed in term of brut revenue.
The inverse is another MarginalTaxSclae which thresholds are expressed in terms of net revenue.
If net = revbrut - ... | 9.209588 | 7.623271 | 1.208089 |
assert isinstance(factor, (float, int))
scaled_tax_scale = self.copy()
return scaled_tax_scale.multiply_thresholds(factor) | def scale_tax_scales(self, factor) | Scale all the MarginalRateTaxScales in the node. | 5.573665 | 5.368193 | 1.038276 |
if type(array) is EnumArray:
return array
if array.dtype.kind in {'U', 'S'}: # String array
array = np.select([array == item.name for item in cls], [item.index for item in cls]).astype(ENUM_ARRAY_DTYPE)
elif array.dtype.kind == 'O': # Enum items arrays
... | def encode(cls, array) | Encode a string numpy array, or an enum item numpy array, into an :any:`EnumArray`. See :any:`EnumArray.decode` for decoding.
:param numpy.ndarray array: Numpy array of string identifiers, or of enum items, to encode.
:returns: An :any:`EnumArray` encoding the input array values.
:... | 6.83665 | 6.445151 | 1.060743 |
return np.select([self == item.index for item in self.possible_values], [item for item in self.possible_values]) | def decode(self) | Return the array of enum items corresponding to self
>>> enum_array = household('housing_occupancy_status', period)
>>> enum_array[0]
>>> 2 # Encoded value
>>> enum_array.decode()[0]
>>> <HousingOccupancyStatus.free_lodger: 'Free lodger'> # Decoded value : ... | 9.33155 | 9.110715 | 1.024239 |
return np.select([self == item.index for item in self.possible_values], [item.name for item in self.possible_values]) | def decode_to_str(self) | Return the array of string identifiers corresponding to self
>>> enum_array = household('housing_occupancy_status', period)
>>> enum_array[0]
>>> 2 # Encoded value
>>> enum_array.decode_to_str()[0]
>>> 'free_lodger' # String identifier | 7.206234 | 7.600552 | 0.94812 |
key = self._get_key(variable_name, period, **parameters)
if self.stack: # The variable is a dependency of another variable
parent = self.stack[-1]
self.trace[parent]['dependencies'].append(key)
else: # The variable has been requested by the client
... | def record_calculation_start(self, variable_name, period, **parameters) | Record that OpenFisca started computing a variable.
:param str variable_name: Name of the variable starting to be computed
:param Period period: Period for which the variable is being computed
:param list parameters: Parameter with which the variable is being computed | 4.580722 | 5.027543 | 0.911125 |
key = self._get_key(variable_name, period, **parameters)
expected_key = self.stack.pop()
if not key == expected_key:
raise ValueError(
"Something went wrong with the simulation tracer: result of '{0}' was expected, got results for '{1}' instead. This does no... | def record_calculation_end(self, variable_name, period, result, **parameters) | Record that OpenFisca finished computing a variable.
:param str variable_name: Name of the variable starting to be computed
:param Period period: Period for which the variable is being computed
:param numpy.ndarray result: Result of the computation
:param list parameters... | 7.171857 | 7.723878 | 0.928531 |
key = self._get_key(variable_name, period)
def _print_details(key, depth):
if depth > 0 and ignore_zero and np.all(self.trace[key]['value'] == 0):
return
yield self._print_node(key, depth, aggregate)
if depth < max_depth:
for ... | def print_trace(self, variable_name, period, max_depth = 1, aggregate = False, ignore_zero = False) | Print value, the dependencies, and the dependencies values of the variable for the given period (and possibly the given set of extra parameters).
:param str variable_name: Name of the variable to investigate
:param Period period: Period to investigate
:param int max_depth: Maximum l... | 3.172595 | 3.422118 | 0.927085 |
for line in self.computation_log(aggregate):
print(line) | def print_computation_log(self, aggregate = False) | Print the computation log of a simulation.
If ``aggregate`` is ``False`` (default), print the value of each computed vector.
If ``aggregate`` is ``True``, only print the minimum, maximum, and average value of each computed vector.
This mode is more suited for simulations on a large... | 6.867529 | 9.357975 | 0.733869 |
simulation = Simulation(tax_benefit_system = tax_benefit_system, **kwargs)
simulation.persons.ids = np.arange(nb_persons)
simulation.persons.count = nb_persons
adults = [0] + sorted(random.sample(range(1, nb_persons), nb_groups - 1))
members_entity_id = np.empty(nb_persons, dtype = int)
#... | def make_simulation(tax_benefit_system, nb_persons, nb_groups, **kwargs) | Generate a simulation containing nb_persons persons spread in nb_groups groups.
Example:
>>> from openfisca_core.scripts.simulation_generator import make_simulation
>>> from openfisca_france import CountryTaxBenefitSystem
>>> tbs = CountryTaxBenefitSystem()
>>> simulation = mak... | 3.690534 | 3.956117 | 0.932868 |
if condition is None:
condition = True
variable = simulation.tax_benefit_system.get_variable(variable_name)
population = simulation.get_variable_population(variable_name)
value = (np.random.rand(population.count) * max_value * condition).astype(variable.dtype)
simulation.set_input(varia... | def randomly_init_variable(simulation, variable_name, period, max_value, condition = None) | Initialise a variable with random values (from 0 to max_value) for the given period.
If a condition vector is provided, only set the value of persons or groups for which condition is True.
Example:
>>> from openfisca_core.scripts.simulation_generator import make_simulation, randomly_init_varia... | 4.626046 | 5.089206 | 0.908992 |
baseline_parameters = self.baseline.parameters
baseline_parameters_copy = copy.deepcopy(baseline_parameters)
reform_parameters = modifier_function(baseline_parameters_copy)
if not isinstance(reform_parameters, ParameterNode):
return ValueError(
'modif... | def modify_parameters(self, modifier_function) | Make modifications on the parameters of the legislation
Call this function in `apply()` if the reform asks for legislation parameter modifications.
:param modifier_function: A function that takes an object of type :any:`ParameterNode` and should return an object of the same type. | 4.513614 | 3.962288 | 1.139143 |
if instant is None:
return None
if isinstance(instant, Instant):
return instant
if isinstance(instant, str):
if not INSTANT_PATTERN.match(instant):
raise ValueError("'{}' is not a valid instant. Instants are described using the 'YYYY-MM-DD' format, for instance '2015... | def instant(instant) | Return a new instant, aka a triple of integers (year, month, day).
>>> instant(2014)
Instant((2014, 1, 1))
>>> instant('2014')
Instant((2014, 1, 1))
>>> instant('2014-02')
Instant((2014, 2, 1))
>>> instant('2014-3-2')
Instant((2014, 3, 2))
>>> instant(instant('2014-3-2'))
Instan... | 2.4638 | 2.38875 | 1.031418 |
if isinstance(value, Period):
return value
if isinstance(value, Instant):
return Period((DAY, value, 1))
def parse_simple_period(value):
try:
date = datetime.datetime.strptime(value, '%Y')
except ValueError:
try:
date = ... | def period(value) | Return a new period, aka a triple (unit, start_instant, size).
>>> period('2014')
Period((YEAR, Instant((2014, 1, 1)), 1))
>>> period('year:2014')
Period((YEAR, Instant((2014, 1, 1)), 1))
>>> period('2014-2')
Period((MONTH, Instant((2014, 2, 1)), 1))
>>> period('2014-02')
Period((MONTH... | 3.181535 | 2.95307 | 1.077365 |
unit, start, size = period
return '{}_{}'.format(unit_weight(unit), size) | def key_period_size(period) | Defines a key in order to sort periods by length. It uses two aspects : first unit then size
:param period: an OpenFisca period
:return: a string
>>> key_period_size(period('2014'))
'2_1'
>>> key_period_size(period('2013'))
'2_1'
>>> key_period_size(period('2014-01'))
'1_1' | 26.468252 | 23.631594 | 1.120037 |
instant_date = date_by_instant_cache.get(self)
if instant_date is None:
date_by_instant_cache[self] = instant_date = datetime.date(*self)
return instant_date | def date(self) | Convert instant to a date.
>>> instant(2014).date
datetime.date(2014, 1, 1)
>>> instant('2014-2').date
datetime.date(2014, 2, 1)
>>> instant('2014-2-3').date
datetime.date(2014, 2, 3) | 4.654008 | 5.529907 | 0.841607 |
assert unit in (DAY, MONTH, YEAR), 'Invalid unit: {} of type {}'.format(unit, type(unit))
assert isinstance(size, int) and size >= 1, 'Invalid size: {} of type {}'.format(size, type(size))
return Period((unit, self, size)) | def period(self, unit, size = 1) | Create a new period starting at instant.
>>> instant(2014).period('month')
Period(('month', Instant((2014, 1, 1)), 1))
>>> instant('2014-2').period('year', 2)
Period(('year', Instant((2014, 2, 1)), 2))
>>> instant('2014-2-3').period('day', size = 2)
Period(('day', Instan... | 3.235071 | 2.90682 | 1.112924 |
if unit_weight(self.unit) < unit_weight(unit):
raise ValueError('Cannot subdivide {0} into {1}'.format(self.unit, unit))
if unit == YEAR:
return [self.this_year.offset(i, YEAR) for i in range(self.size)]
if unit == MONTH:
return [self.first_month.of... | def get_subperiods(self, unit) | Return the list of all the periods of unit ``unit`` contained in self.
Examples:
>>> period('2017').get_subperiods(MONTH)
>>> [period('2017-01'), period('2017-02'), ... period('2017-12')]
>>> period('year:2014:2').get_subperiods(YEAR)
>>> [period('2014'), p... | 2.738005 | 2.84387 | 0.962774 |
if not isinstance(other, Period):
other = period(other)
return self.start <= other.start and self.stop >= other.stop | def contains(self, other) | Returns ``True`` if the period contains ``other``. For instance, ``period(2015)`` contains ``period(2015-01)`` | 3.80454 | 2.902289 | 1.310876 |
if (self[0] == MONTH):
return self[2]
if(self[0] == YEAR):
return self[2] * 12
raise ValueError("Cannot calculate number of months in {0}".format(self[0])) | def size_in_months(self) | Return the size of the period in months.
>>> period('month', '2012-2-29', 4).size_in_months
4
>>> period('year', '2012', 1).size_in_months
12 | 4.679566 | 4.718817 | 0.991682 |
unit, instant, length = self
if unit == DAY:
return length
if unit in [MONTH, YEAR]:
last_day = self.start.offset(length, unit).offset(-1, DAY)
return (last_day.date - self.start.date).days + 1
raise ValueError("Cannot calculate number of da... | def size_in_days(self) | Return the size of the period in days.
>>> period('month', '2012-2-29', 4).size_in_days
28
>>> period('year', '2012', 1).size_in_days
366 | 6.376077 | 7.776191 | 0.819949 |
unit, start_instant, size = self
year, month, day = start_instant
if unit == ETERNITY:
return Instant((float("inf"), float("inf"), float("inf")))
if unit == 'day':
if size > 1:
day += size - 1
month_last_day = calendar.mont... | def stop(self) | Return the last day of the period as an Instant instance.
>>> period('year', 2014).stop
Instant((2014, 12, 31))
>>> period('month', 2014).stop
Instant((2014, 12, 31))
>>> period('day', 2014).stop
Instant((2014, 12, 31))
>>> period('year', '2012-2-29').stop
... | 2.232918 | 2.075705 | 1.07574 |
result = variable.clone()
result.is_neutralized = True
result.label = '[Neutralized]' if variable.label is None else '[Neutralized] {}'.format(variable.label),
return result | def get_neutralized_variable(variable) | Return a new neutralized variable (to be used by reforms).
A neutralized variable always returns its default value, and does not cache anything. | 6.251149 | 5.97042 | 1.04702 |
def raise_error():
raise ValueError(
'Unrecognized formula name in variable "{}". Expecting "formula_YYYY" or "formula_YYYY_MM" or "formula_YYYY_MM_DD where YYYY, MM and DD are year, month and day. Found: "{}".'
.format(self.name, attribute_name))
i... | def parse_formula_name(self, attribute_name) | Returns the starting date of a formula based on its name.
Valid dated name formats are : 'formula', 'formula_YYYY', 'formula_YYYY_MM' and 'formula_YYYY_MM_DD' where YYYY, MM and DD are a year, month and day.
By convention, the starting date of:
- `formula` is `0001-01-01` (minimal date in ... | 3.324126 | 2.915635 | 1.140104 |
comments = inspect.getcomments(cls)
# Handle dynamically generated variable classes or Jupyter Notebooks, which have no source.
try:
absolute_file_path = inspect.getsourcefile(cls)
except TypeError:
source_file_path = None
else:
sourc... | def get_introspection_data(cls, tax_benefit_system) | Get instrospection data about the code of the variable.
:returns: (comments, source file path, source code, start line number)
:rtype: tuple | 3.761736 | 3.326539 | 1.130826 |
if not self.formulas:
return None
if period is None:
return self.formulas.peekitem(index = 0)[1] # peekitem gets the 1st key-value tuple (the oldest start_date and formula). Return the formula.
if isinstance(period, periods.Period):
instant = peri... | def get_formula(self, period = None) | Returns the formula used to compute the variable at the given period.
If no period is given and the variable has several formula, return the oldest formula.
:returns: Formula used to compute the variable
:rtype: function | 4.507066 | 4.260242 | 1.057937 |
self.entity.check_role_validity(role)
group_population = self.simulation.get_population(role.entity.plural)
if role.subroles:
return np.logical_or.reduce([group_population.members_role == subrole for subrole in role.subroles])
else:
return group_populatio... | def has_role(self, role) | Check if a person has a given role within its :any:`GroupEntity`
Example:
>>> person.has_role(Household.CHILD)
>>> array([False]) | 6.044673 | 6.367808 | 0.949255 |
# If entity is for instance 'person.household', we get the reference entity 'household' behind the projector
entity = entity if not isinstance(entity, Projector) else entity.reference_entity
positions = entity.members_position
biggest_entity_size = np.max(positions) + 1
... | def get_rank(self, entity, criteria, condition = True) | Get the rank of a person within an entity according to a criteria.
The person with rank 0 has the minimum value of criteria.
If condition is specified, then the persons who don't respect it are not taken into account and their rank is -1.
Example:
>>> age = person('age', period) # e.g... | 6.67801 | 6.281248 | 1.063166 |
if self._ordered_members_map is None:
return np.argsort(self.members_entity_id)
return self._ordered_members_map | def ordered_members_map(self) | Mask to group the persons by entity
This function only caches the map value, to see what the map is used for, see value_nth_person method. | 5.087168 | 3.695176 | 1.376705 |
self.entity.check_role_validity(role)
self.members.check_array_compatible_with_entity(array)
if role is not None:
role_filter = self.members.has_role(role)
return np.bincount(
self.members_entity_id[role_filter],
weights = array[ro... | def sum(self, array, role = None) | Return the sum of ``array`` for the members of the entity.
``array`` must have the dimension of the number of persons in the simulation
If ``role`` is provided, only the entity member with the given role are taken into account.
Example:
>>> salaries = household.member... | 4.031557 | 3.786839 | 1.064623 |
sum_in_entity = self.sum(array, role = role)
return (sum_in_entity > 0) | def any(self, array, role = None) | Return ``True`` if ``array`` is ``True`` for any members of the entity.
``array`` must have the dimension of the number of persons in the simulation
If ``role`` is provided, only the entity member with the given role are taken into account.
Example:
>>> salaries = hou... | 6.40965 | 12.286271 | 0.521692 |
return self.reduce(array, reducer = np.logical_and, neutral_element = True, role = role) | def all(self, array, role = None) | Return ``True`` if ``array`` is ``True`` for all members of the entity.
``array`` must have the dimension of the number of persons in the simulation
If ``role`` is provided, only the entity member with the given role are taken into account.
Example:
>>> salaries = hou... | 10.945863 | 16.496422 | 0.66353 |
return self.reduce(array, reducer = np.maximum, neutral_element = - np.infty, role = role) | def max(self, array, role = None) | Return the maximum value of ``array`` for the entity members.
``array`` must have the dimension of the number of persons in the simulation
If ``role`` is provided, only the entity member with the given role are taken into account.
Example:
>>> salaries = household.mem... | 11.214331 | 16.349531 | 0.685911 |
return self.reduce(array, reducer = np.minimum, neutral_element = np.infty, role = role) | def min(self, array, role = None) | Return the minimum value of ``array`` for the entity members.
``array`` must have the dimension of the number of persons in the simulation
If ``role`` is provided, only the entity member with the given role are taken into account.
Example:
>>> salaries = household.mem... | 9.753899 | 13.039447 | 0.74803 |
if role:
if role.subroles:
role_condition = np.logical_or.reduce([self.members_role == subrole for subrole in role.subroles])
else:
role_condition = self.members_role == role
return self.sum(role_condition)
else:
re... | def nb_persons(self, role = None) | Returns the number of persons contained in the entity.
If ``role`` is provided, only the entity member with the given role are taken into account. | 4.210663 | 4.222682 | 0.997154 |
self.entity.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.members.check_array_compa... | 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 | 5.970808 | 5.837087 | 1.022909 |
self.members.check_array_compatible_with_entity(array)
positions = self.members_position
nb_persons_per_entity = self.nb_persons()
members_map = self.ordered_members_map
result = self.filled_array(default, dtype = array.dtype)
# For households that have at least ... | def value_nth_person(self, n, array, default = 0) | Get the value of array for the person whose position in the entity is n.
Note that this position is arbitrary, and that members are not sorted.
If the nth person does not exist, return ``default`` instead.
The result is a vector which dimension is the number of entities. | 9.075412 | 8.417521 | 1.078157 |
if self._data_storage_dir is None:
self._data_storage_dir = tempfile.mkdtemp(prefix = "openfisca_")
log.warn((
"Intermediate results will be stored on disk in {} in case of memory overflow. "
"You should remove this directory once you're done with... | def data_storage_dir(self) | Temporary folder used to store intermediate calculation data in case the memory is saturated | 4.699297 | 3.846897 | 1.221581 |
population = self.get_variable_population(variable_name)
holder = population.get_holder(variable_name)
variable = self.tax_benefit_system.get_variable(variable_name, check_existence = True)
if period is not None and not isinstance(period, periods.Period):
period = p... | def calculate(self, variable_name, period, **parameters) | Calculate the variable ``variable_name`` for the period ``period``, using the variable formula if it exists.
:returns: A numpy array containing the result of the calculation | 3.982761 | 4.02391 | 0.989774 |
variable = self.tax_benefit_system.get_variable(variable_name, check_existence = True)
if variable.calculate_output is None:
return self.calculate(variable_name, period)
return variable.calculate_output(self, variable_name, period) | def calculate_output(self, variable_name, period) | Calculate the value of a variable using the ``calculate_output`` attribute of the variable. | 4.748347 | 4.227147 | 1.123298 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.