pvanalytics icon indicating copy to clipboard operation
pvanalytics copied to clipboard

Suggestion: data fitting functions

Open RDaxini opened this issue 6 months ago • 5 comments

I was wondering if functions to fit data would be within the scope of pvanalytics, perhaps in a new data fitting module. Brief chat with @cdeline suggests there is a chance they might be, so I wanted to open an issue.

In the case of performance modeling at least, these could be linked to pvlib-python functions so that users could derive their own model coefficients for use in those functions. Functionality may need to be added to the respective pvlib-python functions to allow user-specified coefficients as input.

Why not just do this in pvlib-python? I think sometimes it is not always clear what fitting method(s) the original author(s) of a performance model used, so it may not always be possible to stick with the original method of the author(s), which is what we try to do in pvlib-python. (For example, see previous discussions pvlib-python #1979 and pvlib-python #2242 Implementation in pvanalytics might offer sufficient separation from the original model, allowing some flexibility, while still providing supporting tools to users of pvlib-python.

A simple example for a function z(x,y):

from scipy.optimize import curve_fit
import numpy as np

def fit_an_example_model(x, y, z): 
    
    def example_model(X, a, b, c):
        x, y = X
        return a * x**b + y**c
    
    X = np.vstack((x, y))
    
    fitted_coeffs, _ = curve_fit(example_model, X, z)
    a, b, c = fitted_coeffs

    return fitted_coeffs

An optional argument, perhaps, could also allow the user to choose whether they want to generate some basic fitting statistics for their fit. The user could also specify the fitting method they want to use.

The alternative would be to publish a gallery example in pvlib-python as suggested here.

Thoughts @cwhanse @kperrynrel?

RDaxini avatar Jul 07 '25 16:07 RDaxini

From my perspective that pvanalytics provides reusable functions that operate on measured PV system data, there's space for a fitting code module. Functions for that module would need to perform some PV-specific data manipulations. A convenience wrapper around existing scipy code doesn't raise to that bar, in my view.

We always envisioned pvanalytics also hosting what we called "workflows", akin to pvlib-python's ModelChain. For now those workflows take the form of gallery examples. The PVFleets examples are great illustrations.

Estimating parameters for a model from raw PV system data seems to be a good subject for an example, as the process entails data filtering and selection, which are (or should be) core pvanalytics functions.

cwhanse avatar Jul 08 '25 14:07 cwhanse

Thanks as always for the feedback @cwhanse

Functions for that module would need to perform some PV-specific data manipulations.

This is a good point. I thought the data processing and fitting would be separate functions, which are then combined in a single workflow later. Do you have an example in mind of a suitable function that fits within a hypothetical fitting module?

We always envisioned pvanalytics also hosting what we called "workflows", akin to pvlib-python's ModelChain. For now those workflows take the form of gallery examples. The PVFleets examples are great illustrations.

Estimating parameters for a model from raw PV system data seems to be a good subject for an example, as the process entails data filtering and selection, which are (or should be) core pvanalytics functions.

Unless I misunderstood something earlier, sounds like a gallery example is probably the way to go with this one. Any thoughts on whether it's best suited for pvlib (it was suggested in the original issue I posted there) or here? I guess it depends on the scope of the example perhaps.

RDaxini avatar Jul 11 '25 21:07 RDaxini

I thought the data processing and fitting would be separate functions, which are then combined in a single workflow later.

Me, too.

Do you have an example in mind of a suitable function that fits within a hypothetical fitting module?

Not off the top of my head.

sounds like a gallery example is probably the way to go

I think so, also. It is easy to factor code out of the example into the library, if it appears likely to be re-used.

cwhanse avatar Jul 11 '25 22:07 cwhanse

Another "requirement" for a fitting example here, is that it uses pvanalytics functions in the data preparation. If the data prep is straightforward, and the example fits a model sourced from pvlib, then I think the example is better placed in pvlib. Maybe that's a better home if the end product are coefficients for a pvlib function.

cwhanse avatar Jul 11 '25 22:07 cwhanse

That all makes sense. I will start off with a more limited-scope example for the pvlib example gallery. In future, this could be developed to include more data process using pvanalytics functions and thus be built up into a full workflow in notebook form.

Feel free to close as complete, in which case we can refer to #2242 for context, or leave open until the pvlib example is published.

RDaxini avatar Jul 28 '25 19:07 RDaxini