Skip to content

Commit 4e12d76

Browse files
authored
Provide the EM Geosci apps (#8)
* Provide the EM Geosci apps * Hide outputs from install steps
1 parent 1c927c0 commit 4e12d76

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed

_toc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ parts:
1515
- file: practical-field-modelling/02b_Modelling-with-Swarm-data
1616
- caption: EM Induction
1717
chapters:
18+
- file: em-induction/00a_EM-GeoSci-setup
1819
- file: em-induction/02a_geomagnetic-depth-sounding
1920
- caption: Data Assimilation
2021
chapters:
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "2e4f84b4-e60c-4ab4-bc32-055f843aaeb5",
6+
"metadata": {
7+
"editable": true,
8+
"slideshow": {
9+
"slide_type": ""
10+
},
11+
"tags": []
12+
},
13+
"source": [
14+
"# EM GeoSci Apps"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"id": "e7e5f9ba-74b6-4252-a50a-054864b12444",
20+
"metadata": {},
21+
"source": [
22+
"- See <https://em.geosci.xyz> for more information\n",
23+
"- Instructions for running the apps using Binder: <https://em.geosci.xyz/apps.html>\n",
24+
"\n",
25+
"***Follow the steps below to use some of the apps directly in the Swarm VRE***"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"id": "260d8214-bef7-4ab6-828a-c32a15a09a84",
31+
"metadata": {},
32+
"source": [
33+
"## Set up the GeoSci apps environment"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"id": "6491990c-e2cd-4e66-931d-2253900e5daf",
40+
"metadata": {
41+
"editable": true,
42+
"slideshow": {
43+
"slide_type": ""
44+
},
45+
"tags": [
46+
"hide-output"
47+
]
48+
},
49+
"outputs": [],
50+
"source": [
51+
"!git clone https://github.com/geoscixyz/em-apps"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"id": "48aa8831-a1e1-4124-896a-7cd5fba7bb91",
58+
"metadata": {
59+
"editable": true,
60+
"scrolled": true,
61+
"slideshow": {
62+
"slide_type": ""
63+
},
64+
"tags": [
65+
"hide-output"
66+
]
67+
},
68+
"outputs": [],
69+
"source": [
70+
"!mamba env create -f em-apps/environment.yml -n geosci-labs\n",
71+
"!mamba install -n geosci-labs simpeg\n",
72+
"!mamba run -n geosci-labs python -m ipykernel install --user --name=geosci-labs --display-name=\"geosci-labs\"\n",
73+
"!python update_notebook_kernels.py"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"id": "6b972335-c52d-465a-a88d-55964b72ff65",
79+
"metadata": {},
80+
"source": [
81+
"## Open the MT Layered Earth notebook\n",
82+
"\n",
83+
"- Open [MT_LayeredEarth](./em-apps/notebooks/em/MT_LayeredEarth.ipynb)"
84+
]
85+
}
86+
],
87+
"metadata": {
88+
"kernelspec": {
89+
"display_name": "Python 3 (ipykernel)",
90+
"language": "python",
91+
"name": "python3"
92+
},
93+
"language_info": {
94+
"codemirror_mode": {
95+
"name": "ipython",
96+
"version": 3
97+
},
98+
"file_extension": ".py",
99+
"mimetype": "text/x-python",
100+
"name": "python",
101+
"nbconvert_exporter": "python",
102+
"pygments_lexer": "ipython3",
103+
"version": "3.11.6"
104+
}
105+
},
106+
"nbformat": 4,
107+
"nbformat_minor": 5
108+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Script to update all Jupyter notebook kernels to use 'geosci-labs'
4+
This script will:
5+
1. Find all .ipynb files in the current directory and subdirectories
6+
2. Update their kernel metadata to use the 'geosci-labs' kernel
7+
3. Preserve all other metadata and content
8+
"""
9+
10+
import os
11+
import json
12+
import glob
13+
14+
def update_notebook_kernel(notebook_path, target_kernel_name="geosci-labs"):
15+
"""
16+
Update a single notebook's kernel metadata
17+
18+
Args:
19+
notebook_path (str): Path to the notebook file
20+
target_kernel_name (str): Name of the target kernel
21+
22+
Returns:
23+
bool: True if update was successful, False otherwise
24+
"""
25+
try:
26+
# Read the notebook
27+
with open(notebook_path, 'r', encoding='utf-8') as f:
28+
notebook_data = json.load(f)
29+
30+
# Get current kernel info for logging
31+
current_kernel = "unknown"
32+
if 'metadata' in notebook_data and 'kernelspec' in notebook_data['metadata']:
33+
current_kernel = notebook_data['metadata']['kernelspec'].get('name', 'unknown')
34+
35+
# Skip if already using target kernel
36+
if current_kernel == target_kernel_name:
37+
print(f"✓ {notebook_path} - Already using {target_kernel_name}")
38+
return True
39+
40+
# Update kernel metadata
41+
if 'metadata' not in notebook_data:
42+
notebook_data['metadata'] = {}
43+
44+
notebook_data['metadata']['kernelspec'] = {
45+
"display_name": target_kernel_name,
46+
"language": "python",
47+
"name": target_kernel_name
48+
}
49+
50+
# Also update language_info if it exists to be consistent
51+
if 'language_info' in notebook_data['metadata']:
52+
notebook_data['metadata']['language_info']['name'] = 'python'
53+
54+
# Write back the updated notebook
55+
with open(notebook_path, 'w', encoding='utf-8') as f:
56+
json.dump(notebook_data, f, indent=1, ensure_ascii=False)
57+
58+
print(f"✓ {notebook_path} - Updated from '{current_kernel}' to '{target_kernel_name}'")
59+
return True
60+
61+
except Exception as e:
62+
print(f"✗ {notebook_path} - Error: {e}")
63+
return False
64+
65+
def find_all_notebooks(root_dir="."):
66+
"""
67+
Find all .ipynb files recursively in the given directory
68+
69+
Args:
70+
root_dir (str): Root directory to search from
71+
72+
Returns:
73+
list: List of notebook file paths
74+
"""
75+
notebook_pattern = os.path.join(root_dir, "**", "*.ipynb")
76+
notebooks = glob.glob(notebook_pattern, recursive=True)
77+
78+
# Filter out checkpoint files
79+
notebooks = [nb for nb in notebooks if '.ipynb_checkpoints' not in nb]
80+
81+
return sorted(notebooks)
82+
83+
def main():
84+
"""Main function to update all notebooks"""
85+
print("🔍 Searching for Jupyter notebooks...")
86+
87+
# Get the current working directory
88+
root_dir = os.getcwd()
89+
print(f"📁 Root directory: {root_dir}")
90+
91+
# Find all notebooks
92+
notebooks = find_all_notebooks(root_dir)
93+
94+
if not notebooks:
95+
print("❌ No notebooks found!")
96+
return
97+
98+
print(f"📓 Found {len(notebooks)} notebooks")
99+
print("\n" + "="*60)
100+
101+
# Update each notebook
102+
success_count = 0
103+
error_count = 0
104+
105+
for notebook_path in notebooks:
106+
# Make path relative for cleaner output
107+
rel_path = os.path.relpath(notebook_path, root_dir)
108+
109+
if update_notebook_kernel(rel_path):
110+
success_count += 1
111+
else:
112+
error_count += 1
113+
114+
# Summary
115+
print("\n" + "="*60)
116+
print(f"✅ Successfully updated: {success_count}")
117+
print(f"❌ Failed to update: {error_count}")
118+
print(f"📊 Total processed: {len(notebooks)}")
119+
120+
if error_count == 0:
121+
print("\n🎉 All notebooks have been successfully updated to use 'geosci-labs' kernel!")
122+
else:
123+
print(f"\n⚠️ {error_count} notebooks had errors. Please check the output above.")
124+
125+
if __name__ == "__main__":
126+
main()

0 commit comments

Comments
 (0)