Skip to content

Commit

Permalink
Merge pull request #33 from azogue/feature/named-plot-objects
Browse files Browse the repository at this point in the history
♻️ Testing suite refactor + model changes
  • Loading branch information
azogue authored Jun 9, 2023
2 parents feaef6b + 48c7aff commit 2f36515
Show file tree
Hide file tree
Showing 21 changed files with 522 additions and 566 deletions.
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exclude: >
)$
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.270
rev: v0.0.272
hooks:
- id: ruff
args:
Expand Down Expand Up @@ -40,6 +40,8 @@ repos:
- id: "mypy"
name: "Check type hints (mypy)"
verbose: true
additional_dependencies:
- types-python-slugify
ci:
autofix_commit_msg: |
[pre-commit.ci] auto fixes from pre-commit.com hooks
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.7.0] - ♻️ Testing suite refactor + model changes - 2023-06-09

Maintenance-only update, without new features.

##### Changes

- 💥 Remove plotting methods from `PsychroCurve`/`PsychroCurves` for a more functional approach, using methods in `plot_logic.py`
- 💥 Add `PsychroCurve.internal_value` field, to identify the trigger value for each curve (constant H/V/RH...), and evolve validation of data arrays
- ✅ tests: Increase test coverage and optimize tests for faster run (~2x)
- 📦️ env: Add slugify to deps and bump version

## [0.6.0] - ✨ Chart config auto-refresh + bugfixes - 2023-06-07

Before, chart customize was done by creating a new `Psychrochart` object based on some modified chart configuration,
Expand Down
30 changes: 29 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions psychrochart/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import gc
from io import StringIO
from pathlib import Path
from typing import Any, Iterable, Mapping, Type
from typing import Any, Iterable, Type

from matplotlib import figure
from matplotlib.artist import Artist
Expand All @@ -27,9 +27,11 @@
)
from psychrochart.models.styles import CurveStyle
from psychrochart.plot_logic import (
add_label_to_curve,
apply_axis_styling,
plot_annots_dbt_rh,
plot_chart,
plot_curve,
)
from psychrochart.process_logic import (
append_zones_to_chart,
Expand Down Expand Up @@ -263,10 +265,12 @@ def plot_vertical_dry_bulb_temp_line(
temp,
self.pressure,
style=style_curve,
type_curve="constant-dbt",
reverse=reverse,
)
if curve.plot_curve(self.axes) and label is not None:
curve.add_label(self.axes, label, **label_params)

if plot_curve(curve, self.axes) and label is not None:
add_label_to_curve(curve, self.axes, label, **label_params)

def plot_legend(
self,
Expand Down Expand Up @@ -326,7 +330,7 @@ def save(
self,
path_dest: Any,
canvas_cls: Type[FigureCanvasBase] | None = None,
**params: Mapping[str, Any],
**params,
) -> None:
"""Write the chart to disk."""
# ensure destination path if folder does not exist
Expand All @@ -342,7 +346,7 @@ def save(
canvas_use(self._fig).print_figure(path_dest, **params)
gc.collect()

def make_svg(self, **params: Mapping[str, Any]) -> str:
def make_svg(self, **params) -> str:
"""Generate chart as SVG and return as text."""
svg_io = StringIO()
self.save(svg_io, canvas_cls=FigureCanvasSVG, **params)
Expand Down
6 changes: 6 additions & 0 deletions psychrochart/chart_entities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from uuid import uuid4


def random_internal_value() -> float:
"""Generate random 'internal_value' for unnamed curves."""
return float(int(f"0x{str(uuid4())[-4:]}", 16))
86 changes: 39 additions & 47 deletions psychrochart/chartdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
)
from scipy.interpolate import interp1d

from psychrochart.chart_entities import random_internal_value
from psychrochart.models.annots import ChartZone
from psychrochart.models.curves import PsychroCurve, PsychroCurves
from psychrochart.models.styles import CurveStyle, ZoneStyle
from psychrochart.models.styles import CurveStyle
from psychrochart.util import solve_curves_with_iteration

f_vec_hum_ratio_from_vap_press = np.vectorize(GetHumRatioFromVapPres)
Expand Down Expand Up @@ -134,6 +135,7 @@ def make_constant_relative_humidity_lines(
type_curve="constant_rh_data",
label_loc=label_loc,
label=f"RH {rh:g} %" if rh in rh_label_values else None,
internal_value=float(rh),
)
for rh, curve_ct_rh in zip(rh_values, curves_ct_rh)
],
Expand Down Expand Up @@ -162,6 +164,7 @@ def make_constant_dry_bulb_v_line(
y_data=np.array(path_y),
style=style,
type_curve=type_curve,
internal_value=temp,
)


Expand All @@ -181,6 +184,7 @@ def make_constant_dry_bulb_v_lines(
y_data=np.array([w_humidity_ratio_min, w_max]),
style=style,
type_curve="constant_dry_temp_data",
internal_value=temp,
)
for temp, w_max in zip(temps_vl, w_max_vec)
],
Expand All @@ -207,6 +211,7 @@ def make_constant_humidity_ratio_h_lines(
y_data=np.array([w, w]),
style=style,
type_curve="constant_humidity_data",
internal_value=w,
)
for w, t_dp in zip(ws_hl, dew_points)
],
Expand All @@ -231,6 +236,7 @@ def make_saturation_line(
y_data=w_sat,
style=style,
type_curve="saturation",
internal_value=100.0,
)
return PsychroCurves(curves=[sat_c])

Expand Down Expand Up @@ -308,6 +314,7 @@ def make_constant_enthalpy_lines(
if round(h, 3) in h_label_values
else None
),
internal_value=round(h, 3),
)
for t_sat, w_sat, t_max, h in zip(
t_sat_points, w_in_sat, temps_max_constant_h, h_objective
Expand Down Expand Up @@ -386,6 +393,7 @@ def make_constant_specific_volume_lines(
if round(vol, 3) in v_label_values
else None
),
internal_value=round(vol, 3),
)
for t_sat, w_sat, t_max, vol in zip(
t_sat_points, w_in_sat, temps_max_constant_v, v_objective
Expand Down Expand Up @@ -474,25 +482,40 @@ def make_constant_wet_bulb_temperature_lines(
type_curve="constant_wbt_data",
label_loc=label_loc,
label=(_make_temp_label(wbt) if wbt in wbt_label_values else None),
internal_value=wbt,
)
curves.append(c)

return PsychroCurves(curves=curves, family_label=family_label)


def _make_zone_dbt_rh(
t_min: float,
t_max: float,
increment: float,
rh_min: float,
rh_max: float,
pressure: float,
style: ZoneStyle,
label: str | None = None,
def make_zone_curve(
zone_conf: ChartZone, increment: float, pressure: float
) -> PsychroCurve:
"""Generate points for zone between constant dry bulb temps and RH."""
temps = np.arange(t_min, t_max + increment, increment)
"""Generate plot-points for zone."""
# todo better id for overlay zones if no label
zone_value = random_internal_value() if zone_conf.label is None else None
if zone_conf.zone_type == "xy-points":
# expect points in plot coordinates!
return PsychroCurve(
x_data=np.array(zone_conf.points_x),
y_data=np.array(zone_conf.points_y),
style=zone_conf.style,
type_curve="xy-points",
label=zone_conf.label,
internal_value=zone_value,
)

assert zone_conf.zone_type == "dbt-rh"
# points for zone between constant dry bulb temps and RH
t_min = zone_conf.points_x[0]
t_max = zone_conf.points_x[-1]
rh_min = zone_conf.points_y[0]
rh_max = zone_conf.points_y[-1]
assert rh_min >= 0.0 and rh_max <= 100.0
assert t_min < t_max

temps = np.arange(t_min, t_max + increment, increment)
curve_rh_up = gen_points_in_constant_relative_humidity(
temps, rh_max, pressure
)
Expand All @@ -506,39 +529,8 @@ def _make_zone_dbt_rh(
return PsychroCurve(
x_data=np.array(temps_zone),
y_data=np.array(abs_humid),
style=style,
type_curve="constant_rh_data",
label=label,
style=zone_conf.style,
type_curve="dbt-rh",
label=zone_conf.label,
internal_value=zone_value,
)


def make_zone_curve(
zone_conf: ChartZone, increment: float, pressure: float
) -> PsychroCurve:
"""Generate points for zone between constant dry bulb temps and RH."""
# TODO make conversion rh -> w and new zone_type: "dbt-rh-points"
assert isinstance(zone_conf.style, ZoneStyle)
if zone_conf.zone_type == "dbt-rh":
t_min = zone_conf.points_x[0]
t_max = zone_conf.points_x[-1]
rh_min = zone_conf.points_y[0]
rh_max = zone_conf.points_y[-1]
return _make_zone_dbt_rh(
t_min,
t_max,
increment,
rh_min,
rh_max,
pressure,
zone_conf.style,
label=zone_conf.label,
)
else:
# zone_type: 'xy-points'
return PsychroCurve(
x_data=np.array(zone_conf.points_x),
y_data=np.array(zone_conf.points_y),
style=zone_conf.style,
type_curve="custom path",
label=zone_conf.label,
)
Loading

0 comments on commit 2f36515

Please sign in to comment.