Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SimViewer configuration option to use log scale for y-axis #108

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mahautils/multics/sim_results_viewer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ def validate_upload_file_name(name: Optional[str]):
Input({'component': 'plot-config', 'tab': 'y', 'field': 'ymin'}, 'value'),
Input({'component': 'plot-config', 'tab': 'y', 'field': 'ymax'}, 'value'),
Input({'component': 'plot-config', 'tab': 'y', 'field': 'tick_spacing'}, 'value'), # noqa: E501 # pylint: disable=C0301
Input({'component': 'plot-config', 'tab': 'y', 'field': 'scale'}, 'value'), # noqa: E501 # pylint: disable=C0301
Input({'component': 'plot-config', 'tab': 'y', 'field': 'legend-title'}, 'value'), # noqa: E501 # pylint: disable=C0301
Input({'component': 'plot-config', 'tab': 'y', 'field': 'trace-file'}, 'value'), # noqa: E501 # pylint: disable=C0301
Input({'component': 'plot-config', 'tab': 'y', 'field': 'trace-variable'}, 'value'), # noqa: E501 # pylint: disable=C0301
Expand Down Expand Up @@ -355,6 +356,7 @@ def update_plot_config(
y_min,
y_max,
y_tick_spacing,
y_scaling: str,
trace_name: Optional[str],
trace_file: Optional[str],
trace_variable: Optional[str],
Expand Down Expand Up @@ -541,6 +543,7 @@ def update_plot_config(
y_axis['ymax'] = None if y_max in (None, '') else float(y_max)
y_axis['tick_spacing'] = None if y_tick_spacing in (None, '') \
else float(y_tick_spacing) # noqa: E127
y_axis['axis_scaling'] = y_scaling

if len(config_y['axes'][y_axis_idx]['traces']) > 0:
trace = config_y['axes'][y_axis_idx]['traces'][trace_idx]
Expand Down
8 changes: 8 additions & 0 deletions mahautils/multics/sim_results_viewer/load_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from mahautils.multics.simresults import SimResults
from .constants import GUI_SHORT_NAME, PROJECT_NAME, VERSION
from .store import default_y_axis_settings


def decode_base64(base64_str: str) -> str:
Expand Down Expand Up @@ -61,6 +62,13 @@ def load_plot_config(dash_base64_contents: str) -> Tuple[dict, dict, dict]:
config_general = combined_data['general']
config_x = combined_data['x']
config_y = combined_data['y']

# Add defaults for missing settings for compatibility with older
# SimViewer versions
for axis in config_y['axes']: # added in v1.2.0
if 'axis_scaling' not in axis:
axis['axis_scaling'] = default_y_axis_settings['axis_scaling']

except KeyError as exception:
exception.args = (
'Invalid plot configuration JSON file. The file does not contain '
Expand Down
11 changes: 11 additions & 0 deletions mahautils/multics/sim_results_viewer/panel/settings_y.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ def render_y_settings(config_y: dict, sim_results_files: SIM_RESULTS_DICT_T,
tick_spacing_val=axis['tick_spacing'],
),
dash.html.Br(),
dash.html.H5('Axis Scaling'),
dbc.RadioItems(
options=[
{'label': ' Linear ', 'value': 'linear'},
{'label': ' Logarithmic (base 10)', 'value': 'log'},
],
id={'component': 'plot-config', 'tab': 'y', 'field': 'scale'},
value=axis['axis_scaling'],
inline=True,
),
dash.html.Br(),
dash.html.Br(),

# Axis data/traces selection
Expand Down
13 changes: 12 additions & 1 deletion mahautils/multics/sim_results_viewer/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import itertools
import math
import random
import sys
from typing import Any, Dict, List

# Mypy type checking disabled for packages that are not PEP 561-compliant
Expand Down Expand Up @@ -166,19 +167,28 @@ def update_graph(config_general: dict, config_x: dict, config_y: dict,

figure.add_trace(go.Scatter(**plot_data))

tick_options = {}
tick_options: Dict[str, Any] = {
'tickformat': '~g',
}

if y_axis_data['tick_spacing'] not in (None, ''):
tick_options['dtick'] = y_axis_data['tick_spacing']

if set_ymin or set_ymax:
if y_axis_data['axis_scaling'] == 'log':
# If using log scale for the axis, the "range" parameter
# must specify the exponent of 10 for the axis limits
ymin = math.log10(max(ymin, sys.float_info.min))
ymax = math.log10(max(ymax, sys.float_info.min))

tick_options['range'] = [ymin, ymax]

if i == 0:
figure.update_layout(
yaxis={
'title': str(y_axis_data['axis_title']),
'color': str(y_axis_data.get('color', 'black')),
'type': y_axis_data['axis_scaling'],
**tick_options,
},
)
Expand All @@ -191,6 +201,7 @@ def update_graph(config_general: dict, config_x: dict, config_y: dict,
'side': 'left',
'position': (width_per_axis * (num_active_axes - i - 1)),
'color': str(y_axis_data.get('color', 'black')),
'type': y_axis_data['axis_scaling'],
**tick_options,
}
})
Expand Down
1 change: 1 addition & 0 deletions mahautils/multics/sim_results_viewer/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def file_metadata_store():
'ymin': None,
'ymax': None,
'tick_spacing': None,
'axis_scaling': 'linear',
'traces': [],
}

Expand Down