Skip to content

Commit f2f085a

Browse files
refactor(dataframe): remove slice DataFrame constructor and add LineParameters constructor
1 parent d07d128 commit f2f085a

1 file changed

Lines changed: 53 additions & 41 deletions

File tree

src/engine/dataframe.jl

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -198,47 +198,6 @@ function DataFrame(
198198
)
199199
end
200200

201-
"""
202-
DataFrame(slice::AbstractVector; freqs, kind=:auto, mode=:RLCG,
203-
coord=:cart, freq_unit=:base, length_unit=:kilo, quantity_units=nothing,
204-
tol=sqrt(eps(Float64)))
205-
206-
Create a frequency-indexed `DataFrame` for a single impedance/admittance element
207-
provided as a vector over frequency samples.
208-
209-
- `freqs`: frequency vector in Hz (must be supplied).
210-
- `kind`: `:series_impedance`, `:shunt_admittance`, or `:auto` (default). `:auto`
211-
attempts to infer the correct type using the magnitude of the real part.
212-
- Remaining keywords follow the array methods.
213-
"""
214-
function DataFrame(
215-
slice::AbstractVector;
216-
freqs::AbstractVector,
217-
kind::Symbol = :auto,
218-
mode::Symbol = :RLCG,
219-
coord::Symbol = :cart,
220-
freq_unit::Symbol = :base,
221-
length_unit::Symbol = :kilo,
222-
quantity_units = nothing,
223-
tol::Real = sqrt(eps(Float64)),
224-
)
225-
freq_raw = _frequency_vector(slice, freqs)
226-
units = _normalize_quantity_units(quantity_units)
227-
kind (:series_impedance, :shunt_admittance, :auto) ||
228-
Base.error("Unsupported kind $(kind). Use :series_impedance, :shunt_admittance, or :auto.")
229-
return _slice_dataframe(
230-
slice,
231-
kind,
232-
freq_raw,
233-
mode,
234-
coord,
235-
units,
236-
length_unit,
237-
freq_unit,
238-
float(tol),
239-
)
240-
end
241-
242201
function _clip_field(x::Real, tol)
243202
isfinite(x) || return x
244203
return _clip(x, tol)
@@ -273,3 +232,56 @@ end
273232

274233
_scalar_abs(x::Real) = abs(x)
275234
_scalar_abs(m::Measurements.Measurement) = abs(value(m))
235+
236+
"""
237+
DataFrame(LP::LineParameters; mode=:RLCG, coord=:cart,
238+
freq_unit=:base, length_unit=:kilo, quantity_units=nothing,
239+
tol=sqrt(eps(Float64)))
240+
Convert `LP.Z` and `LP.Y` to per-element, frequency-indexed `DataFrame`s
241+
using `LP.f` as the authoritative frequency vector. Returns `(df_z, df_y)`,
242+
each an `n×n` `Matrix{DataFrame}`.
243+
"""
244+
function DataFrame(
245+
LP::LineParameters;
246+
mode::Symbol = :RLCG,
247+
coord::Symbol = :cart,
248+
freq_unit::Symbol = :base,
249+
length_unit::Symbol = :kilo,
250+
quantity_units = nothing,
251+
tol::Real = sqrt(eps(Float64)),
252+
)
253+
# --- validations: LP is the source of truth for frequency samples ----
254+
@assert eltype(LP.f) <: Real "LP.f must be real-valued frequencies."
255+
nzx, nzy, nfZ = size(LP.Z.values)
256+
nyx, nyy, nfY = size(LP.Y.values)
257+
nfZ == nfY ||
258+
Base.error("Z and Y have different number of frequency samples: $nfZ$nfY.")
259+
length(LP.f) == nfZ || Base.error(
260+
"Length of LP.f ($(length(LP.f))) does not match samples in Z/Y ($nfZ).",
261+
)
262+
263+
# --- delegate with LP.f explicitly (no guessing, no manual input) ----
264+
df_z = DataFrame(
265+
LP.Z;
266+
freqs = LP.f,
267+
mode = mode,
268+
coord = coord,
269+
freq_unit = freq_unit,
270+
length_unit = length_unit,
271+
quantity_units = quantity_units,
272+
tol = tol,
273+
)
274+
275+
df_y = DataFrame(
276+
LP.Y;
277+
freqs = LP.f,
278+
mode = mode,
279+
coord = coord,
280+
freq_unit = freq_unit,
281+
length_unit = length_unit,
282+
quantity_units = quantity_units,
283+
tol = tol,
284+
)
285+
286+
return df_z, df_y
287+
end

0 commit comments

Comments
 (0)