|
9 | 9 | """ |
10 | 10 |
|
11 | 11 | import json |
| 12 | +import os |
| 13 | +import shutil |
12 | 14 | import subprocess |
13 | 15 | from pathlib import Path |
14 | 16 |
|
|
19 | 21 | from IPython.display import HTML |
20 | 22 | from matplotlib.animation import FuncAnimation |
21 | 23 |
|
| 24 | +CONDA_PATH = Path("envs", "environmental-remote-sensing") |
22 | 25 |
|
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() |
30 | 26 |
|
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 |
34 | 48 |
|
35 | 49 |
|
36 | 50 | 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 |
55 | 61 |
|
56 | 62 |
|
57 | 63 | ffmpeg_path = Path(get_conda_env_path()) / Path("bin/ffmpeg") |
@@ -122,8 +128,7 @@ def step_corr(x): |
122 | 128 | y1.plot(y=var1, ax=ax1) |
123 | 129 | y2.shift(x).plot(y=var2, c="orange", ax=ax1) |
124 | 130 | 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] |
127 | 132 | ) |
128 | 133 | plt.title(f"{var1} and {var2} at lag={x}") |
129 | 134 |
|
|
0 commit comments