簡體   English   中英

使用 scipy curve_fit 與 dask/xarray

[英]using scipy curve_fit with dask/xarray

我正在嘗試使用 dask.distributed 作為計算后端在大緯度/經度/時間 xarray 上使用 scipy.optimize.curve_fit。

這個想法是使用時間序列為每個(緯度,經度)運行單獨的數據。

所有這些在 xarray/dask 之外運行良好。 我使用作為 pandas dataframe 傳遞的單個位置的時間序列對其進行了測試。 但是,如果我嘗試直接在 xarray 上的相同(緯度、經度)上運行相同的過程,curve_fit 操作將返回初始參數。

我正在使用xr.apply_ufunc執行此操作(這里我只提供與問題嚴格相關的代碼):

    # function to perform the fit
    def _fit_rti_curve(data, data_rti, fit, loc=False):
        fit_func, linearize, find_init_params = _get_fit_functions(fit)
        # remove nans
        x, y = _filter_nodata(data_rti, data)
        # remove outliers
        x, y = _filter_for_outliers(x, y, linearize=linearize)

        # find a first guess for maximum achieveable value
        yscale = np.max(y) * 1.05
        # find a first guess for the other parameters
        # here loc can be manually passed if you have a good estimation
        init_parms = find_init_params(x, y, yscale, loc=loc, linearize=linearize)
        # fit the curve and return parameters
        parms = curve_fit(fit_func, x, y, p0=init_parms, maxfev=10000)
        parms = parms[0]
        return parms

    # shell around _fit_rti_curve
    def find_rti_func_parms(data, rti, fit):
        # sort and fit highest n values
        top_data = np.sort(data)
        top_data = top_data[-len(rti):]

        # convert to float64 if needed
        top_data = top_data.astype(np.float64)
        rti = rti.astype(np.float64)

        # run the fit
        parms = _fit_rti_curve(top_data, rti, fit, loc=0) #TODO maybe add function to allow a free loc
        return parms


    # call for the apply_ufunc
    # `fit` is a string that defines the distribution type
    # `rti` is an array for the x values
    parms_data = xr.apply_ufunc(
        find_rti_func_parms,
        xr_obj,
        input_core_dims=[['time']],
        output_core_dims=[[fit + ' parameters']],
        output_sizes = {fit + ' parameters': len(signature(fit_func).parameters) - 1},
        vectorize=True,
        kwargs={'rti':return_time_interval, 'fit':fit},
        dask='parallelized',
        output_dtypes=['float64']
    )

我的猜測是,這是與線程相關的問題,或者至少是一些共享的 memory 空間在工作程序和調度程序之間沒有正確傳遞。 但是,我只是沒有足夠的知識來測試這個。

對這個問題有任何想法嗎?

這個先前的答案可能會有所幫助? 它使用numpy.polyfit但我認為一般方法應該相似。

將 numpy.polyfit 應用於 xarray 數據集

另外,我還沒有嘗試過,但xr.polyfit()最近剛剛合並。 也可能是要研究的東西。 http://xarray.pydata.org/en/stable/generated/xarray.DataArray.polyfit.html#xarray.DataArray.polyfit

你應該看看這個問題https://github.com/pydata/xarray/issues/4300我有同樣的問題,我用 apply_ufunc 解決了。 它沒有優化,因為它必須執行重新分塊操作,但它可以工作! 我為它創建了一個 GitHub Gist https://gist.github.com/clausmichele/8276871526

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM