Skip to content

Commit

Permalink
Merge pull request #863 from openfisca/symbolic-trace
Browse files Browse the repository at this point in the history
Display symbolic values of Enums in /trace and print_computation_log
  • Loading branch information
Morendil authored Apr 13, 2019
2 parents c64f908 + 9502f37 commit 5a1fd43
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 32.1.0 [#863](https://github.com/openfisca/openfisca-core/pull/863)

- Display symbolic values of Enums in /trace and print_computation_log

# 32.0.0 [#857](https://github.com/openfisca/openfisca-core/pull/857)

### Breaking changes
Expand Down
3 changes: 3 additions & 0 deletions openfisca_core/indexed_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,6 @@ def decode_to_str(self):

def __repr__(self):
return '{}({})'.format(self.__class__.__name__, str(self.decode()))

def __str__(self):
return str(self.decode_to_str())
6 changes: 3 additions & 3 deletions openfisca_web_api/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dpath

from openfisca_core.simulation_builder import SimulationBuilder
from openfisca_core.indexed_enums import Enum
from openfisca_core.indexed_enums import Enum, EnumArray


def calculate(tax_benefit_system, input_data):
Expand Down Expand Up @@ -52,8 +52,8 @@ def trace(tax_benefit_system, input_data):
trace = deepcopy(simulation.tracer.trace)
for _vector_key, vector_trace in trace.items():
value = vector_trace['value'].tolist()
if isinstance(value[0], Enum):
value = [item.name for item in value]
if isinstance(vector_trace['value'], EnumArray):
value = [item.name for item in vector_trace['value'].decode()]
if isinstance(value[0], bytes):
value = [str(item) for item in value]
vector_trace['value'] = value
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

setup(
name = 'OpenFisca-Core',
version = '32.0.0',
version = '32.1.0',
author = 'OpenFisca Team',
author_email = 'contact@openfisca.org',
classifiers = [
Expand Down
10 changes: 10 additions & 0 deletions tests/core/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from openfisca_core.tracers import Tracer, TracingParameterNodeAtInstant
from openfisca_core.tools import assert_near

from openfisca_country_template.variables.housing import HousingOccupancyStatus
from .parameters_fancy_indexing.test_fancy_indexing import parameters


Expand Down Expand Up @@ -40,6 +41,15 @@ def test_log_format():
assert lines[1] == ' B<2017> >> 1'


def test_trace_enums():
tracer = Tracer()
tracer.record_calculation_start("A", 2017)
tracer.record_calculation_end("A", 2017, HousingOccupancyStatus.encode(np.array(['tenant'])))

lines = tracer.computation_log()
assert lines[0] == " A<2017> >> ['tenant']"


# Tests on tracing with fancy indexing
zone = np.asarray(['z1', 'z2', 'z2', 'z1'])
housing_occupancy_status = np.asarray(['owner', 'owner', 'tenant', 'tenant'])
Expand Down
10 changes: 10 additions & 0 deletions tests/web_api/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ def test_trace_basic():
assert_items_equal(basic_income_dep, ['age<2017-01>'])


def test_trace_enums():
new_single = deepcopy(single)
new_single['households']['_']['housing_occupancy_status'] = {"2017-01": None}
simulation_json = json.dumps(new_single)
response = subject.post('/trace', data = simulation_json, content_type = 'application/json')
response_json = json.loads(response.data)
housing_status = dpath.util.get(response_json, 'trace/housing_occupancy_status<2017-01>/value')
assert housing_status[0] == 'tenant' # The default value


def test_entities_description():
simulation_json = json.dumps(couple)
response = subject.post('/trace', data = simulation_json, content_type = 'application/json')
Expand Down

0 comments on commit 5a1fd43

Please sign in to comment.