Skip to content

Commit

Permalink
Merge 40b9832 into e8c4688
Browse files Browse the repository at this point in the history
  • Loading branch information
Flix6x committed Jun 21, 2023
2 parents e8c4688 + 40b9832 commit d57ff86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
21 changes: 18 additions & 3 deletions flexmeasures/data/models/charts/belief_charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ def chart_for_multiple_sensors(
]
}

# Set up field definition for sensor descriptions
sensor_field_definition = FIELD_DEFINITIONS["sensor_description"]
sensor_field_definition["scale"] = dict(
domain=[
sensor.to_dict()["description"]
for sensor in flatten_unique(sensors_to_show)
]
)

sensors_specs = []
for s in sensors_to_show:
# List the sensors that go into one row
Expand Down Expand Up @@ -164,7 +173,10 @@ def chart_for_multiple_sensors(
# Draw a line for each sensor (and each source)
layers = [
create_line_layer(
row_sensors, event_start_field_definition, event_value_field_definition
row_sensors,
event_start_field_definition,
event_value_field_definition,
sensor_field_definition,
)
]

Expand All @@ -186,6 +198,7 @@ def chart_for_multiple_sensors(
row_sensors,
event_start_field_definition,
event_value_field_definition,
sensor_field_definition,
shared_tooltip,
)
)
Expand Down Expand Up @@ -269,6 +282,7 @@ def create_line_layer(
sensors: list["Sensor"], # noqa F821
event_start_field_definition: dict,
event_value_field_definition: dict,
sensor_field_definition: dict,
):
event_resolutions = list(set([sensor.event_resolution for sensor in sensors]))
assert (
Expand All @@ -286,7 +300,7 @@ def create_line_layer(
"encoding": {
"x": event_start_field_definition,
"y": event_value_field_definition,
"color": FIELD_DEFINITIONS["sensor_description"],
"color": sensor_field_definition,
"strokeDash": {
"scale": {
# Distinguish forecasters and schedulers by line stroke
Expand All @@ -309,6 +323,7 @@ def create_circle_layer(
sensors: list["Sensor"], # noqa F821
event_start_field_definition: dict,
event_value_field_definition: dict,
sensor_field_definition: dict,
shared_tooltip: list,
):
params = [
Expand Down Expand Up @@ -348,7 +363,7 @@ def create_circle_layer(
"encoding": {
"x": event_start_field_definition,
"y": event_value_field_definition,
"color": FIELD_DEFINITIONS["sensor_description"],
"color": sensor_field_definition,
"size": {
"condition": {"value": "200", "test": {"or": or_conditions}},
"value": "0",
Expand Down
8 changes: 5 additions & 3 deletions flexmeasures/utils/coding_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,19 @@ def sort_dict(unsorted_dict: dict) -> dict:
def flatten_unique(nested_list_of_objects: list) -> list:
"""Returns unique objects in a possibly nested (one level) list of objects.
Preserves the original order in which unique objects first occurred.
For example:
>>> flatten_unique([1, [2, 3, 4], 3, 5])
<<< [1, 2, 3, 4, 5]
>>> flatten_unique([1, [2, 20, 6], 10, [6, 2]])
<<< [1, 2, 20, 6, 10]
"""
all_objects = []
for s in nested_list_of_objects:
if isinstance(s, list):
all_objects.extend(s)
else:
all_objects.append(s)
return list(set(all_objects))
return list(dict.fromkeys(all_objects).keys())


def timeit(func):
Expand Down

0 comments on commit d57ff86

Please sign in to comment.