lcmodel-wrapper 0.4.0__tar.gz → 0.6.0__tar.gz

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.
Files changed (28) hide show
  1. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/PKG-INFO +1 -1
  2. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper/_version.py +1 -1
  3. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper/container.py +18 -16
  4. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper/control.py +21 -13
  5. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper/convert.py +3 -26
  6. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper/coord.py +8 -2
  7. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper/core.py +5 -3
  8. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper/io.py +37 -13
  9. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper.egg-info/PKG-INFO +1 -1
  10. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper.egg-info/SOURCES.txt +1 -0
  11. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/tests/test_binaries.py +43 -12
  12. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/tests/test_control.py +18 -2
  13. lcmodel_wrapper-0.6.0/tests/test_io.py +87 -0
  14. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/tests/test_lcm.py +2 -1
  15. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/LICENSE +0 -0
  16. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/LICENSE.lcmodel +0 -0
  17. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/MANIFEST.in +0 -0
  18. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/NOTICE +0 -0
  19. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/README.md +0 -0
  20. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper/__init__.py +0 -0
  21. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper/basis.py +0 -0
  22. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper/binaries.py +0 -0
  23. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper.egg-info/dependency_links.txt +0 -0
  24. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper.egg-info/requires.txt +0 -0
  25. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/lcmodel_wrapper.egg-info/top_level.txt +0 -0
  26. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/pyproject.toml +0 -0
  27. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/setup.cfg +0 -0
  28. {lcmodel_wrapper-0.4.0 → lcmodel_wrapper-0.6.0}/tests/test_convert.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lcmodel_wrapper
3
- Version: 0.4.0
3
+ Version: 0.6.0
4
4
  Summary: Lightweight Python wrapper for LCModel MRS fitting
5
5
  Author-email: Julian Merkofer <j.p.merkofer@tue.nl>
6
6
  License: Apache-2.0
@@ -1,4 +1,4 @@
1
1
  # Single source of truth for the package version. pyproject.toml reads it at build time,
2
2
  # and binaries.py derives the GitHub release / container image tag ("v<version>") from
3
3
  # it, so every wheel pairs with the LCModel artifacts built alongside it.
4
- __version__ = "0.4.0"
4
+ __version__ = "0.6.0"
@@ -80,16 +80,20 @@ def translate_control(lines: List[str],
80
80
  # mount computation #
81
81
  #**********************#
82
82
  def posix_mounts(cwd: str, home: Optional[str]) -> List[Tuple[str, str]]:
83
- """(host, container) pairs: the working directory, its physical path if that differs
84
- (a symlinked cwd is reachable under both spellings), and the home directory unless
85
- the cwd already lies inside it - docker refuses duplicate mount points."""
86
- mounts = [(cwd, cwd)]
87
- phys = os.path.realpath(cwd)
88
- if phys != cwd:
89
- mounts.append((phys, phys))
90
- if home and not (cwd == home or cwd.startswith(home.rstrip(os.sep) + os.sep)):
91
- mounts.append((home, home))
92
- return mounts
83
+ """(host, container) pairs: the home directory and the working directory, each under
84
+ its own spelling and its physical one (a symlinked home is common on clusters), each
85
+ unless it already lies inside one before it - docker refuses a mount point twice - and
86
+ never "/", which docker refuses outright. The home directory stays visible as a
87
+ whole, since the basis set usually lives there."""
88
+ roots: List[str] = []
89
+ for path in (home, home and os.path.realpath(home), cwd, os.path.realpath(cwd)):
90
+ if path and path != os.sep and not _inside(path, roots):
91
+ roots.append(path)
92
+ return [(r, r) for r in roots]
93
+
94
+
95
+ def _inside(path: str, roots: List[str]) -> bool:
96
+ return any(path == r or path.startswith(r.rstrip(os.sep) + os.sep) for r in roots)
93
97
 
94
98
 
95
99
  def windows_mounts(cwd: str, home: Optional[str]) -> List[Tuple[str, str]]:
@@ -103,16 +107,14 @@ def windows_mounts(cwd: str, home: Optional[str]) -> List[Tuple[str, str]]:
103
107
 
104
108
 
105
109
  def can_see(path) -> bool:
106
- """Would the launcher's mounts make "path" visible inside the container? Lets the
107
- wrapper fail early with a clear message instead of LCModel reporting a missing file."""
110
+ """Would the launcher's mounts make "path", spelled as the wrapper hands it to LCModel
111
+ (absolute, unresolved), visible inside the container? Lets the wrapper fail early with
112
+ a clear message instead of LCModel reporting a missing file."""
108
113
  cwd, home = os.getcwd(), str(Path.home())
109
114
  if os.name == "nt":
110
115
  drive = ntpath.splitdrive(os.path.abspath(str(path)))[0].upper()
111
116
  return any(host.startswith(drive) for host, _ in windows_mounts(cwd, home))
112
- p = Path(path).resolve()
113
- roots = {Path(host).resolve() for host, _ in posix_mounts(cwd, home)}
114
- roots.add(Path(os.environ.get("PWD", cwd)).resolve())
115
- return any(p == root or root in p.parents for root in roots)
117
+ return _inside(os.path.abspath(path), [host for host, _ in posix_mounts(cwd, home)])
116
118
 
117
119
 
118
120
  #********************#
@@ -13,32 +13,39 @@
13
13
  ####################################################################################################
14
14
 
15
15
  import os
16
- from typing import List, Tuple
16
+ from typing import List, Optional, Tuple
17
17
 
18
18
 
19
- DEFAULT_IGNORE = ["Lip13a", "Lip13b", "Lip09", "Lip20",
20
- "MM09", "MM12", "MM14", "MM17", "MM20",
21
- "-CrCH2", "CrCH2"]
19
+ # the lipids, macromolecules and creatine CH2 correction LCModel simulates and adds to
20
+ # every basis set unless told otherwise (manual, Sec 11.7)
21
+ SIMULATED = ["Lip13a", "Lip13b", "Lip09", "Lip20",
22
+ "MM09", "MM12", "MM14", "MM17", "MM20",
23
+ "-CrCH2", "CrCH2"]
22
24
 
23
25
 
24
26
  #********************#
25
27
  # ignore presets #
26
28
  #********************#
27
- def resolve_ignore(ignore) -> List[str]:
29
+ def resolve_ignore(ignore) -> Optional[List[str]]:
30
+ """None leaves the omitted metabolites to LCModel, or to the control file given."""
31
+ if ignore is None:
32
+ return None
28
33
  if isinstance(ignore, str):
29
- if ignore.lower() == "default":
30
- return list(DEFAULT_IGNORE)
34
+ if ignore.lower() == "simulated":
35
+ return list(SIMULATED)
31
36
  if ignore.lower() == "none":
32
37
  return []
33
38
  raise ValueError(
34
- "Unknown ignore preset. Use 'default', 'none', or a list of metabolite names."
39
+ "Unknown ignore preset. Use 'simulated', 'none', or a list of metabolite names."
35
40
  )
36
41
  if isinstance(ignore, (list, tuple)):
37
42
  return list(ignore)
38
43
  raise ValueError("ignore must be a list of metabolite names or a preset string.")
39
44
 
40
45
 
41
- def _omit_lines(ignore: List[str]) -> List[str]:
46
+ def _omit_lines(ignore: Optional[List[str]]) -> List[str]:
47
+ if ignore is None:
48
+ return []
42
49
  return [f"nomit={len(ignore)}"] + [f"chomit({i + 1})='{m}'" for i, m in enumerate(ignore)]
43
50
 
44
51
 
@@ -46,7 +53,7 @@ def _omit_lines(ignore: List[str]) -> List[str]:
46
53
  # build a control set #
47
54
  #*************************#
48
55
  def build_control(path2basis: str, n_points: int, bandwidth: float, central_freq: float,
49
- ppmlim: Tuple[float, float] = (0.5, 4.2), ignore=DEFAULT_IGNORE,
56
+ ppmlim: Tuple[float, float] = (0.5, 4.2), ignore=None,
50
57
  dows: bool = False) -> List[str]:
51
58
  """Create a default LCModel control file as a list of lines."""
52
59
  return [
@@ -74,12 +81,13 @@ def build_control(path2basis: str, n_points: int, bandwidth: float, central_freq
74
81
  # load + override file #
75
82
  #**************************#
76
83
  def load_control(control_path: str, path2basis: str, ppmlim: Tuple[float, float],
77
- ignore=DEFAULT_IGNORE) -> List[str]:
78
- """Read an existing control file and override basis, ppm limits and ignored metabolites."""
84
+ ignore=None) -> List[str]:
85
+ """Read an existing control file and override basis, ppm limits and, when given,
86
+ ignored metabolites."""
79
87
  ignore = resolve_ignore(ignore)
80
88
  with open(control_path, "r") as fh:
81
89
  control = [line for line in fh.read().splitlines()
82
- if not line.startswith(("chomit(", "nomit="))]
90
+ if ignore is None or not line.startswith(("chomit(", "nomit="))]
83
91
 
84
92
  set_key(control, "filbas", f"'{os.path.abspath(path2basis)}'")
85
93
  set_key(control, "ppmst", ppmlim[1])
@@ -18,12 +18,11 @@
18
18
 
19
19
  import json
20
20
  import os
21
- import re
22
- from typing import Callable, Dict, List, NamedTuple, Optional, Tuple
21
+ from typing import Callable, Dict, List, NamedTuple, Optional
23
22
 
24
23
  import numpy as np
25
24
 
26
- from .io import read_jmrui_txt, jmrui_metadata
25
+ from .io import read_jmrui_txt, jmrui_metadata, read_raw
27
26
 
28
27
 
29
28
  #*******************#
@@ -166,28 +165,6 @@ def _read_fsl_folder(folder: str) -> ParsedBasis:
166
165
  #*************************#
167
166
  # LCModel .RAW folder #
168
167
  #*************************#
169
- _NUM_RE = re.compile(r"[-+]?\d*\.?\d+(?:[eEdD][-+]?\d+)?")
170
-
171
-
172
- def _read_raw_file(path: str) -> Tuple[np.ndarray, Dict[str, str]]:
173
- keys: Dict[str, str] = {}
174
- values = []
175
- with open(path, "r", errors="ignore") as fh:
176
- for line in fh:
177
- s = line.strip()
178
- if not s or s.startswith("$"):
179
- continue
180
- if "=" in s:
181
- key, _, val = s.partition("=")
182
- keys[key.strip().upper()] = val.strip().rstrip(",").strip().strip("'")
183
- continue
184
- # FMTDAT may put several (real, imag) pairs on a line, e.g. '(8E13.5)'
185
- nums = _NUM_RE.findall(s.replace("D", "E").replace("d", "e"))
186
- values.extend(float(v) for v in nums)
187
- values = np.asarray(values[:len(values) // 2 * 2], dtype=np.float64)
188
- return values[0::2] + 1j * values[1::2], keys
189
-
190
-
191
168
  def _read_raw_folder(folder: str) -> ParsedBasis:
192
169
  files = sorted(
193
170
  f for f in os.listdir(folder) if f.lower().endswith((".raw", ".basis_raw"))
@@ -198,7 +175,7 @@ def _read_raw_folder(folder: str) -> ParsedBasis:
198
175
  names, fids = [], []
199
176
  dwell = central = echot = None
200
177
  for f in files:
201
- fid, keys = _read_raw_file(os.path.join(folder, f))
178
+ fid, keys = read_raw(os.path.join(folder, f))
202
179
  if fid.size == 0:
203
180
  continue
204
181
  names.append(keys.get("METABO") or keys.get("ID") or os.path.splitext(f)[0])
@@ -35,6 +35,12 @@ _SERIES_END = re.compile(r"lines in following|^[ ]+[a-zA-Z0-9]+[ ]+Conc\. = [-+.
35
35
  #*****************************#
36
36
  # load LCModel coord data #
37
37
  #*****************************#
38
+ def _number(text):
39
+ """A number as LCModel prints it; Fortran prints one that does not fit its field as
40
+ stars ('****'), which is no value."""
41
+ return float("nan") if set(text) <= {"*"} else float(text)
42
+
43
+
38
44
  def read_coord(path, coord=True, meta=True):
39
45
  """Read an LCModel ".coord" file.
40
46
 
@@ -66,8 +72,8 @@ def read_coord(path, coord=True, meta=True):
66
72
  misc_rows -= 1
67
73
  values = line.split()
68
74
  if "FWHM" in values:
69
- fwhm = float(values[2])
70
- snr = float(values[-1].split("=")[-1])
75
+ fwhm = _number(values[2])
76
+ snr = _number(values[-1].split("=")[-1])
71
77
  elif "shift" in values:
72
78
  # a negative shift fuses with the "=": "shift =-0.012 ppm"
73
79
  shift = float(values[2][1:]) if values[3] == "ppm" else float(values[3])
@@ -76,8 +76,10 @@ class PyLCModel:
76
76
  Analysis window (ppmend, ppmst).
77
77
  conj : bool
78
78
  Conjugate the FIDs before fitting (convention dependent).
79
- ignore : str | list
80
- "default", "none" or a list of metabolite names to omit.
79
+ ignore : str | list, optional
80
+ Metabolites to omit: a list of names, "none", or "simulated" for LCModel's
81
+ simulated lipids, macromolecules and creatine CH2 correction. By default
82
+ (None) LCModel's own choice, or the control file's.
81
83
  save_path : str, optional
82
84
  Directory to keep intermediate files (otherwise a temporary one is used).
83
85
  path2exec : str, optional
@@ -112,7 +114,7 @@ class PyLCModel:
112
114
  """
113
115
 
114
116
  def __init__(self, path2basis, control=None, multiprocessing=False, ppmlim=(0.5, 4.2),
115
- conj=True, ignore="default", save_path="", path2exec=None,
117
+ conj=True, ignore=None, save_path="", path2exec=None,
116
118
  domain="time", sample_points=None, bandwidth=None, central_freq=None,
117
119
  allow_download=True, allow_docker=True, allow_build=True,
118
120
  convert_basis=False, basis_format=None, timeout=900, io_timeout=10,
@@ -15,8 +15,9 @@
15
15
  ####################################################################################################
16
16
 
17
17
  import json
18
+ import re
18
19
  from dataclasses import dataclass
19
- from typing import List, Optional, Sequence, Tuple, Union
20
+ from typing import Dict, List, Optional, Sequence, Tuple, Union
20
21
 
21
22
  import numpy as np
22
23
 
@@ -253,7 +254,9 @@ def load_signals(data, domain: str = "time", dwell: Optional[float] = None,
253
254
  elif lower.endswith(".txt"):
254
255
  sig = read_jmrui(data)
255
256
  elif lower.endswith((".raw", ".h2o")):
256
- sig = Signals(fids=from_raw(data)[np.newaxis, :])
257
+ # .RAW stores the orientation LCModel reads, the conjugate of everything else
258
+ # loaded here - which "conj" (on by default) turns back
259
+ sig = Signals(fids=np.conj(from_raw(data))[np.newaxis, :])
257
260
  else:
258
261
  raise ValueError(f"Unsupported file type: {data}")
259
262
  elif isinstance(data, (list, tuple)) and data and all(
@@ -287,15 +290,36 @@ def to_raw(fid, file_path, header=RAW_HEADER):
287
290
  fh.write(f" {num.real: .6E} {num.imag: .6E}\n")
288
291
 
289
292
 
293
+ def read_raw(path) -> Tuple[np.ndarray, Dict[str, str]]:
294
+ """Read a ".RAW" file -> (complex points as stored, keys of its "key = value" lines).
295
+
296
+ The samples follow the last namelist end ("$END", "&END" or a lone "/"), which may
297
+ close a line of header values, and run on as FMTDAT says - often several (real, imag)
298
+ pairs per line. They are LCModel's orientation, the conjugate of what "load_signals"
299
+ hands out for a ".RAW".
300
+ """
301
+ with open(path, "r", errors="ignore") as fh:
302
+ lines = fh.read().splitlines()
303
+ ends = [i for i, line in enumerate(lines)
304
+ if re.search(r"[$&]END\b", line, re.IGNORECASE) or line.strip() == "/"]
305
+ start = ends[-1] + 1 if ends else 0
306
+
307
+ keys: Dict[str, str] = {}
308
+ for line in lines[:start]:
309
+ s = line.strip()
310
+ if "=" in s and not s.startswith(("$", "&")):
311
+ key, _, val = s.partition("=")
312
+ keys[key.strip().upper()] = val.strip().rstrip(",").strip().strip("'")
313
+
314
+ values = [float(v.replace("D", "E").replace("d", "e"))
315
+ for line in lines[start:] for v in line.split()]
316
+ if len(values) % 2:
317
+ raise ValueError(f"{path}: {len(values)} values after the header, not (real, imag) pairs")
318
+ values = np.asarray(values, dtype=np.float64)
319
+ return values[0::2] + 1j * values[1::2], keys
320
+
321
+
290
322
  def from_raw(path) -> np.ndarray:
291
- """Read the complex points that follow the "$END" of the header namelist."""
292
- fid = []
293
- in_data = False
294
- with open(path, "r") as fh:
295
- for line in fh:
296
- parts = line.split()
297
- if not in_data:
298
- in_data = parts[:1] == ["$END"]
299
- elif len(parts) >= 2:
300
- fid.append(complex(float(parts[0]), float(parts[1])))
301
- return np.array(fid)
323
+ """Read the complex points of a ".RAW" file as stored - LCModel's orientation, the
324
+ conjugate of what "load_signals" hands out for it (see "read_raw")."""
325
+ return read_raw(path)[0]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lcmodel_wrapper
3
- Version: 0.4.0
3
+ Version: 0.6.0
4
4
  Summary: Lightweight Python wrapper for LCModel MRS fitting
5
5
  Author-email: Julian Merkofer <j.p.merkofer@tue.nl>
6
6
  License: Apache-2.0
@@ -32,4 +32,5 @@ lcmodel_wrapper.egg-info/top_level.txt
32
32
  tests/test_binaries.py
33
33
  tests/test_control.py
34
34
  tests/test_convert.py
35
+ tests/test_io.py
35
36
  tests/test_lcm.py
@@ -282,9 +282,10 @@ def test_shim_mounts_cwd_at_the_same_path(tmp_path, monkeypatch):
282
282
  assert f"{home}:{home}" in mounts
283
283
 
284
284
 
285
- def test_shim_does_not_mount_home_twice(tmp_path, monkeypatch):
286
- """Docker refuses duplicate mount points, so a working directory under $HOME must
287
- not add a second, overlapping mount of $HOME."""
285
+ def test_shim_mounts_home_whole_when_the_work_lies_inside_it(tmp_path, monkeypatch):
286
+ """Docker refuses a mount point twice, so a working directory under $HOME adds no
287
+ mount of its own - and $HOME stays mounted whole, so a basis set elsewhere in it is
288
+ still seen."""
288
289
  import subprocess
289
290
  log = tmp_path / "args.log"
290
291
  engine = _fake_engine(tmp_path, f'printf "%s\\n" "$@" > "{log}"\n')
@@ -297,8 +298,10 @@ def test_shim_does_not_mount_home_twice(tmp_path, monkeypatch):
297
298
  monkeypatch.setenv("PYTHONPATH", _REPO)
298
299
  subprocess.run([str(shim)], cwd=work, check=True, stdin=subprocess.DEVNULL)
299
300
 
300
- mounts = [a for a in log.read_text().splitlines() if a.startswith(str(home) + ":")]
301
- assert mounts == []
301
+ args = log.read_text().splitlines()
302
+ mounts = [args[j + 1] for j, a in enumerate(args) if a == "-v"]
303
+ assert mounts == [f"{home}:{home}"]
304
+ assert args[args.index("-w") + 1] == str(work)
302
305
 
303
306
 
304
307
  def test_shim_passes_the_health_probe_when_the_container_answers(tmp_path, monkeypatch):
@@ -325,11 +328,35 @@ def test_container_can_see_only_cwd_and_home(tmp_path, monkeypatch):
325
328
  d.mkdir()
326
329
  monkeypatch.setenv("HOME", str(home))
327
330
  monkeypatch.chdir(work)
328
- monkeypatch.setenv("PWD", str(work))
329
331
  assert container.can_see(work / "tmp" / "temp0.raw")
330
332
  assert container.can_see(home / "basis" / "x.basis")
331
333
  assert container.can_see(home)
332
334
  assert not container.can_see(other / "x.basis")
335
+ # a working directory inside $HOME does not narrow what is seen to itself
336
+ (home / "project").mkdir()
337
+ monkeypatch.chdir(home / "project")
338
+ assert container.can_see(home / "basis" / "x.basis")
339
+ # a stale $PWD names no mount
340
+ monkeypatch.setenv("PWD", str(other))
341
+ assert not container.can_see(other / "x.basis")
342
+
343
+
344
+ def test_mounts_never_include_the_root():
345
+ """Docker refuses '/' as a mount point (a home of '/' under some service accounts)."""
346
+ if os.name == "nt":
347
+ pytest.skip("POSIX mounts")
348
+ assert container.posix_mounts("/", "/") == []
349
+ assert container.posix_mounts("/data/run", "/") == [("/data/run", "/data/run")]
350
+
351
+
352
+ def test_a_symlinked_home_is_mounted_under_both_spellings(tmp_path):
353
+ real = tmp_path / "gpfs" / "me"
354
+ real.mkdir(parents=True)
355
+ (tmp_path / "home").mkdir()
356
+ link = tmp_path / "home" / "me"
357
+ link.symlink_to(real)
358
+ mounts = [host for host, _ in container.posix_mounts(str(link / "run"), str(link))]
359
+ assert str(link) in mounts and str(real) in mounts
333
360
 
334
361
 
335
362
  def test_cached_shim_is_skipped_not_quarantined_when_engine_is_down(tmp_path, monkeypatch):
@@ -409,18 +436,22 @@ def test_run_command_windows_mounts_drives_under_host():
409
436
  assert cmd[-1] == "img:v1"
410
437
 
411
438
 
412
- def test_run_command_posix_uses_identical_paths():
413
- cmd = container.run_command("docker", "img:v1", cwd="/data/run", home="/home/me",
439
+ def test_run_command_posix_uses_identical_paths(tmp_path):
440
+ home, data = tmp_path / "home", tmp_path / "data" / "run" # real folders: realpath is asked
441
+ (home / "run").mkdir(parents=True)
442
+ data.mkdir(parents=True)
443
+ cmd = container.run_command("docker", "img:v1", cwd=str(data), home=str(home),
414
444
  windows=False, uid=(1000, 1000))
415
445
  assert cmd[cmd.index("-u") + 1] == "1000:1000"
416
446
  mounts = [cmd[i + 1] for i, a in enumerate(cmd) if a == "-v"]
417
- assert "/data/run:/data/run" in mounts and "/home/me:/home/me" in mounts
418
- assert cmd[cmd.index("-w") + 1] == "/data/run"
447
+ assert mounts == [f"{home}:{home}", f"{data}:{data}"]
448
+ assert cmd[cmd.index("-w") + 1] == str(data)
419
449
  # podman: uid mapping via user namespace instead of -u
420
- cmd = container.run_command("podman", "img", cwd="/home/me/run", home="/home/me",
450
+ cmd = container.run_command("podman", "img", cwd=str(home / "run"), home=str(home),
421
451
  windows=False, uid=(1000, 1000))
422
452
  assert "--userns=keep-id" in cmd and "-u" not in cmd
423
- assert [c for c in cmd if c.startswith("/home/me:")] == [] # cwd inside home: no 2nd mount
453
+ mounts = [cmd[i + 1] for i, a in enumerate(cmd) if a == "-v"]
454
+ assert mounts == [f"{home}:{home}"] # cwd inside home: no 2nd mount
424
455
 
425
456
 
426
457
  def test_container_rung_is_opt_out(monkeypatch, tmp_path):
@@ -3,7 +3,8 @@
3
3
  ####################################################################################################
4
4
  # #
5
5
  # Purpose: Extra control parameters: written in LCModel's namelist syntax, and kept whichever #
6
- # way the control is made, including when it is rebuilt once the data is known. #
6
+ # way the control is made, including when it is rebuilt once the data is known. Omitted #
7
+ # metabolites are LCModel's or the control file's own unless given. #
7
8
  # #
8
9
  ####################################################################################################
9
10
 
@@ -16,7 +17,7 @@ import pytest
16
17
  sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
17
18
 
18
19
  from lcmodel_wrapper import PyLCModel, binaries
19
- from lcmodel_wrapper.control import build_control, set_params
20
+ from lcmodel_wrapper.control import build_control, load_control, set_params
20
21
 
21
22
  _BASIS = (Path(__file__).resolve().parent.parent / "example_data" / "2016_fitting_challenge"
22
23
  / "basisset_LCModel" / "press3T_30ms.BASIS")
@@ -34,6 +35,21 @@ def test_params_are_written_as_the_namelist_expects():
34
35
  assert control[-1] == "$END"
35
36
 
36
37
 
38
+ #**************#
39
+ # omitting #
40
+ #**************#
41
+ def test_lcmodel_chooses_what_to_omit_unless_told(tmp_path):
42
+ built = build_control("x.basis", 2048, 4000.0, 123.2)
43
+ assert not any(line.startswith(("nomit", "chomit")) for line in built)
44
+ assert "chomit(1)='Lip13a'" in build_control("x.basis", 2048, 4000.0, 123.2,
45
+ ignore="simulated")
46
+
47
+ study = tmp_path / "study.control"
48
+ study.write_text("$LCMODL\nnomit=1\nchomit(1)='Lac'\n$END\n")
49
+ assert "chomit(1)='Lac'" in load_control(str(study), "x.basis", (0.5, 4.2))
50
+ assert "nomit=0" in load_control(str(study), "x.basis", (0.5, 4.2), ignore="none")
51
+
52
+
37
53
  #*************#
38
54
  # rebuilt #
39
55
  #*************#
@@ -0,0 +1,87 @@
1
+ ####################################################################################################
2
+ # test_io.py #
3
+ ####################################################################################################
4
+ # #
5
+ # Purpose: Reading LCModel ".RAW" files as LCModel writes them, and loading them in the same #
6
+ # orientation as every other input, so that "conj" (on by default) suits them all. #
7
+ # #
8
+ ####################################################################################################
9
+
10
+ import os
11
+ import sys
12
+ from pathlib import Path
13
+
14
+ import numpy as np
15
+ import pytest
16
+
17
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
18
+
19
+ from lcmodel_wrapper import io
20
+
21
+ _CHALLENGE = Path(__file__).resolve().parent.parent / "example_data" / "2016_fitting_challenge"
22
+
23
+
24
+ #****************#
25
+ # .raw files #
26
+ #****************#
27
+ def test_a_raw_header_may_close_on_a_value_line(tmp_path):
28
+ """As the challenge writes them: '$END' after the last header value, and four
29
+ (real, imag) pairs per line."""
30
+ path = tmp_path / "x.RAW"
31
+ path.write_text(" $NMID ID='x', FMTDAT='(8E13.5)'\n TRAMP=1, VOLUME=1 $END\n"
32
+ " 1.0E+00 2.0E+00 3.0E+00 4.0E+00 5.0E+00 6.0E+00 7.0E+00 8.0E+00\n"
33
+ " -1.0E+00 -2.0E+00\n")
34
+ assert np.array_equal(io.from_raw(path), [1 + 2j, 3 + 4j, 5 + 6j, 7 + 8j, -1 - 2j])
35
+
36
+
37
+ def test_only_what_follows_the_header_is_samples(tmp_path):
38
+ """A header line without '=' is no sample, a lone '/' ends a namelist as '$END' does,
39
+ and Fortran's D exponents and NaN read as numbers."""
40
+ path = tmp_path / "x.RAW"
41
+ path.write_text(" $SEQPAR\n Echo 30 ms\n ECHOT = 30.\n $END\n &NMID ID='x'\n /\n"
42
+ " 1.0D+00 2.0D+00 NAN 4.0E+00\n")
43
+ fid, keys = io.read_raw(path)
44
+ assert fid[0] == 1 + 2j and np.isnan(fid[1].real) and fid[1].imag == 4
45
+ assert keys["ECHOT"] == "30."
46
+
47
+
48
+ def test_an_odd_count_of_values_is_refused(tmp_path):
49
+ path = tmp_path / "x.RAW"
50
+ path.write_text(" $NMID\n $END\n 1.0 2.0 3.0\n")
51
+ with pytest.raises(ValueError, match="pairs"):
52
+ io.read_raw(path)
53
+
54
+
55
+ def test_a_raw_written_here_reads_back(tmp_path):
56
+ fid = np.exp(-np.arange(16) / 4.0) * np.exp(1j * np.arange(16))
57
+ io.to_raw(fid, tmp_path / "x.raw")
58
+ assert np.allclose(io.from_raw(tmp_path / "x.raw"), fid, rtol=1e-5)
59
+
60
+
61
+ #*****************#
62
+ # orientation #
63
+ #*****************#
64
+ @pytest.mark.skipif(not (_CHALLENGE / "datasets_LCModel" / "dataset1.RAW").is_file(),
65
+ reason="challenge data not checked out (git submodule update --init)")
66
+ def test_a_raw_loads_like_the_same_spectrum_in_jmrui_text():
67
+ """jMRUI text stores the conjugate of LCModel's .RAW; loading turns the .RAW round, so
68
+ the default conj hands LCModel each file as it would read the .RAW itself."""
69
+ jmrui = io.load_signals(str(_CHALLENGE / "datasets_JMRUI" / "dataset1_WS.txt"))
70
+ raw = io.load_signals(str(_CHALLENGE / "datasets_LCModel" / "dataset1.RAW"))
71
+ assert raw.fids.shape == (1, 2048)
72
+ assert np.allclose(jmrui.fids, raw.fids, rtol=1e-4, atol=1e-3)
73
+
74
+
75
+ def test_a_value_lcmodel_could_not_print_is_missing_not_a_crash(tmp_path):
76
+ """Fortran prints a number too wide for its field as stars; LCModel does so for S/N."""
77
+ from lcmodel_wrapper.coord import read_coord
78
+ coord = tmp_path / "x.coord"
79
+ coord.write_text(
80
+ " 2 lines in following concentration table:\n"
81
+ " Conc. %SD /Cr+PCr Metabolite\n"
82
+ " 8.330 3% 1.397 NAA\n"
83
+ " 2 lines in following misc. output table:\n"
84
+ " FWHM = 0.024 ppm S/N = ****\n"
85
+ " Data shift = -0.012 ppm\n")
86
+ metabs, concs, crlbs, tcr, fwhm, snr, shift, phase = read_coord(str(coord), meta=True)
87
+ assert metabs == ["NAA"] and fwhm == 0.024 and snr != snr # NaN
@@ -60,7 +60,8 @@ def challenge_mae(test_size: int = 5) -> float:
60
60
  basis = _CHALLENGE / "basisset_LCModel" / "press3T_30ms.BASIS"
61
61
  data_dir = _CHALLENGE / "datasets_JMRUI"
62
62
 
63
- lcm = PyLCModel(path2basis=str(basis), sample_points=2048, domain="time")
63
+ lcm = PyLCModel(path2basis=str(basis), sample_points=2048, domain="time",
64
+ ignore="simulated")
64
65
  n_metabs = lcm.basis.n_metabs
65
66
 
66
67
  # pair ground truths with challenge datasets by dataset number
File without changes
File without changes