lcmodel-wrapper 0.3.2__tar.gz → 0.5.1__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.3.2 → lcmodel_wrapper-0.5.1}/PKG-INFO +1 -1
  2. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper/_version.py +1 -1
  3. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper/container.py +18 -16
  4. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper/control.py +18 -0
  5. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper/convert.py +3 -26
  6. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper/coord.py +8 -2
  7. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper/core.py +12 -3
  8. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper/io.py +37 -13
  9. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper.egg-info/PKG-INFO +1 -1
  10. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper.egg-info/SOURCES.txt +2 -0
  11. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/tests/test_binaries.py +43 -12
  12. lcmodel_wrapper-0.5.1/tests/test_control.py +56 -0
  13. lcmodel_wrapper-0.5.1/tests/test_io.py +87 -0
  14. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/LICENSE +0 -0
  15. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/LICENSE.lcmodel +0 -0
  16. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/MANIFEST.in +0 -0
  17. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/NOTICE +0 -0
  18. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/README.md +0 -0
  19. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper/__init__.py +0 -0
  20. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper/basis.py +0 -0
  21. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper/binaries.py +0 -0
  22. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper.egg-info/dependency_links.txt +0 -0
  23. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper.egg-info/requires.txt +0 -0
  24. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/lcmodel_wrapper.egg-info/top_level.txt +0 -0
  25. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/pyproject.toml +0 -0
  26. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/setup.cfg +0 -0
  27. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/tests/test_convert.py +0 -0
  28. {lcmodel_wrapper-0.3.2 → lcmodel_wrapper-0.5.1}/tests/test_lcm.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lcmodel_wrapper
3
- Version: 0.3.2
3
+ Version: 0.5.1
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.3.2"
4
+ __version__ = "0.5.1"
@@ -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
  #********************#
@@ -90,6 +90,24 @@ def load_control(control_path: str, path2basis: str, ppmlim: Tuple[float, float]
90
90
  return control
91
91
 
92
92
 
93
+ #********************#
94
+ # set parameters #
95
+ #********************#
96
+ def set_params(control: List[str], params: dict) -> List[str]:
97
+ """Set each of *params* in place, written as LCModel's namelist expects.
98
+
99
+ Strings are quoted unless they already are, and booleans become T or F; anything
100
+ else is written as it is.
101
+ """
102
+ for key, value in params.items():
103
+ if isinstance(value, bool):
104
+ value = "T" if value else "F"
105
+ elif isinstance(value, str) and not value.startswith("'"):
106
+ value = f"'{value}'"
107
+ set_key(control, key, value)
108
+ return control
109
+
110
+
93
111
  #********************************#
94
112
  # set a key in a control set #
95
113
  #********************************#
@@ -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])
@@ -104,13 +104,19 @@ class PyLCModel:
104
104
  Seconds to wait for the ".coord" output after LCModel exits. Only covers metadata
105
105
  lag on network filesystems; raises "LCModelError" with LCModel's diagnostics when
106
106
  the fit produced nothing.
107
+ params : dict, optional
108
+ Further LCModel control parameters by their control-file names, e.g.
109
+ {"nuse1": 2, "chuse1(1)": "NAA", "chuse1(2)": "Cr"}. Applied on top of the
110
+ control however it is made - built, loaded from "control", or rebuilt once the
111
+ data is known - so a setting or two needs no control file of its own.
107
112
  """
108
113
 
109
114
  def __init__(self, path2basis, control=None, multiprocessing=False, ppmlim=(0.5, 4.2),
110
115
  conj=True, ignore="default", save_path="", path2exec=None,
111
116
  domain="time", sample_points=None, bandwidth=None, central_freq=None,
112
117
  allow_download=True, allow_docker=True, allow_build=True,
113
- convert_basis=False, basis_format=None, timeout=900, io_timeout=10):
118
+ convert_basis=False, basis_format=None, timeout=900, io_timeout=10,
119
+ params=None):
114
120
 
115
121
  if convert_basis:
116
122
  conv_dwell = (1.0 / bandwidth) if bandwidth else None
@@ -119,6 +125,7 @@ class PyLCModel:
119
125
  )
120
126
  self.path2basis = path2basis
121
127
  self.basis = read_basis(path2basis)
128
+ self.params = dict(params or {})
122
129
 
123
130
  self.multiprocessing = multiprocessing
124
131
  self.save_path = save_path
@@ -156,7 +163,8 @@ class PyLCModel:
156
163
  self._ignore = control_mod.resolve_ignore(ignore)
157
164
 
158
165
  if control is not None:
159
- self.control = control_mod.load_control(control, path2basis, ppmlim, self._ignore)
166
+ self.control = control_mod.set_params(
167
+ control_mod.load_control(control, path2basis, ppmlim, self._ignore), self.params)
160
168
  else:
161
169
  self.control = self._build_control()
162
170
 
@@ -164,10 +172,11 @@ class PyLCModel:
164
172
  # helpers #
165
173
  #*************#
166
174
  def _build_control(self):
167
- return control_mod.build_control(
175
+ control = control_mod.build_control(
168
176
  self.path2basis, self.sample_points, self.bandwidth, self.central_freq,
169
177
  ppmlim=self.ppmlim, ignore=self._ignore,
170
178
  )
179
+ return control_mod.set_params(control, self.params)
171
180
 
172
181
  def _workdir(self):
173
182
  """Directory for LCModel's input and output files, with a trailing separator.
@@ -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.3.2
3
+ Version: 0.5.1
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
@@ -30,5 +30,7 @@ lcmodel_wrapper.egg-info/dependency_links.txt
30
30
  lcmodel_wrapper.egg-info/requires.txt
31
31
  lcmodel_wrapper.egg-info/top_level.txt
32
32
  tests/test_binaries.py
33
+ tests/test_control.py
33
34
  tests/test_convert.py
35
+ tests/test_io.py
34
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):
@@ -0,0 +1,56 @@
1
+ ####################################################################################################
2
+ # test_control.py #
3
+ ####################################################################################################
4
+ # #
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. #
7
+ # #
8
+ ####################################################################################################
9
+
10
+ import os
11
+ import sys
12
+ from pathlib import Path
13
+
14
+ import pytest
15
+
16
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
17
+
18
+ from lcmodel_wrapper import PyLCModel, binaries
19
+ from lcmodel_wrapper.control import build_control, set_params
20
+
21
+ _BASIS = (Path(__file__).resolve().parent.parent / "example_data" / "2016_fitting_challenge"
22
+ / "basisset_LCModel" / "press3T_30ms.BASIS")
23
+
24
+
25
+ #************#
26
+ # syntax #
27
+ #************#
28
+ def test_params_are_written_as_the_namelist_expects():
29
+ control = set_params(build_control("x.basis", 2048, 4000.0, 123.2),
30
+ {"nuse1": 2, "chuse1(1)": "NAA", "dows": True, "namrel": "'Cr'"})
31
+
32
+ assert "nuse1=2" in control and "chuse1(1)='NAA'" in control
33
+ assert "dows=T" in control and "namrel='Cr'" in control
34
+ assert control[-1] == "$END"
35
+
36
+
37
+ #*************#
38
+ # rebuilt #
39
+ #*************#
40
+ def _binary():
41
+ try:
42
+ return binaries.resolve_executable(allow_download=False, allow_build=False)
43
+ except Exception: # noqa: BLE001 - any failure means absent
44
+ return None
45
+
46
+
47
+ @pytest.mark.skipif(not _BASIS.exists() or _binary() is None,
48
+ reason="needs the challenge basis and a cached LCModel binary")
49
+ def test_params_survive_the_control_being_rebuilt():
50
+ """The control is rebuilt from the data at fit time; the params must outlive that."""
51
+ engine = PyLCModel(str(_BASIS), params={"chuse1(1)": "NAA"},
52
+ allow_download=False, allow_build=False, allow_docker=False)
53
+
54
+ assert "chuse1(1)='NAA'" in engine.control
55
+ engine.sample_points += 1 # as a different acquisition would
56
+ assert "chuse1(1)='NAA'" in engine._build_control()
@@ -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
File without changes
File without changes