Skip to content

Commit 8e4d82b

Browse files
authored
Merge pull request #9 from NiftyPET/devel
2 parents 2615233 + cd5c23a commit 8e4d82b

File tree

8 files changed

+116
-120
lines changed

8 files changed

+116
-120
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
path: ~/.cache/pre-commit
2323
key: pre-commit|${{ env.PYSHA }}|${{ hashFiles('.pre-commit-config.yaml') }}
2424
- run: pip install -U .[dev]
25-
- run: python setup.py sdist
25+
- run: python setup.py sdist bdist_wheel
2626
- run: twine check dist/*
2727
- uses: reviewdog/action-setup@v1
2828
- if: github.event_name != 'schedule'

.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
*.py[co]
22
*.so
3-
*.log
43
__pycache__/
4+
5+
/niftypet/ninst/_dist_ver.py
56
/.eggs/
67
/build/
78
/dist/
8-
/ninst.egg*/
9-
.DS_Store
9+
/*.egg*/
1010

1111
/.coverage*
1212
/coverage.xml

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ It's also recommended (but not required) to use `conda`.
2626
# optional (Linux syntax) to avoid prompts
2727
export PATHTOOLS=$HOME/NiftyPET_tools
2828
# cross-platform install
29-
conda create -n niftypet -c conda-forge python=3 numpy
29+
conda create -n niftypet python=3
3030
conda activate niftypet
3131
pip install ninst
3232

niftypet/ninst/__init__.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# version detector. Precedence: installed dist, git, 'UNKNOWN'
2+
try:
3+
from ._dist_ver import __version__
4+
except ImportError:
5+
try:
6+
from setuptools_scm import get_version
7+
8+
__version__ = get_version(root="../..", relative_to=__file__)
9+
except (ImportError, LookupError):
10+
__version__ = "UNKNOWN"

niftypet/ninst/install_tools.py

+90-111
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,54 @@
33
* NiftyReg
44
* dcm2niix
55
"""
6-
import glob
76
import logging
87
import multiprocessing
98
import os
109
import platform
1110
import re
1211
import shutil
1312
import sys
13+
from os import fspath, path
14+
from pathlib import Path
1415
from subprocess import PIPE, CalledProcessError, check_output, run
1516
from textwrap import dedent
1617

18+
from . import cudasetup as cs
19+
1720
if os.getenv("DISPLAY", False):
1821
from functools import wraps
1922
from tkinter import Tk
2023
from tkinter.filedialog import askdirectory as ask
2124

2225
@wraps(ask)
23-
def askdirectory(*args, **kwargs):
26+
def askdir(title, initialdir):
2427
Tk().withdraw()
25-
res = ask(*args, **kwargs)
28+
res = ask(title=title, initialdir=initialdir)
2629
Tk().destroy()
2730
return res
2831

2932

3033
else:
3134

32-
def askdirectory(title="Folder: ", initialdir=None, name=""):
33-
"""
34-
decreasing precedence: os.environ[name], raw_input, initialdir
35+
def askdir(title, initialdir):
36+
res = input(title + (f" [{initialdir}]: " if initialdir else ": "))
37+
return initialdir if res == "" else res
3538

36-
Args:
37-
initialdir (str): default: ~
38-
"""
39-
if initialdir is None:
40-
initialdir = os.path.expanduser("~")
41-
path = os.environ.get(name, None)
42-
if path is None:
43-
path = input(title)
44-
if path == "":
45-
return initialdir
46-
return path
4739

40+
def askdirectory(title="Folder", initialdir="~", name=""):
41+
"""
42+
Args:
43+
initialdir (str): default: "~"
44+
Returns (str):
45+
one of (decreasing Precedence):
46+
- `os.getenv(name)`
47+
- `input()` or `tkinter.filedialog.askdirectory()`
48+
- `initialdir`
49+
"""
50+
initialdir = path.expanduser(initialdir)
51+
res = os.getenv(name, None)
52+
return askdir(title, initialdir) if res is None else res
4853

49-
from . import cudasetup as cs
5054

5155
log = logging.getLogger(__name__)
5256

@@ -81,7 +85,7 @@ def askdirectory(title="Folder: ", initialdir=None, name=""):
8185

8286
# source and build folder names
8387
dirsrc = "_src"
84-
dirbld = "_bld"
88+
dirbld = Path("_bld")
8589

8690
# number of threads
8791
ncpu = multiprocessing.cpu_count()
@@ -209,8 +213,8 @@ def check_depends(git=1, cuda=1, cmake=1, ninja=1, **kwargs):
209213
)
210214

211215
for bin in set(outdct) - {"cuda", "nvcc", "git", "cmake", "ninja"}:
212-
# for p in os.getenv("PATH").split(os.path.pathsep):
213-
# if os.path.isfile(os.path.join(p, bin)):
216+
# for p in os.getenv("PATH").split(path.pathsep):
217+
# if path.isfile(path.join(p, bin)):
214218
# outdct[bin] = True
215219
# break
216220
# else:
@@ -270,7 +274,7 @@ def check_version(Cnt, chcklst=None):
270274
# hdw mu-map list
271275
if "HMUDIR" in chcklst and "HMUDIR" in Cnt:
272276
for hi in Cnt["HMULIST"]:
273-
if os.path.isfile(os.path.join(Cnt["HMUDIR"], hi)):
277+
if path.isfile(path.join(Cnt["HMUDIR"], hi)):
274278
output["HMUDIR"] = True
275279
else:
276280
output["HMUDIR"] = False
@@ -279,7 +283,7 @@ def check_version(Cnt, chcklst=None):
279283
return output
280284

281285

282-
def download_dcm2niix(Cnt, path):
286+
def download_dcm2niix(Cnt, dest):
283287
log.info(
284288
dedent(
285289
"""\
@@ -291,11 +295,11 @@ def download_dcm2niix(Cnt, path):
291295
)
292296

293297
# -create the installation folder
294-
if not os.path.isdir(path):
295-
os.mkdir(path)
296-
binpath = os.path.join(path, "bin")
297-
if not os.path.isdir(binpath):
298-
os.mkdir(binpath)
298+
if not dest.is_dir():
299+
dest.mkdir()
300+
binpath = dest / "bin"
301+
if not binpath.is_dir():
302+
binpath.mkdir()
299303

300304
import urllib.error
301305
import urllib.parse
@@ -304,13 +308,13 @@ def download_dcm2niix(Cnt, path):
304308

305309
http_dcm = {"Windows": http_dcm_win, "Linux": http_dcm_lin, "Darwin": http_dcm_mac}
306310
urllib.request.urlretrieve(
307-
http_dcm[platform.system()], os.path.join(path, "dcm2niix.zip")
311+
http_dcm[platform.system()], fspath(dest / "dcm2niix.zip")
308312
)
309313

310-
zipf = zipfile.ZipFile(os.path.join(path, "dcm2niix.zip"), "r")
311-
zipf.extractall(os.path.join(path, "bin"))
314+
zipf = zipfile.ZipFile(fspath(dest / "dcm2niix.zip"), "r")
315+
zipf.extractall(fspath(binpath))
312316
zipf.close()
313-
Cnt["DCM2NIIX"] = glob.glob(os.path.join(os.path.join(path, "bin"), "dcm2niix*"))[0]
317+
Cnt["DCM2NIIX"] = fspath(next(binpath.glob("dcm2niix*")))
314318
# ensure the permissions are given to the executable
315319
os.chmod(Cnt["DCM2NIIX"], 755)
316320
# update the resources.py file in ~/.niftypet
@@ -327,104 +331,86 @@ def install_tool(app, Cnt):
327331
cwd = os.getcwd()
328332

329333
# pick the target installation folder for tools
330-
if "PATHTOOLS" in Cnt and Cnt["PATHTOOLS"] != "":
331-
path_tools = Cnt["PATHTOOLS"]
332-
elif "PATHTOOLS" not in Cnt or Cnt["PATHTOOLS"] != "":
333-
if os.getenv("DISPLAY", False) and platform.system() in ["Linux", "Windows"]:
334-
log.info("DISPLAY: {}".format(os.environ["DISPLAY"]))
335-
dircore = askdirectory(
336-
title="choose a place for NiftyPET tools",
337-
initialdir=os.path.expanduser("~"),
338-
)
339-
# get the full (combined path)
340-
path_tools = os.path.join(dircore, Cnt["DIRTOOLS"])
341-
else:
342-
try:
343-
path_tools = askdirectory(
344-
title="Enter path for NiftyPET tools (registration, etc): ",
345-
name="PATHTOOLS",
346-
)
347-
except Exception:
348-
log.warning(
349-
"manually enter the intended PATHTOOLS in resources.py"
350-
" located in ~/.niftypet/"
351-
)
352-
raise ValueError("\n e> could not get the path for NiftyPET_tools \n")
353-
Cnt["PATHTOOLS"] = path_tools
354-
334+
if Cnt.get("PATHTOOLS", None):
335+
path_tools = Path(Cnt["PATHTOOLS"])
336+
if not path_tools.is_dir():
337+
path_tools.mkdir()
355338
else:
356-
if platform.system() == "Linux":
357-
path_tools = os.path.join(os.path.expanduser("~"), Cnt["DIRTOOLS"])
358-
elif platform.system() == "Windows":
359-
path_tools = os.path.join(os.getenv("LOCALAPPDATA"), Cnt["DIRTOOLS"])
360-
else:
361-
log.error(
339+
path_tools = Path(
340+
askdirectory(title="Path to place NiftyPET tools", name="PATHTOOLS")
341+
)
342+
if not path_tools.is_dir():
343+
path_tools.mkdir()
344+
if path_tools.name != Cnt["DIRTOOLS"]:
345+
path_tools /= Cnt["DIRTOOLS"]
346+
Cnt["PATHTOOLS"] = fspath(path_tools)
347+
if platform.system() not in {"Linux", "Windows"}:
348+
raise SystemError(
349+
dedent(
362350
"""\
363-
\r=============================================================
364-
\ronly Linux and Windows operating systems are supported
365-
\rfor the additional tools installation!
366-
\r=============================================================
367-
"""
351+
=============================================================
352+
Only Linux and Windows operating systems are supported
353+
for installation of additional tools.
354+
=============================================================
355+
"""
368356
)
369-
raise SystemError("OS not supported!")
370-
Cnt["PATHTOOLS"] = path_tools
357+
)
371358

372359
# create the main tools folder
373-
if not os.path.isdir(path_tools):
374-
os.mkdir(path_tools)
360+
if not path_tools.is_dir():
361+
path_tools.mkdir()
375362
# identify the specific path for the requested app
376363
if app == "niftyreg":
377364
repo = repo_reg
378365
sha1 = sha1_reg
379-
path = os.path.join(path_tools, "niftyreg")
366+
dest = path_tools / "niftyreg"
380367
elif app == "dcm2niix":
381368
repo = repo_dcm
382369
sha1 = sha1_dcm
383-
path = os.path.join(path_tools, "dcm2niix")
370+
dest = path_tools / "dcm2niix"
384371

385372
if not Cnt["CMPL_DCM2NIIX"]:
386373
# avoid installing from source, instead download the full version:
387-
Cnt = download_dcm2niix(Cnt, path)
374+
Cnt = download_dcm2niix(Cnt, dest)
388375
return Cnt
376+
else:
377+
raise ValueError(f"unknown tool:{app}")
389378

390379
# Check if the source folder exists and delete it, if it does
391-
if os.path.isdir(path):
392-
shutil.rmtree(path)
393-
# Create an empty folder and enter it
394-
os.mkdir(path)
395-
os.chdir(path)
380+
if dest.is_dir():
381+
shutil.rmtree(fspath(dest))
382+
dest.mkdir()
383+
os.chdir(dest)
396384

397385
# clone the git repository
398386
run(["git", "clone", repo, dirsrc])
399387
os.chdir(dirsrc)
400388
log.info("checking out the specific git version of the software...")
401389
run(["git", "checkout", sha1])
402-
os.chdir("../")
390+
os.chdir(cwd)
403391

404-
# create the building folder
405-
if not os.path.isdir(dirbld):
392+
if not dirbld.is_dir():
406393
os.mkdir(dirbld)
407-
# go inside the build folder
408394
os.chdir(dirbld)
409-
410395
# run cmake with arguments
411396
if platform.system() == "Windows":
412-
cmd = [
413-
"cmake",
414-
"../" + dirsrc,
415-
"-DBUILD_ALL_DEP=ON",
416-
"-DCMAKE_INSTALL_PREFIX=" + path,
417-
"-G",
418-
Cnt["MSVC_VRSN"],
419-
]
420-
run(cmd)
397+
run(
398+
[
399+
"cmake",
400+
"../" + dirsrc,
401+
"-DBUILD_ALL_DEP=ON",
402+
f"-DCMAKE_INSTALL_PREFIX={dest}",
403+
"-G",
404+
Cnt["MSVC_VRSN"],
405+
]
406+
)
421407
run(["cmake", "--build", "./", "--config", "Release", "--target", "install"])
422408
elif platform.system() in ["Linux", "Darwin"]:
423409
cmd = [
424410
"cmake",
425411
"../" + dirsrc,
426412
"-DBUILD_ALL_DEP=ON",
427-
"-DCMAKE_INSTALL_PREFIX=" + path,
413+
f"-DCMAKE_INSTALL_PREFIX={dest}",
428414
]
429415
if Cnt["CMAKE_TLS_PAR"] != "":
430416
cmd.append(Cnt["CMAKE_TLS_PAR"])
@@ -449,13 +435,9 @@ def install_tool(app, Cnt):
449435

450436
if app == "niftyreg":
451437
try:
452-
Cnt["RESPATH"] = glob.glob(
453-
os.path.join(os.path.join(path, "bin"), "reg_resample*")
454-
)[0]
455-
Cnt["REGPATH"] = glob.glob(
456-
os.path.join(os.path.join(path, "bin"), "reg_aladin*")
457-
)[0]
458-
except IndexError:
438+
Cnt["RESPATH"] = fspath(next((dest / "bin").glob("reg_resample*")))
439+
Cnt["REGPATH"] = fspath(next((dest / "bin").glob("reg_aladin*")))
440+
except StopIteration:
459441
log.error("NiftyReg has NOT been successfully installed.")
460442
raise SystemError("Failed Installation (NiftyReg)")
461443
# updated the file resources.py
@@ -465,19 +447,16 @@ def install_tool(app, Cnt):
465447
if not all(chck_niftyreg.values()):
466448
log.error("NiftyReg has NOT been successfully installed.")
467449
raise SystemError("Failed Installation (NiftyReg)")
468-
469450
elif app == "dcm2niix":
470451
try:
471-
Cnt["DCM2NIIX"] = glob.glob(
472-
os.path.join(os.path.join(path, "bin"), "dcm2niix*")
473-
)[0]
474-
except IndexError:
452+
Cnt["DCM2NIIX"] = fspath(next((dest / "bin").glob("dcm2niix*")))
453+
except StopIteration:
475454
log.error("dcm2niix has NOT been successfully installed.")
476-
Cnt = download_dcm2niix(Cnt, path)
455+
Cnt = download_dcm2niix(Cnt, dest)
477456
# check the installation:
478457
if not check_version(Cnt, chcklst=["DCM2NIIX"]):
479458
log.error("dcm2niix has NOT been successfully compiled from github.")
480-
Cnt = download_dcm2niix(Cnt, path)
459+
Cnt = download_dcm2niix(Cnt, dest)
481460
return Cnt
482461

483462

@@ -488,10 +467,10 @@ def update_resources(Cnt):
488467

489468
# get the local path to NiftyPET resources.py
490469
path_resources = cs.path_niftypet_local()
491-
resources_file = os.path.join(path_resources, "resources.py")
470+
resources_file = path.join(path_resources, "resources.py")
492471

493472
# update resources.py
494-
if os.path.isfile(resources_file):
473+
if path.isfile(resources_file):
495474
f = open(resources_file, "r")
496475
rsrc = f.read()
497476
f.close()

0 commit comments

Comments
 (0)