ocf-data-sampler 0.2.16__py3-none-any.whl → 0.2.18__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ocf-data-sampler might be problematic. Click here for more details.

@@ -6,25 +6,54 @@ import pandas as pd
6
6
  import xarray as xr
7
7
 
8
8
 
9
- def open_gsp(zarr_path: str) -> xr.DataArray:
9
+ def get_gsp_boundaries(version: str) -> pd.DataFrame:
10
+ """Get the GSP boundaries for a given version.
11
+
12
+ Args:
13
+ version: Version of the GSP boundaries to use. Options are "20220314" or "20250109".
14
+
15
+ Returns:
16
+ pd.DataFrame: The GSP boundaries
17
+ """
18
+ if version not in ["20220314", "20250109"]:
19
+ raise ValueError(
20
+ "Invalid version. Options are '20220314' or '20250109'.",
21
+ )
22
+
23
+ return pd.read_csv(
24
+ files("ocf_data_sampler.data").joinpath(f"uk_gsp_locations_{version}.csv"),
25
+ index_col="gsp_id",
26
+ )
27
+
28
+
29
+ def open_gsp(zarr_path: str, boundaries_version: str = "20220314") -> xr.DataArray:
10
30
  """Open the GSP data.
11
31
 
12
32
  Args:
13
33
  zarr_path: Path to the GSP zarr data
34
+ boundaries_version: Version of the GSP boundaries to use. Options are "20220314" or
35
+ "20250109".
14
36
 
15
37
  Returns:
16
38
  xr.DataArray: The opened GSP data
17
39
  """
18
- ds = xr.open_zarr(zarr_path)
19
-
20
- ds = ds.rename({"datetime_gmt": "time_utc"})
21
-
22
40
  # Load UK GSP locations
23
- df_gsp_loc = pd.read_csv(
24
- files("ocf_data_sampler.data").joinpath("uk_gsp_locations.csv"),
25
- index_col="gsp_id",
41
+ df_gsp_loc = get_gsp_boundaries(boundaries_version)
42
+
43
+ # Open the GSP generation data
44
+ ds = (
45
+ xr.open_zarr(zarr_path)
46
+ .rename({"datetime_gmt": "time_utc"})
26
47
  )
27
48
 
49
+ if not (ds.gsp_id.isin(df_gsp_loc.index)).all():
50
+ raise ValueError(
51
+ "Some GSP IDs in the GSP generation data are available in the locations file.",
52
+ )
53
+
54
+ # Select the locations by the GSP IDs in the generation data
55
+ df_gsp_loc = df_gsp_loc.loc[ds.gsp_id.values]
56
+
28
57
  # Add locations and capacities as coordinates for each GSP and datetime
29
58
  ds = ds.assign_coords(
30
59
  x_osgb=(df_gsp_loc.x_osgb.to_xarray()),
@@ -6,8 +6,10 @@ from ocf_data_sampler.config import InputData
6
6
  from ocf_data_sampler.load import open_gsp, open_nwp, open_sat_data, open_site
7
7
 
8
8
 
9
- def get_dataset_dict(input_config: InputData, gsp_ids: list[int] | None = None)\
10
- -> dict[str, dict[xr.DataArray] | xr.DataArray]:
9
+ def get_dataset_dict(
10
+ input_config: InputData,
11
+ gsp_ids: list[int] | None = None,
12
+ ) -> dict[str, dict[xr.DataArray] | xr.DataArray]:
11
13
  """Construct dictionary of all of the input data sources.
12
14
 
13
15
  Args:
@@ -19,7 +21,10 @@ def get_dataset_dict(input_config: InputData, gsp_ids: list[int] | None = None)\
19
21
  # Load GSP data unless the path is None
20
22
  if input_config.gsp and input_config.gsp.zarr_path:
21
23
 
22
- da_gsp = open_gsp(zarr_path=input_config.gsp.zarr_path).compute()
24
+ da_gsp = open_gsp(
25
+ zarr_path=input_config.gsp.zarr_path,
26
+ boundaries_version=input_config.gsp.boundaries_version,
27
+ ).compute()
23
28
 
24
29
  if gsp_ids is None:
25
30
  # Remove national (gsp_id=0)
@@ -1,7 +1,5 @@
1
1
  """Torch dataset for UK PVNet."""
2
2
 
3
- from importlib.resources import files
4
-
5
3
  import numpy as np
6
4
  import pandas as pd
7
5
  import xarray as xr
@@ -9,6 +7,7 @@ from torch.utils.data import Dataset
9
7
  from typing_extensions import override
10
8
 
11
9
  from ocf_data_sampler.config import Configuration, load_yaml_configuration
10
+ from ocf_data_sampler.load.gsp import get_gsp_boundaries
12
11
  from ocf_data_sampler.load.load_dataset import get_dataset_dict
13
12
  from ocf_data_sampler.numpy_sample import (
14
13
  convert_gsp_to_numpy_sample,
@@ -47,22 +46,26 @@ def compute(xarray_dict: dict) -> dict:
47
46
  return xarray_dict
48
47
 
49
48
 
50
- def get_gsp_locations(gsp_ids: list[int] | None = None) -> list[Location]:
49
+ def get_gsp_locations(
50
+ gsp_ids: list[int] | None = None,
51
+ version: str = "20220314",
52
+ ) -> list[Location]:
51
53
  """Get list of locations of all GSPs.
52
54
 
53
55
  Args:
54
- gsp_ids: List of GSP IDs to include. Defaults to all
56
+ gsp_ids: List of GSP IDs to include. Defaults to all GSPs except national
57
+ version: Version of GSP boundaries to use. Defaults to "20220314"
55
58
  """
59
+ df_gsp_loc = get_gsp_boundaries(version)
60
+
61
+ # Default GSP IDs is all except national (gsp_id=0)
56
62
  if gsp_ids is None:
57
- gsp_ids = list(range(1, 318))
63
+ gsp_ids = df_gsp_loc.index.values
64
+ gsp_ids = gsp_ids[gsp_ids != 0]
58
65
 
59
- locations = []
66
+ df_gsp_loc = df_gsp_loc.loc[gsp_ids]
60
67
 
61
- # Load UK GSP locations
62
- df_gsp_loc = pd.read_csv(
63
- files("ocf_data_sampler.data").joinpath("uk_gsp_locations.csv"),
64
- index_col="gsp_id",
65
- )
68
+ locations = []
66
69
 
67
70
  for gsp_id in gsp_ids:
68
71
  locations.append(
@@ -108,7 +111,10 @@ class AbstractPVNetUKDataset(Dataset):
108
111
  valid_t0_times = valid_t0_times[valid_t0_times <= pd.Timestamp(end_time)]
109
112
 
110
113
  # Construct list of locations to sample from
111
- self.locations = get_gsp_locations(gsp_ids)
114
+ self.locations = get_gsp_locations(
115
+ gsp_ids,
116
+ version=config.input_data.gsp.boundaries_version,
117
+ )
112
118
  self.valid_t0_times = valid_t0_times
113
119
 
114
120
  # Assign config and input data to self
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ocf-data-sampler
3
- Version: 0.2.16
3
+ Version: 0.2.18
4
4
  Author: James Fulton, Peter Dudfield
5
5
  Author-email: Open Climate Fix team <info@openclimatefix.org>
6
6
  License: MIT License
@@ -2,12 +2,13 @@ ocf_data_sampler/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,
2
2
  ocf_data_sampler/utils.py,sha256=DjuneGGisl08ENvPZV_lrcX4b2NCKJC1ZpXgIpxuQi4,290
3
3
  ocf_data_sampler/config/__init__.py,sha256=O29mbH0XG2gIY1g3BaveGCnpBO2SFqdu-qzJ7a6evl0,223
4
4
  ocf_data_sampler/config/load.py,sha256=LL-7wemI8o4KPkx35j-wQ3HjsMvDgqXr7G46IcASfnU,632
5
- ocf_data_sampler/config/model.py,sha256=pb02qtCmWhJhrU3_T_gUzC7i2_JcO8xGwwhKGd8yMuk,10209
5
+ ocf_data_sampler/config/model.py,sha256=SyjtlSK6gzQHWUfgX3VNKYLODyiKuD0Mu4hlm9GoHeg,10427
6
6
  ocf_data_sampler/config/save.py,sha256=m8SPw5rXjkMm1rByjh3pK5StdBi4e8ysnn3jQopdRaI,1064
7
- ocf_data_sampler/data/uk_gsp_locations.csv,sha256=RSh7DRh55E3n8lVAaWXGTaXXHevZZtI58td4d4DhGos,10415772
7
+ ocf_data_sampler/data/uk_gsp_locations_20220314.csv,sha256=RSh7DRh55E3n8lVAaWXGTaXXHevZZtI58td4d4DhGos,10415772
8
+ ocf_data_sampler/data/uk_gsp_locations_20250109.csv,sha256=XZISFatnbpO9j8LwaxNKFzQSjs6hcHFsV8a9uDDpy2E,9055334
8
9
  ocf_data_sampler/load/__init__.py,sha256=-vQP9g0UOWdVbjEGyVX_ipa7R1btmiETIKAf6aw4d78,201
9
- ocf_data_sampler/load/gsp.py,sha256=keB3Nv_CNK1P6pS9Kdfc8PoZXTI1_YFN-spsvEv_Ewc,899
10
- ocf_data_sampler/load/load_dataset.py,sha256=Cn-yz7RgHR2HkH3xQM1njivVEkp8rZC3KXXgcidwuME,1863
10
+ ocf_data_sampler/load/gsp.py,sha256=UfPxwHw2Dw2xYSO5Al28oTamgnEM_n_4bYXsqGwY5Tc,1884
11
+ ocf_data_sampler/load/load_dataset.py,sha256=sIi0nkijR_-1fRfW5JcXNTR0ccGbpkhxb7JX_zjJ-W4,1956
11
12
  ocf_data_sampler/load/satellite.py,sha256=E7Ln7Y60Qr1RTV-_R71YoxXQM-Ca7Y1faIo3oKB2eFk,2292
12
13
  ocf_data_sampler/load/site.py,sha256=zOzlWk6pYZBB5daqG8URGksmDXWKrkutUvN8uALAIh8,1468
13
14
  ocf_data_sampler/load/utils.py,sha256=sZ0-zzconcLkVQwAkCYrqKDo98Hrh5ChdiQJv5Bh91g,2040
@@ -38,7 +39,7 @@ ocf_data_sampler/select/location.py,sha256=AZvGR8y62opiW7zACGXjoOtBEWRfSLOZIA73O
38
39
  ocf_data_sampler/select/select_spatial_slice.py,sha256=liAqIa-Amj58pOqx5r16i99HURj9oQ41j7gnPgRDQP4,8201
39
40
  ocf_data_sampler/select/select_time_slice.py,sha256=HeHbwZ0CP03x0-LaJtpbSdtpLufwVTR73p6wH6O_PS8,5513
40
41
  ocf_data_sampler/torch_datasets/datasets/__init__.py,sha256=jfJSFcR0eO1AqeH7S3KnGjsBqVZT5w3oyi784PUR6Q0,146
41
- ocf_data_sampler/torch_datasets/datasets/pvnet_uk.py,sha256=9BJ4wVcZUMEzStVCbbWrf2eK8WPpV9SoeOQviZktHAc,12355
42
+ ocf_data_sampler/torch_datasets/datasets/pvnet_uk.py,sha256=ZV2FoMPxFU2aPTWipj9HhJhGfrEg9MYOJRNR8aFcmvs,12613
42
43
  ocf_data_sampler/torch_datasets/datasets/site.py,sha256=nRUlhXQQGVrTuBmE1QnwXAUsPTXz0dsezlQjwK71jIQ,17641
43
44
  ocf_data_sampler/torch_datasets/sample/__init__.py,sha256=GL84vdZl_SjHDGVyh9Uekx2XhPYuZ0dnO3l6f6KXnHI,100
44
45
  ocf_data_sampler/torch_datasets/sample/base.py,sha256=cQ1oIyhdmlotejZK8B3Cw6MNvpdnBPD8G_o2h7Ye4Vc,2206
@@ -51,9 +52,10 @@ ocf_data_sampler/torch_datasets/utils/spatial_slice_for_dataset.py,sha256=Hvz0wH
51
52
  ocf_data_sampler/torch_datasets/utils/time_slice_for_dataset.py,sha256=1DN6VsWWdLvkpJxodZtBRDUgC4vJE2td_RP5J3ZqPNw,4268
52
53
  ocf_data_sampler/torch_datasets/utils/valid_time_periods.py,sha256=xcy75cVxl0WrglnX5YUAFjXXlO2GwEBHWyqo8TDuiOA,4714
53
54
  ocf_data_sampler/torch_datasets/utils/validation_utils.py,sha256=YqmT-lExWlI8_ul3l0EP73Ik002fStr_bhsZh9mQqEU,4735
55
+ scripts/download_gsp_location_data.py,sha256=rRDXMoqX-RYY4jPdxhdlxJGhWdl6r245F5UARgKV6P4,3121
54
56
  scripts/refactor_site.py,sha256=skzvsPP0Cn9yTKndzkilyNcGz4DZ88ctvCJ0XrBdc2A,3135
55
57
  utils/compute_icon_mean_stddev.py,sha256=a1oWMRMnny39rV-dvu8rcx85sb4bXzPFrR1gkUr4Jpg,2296
56
- ocf_data_sampler-0.2.16.dist-info/METADATA,sha256=JplbQHR2wlMPCQEDXYk5GPDgu15wYKV3SYZmL0kH2Ho,11581
57
- ocf_data_sampler-0.2.16.dist-info/WHEEL,sha256=pxyMxgL8-pra_rKaQ4drOZAegBVuX-G_4nRHjjgWbmo,91
58
- ocf_data_sampler-0.2.16.dist-info/top_level.txt,sha256=LEFU4Uk-PEo72QGLAfnVZIUEm37Q8mKuMeg_Xk-p33g,31
59
- ocf_data_sampler-0.2.16.dist-info/RECORD,,
58
+ ocf_data_sampler-0.2.18.dist-info/METADATA,sha256=tW_PCGhhXGaFnPp3ChT8cmX640KcWeaAnCXRy4xsccw,11581
59
+ ocf_data_sampler-0.2.18.dist-info/WHEEL,sha256=0CuiUZ_p9E4cD6NyLD6UG80LBXYyiSYZOKDm5lp32xk,91
60
+ ocf_data_sampler-0.2.18.dist-info/top_level.txt,sha256=LEFU4Uk-PEo72QGLAfnVZIUEm37Q8mKuMeg_Xk-p33g,31
61
+ ocf_data_sampler-0.2.18.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (79.0.0)
2
+ Generator: setuptools (80.3.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -0,0 +1,95 @@
1
+ """This script downloads the GSP location data from the Neso API and saves it to a CSV file.
2
+
3
+ This script was used to create the `uk_gsp_locations_20250109.csv` file in the `data` directory.
4
+ """
5
+
6
+ import io
7
+ import os
8
+ import tempfile
9
+ import zipfile
10
+
11
+ import geopandas as gpd
12
+ import pandas as pd
13
+ import requests
14
+
15
+ SAVE_PATH = "uk_gsp_locations_20250109.csv"
16
+
17
+ # --- Configuration ---
18
+ GSP_REGIONS_URL = (
19
+ "https://api.neso.energy/dataset/2810092e-d4b2-472f-b955-d8bea01f9ec0/"
20
+ "resource/d95e8c1b-9cd9-41dd-aacb-4b53b8c07c20/download/gsp_regions_20250109.zip"
21
+ )
22
+ # This is the path to the OSBG version of the boundaries. The lon-lats version can be found at:
23
+ # Proj_4326/GSP_regions_4326_20250109.geojson
24
+ GSP_REGIONS_GEOJSON_PATH_IN_ZIP = "Proj_27700/GSP_regions_27700_20250109.geojson"
25
+ GSP_NAME_MAP_URL = "https://api.pvlive.uk/pvlive/api/v4/gsp_list"
26
+ SAVE_PATH = "uk_gsp_locations_20250109.csv"
27
+ # --- End Configuration ---
28
+
29
+
30
+ with tempfile.TemporaryDirectory() as tmpdirname:
31
+
32
+ # Download the GSP regions
33
+ response_regions = requests.get(GSP_REGIONS_URL, timeout=30)
34
+ response_regions.raise_for_status()
35
+
36
+ # Unzip
37
+ with zipfile.ZipFile(io.BytesIO(response_regions.content)) as z:
38
+ geojson_extract_path = os.path.join(tmpdirname, GSP_REGIONS_GEOJSON_PATH_IN_ZIP)
39
+ z.extract(GSP_REGIONS_GEOJSON_PATH_IN_ZIP, tmpdirname)
40
+
41
+ # Load the GSP regions
42
+ df_bound = gpd.read_file(geojson_extract_path)
43
+
44
+ # Download the GSP name mapping
45
+ response_map = requests.get(GSP_NAME_MAP_URL, timeout=10)
46
+ response_map.raise_for_status()
47
+
48
+ # Load the GSP name mapping
49
+ gsp_name_map = response_map.json()
50
+ df_gsp_name_map = (
51
+ pd.DataFrame(data=gsp_name_map["data"], columns=gsp_name_map["meta"])
52
+ .drop("pes_id", axis=1)
53
+ )
54
+
55
+
56
+ def combine_gsps(gdf: gpd.GeoDataFrame) -> gpd.GeoSeries:
57
+ """Combine GSPs which have been split into mutliple rows."""
58
+ # If only one row for the GSP name then just return the row
59
+ if len(gdf)==0:
60
+ return gdf.iloc[0]
61
+
62
+ # If multiple rows for the GSP then get union of the GSP shapes
63
+ else:
64
+ return gpd.GeoSeries(gdf.unary_union, index=["geometry"], crs=gdf.crs)
65
+
66
+
67
+ # Combine GSPs which have been split into multiple rows
68
+ df_bound = (
69
+ df_bound.groupby("GSPs")
70
+ .apply(combine_gsps, include_groups=False)
71
+ .reset_index()
72
+ )
73
+
74
+ # Add the PVLive GSP ID for each GSP
75
+ df_bound = (
76
+ df_bound.merge(df_gsp_name_map, left_on="GSPs", right_on="gsp_name")
77
+ .drop("GSPs", axis=1)
78
+ )
79
+
80
+ # Add the national GSP - this is the union of all GSPs
81
+ national_boundaries = gpd.GeoDataFrame(
82
+ [["NATIONAL", df_bound.unary_union, 0]],
83
+ columns=["gsp_name", "geometry", "gsp_id"],
84
+ crs=df_bound.crs,
85
+ )
86
+
87
+ df_bound = pd.concat([national_boundaries, df_bound], ignore_index=True)
88
+
89
+ # Add the coordinates for the centroid of each GSP
90
+ df_bound["x_osgb"] = df_bound.geometry.centroid.x
91
+ df_bound["y_osgb"] = df_bound.geometry.centroid.y
92
+
93
+ # Reorder columns, sort by gsp_id (increasing) and save
94
+ columns = ["gsp_id", "gsp_name", "geometry", "x_osgb", "y_osgb"]
95
+ df_bound[columns].sort_values("gsp_id").to_csv(SAVE_PATH, index=False)