Skip to content
Snippets Groups Projects
Unverified Commit 2b3eaf5a authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

allow for open limits with "null"

parent 6ce9df57
Branches
Tags
No related merge requests found
......@@ -6,3 +6,4 @@ class GPLTError(Enum):
INVALID_ARGUMENTS = 1
MODULE_SHOULD_NOT_RUN = 2
COULD_NOT_DECODE_JSON = 3
COULD_NOT_CONVERT = 4
......@@ -12,6 +12,8 @@ def panic(err: GPLTError, msg: str):
x = "MODULE_SHOULD_NOT_RUN"
case GPLTError.COULD_NOT_DECODE_JSON:
x = "COULD_NOT_DECODE_JSON"
case GPLTError.COULD_NOT_CONVERT:
x = "COULD_NOT_CONVERT"
case _:
print(f"[bold red]PANIC[/bold red]: unexpected error: {err} is not known")
exit(1)
......
......@@ -37,6 +37,27 @@ def load_json(json_value: str, filename: str = None):
return data
def _parse_float_or_none(v: str, msg: str) -> float | None:
if v == "null":
return None
else:
try:
return float(v)
except ValueError as e:
panic(
GPLTError.COULD_NOT_CONVERT,
f"{msg}: {e}",
)
def _parse_plot_bounds(bounds: Tuple[str, str] | None, msg: str) -> Tuple[float | None, float | None]:
if bounds is None:
return (None, None)
low, high = bounds
return (_parse_float_or_none(low, msg=msg), _parse_float_or_none(high, msg=msg))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
"gplt",
......@@ -81,8 +102,8 @@ if __name__ == "__main__":
plot_parser.add_argument("--x-scale-base", type=int, default=None, help="the base of the x scale")
plot_parser.add_argument("--y-scale", "-Y", type=str, choices=["linear", "log"], default="linear", help="the y scale of the plot")
plot_parser.add_argument("--y-scale-base", type=int, default=None, help="the base of the y scale")
plot_parser.add_argument("--x-lim", type=float, nargs=2, help="limit the x axis of the plot")
plot_parser.add_argument("--y-lim", type=float, nargs=2, help="limit the y axis of the plot")
plot_parser.add_argument("--x-lim", nargs=2, help="limit the x axis of the plot, use `null` for open bounds")
plot_parser.add_argument("--y-lim", nargs=2, help="limit the y axis of the plot, use `null` for open bounds")
plot_parser.add_argument("--x-ticks-rotation", type=float, help="the rotation angle, in degrees, of the ticks of the X axis")
plot_parser.add_argument("--x-ticks", type=float, nargs='+', help="the ticks for the X axis")
plot_parser.add_argument("--x-tick-labels", type=str, nargs='+', help="the tick labels for the X axis, should be the same length as `--x-ticks`")
......@@ -152,8 +173,8 @@ if __name__ == "__main__":
x_scale_base=args.x_scale_base,
y_scale=args.y_scale,
y_scale_base=args.y_scale_base,
x_lim=args.x_lim,
y_lim=args.y_lim,
x_lim=_parse_plot_bounds(args.x_lim, msg="X limits"),
y_lim=_parse_plot_bounds(args.y_lim, msg="Y limits"),
x_ticks=args.x_ticks,
x_tick_labels=args.x_tick_labels,
x_ticks_rotation=args.x_ticks_rotation,
......
......@@ -309,8 +309,8 @@ def plot(
x_scale_base: int = None,
y_scale: str = "linear",
y_scale_base: int = None,
x_lim: Tuple[float, float] = None,
y_lim: Tuple[float, float] = None,
x_lim: Tuple[float | None, float | None] = (None, None),
y_lim: Tuple[float | None, float | None] = (None, None),
x_ticks: List[float] = None,
x_tick_labels: List[str] = None,
x_ticks_rotation: float = None,
......@@ -373,9 +373,7 @@ def plot(
y_scale_base = 10
ax.set_yscale("log", base=y_scale_base)
if x_lim is not None:
ax.set_xlim(x_lim)
if y_lim is not None:
ax.set_ylim(y_lim)
# ======================== grid ========================
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment