Coverage for backend/django/core/auxiliary/methods/export_scenario_data.py: 75%
39 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-02-11 21:43 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-02-11 21:43 +0000
1from core.auxiliary.models.Scenario import Scenario
2from core.auxiliary.models.Flowsheet import Flowsheet
3from core.auxiliary.models.PropertyValue import PropertyValue
4from core.auxiliary.models.Solution import Solution
7def values_per_index(scenario: Scenario):
8 if not scenario.enable_dynamics: 8 ↛ 11line 8 didn't jump to line 11 because the condition on line 8 was always true
9 return 1
10 else :
11 return scenario.num_time_steps
13# Tested in test_mss.py
14def export_scenario_data(flowsheet: Flowsheet, scenario: Scenario, properties: list[int] | None = None):
15 # Create a column for each property value
16 if properties:
17 solutions = Solution.objects.filter(scenario=scenario, property__property_id__in=properties).order_by("solve_index")
18 else:
19 solutions = Solution.objects.filter(scenario=scenario).order_by("solve_index")
20 property_values = PropertyValue.objects.filter(flowsheet=flowsheet, solutions__in=solutions).distinct()
22 # Create a list of blanks to fill in missing data
23 blanks = [None for _ in range(values_per_index(scenario))]
25 columns = {}
26 data = {}
28 for uo_name, prop_key, prop_id in property_values.values_list(
29 "property__set__simulationObject__componentName", # Link to PropertyInfo -> PropertySet -> SimulationObject Name
30 "property__key", # PropertyInfo key
31 "id"
32 ):
33 column_name = f"{uo_name} - {prop_key}"
34 columns[prop_id] = column_name
35 data[column_name] = []
37 # Populate the columns
38 current_solve_index = 0
40 for solution in solutions:
41 if solution.solve_index > current_solve_index:
42 # Fill in blanks for missing solve indices
43 for _ in range(solution.solve_index - current_solve_index - 1): 43 ↛ 44line 43 didn't jump to line 44 because the loop on line 43 never started
44 for column_name in data.keys():
45 data[column_name].extend(blanks)
46 current_solve_index = solution.solve_index
48 column_name = columns[solution.property_id]
49 # Because the values are an array, we flatten them into the data column
50 data[column_name].extend(solution.values)
52 return data
54def collate(data: dict[str,list]):
55 """
56 Collate the data into rows for CSV export
57 """
58 # Collate the data into rows
59 # Each row is a dict with keys as column names
60 rows = []
61 max_length = max((len(v) for v in data.values()),default=0)
62 for i in range(max_length): 62 ↛ 63line 62 didn't jump to line 63 because the loop on line 62 never started
63 row = {}
64 for key, values in data.items():
65 row[key] = values[i] if i < len(values) else None
66 rows.append(row)
67 return rows