Port a binned spatial trend analysis to Modal by fanning out per-bin linear trend computations as independent parallel shards, with a helper Modal Function for per-bin polyfit and the orchestrator collecting all results.
harbor run -t vmax-modal/modal-port-v1-patched-xr-groupby-bins-fanoutharbor download vmax-modal/modal-port-v1-patched-xr-groupby-bins-fanoutPort the binned spatial trend analysis to Modal by fanning out independent per-bin computations. Each spatial bin's trend fitting should run as an independent shard rather than sequentially.
The original workflow in original_workflow.py implements binned_trend_analysis, which builds a 3-D DataArray of shape (n_time, n_lat, n_lon) from a seeded RNG and computes linear trends per spatial bin. The workflow proceeds in three phases:
Build input data: Creates lat = np.linspace(-60, 60, n_lat), lon = np.linspace(0, 120, n_lon), and t = np.arange(n_time, dtype=float). Fills the DataArray with rng.standard_normal((n_time, n_lat, n_lon)) + t[:, None, None] * 0.02.
Partition into bins: Computes n_bins + 1 equally-spaced bin edges from -60 to 60 using np.linspace(-60, 60, n_bins + 1). For each bin b, the latitude window is [bin_edges[b], bin_edges[b+1]].
Per-bin trend fitting: For each bin, selects the latitude slice with da.sel(lat=slice(lat_min, lat_max)). Skips bins with no latitude points. Averages over latitude with .mean('lat') to produce shape (n_time, n_lon). Fits a degree-1 polynomial with polyfit('time', deg=1). Extracts slopes at degree=1. Records mean_slope (mean of slopes over all lon points) and n_lat_points (number of lat grid points in the bin).
The function returns a dict mapping "bin_0", "bin_1", ... to {"mean_slope": float, "n_lat_points": int} for each non-empty bin.
You are given original_workflow.py in the workspace. Port it to Modal and save the result as /app/solution/modal_app.py.
The file must define:
app object.fit_bin_trend) that accepts a bin payload (the bin index, the latitude slice boundaries, the pre-built DataArray values and coordinates, and the bin edge information) and returns the per-bin result dict {"mean_slope": float, "n_lat_points": int} or None if the bin has no latitude points.run_case(case: dict) Modal Function that the verifier calls remotely. It must:
None entries (empty bins)Input (case dict):
"seed": int — random seed for data generation"n_time": int — number of time steps"n_lat": int — number of latitude grid points"n_lon": int — number of longitude grid points"n_bins": int — number of latitude bins to partition the domain intoOutput: dict mapping "bin_0", "bin_1", ... to {"mean_slope": float, "n_lat_points": int} for each non-empty bin. Bins with no latitude points within their window are omitted from the output.
lat: np.linspace(-60, 60, n_lat)lon: np.linspace(0, 120, n_lon)t: np.arange(n_time, dtype=float)data: rng.standard_normal((n_time, n_lat, n_lon)) + t[:, None, None] * 0.02 using np.random.default_rng(seed)da: xr.DataArray(data, dims=['time', 'lat', 'lon'], coords={'time': t, 'lat': lat, 'lon': lon})bin_edges: np.linspace(-60, 60, n_bins + 1) — length n_bins + 1b: lat_min = bin_edges[b], lat_max = bin_edges[b + 1]da_bin = da.sel(lat=slice(lat_min, lat_max))da_mean = da_bin.mean('lat') — shape (n_time, n_lon)fit = da_mean.polyfit('time', deg=1)slopes = fit.polyfit_coefficients.sel(degree=1).values — shape (n_lon,){"mean_slope": float(slopes.mean()), "n_lat_points": len(da_bin.lat)}Expose the workflow as a single Modal remote function: define run_case as an @app.function so the harness can invoke it with run_case.remote(case).
The solution must also use: