Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify moving average example and base on SSP5-8.5 projected methane emissions #1122

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions fluid/example/linked-outputs/moving-average.fld
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ let nthPad n xs =
movingAvg ys window =
[ sum [ nthPad n ys | n <- [ i - window .. i + window ] ] / (1 + 2 * window)
| i <- [ 0 .. length ys - 1 ] ];
movingAvg' ps window =
movingAvg' rs window =
zipWith
(fun year quantity -> {x: year, y: quantity})
(map (fun p -> p.year) ps)
(movingAvg (map (fun p -> p.quantity) ps) window);
let points' =
map (fun p -> { x: p.year, y: floor p.quantity }) dataset
(fun x y -> {x: x, y: y})
(map (fun r -> r.x) rs)
(movingAvg (map (fun r -> r.y) rs) window);
let points =
[ { x: r.year, y: r.emissions } | r <- methane, r.type == "Agriculture" ]
in LineChart {
tickLabels: { x: Default, y: Default },
tickLabels: { x: Rotated, y: Default },
size: { width: 330, height: 285 },
caption: "Moving averages",
plots: [ LinePlot { name: "Moving average", points: movingAvg' dataset 1 },
LinePlot { name: "Original curve", points: points' } ]
caption: "SSP5-8.5 projected methane emissions (Agriculture)",
plots: [ LinePlot { name: "Moving average", points: movingAvg' points 1 },
LinePlot { name: "Original curve", points: points } ]
}
8 changes: 8 additions & 0 deletions fluid/lib/prelude.fld
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ let filter p [] = [];
let ys = filter p xs in
if p x then x : ys else ys;

-- (a -> Option b) -> List a -> List b
let filterMap p [] = [];
filterMap p (x : xs) =
match p x as {
None -> filterMap f xs;
Some y -> y : filterMap f xs
};

-- (a -> b -> a) -> a -> List b -> a
let foldl op z [] = z;
foldl op z (x : xs) = foldl op (op z x) xs;
Expand Down
4 changes: 3 additions & 1 deletion src/Standalone/MovingAverage.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ import Test.Specs.LinkedOutputs (movingAverages_spec)
import Util ((×))

main :: Effect Unit
main = runAffs_ (uncurry drawFig) [ ("fig" × _) <$> loadFig movingAverages_spec.spec ]
main = runAffs_ (uncurry drawFig)
[ ("fig" × _) <$> loadFig movingAverages_spec.spec
]
6 changes: 3 additions & 3 deletions test/Specs/LinkedOutputs.purs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ linkedOutputs_spec2 =
movingAverages_spec :: TestLinkedOutputsSpec
movingAverages_spec =
{ spec:
{ datasets: [ "dataset" ↦ "example/linked-outputs/moving-average-data" ]
, imports: [ "lib/nombre" ]
{ datasets: [ "methane" ↦ "dataset/methane-emissions" ]
, imports: []
, file: File "linked-outputs/moving-average"
, inputs: [ "dataset" ]
, inputs: [ "methane" ]
}
, δ_out: identity -- TODO: make this a non-trivial test
, out_expect: identity
Expand Down