Skip to content

Commit

Permalink
refactor: system method .plot_interp() gets figure annotations from c…
Browse files Browse the repository at this point in the history
…omponent method ._get_annot()
  • Loading branch information
geddy11 committed May 26, 2024
1 parent 9275463 commit bb2f017
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
48 changes: 48 additions & 0 deletions src/sysloss/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,20 @@ def _solv_get_warns(self, vi, vo, ii, io, phase, phase_conf={}):
"""Check limits"""
return _get_warns(self._limits, {"vi": vi, "vo": vo, "ii": ii, "io": io})

def _get_annot(self):
"""Get interpolation figure annotations in format [xlabel, ylabel, title]"""
if isinstance(self._ipr, _Interp1d):
return [
"Output current (A)",
"Voltage drop (V)",
"{} voltage drop".format(self._params["name"]),
]
return [
"Output current (A)",
"Input voltage (V)",
"{} voltage drop".format(self._params["name"]),
]


class Converter:
"""Voltage converter.
Expand Down Expand Up @@ -846,6 +860,22 @@ def _solv_get_warns(self, vi, vo, ii, io, phase, phase_conf=[]):
return ""
return _get_warns(self._limits, {"vi": vi, "vo": vo, "ii": ii, "io": io})

def _get_annot(self):
"""Get interpolation figure annotations in format [xlabel, ylabel, title]"""
if isinstance(self._ipr, _Interp1d):
return [
"Output current (A)",
"Efficiency",
"{} efficiency for Vo={}V".format(
self._params["name"], self._params["vo"]
),
]
return [
"Output current (A)",
"Input voltage (V)",
"{} efficiency for Vo={}V".format(self._params["name"], self._params["vo"]),
]


class LinReg:
"""Linear voltage converter.
Expand Down Expand Up @@ -1004,3 +1034,21 @@ def _solv_get_warns(self, vi, vo, ii, io, phase, phase_conf=[]):
if phase not in phase_conf:
return ""
return _get_warns(self._limits, {"vi": vi, "vo": vo, "ii": ii, "io": io})

def _get_annot(self):
"""Get interpolation figure annotations in format [xlabel, ylabel, title]"""
if isinstance(self._ipr, _Interp1d):
return [
"Output current (A)",
"Ground current (A)",
"{} ground current for Vo={}V".format(
self._params["name"], self._params["vo"]
),
]
return [
"Output current (A)",
"Input voltage (V)",
"{} ground current for Vo={}V".format(
self._params["name"], self._params["vo"]
),
]
43 changes: 11 additions & 32 deletions src/sysloss/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,7 @@ def plot_interp(
raise ValueError("Component name is not valid!")
n = self._g.attrs["nodes"][name]
if isinstance(self._g[n]._ipr, _Interp1d):
annot = self._g[n]._get_annot()
fig = plt.figure()
xmin = max(self._g[n]._ipr._x[0] - 0.15 * max(self._g[n]._ipr._x), 0.0)
xmax = 1.15 * max(self._g[n]._ipr._x)
Expand All @@ -1198,26 +1199,14 @@ def plot_interp(
self._g[n]._ipr._x, self._g[n]._ipr._fx, ".r", label="input data"
)
plt.legend(loc="lower right")
plt.xlabel("Output current (A)")
if self._g[n]._component_type.name == "CONVERTER":
plt.ylabel("Efficiency")
plt.title(
"{} efficiency for Vo={}V".format(name, self._g[n]._params["vo"])
)
elif self._g[n]._component_type.name == "LINREG":
plt.ylabel("Ground current (A)")
plt.title(
"{} ground current for Vo={}V".format(
name, self._g[n]._params["vo"]
)
)
else:
plt.ylabel("Voltage drop (V)")
plt.title("{} voltage drop".format(name))
plt.xlabel(annot[0])
plt.ylabel(annot[1])
plt.title(annot[2])
plt.rc("axes", axisbelow=True)
plt.grid()
return fig
elif isinstance(self._g[n]._ipr, _Interp2d):
annot = self._g[n]._get_annot()
xmin = max(min(self._g[n]._ipr._x) - 0.15 * max(self._g[n]._ipr._x), 0.0)
xmax = 1.15 * max(self._g[n]._ipr._x)
X = np.linspace(xmin, xmax, num=100)
Expand All @@ -1242,8 +1231,8 @@ def plot_interp(
)
plt.legend(loc="upper left")
plt.colorbar()
plt.xlabel("Output current (A)")
plt.ylabel("Input voltage (V)")
plt.xlabel(annot[0])
plt.ylabel(annot[1])
else:
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
surf = ax.plot_surface(
Expand All @@ -1265,20 +1254,10 @@ def plot_interp(
)
plt.legend(loc="upper left")
fig.colorbar(surf, shrink=0.5, aspect=10, pad=0.1)
plt.ylabel("Output current (A)")
plt.xlabel("Input voltage (V)")
if self._g[n]._component_type.name == "CONVERTER":
plt.title(
"{} efficiency for Vo={}V".format(name, self._g[n]._params["vo"])
)
elif self._g[n]._component_type.name == "LINREG":
plt.title(
"{} ground current for Vo={}V".format(
name, self._g[n]._params["vo"]
)
)
else:
plt.title("{} voltage drop".format(name))
# labels swapped
plt.xlabel(annot[1])
plt.ylabel(annot[0])
plt.title(annot[2])
return fig
else:
print("Component does not have interpolation data")
Expand Down

0 comments on commit bb2f017

Please sign in to comment.