Files
ovf/ovf/core/geometry/plot_poles.py

101 lines
4.4 KiB
Python

import plotly as px
import plotly.graph_objs as go
from plotly.offline import plot
import numpy as np
from sklearn.linear_model import Ridge
from ovf.schemas.geometry.poles import PolesPlot3dUnit, PolesPlot3dDataUnit, PolesPlot2dUnit, PolesPlot2dDataUnit
from ovf.schemas.geometry.basic import GeometryPlot3dDataUnit, GeometryPlot3dUnit,GeometryPlot2dUnit,GeometryPlot2dComplexDataUnit
from ovf.core.geometry.plot3d import get_plot_instance_in_3d
from ovf.core.geometry.plot2d import plot_2d
from plotly.subplots import make_subplots
import os
def plot_poles_in_3d(poles:PolesPlot3dUnit,dirname:str):
os.makedirs(dirname,exist_ok=True)
index = 0
while index < len(poles.datas[0].poles):
if poles.real_part:
plotunit = GeometryPlot3dUnit(
title="Real Part",
datas=[GeometryPlot3dDataUnit(x=p.geometry_1, y=p.geometry_2, z=np.real(p.poles[index])) for p in poles.datas],
x_label=poles.geometry_1_label,
y_label=poles.geometry_2_label,
z_label=poles.pole_label,
x_scale=poles.geometry_1_scale,
y_scale=poles.geometry_2_scale,
z_scale=poles.pole_scale,
ridge_alpha=poles.ridge_alpha,
ridge_degree=poles.ridge_degree
)
trace, surface, layout = get_plot_instance_in_3d(plotunit)
fig = go.Figure(data=[trace, surface], layout=layout)
fig.write_html(f"{dirname}/pole_{index}_real.html")
if poles.imag_part:
plotunit = GeometryPlot3dUnit(
title="Imaginary Part",
datas=[GeometryPlot3dDataUnit(x=p.geometry_1, y=p.geometry_2, z=np.imag(p.poles[index])) for p in poles.datas],
x_label=poles.geometry_1_label,
y_label=poles.geometry_2_label,
z_label=poles.pole_label,
x_scale=poles.geometry_1_scale,
y_scale=poles.geometry_2_scale,
z_scale=poles.pole_scale,
ridge_alpha=poles.ridge_alpha
)
trace, surface, layout = get_plot_instance_in_3d(plotunit)
fig = go.Figure(data=[trace, surface], layout=layout)
fig.write_html(f"{dirname}/pole_{index}_imag.html")
if poles.magnitude:
plotunit = GeometryPlot3dUnit(
title="Magnitude",
datas=[GeometryPlot3dDataUnit(x=p.geometry_1, y=p.geometry_2, z=np.abs(p.poles[index])) for p in poles.datas],
x_label=poles.geometry_1_label,
y_label=poles.geometry_2_label,
z_label=poles.pole_label,
x_scale=poles.geometry_1_scale,
y_scale=poles.geometry_2_scale,
z_scale=poles.pole_scale,
ridge_alpha=poles.ridge_alpha
)
trace, surface, layout = get_plot_instance_in_3d(plotunit)
fig = go.Figure(data=[trace, surface], layout=layout)
fig.write_html(f"{dirname}/pole_{index}_magnitude.html")
if poles.phase:
plotunit = GeometryPlot3dUnit(
title="Phase",
datas=[GeometryPlot3dDataUnit(x=p.geometry_1, y=p.geometry_2, z=np.float64(np.angle(p.poles[index]))) for p in poles.datas],
x_label=poles.geometry_1_label,
y_label=poles.geometry_2_label,
z_label=poles.pole_label,
x_scale=poles.geometry_1_scale,
y_scale=poles.geometry_2_scale,
z_scale=poles.pole_scale,
ridge_alpha=poles.ridge_alpha
)
trace, surface, layout = get_plot_instance_in_3d(plotunit)
fig = go.Figure(data=[trace, surface], layout=layout)
fig.write_html(f"{dirname}/pole_{index}_phase.html")
if index + 1 < len(poles.datas[0].poles):
if np.isclose(poles.datas[0].poles[index] , np.conj(poles.datas[0].poles[index + 1])):
index += 1 # 跳过共轭点
index += 1
def plot_poles_in_2d(poles:PolesPlot2dUnit,filename:str):
data = GeometryPlot2dUnit(
title=poles.title,
datas=[GeometryPlot2dComplexDataUnit(
x=[np.real(p) for p in d.poles],
y=[np.imag(p) for p in d.poles],
geometries=d.geometries
) for d in poles.datas],
x_label=poles.x_label,
y_label=poles.y_label
)
plot_2d(data, filename)