Skip to content

Commit e1343d5

Browse files
hydra works with nbdev
1 parent a07fe6e commit e1343d5

File tree

5 files changed

+94
-21
lines changed

5 files changed

+94
-21
lines changed

conf/config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
db:
2+
driver: mysql
3+
user: omry
4+
password: secret
5+
6+
hydra:
7+
run:
8+
dir: logs/${now:%Y-%m-%d}/${now:%H-%M-%S}
0 Bytes
Binary file not shown.
676 Bytes
Binary file not shown.

era5_sandbox/core.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,28 @@ def _expand_path(
2828
return path
2929

3030
# %% ../notes/00_core.ipynb 5
31-
def describe():
32-
print("This package fetches ERA5 data.")
31+
@hydra.main(version_base=None, config_path="../conf", config_name="config")
32+
def describe(
33+
cfg: DictConfig=None, # Configuration file
34+
)-> None:
35+
"Describe the configuration file used by Hydra for the pipeline"
36+
37+
if cfg is None:
38+
cfg = OmegaConf.create()
39+
40+
print("This package fetches ERA5 data. The following is the config file used by Hydra for the pipeline:\n")
41+
print(OmegaConf.to_yaml(cfg))
3342

3443
# %% ../notes/00_core.ipynb 6
35-
#@hydra.main(version_base=None, config_path=here() / "conf", config_name="config")
44+
@hydra.main(version_base=None, config_path="../conf", config_name="config")
3645
def testAPI(
46+
cfg: DictConfig=None,
3747
output_path:str=None,
3848
dataset:str="reanalysis-era5-pressure-levels",
39-
remove:bool=False,
40-
cfg: DictConfig = None
49+
remove:bool=True
4150
)-> bool:
4251

43-
#print(OmegaConf.to_yaml(cfg))
52+
print(OmegaConf.to_yaml(cfg))
4453

4554
try:
4655
client = cdsapi.Client()
@@ -80,6 +89,6 @@ def testAPI(
8089

8190
except Exception as e:
8291
print("API connection test failed.")
83-
print("Did you set up your API key with CDS? If not, please visit https://cds.climate.copernicus.eu/api-how-to")
92+
print("Did you set up your API key with CDS? If not, please visit https://cds.climate.copernicus.eu/how-to-api#install-the-cds-api-client")
8493
print("Error: {}".format(e))
8594
return False

notes/00_core.ipynb

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
},
3232
{
3333
"cell_type": "code",
34-
"execution_count": null,
34+
"execution_count": 2,
3535
"metadata": {},
3636
"outputs": [],
3737
"source": [
@@ -46,7 +46,7 @@
4646
},
4747
{
4848
"cell_type": "code",
49-
"execution_count": null,
49+
"execution_count": 3,
5050
"metadata": {},
5151
"outputs": [],
5252
"source": [
@@ -72,26 +72,35 @@
7272
"outputs": [],
7373
"source": [
7474
"#| export\n",
75-
"def describe():\n",
76-
" print(\"This package fetches ERA5 data.\")"
75+
"@hydra.main(version_base=None, config_path=\"../conf\", config_name=\"config\")\n",
76+
"def describe(\n",
77+
" cfg: DictConfig=None, # Configuration file\n",
78+
" )-> None:\n",
79+
" \"Describe the configuration file used by Hydra for the pipeline\"\n",
80+
" \n",
81+
" if cfg is None:\n",
82+
" cfg = OmegaConf.create()\n",
83+
" \n",
84+
" print(\"This package fetches ERA5 data. The following is the config file used by Hydra for the pipeline:\\n\")\n",
85+
" print(OmegaConf.to_yaml(cfg))"
7786
]
7887
},
7988
{
8089
"cell_type": "code",
81-
"execution_count": null,
90+
"execution_count": 21,
8291
"metadata": {},
8392
"outputs": [],
8493
"source": [
8594
"#| export\n",
86-
"#@hydra.main(version_base=None, config_path=here() / \"conf\", config_name=\"config\")\n",
95+
"@hydra.main(version_base=None, config_path=\"../conf\", config_name=\"config\")\n",
8796
"def testAPI(\n",
97+
" cfg: DictConfig=None,\n",
8898
" output_path:str=None,\n",
8999
" dataset:str=\"reanalysis-era5-pressure-levels\",\n",
90-
" remove:bool=False,\n",
91-
" cfg: DictConfig = None\n",
100+
" remove:bool=True\n",
92101
" )-> bool: \n",
93102
" \n",
94-
" #print(OmegaConf.to_yaml(cfg))\n",
103+
" print(OmegaConf.to_yaml(cfg))\n",
95104
"\n",
96105
" try:\n",
97106
" client = cdsapi.Client()\n",
@@ -131,11 +140,52 @@
131140
"\n",
132141
" except Exception as e:\n",
133142
" print(\"API connection test failed.\")\n",
134-
" print(\"Did you set up your API key with CDS? If not, please visit https://cds.climate.copernicus.eu/api-how-to\")\n",
143+
" print(\"Did you set up your API key with CDS? If not, please visit https://cds.climate.copernicus.eu/how-to-api#install-the-cds-api-client\")\n",
135144
" print(\"Error: {}\".format(e))\n",
136145
" return False"
137146
]
138147
},
148+
{
149+
"cell_type": "markdown",
150+
"metadata": {},
151+
"source": [
152+
"We can see that this API tester tool works with Hydra configuration:"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": 33,
158+
"metadata": {},
159+
"outputs": [
160+
{
161+
"name": "stdout",
162+
"output_type": "stream",
163+
"text": [
164+
"This package fetches ERA5 data. The following is the config file used by Hydra for the pipeline:\n",
165+
"\n",
166+
"db:\n",
167+
" driver: mysql\n",
168+
" user: omry\n",
169+
" password: secret\n",
170+
"\n"
171+
]
172+
}
173+
],
174+
"source": [
175+
"from hydra import initialize, compose\n",
176+
"from omegaconf import OmegaConf\n",
177+
"\n",
178+
"# unfortunately, we have to use the initialize function to load the config file\n",
179+
"# this is because the @hydra decorator does not work with Notebooks very well\n",
180+
"# this is a known issue with Hydra: https://gist.github.com/bdsaglam/586704a98336a0cf0a65a6e7c247d248\n",
181+
"# \n",
182+
"# just use the relative path from the notebook to the config dir\n",
183+
"with initialize(version_base=None, config_path=\"../conf\"):\n",
184+
" cfg = compose(config_name='config.yaml')\n",
185+
"\n",
186+
"describe(cfg)"
187+
]
188+
},
139189
{
140190
"cell_type": "code",
141191
"execution_count": null,
@@ -144,10 +194,8 @@
144194
"source": [
145195
"#export\n",
146196
"if __name__ == \"__main__\":\n",
147-
" describe()\n",
148-
" testAPI()\n",
149-
"else:\n",
150-
" pass"
197+
" # for testing\n",
198+
" describe()"
151199
]
152200
},
153201
{
@@ -168,7 +216,15 @@
168216
"name": "python3"
169217
},
170218
"language_info": {
219+
"codemirror_mode": {
220+
"name": "ipython",
221+
"version": 3
222+
},
223+
"file_extension": ".py",
224+
"mimetype": "text/x-python",
171225
"name": "python",
226+
"nbconvert_exporter": "python",
227+
"pygments_lexer": "ipython3",
172228
"version": "3.13.2"
173229
}
174230
},

0 commit comments

Comments
 (0)