Skip to content

Commit

Permalink
Allow single-series right plots
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-caron committed Oct 31, 2023
1 parent 46b5d52 commit 91be580
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Allow single-series right plots
- Argument ``print_command_line`` to plotting function

### Changed
Expand Down Expand Up @@ -37,7 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Allow single-series plots
- Allow single-series left plots
- Detect time key from root keys in the input
- Length attribute to indexed series

Expand Down
66 changes: 41 additions & 25 deletions foxplot/fox.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import logging
import sys
import webbrowser
from typing import BinaryIO, List, Optional, TextIO, Union
from typing import BinaryIO, Dict, List, Optional, TextIO, Union

from .decoders.json import decode_json
from .decoders.msgpack import decode_msgpack
Expand Down Expand Up @@ -78,10 +78,45 @@ def __print_command_line(self, left_series, right_series):
timestamp = f"-t {self.__time} " if self.__time is not None else ""
print(f"foxplot {timestamp}-l {left_args} {right_args}{file}")

def __list_to_dict(
self, series_list: List[Union[Series, Node]]
) -> Dict[str, List[float]]:
"""Convert a list of series (or nodes) to a dictionary.
The output dictionary has one key per series in the list. Nodes are
expanded just once, assuming all their children in the data tree
are series.
Args:
series_list: Input list of series;
Returns:
Dictionary mapping series names to their values.
"""
series_dict = {}
for series in series_list:
if isinstance(series, Series):
series_dict[series._label] = series._get(self.length)
elif isinstance(series, Node):
for key, child in series._items():
label = series._label + f"/{key}"
if isinstance(child, Series):
series_dict[label] = child._get(self.length)
else:
logging.warn(
"Skipping '%s' as it is not an indexed series",
label,
)
else:
raise TypeError(
f"Series '{series}' has unknown type {type(series)}"
)
return series_dict

def plot(
self,
left: Union[Series, Node, List[Union[Series, Node]]],
right: Optional[List[Series]] = None,
right: Optional[Union[Series, Node, List[Union[Series, Node]]]] = None,
title: Optional[str] = None,
left_axis_unit: str = "",
right_axis_unit: str = "",
Expand All @@ -100,6 +135,8 @@ def plot(
"""
if isinstance(left, Series) or isinstance(left, Node):
left = [left]
if isinstance(right, Series) or isinstance(right, Node):
right = [right]
if title is None:
title = f"Plot from {self.__file}"

Expand All @@ -109,29 +146,8 @@ def plot(
else [float(x) for x in range(self.length)]
)

def list_to_dict(series_list):
series_dict = {}
for series in series_list:
if isinstance(series, Series):
series_dict[series._label] = series._get(self.length)
elif isinstance(series, Node):
for key, child in series._items():
label = series._label + f"/{key}"
if isinstance(child, Series):
series_dict[label] = child._get(self.length)
else:
logging.warn(
"Skipping '%s' as it is not an indexed series",
label,
)
else:
raise TypeError(
f"Series '{series}' has unknown type {type(series)}"
)
return series_dict

left_series = list_to_dict(left)
right_series = list_to_dict(right) if right is not None else {}
left_series = self.__list_to_dict(left)
right_series = self.__list_to_dict(right) if right is not None else {}
html = generate_html(
times,
left_series,
Expand Down
2 changes: 1 addition & 1 deletion foxplot/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __repr__(self):
values = list(self.__data.values())
return f"Time series with values: {values}"

def _get(self, max_index: int):
def _get(self, max_index: int) -> List[float]:
"""Get indexed series as a list of values.
Args:
Expand Down

0 comments on commit 91be580

Please sign in to comment.