def calculate(simulations, decomposition_json): def new_test_case_array(holder, array): entity_step_size = holder.entity.step_size return array.reshape([holder.simulation.steps_count, entity_step_size]).sum(1) response_json = copy.deepcopy( decomposition_json) # Use decomposition as a skeleton for response. for node in iter_decomposition_nodes(response_json, children_first=True): children = node.get('children') if children: node['values'] = map(lambda *l: sum(l), *(child['values'] for child in children)) else: node['values'] = values = [] for simulation_index, simulation in enumerate(simulations): try: try: array = simulation.calculate_output( node['code'], simulation.period) except ValueError: array = simulation.calculate_add( node['code'], simulation.period) except parameters.ParameterNotFound as exc: exc.simulation_index = simulation_index raise holder = simulation.get_holder(node['code']) column = make_column_from_variable(holder.variable) values.extend( column.transform_value_to_json(value) for value in new_test_case_array(holder, array).tolist()) return response_json
def to_value_json(self, use_label=False): column = make_column_from_variable(self.variable) transform_dated_value_to_json = column.transform_dated_value_to_json def extra_params_to_json_key(extra_params, period): return '{' + ', '.join([ '{}: {}'.format(name, value) for name, value in zip( self.get_extra_param_names(period), extra_params) ]) + '}' if self.variable.definition_period == ETERNITY: array = self.get_array(None) if array is None: return None return [ transform_dated_value_to_json(cell, use_label=use_label) for cell in array.tolist() ] value_json = {} for period, array_or_dict in self._memory_storage._arrays.items(): if type(array_or_dict) == dict: values_dict = {} for extra_params, array in array_or_dict.items(): extra_params_key = extra_params_to_json_key( extra_params, period) values_dict[str(extra_params_key)] = [ transform_dated_value_to_json(cell, use_label=use_label) for cell in array.tolist() ] value_json[str(period)] = values_dict else: value_json[str(period)] = [ transform_dated_value_to_json(cell, use_label=use_label) for cell in array_or_dict.tolist() ] if self._disk_storage: for period, file_or_dict in self._disk_storage._files.items(): if type(file_or_dict) == dict: values_dict = {} for extra_params, file in file_or_dict.items(): extra_params_key = extra_params_to_json_key( extra_params, period) values_dict[str(extra_params_key)] = [ transform_dated_value_to_json(cell, use_label=use_label) for cell in np.load(file).tolist() ] value_json[str(period)] = values_dict else: value_json[str(period)] = [ transform_dated_value_to_json(cell, use_label=use_label) for cell in np.load(file_or_dict).tolist() ] return value_json
def map_path_to_swagger(variable): column = columns.make_column_from_variable(variable) column_json = column.to_json() result = map_path_base_to_swagger(column_json) result['responses'] = make_responses_for(column_json) try: result['parameters'] = get_parameters(column) except Exception, e: print('Error mapping parameters of formula "{}":'.format(column.to_json().get('name'))) # noqa print(e) # noqa
def check_variable(value, key): variable = tax_benefit_system.variables[key] column = make_column_from_variable(variable) if column.entity != entity_class: raise ValueError( "Variable {} is defined for entity {}. It cannot be set for entity {}." .format(key, column.entity.key, entity_class.key).encode('utf-8')) value, error = column.json_to_python(value) if error is not None: raise ValueError( "Invalid value {} for variable {}. Error: {}".format( value, key, error).encode('utf-8')) entity_json[key] = value
def normalize_param(name, value, tax_benefit_system): variable = tax_benefit_system.get_variable(name, check_existence = True) column = columns.make_column_from_variable(variable) result, error = conv.pipe( column.input_to_dated_python # if column is not a date, this will be None and conv.pipe will be pass-through )(value) if error is not None: raise Exception(dict( code = 400, message = u"Parameter '{}' could not be normalized: {}".format(name, error) )) return result
def normalize_param(name, value, tax_benefit_system): variable = tax_benefit_system.get_variable(name, check_existence=True) column = columns.make_column_from_variable(variable) result, error = conv.pipe( column. input_to_dated_python # if column is not a date, this will be None and conv.pipe will be pass-through )(value) if error is not None: raise Exception( dict(code=400, message=u"Parameter '{}' could not be normalized: {}".format( name, error))) return result
def map_parameter_to_swagger(variable): column = columns.make_column_from_variable(variable) column_json = column.to_json() result = map_type_to_swagger(column_json.get('@type')) if column_json.get('labels'): result['enum'] = {i[0]: i[1] for i in column_json.get('labels').items()} result.update({ 'name': column_json.get('name'), 'description': column_json.get('label'), 'default': get_default_value(column, column_json), 'in': 'query' }) return result
def json_or_python_to_array_by_period_by_variable_name(value, state=None): if value is None: return value, None if state is None: state = conv.default_state error_by_variable_name = {} array_by_period_by_variable_name = collections.OrderedDict() for variable_name, variable_value in value.items(): column = tax_benefit_system.get_variable(variable_name) if isinstance(variable_value, np.ndarray): variable_array_by_period = {period: variable_value} else: variable_array_by_period, error = columns.make_column_from_variable( column).make_json_to_array_by_period(period)( variable_value, state=state) if error is not None: error_by_variable_name[variable_name] = error if variable_array_by_period is not None: array_by_period_by_variable_name[ variable_name] = variable_array_by_period return array_by_period_by_variable_name, error_by_variable_name or None
def to_json(self): self_json = {} if self.axes is not None: self_json['axes'] = self.axes if self.period is not None: self_json['period'] = str(self.period) test_case = self.test_case if test_case is not None: variables = self.tax_benefit_system.variables test_case_json = {} for entity_type in self.tax_benefit_system.entities: entities_json = [] for entity in (test_case.get(entity_type.plural) or []): entity_json = {} entity_json['id'] = entity['id'] if not entity_type.is_person: for role in entity_type.roles: if entity.get(role.plural or role.key): entity_json[role.plural or role.key] = entity.get( role.plural or role.key) for column_name, variable_value in entity.items(): variable = variables.get(column_name) if variable is not None and variable.entity == entity_type: column = columns.make_column_from_variable( variable) variable_value_json = column.transform_value_to_json( variable_value) if variable_value_json is not None: entity_json[column_name] = variable_value_json entities_json.append(entity_json) if entities_json: test_case_json[entity_type.plural] = entities_json self_json['test_case'] = test_case_json return self_json
def compute(formula_name, simulation): array = simulation.calculate(formula_name, simulation.period) column = columns.make_column_from_variable( simulation.tax_benefit_system.get_variable(formula_name)) transform_dated_value_to_json = column.transform_dated_value_to_json return transform_dated_value_to_json(array.tolist()[0])
def api1_variables(req): wsgihelpers.track(req.url.decode('utf-8')) ctx = contexts.Ctx(req) headers = wsgihelpers.handle_cross_origin_resource_sharing(ctx) assert req.method == 'GET', req.method params = req.GET inputs = dict( names = params.getall('name'), ) tax_benefit_system = model.tax_benefit_system tax_benefit_system_variables_name = tax_benefit_system.variables.keys() data, errors = conv.pipe( conv.struct( dict( names = conv.pipe( conv.uniform_sequence( conv.pipe( conv.empty_to_none, conv.test_in(tax_benefit_system_variables_name, error = u'Variable does not exist'), ), drop_none_items = True, ), conv.empty_to_none, ), ), default = 'drop', ), )(inputs, state = ctx) if errors is not None: return wsgihelpers.respond_json(ctx, collections.OrderedDict(sorted(dict( apiVersion = 1, error = collections.OrderedDict(sorted(dict( code = 400, # Bad Request errors = [conv.jsonify_value(errors)], message = ctx._(u'Bad parameters in request'), ).iteritems())), method = req.script_name, params = inputs, url = req.url.decode('utf-8'), ).iteritems())), headers = headers, ) simulation = None variables_json = [] for variable_name in data['names'] or tax_benefit_system_variables_name: variable = tax_benefit_system.variables[variable_name] column = columns.make_column_from_variable(variable) variable_json = column.to_json() # This part checks that no enum tries to go through the API if 'cerfa_field' in variable_json.keys(): cerfa = variable_json['cerfa_field'] for key in cerfa: from enum import Enum if isinstance(key, Enum): cerfa[key.name] = cerfa[key] del cerfa[key] if variable_json.get('source_file_path'): variable_json['source_file_path'] = environment.get_relative_file_path(variable_json['source_file_path']) label = variable_json.get('label') if label is not None and label == variable_name: del variable_json['label'] if not column.is_input_variable(): if simulation is None: simulation = simulations.Simulation( period = periods.period(datetime.date.today().year), tax_benefit_system = model.tax_benefit_system, ) variables_json.append(variable_json) response_dict = dict( apiVersion = 1, country_package_name = conf['country_package'], country_package_version= environment.country_package_version, method = req.script_name, url = req.url.decode('utf-8'), variables = variables_json, ) if hasattr(tax_benefit_system, 'CURRENCY'): response_dict['currency'] = tax_benefit_system.CURRENCY return wsgihelpers.respond_json(ctx, collections.OrderedDict(sorted(response_dict.iteritems())), headers = headers, )
def compute(formula_name, simulation): array = simulation.calculate(formula_name, simulation.period) column = columns.make_column_from_variable(simulation.tax_benefit_system.get_variable(formula_name)) transform_dated_value_to_json = column.transform_dated_value_to_json return transform_dated_value_to_json(array.tolist()[0])
def api1_field(req): wsgihelpers.track(req.url.decode('utf-8')) ctx = contexts.Ctx(req) headers = wsgihelpers.handle_cross_origin_resource_sharing(ctx) assert req.method == 'GET', req.method params = req.GET inputs = dict( context=params.get('context'), input_variables=params.get('input_variables'), reforms=params.getall('reform'), variable=params.get('variable'), ) str_list_to_reforms = conv.make_str_list_to_reforms() data, errors = conv.pipe( conv.struct( dict( context=conv.noop, # For asynchronous calls input_variables=conv.pipe( conv.test_isinstance((bool, int, basestring)), conv.anything_to_bool, conv.default(True), ), reforms=str_list_to_reforms, variable=conv. noop, # Real conversion is done once tax-benefit system is known. ), default='drop', ), )(inputs, state=ctx) if errors is None: country_tax_benefit_system = model.tax_benefit_system tax_benefit_system = model.get_cached_composed_reform( reform_keys=data['reforms'], tax_benefit_system=country_tax_benefit_system, ) if data['reforms'] is not None else country_tax_benefit_system data, errors = conv.struct( dict(variable=conv.pipe( conv.empty_to_none, conv.default(u'revenu_disponible'), conv.test_in(tax_benefit_system.variables), ), ), default=conv.noop, )(data, state=ctx) if errors is not None: return wsgihelpers.respond_json( ctx, collections.OrderedDict( sorted( dict( apiVersion=1, context=inputs.get('context'), error=collections.OrderedDict( sorted( dict( code=400, # Bad Request errors=[conv.jsonify_value(errors)], message=ctx._( u'Bad parameters in request'), ).iteritems())), method=req.script_name, params=inputs, url=req.url.decode('utf-8'), ).iteritems())), headers=headers, ) variable_name = data['variable'] variable = tax_benefit_system.get_variable(variable_name) value_json = columns.make_column_from_variable(variable).to_json() value_json[ 'entity'] = variable.entity.key # Overwrite with symbol instead of key plural for compatibility. return wsgihelpers.respond_json( ctx, collections.OrderedDict( sorted( dict( apiStatus=u'deprecated', apiVersion=1, context=data['context'], method=req.script_name, params=inputs, url=req.url.decode('utf-8'), value=value_json, ).iteritems())), headers=headers, )