Skip to content

Commit ad9b0ca

Browse files
Quarto output
1 parent e22a691 commit ad9b0ca

File tree

1 file changed

+35
-30
lines changed
  • notebooks/courses/environmental-remote-sensing/src/envrs

1 file changed

+35
-30
lines changed

notebooks/courses/environmental-remote-sensing/src/envrs/corr_plots.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"""
1010

1111
import json
12+
import os
13+
import shutil
1214
import subprocess
1315
from pathlib import Path
1416

@@ -19,39 +21,43 @@
1921
from IPython.display import HTML
2022
from matplotlib.animation import FuncAnimation
2123

24+
CONDA_PATH = Path("envs", "environmental-remote-sensing")
2225

23-
def get_git_repo_name():
24-
try:
25-
toplevel_path = subprocess.check_output(
26-
["git", "rev-parse", "--show-toplevel"], # noqa
27-
stderr=subprocess.DEVNULL,
28-
text=True,
29-
).strip()
3026

31-
return Path(toplevel_path).name
32-
except subprocess.CalledProcessError:
33-
return None
27+
def get_base(solver: str):
28+
conda_prefix = os.environ.get("CONDA_PREFIX")
29+
if conda_prefix:
30+
return conda_prefix
31+
32+
conda_exe = shutil.which(solver)
33+
if conda_exe:
34+
try:
35+
result = subprocess.run(
36+
[conda_exe, "info", "--json"],
37+
check=False,
38+
capture_output=True,
39+
text=True,
40+
)
41+
info = json.loads(result.stdout)
42+
envs = [s for s in info.get("envs") if "environmental-remote-sensing" in s]
43+
return next(iter(envs), None)
44+
except (subprocess.CalledProcessError, json.JSONDecodeError, KeyError):
45+
pass
46+
47+
return None
3448

3549

3650
def get_conda_env_path():
37-
conda_prefix = Path("../.conda_envs")
38-
result = subprocess.run(
39-
["micromamba", "env", "list", "--json"], # noqa
40-
check=False,
41-
capture_output=True,
42-
text=True,
43-
)
44-
info = json.loads(result.stdout)
45-
envs = [s for s in info.get("envs") if "environmental-remote-sensing" in s]
46-
if len(envs) == 0:
47-
# when cached on GH actions
48-
root_gh_cache = "/home/runner/work/eo-datascience/eo-datascience/"
49-
return root_gh_cache + ".conda_envs/environmental-remote-sensing"
50-
if conda_prefix.is_dir():
51-
conda_prefix_path = f"{get_git_repo_name()}/{conda_prefix.name}"
52-
envs_with_prefix = [s for s in envs if conda_prefix_path in s]
53-
return next(iter(envs_with_prefix), None)
54-
return next(iter(envs), None)
51+
conda_base = get_base("conda")
52+
if conda_base:
53+
return conda_base
54+
55+
micromamba_base = get_base("micromamba")
56+
if micromamba_base:
57+
return micromamba_base
58+
59+
print("Neither Conda nor Micromamba is installed or detected.")
60+
return None
5561

5662

5763
ffmpeg_path = Path(get_conda_env_path()) / Path("bin/ffmpeg")
@@ -122,8 +128,7 @@ def step_corr(x):
122128
y1.plot(y=var1, ax=ax1)
123129
y2.shift(x).plot(y=var2, c="orange", ax=ax1)
124130
res = pd.Series(
125-
smt.ccf(y1.values, y2.values, nlags=length),
126-
index=df.index[:length], # noqa
131+
smt.ccf(y1.values, y2.values, nlags=length), index=df.index[:length]
127132
)
128133
plt.title(f"{var1} and {var2} at lag={x}")
129134

0 commit comments

Comments
 (0)