shuoxu2018 commited on
Commit
a68e45a
·
verified ·
1 Parent(s): 59acee4

Upload 2 files

Browse files
Files changed (2) hide show
  1. scripts/create_viewer_tables.py +158 -0
  2. viewer/README.md +21 -0
scripts/create_viewer_tables.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Create lightweight Parquet tables for the Hugging Face Dataset Viewer.
3
+
4
+ The full multidimensional arrays remain in NPZ files. Each Parquet row
5
+ corresponds to one NPZ sample and contains only identifiers, shapes, and
6
+ compact summary statistics.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import argparse
12
+ from pathlib import Path
13
+ from typing import Any
14
+
15
+ import numpy as np
16
+ import pandas as pd
17
+
18
+
19
+ DATASETS = {
20
+ "global_mask": {
21
+ "train": "GlobalMask/train/globalmask_training_dataset.npz",
22
+ "test": "GlobalMask/test/globalmask_testing_dataset.npz",
23
+ },
24
+ "above": {
25
+ "train": "InSituMatched/above/train/InSituMatched_above_train.npz",
26
+ "test": "InSituMatched/above/test/InSituMatched_above_test.npz",
27
+ },
28
+ "ameriflux": {
29
+ "train": "InSituMatched/ameriflux/train/InSituMatched_ameriflux_train.npz",
30
+ "test": "InSituMatched/ameriflux/test/InSituMatched_ameriflux_test.npz",
31
+ },
32
+ "fluxnet": {
33
+ "train": "InSituMatched/fluxnet/train/InSituMatched_fluxnet_train.npz",
34
+ "test": "InSituMatched/fluxnet/test/InSituMatched_fluxnet_test.npz",
35
+ },
36
+ "icos_ww": {
37
+ "train": "InSituMatched/icos-ww/train/InSituMatched_icos-ww_train.npz",
38
+ "test": "InSituMatched/icos-ww/test/InSituMatched_icos-ww_test.npz",
39
+ },
40
+ "mix": {
41
+ "train": "InSituMatched/mix/train/InSituMatched_mix_train.npz",
42
+ "test": "InSituMatched/mix/test/InSituMatched_mix_test.npz",
43
+ },
44
+ }
45
+
46
+
47
+ def shape_text(array: np.ndarray | None) -> str:
48
+ if array is None:
49
+ return ""
50
+ return " × ".join(str(v) for v in array.shape[1:])
51
+
52
+
53
+ def finite_mean(values: np.ndarray) -> float | None:
54
+ finite = np.isfinite(values)
55
+ if not finite.any():
56
+ return None
57
+ return float(values[finite].mean())
58
+
59
+
60
+ def finite_fraction(values: np.ndarray) -> float:
61
+ return float(np.isfinite(values).mean())
62
+
63
+
64
+ def build_rows(npz_path: Path, config_name: str, split: str, relative_path: str) -> list[dict[str, Any]]:
65
+ with np.load(npz_path, allow_pickle=False) as data:
66
+ arrays = {key: data[key] for key in data.files}
67
+
68
+ if "ed_simulation_x" not in arrays:
69
+ raise KeyError(f"{npz_path}: missing required key 'ed_simulation_x'")
70
+
71
+ sample_count = arrays["ed_simulation_x"].shape[0]
72
+
73
+ for key, value in arrays.items():
74
+ if value.ndim > 0 and value.shape[0] != sample_count:
75
+ raise ValueError(
76
+ f"{npz_path}: key {key!r} is not sample-first: "
77
+ f"first dimension {value.shape[0]} != {sample_count}"
78
+ )
79
+
80
+ x = arrays.get("ed_simulation_x")
81
+ y = arrays.get("ed_simulation_y")
82
+ observed = arrays.get("observed_y")
83
+ age = arrays.get("lidar_age_weight_fraction")
84
+ esa_bl = arrays.get("esa_cci_bl_fraction")
85
+ esa_nl = arrays.get("esa_cci_nl_fraction")
86
+ esa_gs = arrays.get("esa_cci_gs_fraction")
87
+ pft_bl = arrays.get("ed_simulation_pft_bl")
88
+ pft_nl = arrays.get("ed_simulation_pft_nl")
89
+ pft_gs = arrays.get("ed_simulation_pft_gs")
90
+
91
+ rows: list[dict[str, Any]] = []
92
+ for i in range(sample_count):
93
+ row: dict[str, Any] = {
94
+ "sample_index": i,
95
+ "split": split,
96
+ "subset": config_name,
97
+ "data_file": relative_path,
98
+ "ed_simulation_x_shape": shape_text(x),
99
+ "ed_simulation_y_shape": shape_text(y),
100
+ "observed_y_shape": shape_text(observed),
101
+ }
102
+
103
+ if age is not None:
104
+ row["lidar_age_weight_sum"] = float(np.sum(age[i]))
105
+ if esa_bl is not None:
106
+ row["esa_cci_bl_mean"] = finite_mean(esa_bl[i])
107
+ if esa_nl is not None:
108
+ row["esa_cci_nl_mean"] = finite_mean(esa_nl[i])
109
+ if esa_gs is not None:
110
+ row["esa_cci_gs_mean"] = finite_mean(esa_gs[i])
111
+ if observed is not None:
112
+ row["observed_y_valid_fraction"] = finite_fraction(observed[i])
113
+ if pft_bl is not None:
114
+ row["ed_simulation_pft_bl_mean"] = finite_mean(pft_bl[i])
115
+ if pft_nl is not None:
116
+ row["ed_simulation_pft_nl_mean"] = finite_mean(pft_nl[i])
117
+ if pft_gs is not None:
118
+ row["ed_simulation_pft_gs_mean"] = finite_mean(pft_gs[i])
119
+
120
+ rows.append(row)
121
+
122
+ return rows
123
+
124
+
125
+ def output_path(root: Path, config_name: str, split: str) -> Path:
126
+ if config_name == "global_mask":
127
+ return root / "viewer" / "global_mask" / f"{split}.parquet"
128
+ return root / "viewer" / "insitu_matched" / config_name / f"{split}.parquet"
129
+
130
+
131
+ def main() -> None:
132
+ parser = argparse.ArgumentParser()
133
+ parser.add_argument(
134
+ "--root",
135
+ type=Path,
136
+ default=Path("."),
137
+ help="DERE repository root (default: current directory)",
138
+ )
139
+ args = parser.parse_args()
140
+ root = args.root.resolve()
141
+
142
+ for config_name, splits in DATASETS.items():
143
+ for split, relative_path in splits.items():
144
+ npz_path = root / relative_path
145
+ if not npz_path.exists():
146
+ raise FileNotFoundError(f"Missing data file: {npz_path}")
147
+
148
+ rows = build_rows(npz_path, config_name, split, relative_path)
149
+ destination = output_path(root, config_name, split)
150
+ destination.parent.mkdir(parents=True, exist_ok=True)
151
+
152
+ frame = pd.DataFrame(rows)
153
+ frame.to_parquet(destination, index=False)
154
+ print(f"Wrote {destination.relative_to(root)} ({len(frame)} rows)")
155
+
156
+
157
+ if __name__ == "__main__":
158
+ main()
viewer/README.md ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dataset Viewer tables
2
+
3
+ This directory contains lightweight Parquet tables used by the Hugging Face
4
+ Dataset Viewer.
5
+
6
+ Each row corresponds to one sample in an NPZ file and includes:
7
+
8
+ - sample index, split, subset, and source NPZ path
9
+ - per-sample array shapes
10
+ - compact PFT and forest-age summaries
11
+ - the valid-observation fraction for in-situ samples
12
+
13
+ The full multidimensional scientific arrays are not duplicated or flattened
14
+ here. They remain in the NPZ files under `GlobalMask/` and `InSituMatched/`.
15
+
16
+ Regenerate the Viewer tables from the repository root with:
17
+
18
+ ```bash
19
+ pip install pandas pyarrow
20
+ python scripts/create_viewer_tables.py --root .
21
+ ```