Skip to content

Commit a70da17

Browse files
committed
NF: trx --debug to help with environment mismatch
1 parent f436d76 commit a70da17

3 files changed

Lines changed: 137 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ export TRX_TMPDIR=use_working_dir
147147

148148
Temporary folders are automatically cleaned, but if the code crashes unexpectedly, ensure folders are deleted manually.
149149

150+
## Troubleshooting
151+
152+
If the `trx` command is not working as expected, run `trx --debug` to print diagnostic information about the Python interpreter, package location, and whether all required and optional dependencies are installed.
153+
150154
## Documentation
151155

152156
Full documentation is available at https://tee-ar-ex.github.io/trx-python/

docs/source/scripts.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The recommended way to use TRX commands is through the unified ``trx`` CLI:
1919
2020
Available subcommands:
2121

22+
- ``trx info`` - Display detailed TRX file information
2223
- ``trx concatenate`` - Concatenate multiple tractograms
2324
- ``trx convert`` - Convert between tractography formats
2425
- ``trx convert-dsi`` - Fix DSI-Studio TRK files
@@ -34,6 +35,20 @@ Standalone Commands
3435

3536
For backward compatibility, standalone commands are also available:
3637

38+
trx_info
39+
~~~~~~~~
40+
Display detailed information about a TRX file, including file size, compression status, header metadata (affine, dimensions, voxel sizes), streamline/vertex counts, data keys (dpv, dps, dpg), groups, and archive contents.
41+
42+
- Only ``.trx`` files are supported.
43+
44+
.. code-block:: bash
45+
46+
# Using unified CLI
47+
trx info tractogram.trx
48+
49+
# Using standalone command
50+
trx_info tractogram.trx
51+
3752
trx_concatenate_tractograms
3853
~~~~~~~~~~~~~~~~~~~~~~~~~~~
3954
Concatenate multiple tractograms into a single output.
@@ -151,6 +166,35 @@ Visualize streamline overlap between tractograms (requires visualization depende
151166
# Using standalone command
152167
trx_visualize_overlap tractogram.trk reference.nii.gz
153168
169+
Troubleshooting
170+
---------------
171+
172+
If the ``trx`` command is not working as expected, run ``trx --debug`` to print
173+
diagnostic information about the Python interpreter, package location, and
174+
whether all required and optional dependencies are installed:
175+
176+
.. code-block:: bash
177+
178+
trx --debug
179+
180+
# Example output:
181+
# Environment diagnostics:
182+
# Python executable : /Users/you/myenv/bin/python
183+
# sys.prefix : /Users/you/myenv
184+
# trx-python version: 0.3.1
185+
# trx package : /Users/you/myenv/lib/python3.11/site-packages/trx
186+
#
187+
# Required dependencies:
188+
# deepdiff found
189+
# nibabel found
190+
# numpy found
191+
# typer found
192+
#
193+
# Optional dependencies:
194+
# dipy found
195+
# fury not found
196+
# vtk not found
197+
154198
Notes
155199
-----
156200
- Test datasets for examples can be fetched with ``python -m trx.fetcher`` helpers: ``fetch_data(get_testing_files_dict())`` downloads to ``$TRX_HOME`` (default ``~/.tee_ar_ex``).

trx/cli.py

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
"""
77

88
from pathlib import Path
9-
from typing import List, Optional
9+
from typing import Annotated, List, Optional
1010

1111
import numpy as np
1212
import typer
13-
from typing_extensions import Annotated
1413

1514
from trx.io import load, save
1615
from trx.trx_file_memmap import TrxFile, concatenate, load as load_trx
@@ -25,11 +24,99 @@
2524
verify_header_compatibility,
2625
)
2726

27+
28+
def _debug_callback(value: bool) -> None:
29+
"""Print environment and dependency diagnostics, then exit.
30+
31+
Parameters
32+
----------
33+
value : bool
34+
Whether the ``--debug`` flag was passed.
35+
"""
36+
if not value:
37+
return
38+
39+
import importlib.metadata
40+
import importlib.util
41+
import sys
42+
43+
from trx import __version__
44+
45+
typer.echo("Environment diagnostics:")
46+
typer.echo(f" Python executable : {sys.executable}")
47+
typer.echo(f" sys.prefix : {sys.prefix}")
48+
typer.echo(f" trx-python version: {__version__}")
49+
50+
trx_spec = importlib.util.find_spec("trx")
51+
trx_location = (
52+
trx_spec.submodule_search_locations[0]
53+
if trx_spec and trx_spec.submodule_search_locations
54+
else "unknown"
55+
)
56+
typer.echo(f" trx package : {trx_location}")
57+
58+
# Read required dependencies from package metadata
59+
required_deps = []
60+
try:
61+
import re
62+
63+
for req in importlib.metadata.requires("trx-python") or []:
64+
# Skip optional / extra deps (they contain "; extra ==")
65+
if "extra ==" in req:
66+
continue
67+
# Extract the package name (strip version specifiers like >=, <=, ~=)
68+
dep_name = re.split(r"[>=<!~;\s]", req)[0]
69+
required_deps.append(dep_name)
70+
except importlib.metadata.PackageNotFoundError:
71+
required_deps = ["deepdiff", "nibabel", "numpy", "typer"]
72+
73+
optional_deps = ["dipy", "fury", "vtk"]
74+
75+
typer.echo("\nRequired dependencies:")
76+
for dep in required_deps:
77+
spec = importlib.util.find_spec(dep)
78+
if spec is None:
79+
typer.echo(f" {dep:12s} NOT FOUND")
80+
else:
81+
typer.echo(f" {dep:12s} found")
82+
83+
typer.echo("\nOptional dependencies:")
84+
for dep in optional_deps:
85+
spec = importlib.util.find_spec(dep)
86+
if spec is None:
87+
typer.echo(f" {dep:12s} not found")
88+
else:
89+
typer.echo(f" {dep:12s} found")
90+
91+
raise typer.Exit()
92+
93+
94+
def _main_callback(
95+
_debug: Annotated[
96+
bool,
97+
typer.Option(
98+
"--debug",
99+
help="Print environment and dependency diagnostics.",
100+
callback=_debug_callback,
101+
is_eager=True,
102+
),
103+
] = False,
104+
) -> None:
105+
"""TRX File Format Tools - CLI for brain tractography data manipulation.
106+
107+
Parameters
108+
----------
109+
_debug : bool, optional
110+
If True, print environment and dependency diagnostics and exit.
111+
"""
112+
113+
28114
app = typer.Typer(
29115
name="trx",
30116
help="TRX File Format Tools - CLI for brain tractography data manipulation.",
31117
add_completion=False,
32118
rich_markup_mode="rich",
119+
callback=_main_callback,
33120
)
34121

35122

0 commit comments

Comments
 (0)