File: polyfit.py

package info (click to toggle)
python-xarray 2025.08.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,796 kB
  • sloc: python: 115,416; makefile: 258; sh: 47
file content (38 lines) | stat: -rw-r--r-- 1,021 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np

import xarray as xr

from . import parameterized, randn, requires_dask

NDEGS = (2, 5, 20)
NX = (10**2, 10**6)


class Polyval:
    def setup(self, *args, **kwargs):
        self.xs = {nx: xr.DataArray(randn((nx,)), dims="x", name="x") for nx in NX}
        self.coeffs = {
            ndeg: xr.DataArray(
                randn((ndeg,)), dims="degree", coords={"degree": np.arange(ndeg)}
            )
            for ndeg in NDEGS
        }

    @parameterized(["nx", "ndeg"], [NX, NDEGS])
    def time_polyval(self, nx, ndeg):
        x = self.xs[nx]
        c = self.coeffs[ndeg]
        xr.polyval(x, c).compute()

    @parameterized(["nx", "ndeg"], [NX, NDEGS])
    def peakmem_polyval(self, nx, ndeg):
        x = self.xs[nx]
        c = self.coeffs[ndeg]
        xr.polyval(x, c).compute()


class PolyvalDask(Polyval):
    def setup(self, *args, **kwargs):
        requires_dask()
        super().setup(*args, **kwargs)
        self.xs = {k: v.chunk({"x": 10000}) for k, v in self.xs.items()}