code: def test_parrec2nii_with_data():
      # Use nibabel-data to test conversion
      # Premultiplier to relate our affines to Philips conversion
      LAS2LPS = inv_ornt_aff([[0, 1], [1, -1], [2, 1]], (80, 80, 10))
      with InTemporaryDirectory():
          for par in glob(pjoin(BALLS, "PARREC", "*.PAR")):
              par_root, ext = splitext(basename(par))
              # NA.PAR appears to be a localizer, with three slices in each of
              # the three orientations: sagittal; coronal, transverse
              if par_root == "NA":
                  continue
              # Do conversion
              run_command(["parrec2nii", par])
              conved_img = load(par_root + ".nii")
              # Confirm parrec2nii conversions are LAS
              assert aff2axcodes(conved_img.affine) == tuple("LAS")
              # Shape same whether LPS or LAS
              assert conved_img.shape[:3] == (80, 80, 10)
              # Test against original converted NIfTI
              nifti_fname = pjoin(BALLS, "NIFTI", par_root + ".nii.gz")
              if exists(nifti_fname):
                  philips_img = load(nifti_fname)
                  # Confirm Philips converted image always LPS
                  assert aff2axcodes(philips_img.affine) == tuple("LPS")
                  # Equivalent to Philips LPS affine
                  equiv_affine = conved_img.affine.dot(LAS2LPS)
                  assert_almost_equal(
                      philips_img.affine[:3, :3],
                      equiv_affine[:3, :3],
                      3,
                  )
                  # The translation part is always off by the same ammout
                  aff_off = (
                      equiv_affine[:3, 3] - philips_img.affine[:3, 3]
                  )
                  assert_almost_equal(aff_off, AFF_OFF, 3)
                  # The difference is max in the order of 0.5 voxel
                  vox_sizes = vox_size(philips_img.affine)
                  assert np.all(np.abs(aff_off / vox_sizes) <= 0.501)
                  # The data is very close, unless it's the fieldmap
                  if par_root != "fieldmap":
                      conved_data_lps = np.flip(conved_img.dataobj, 1)
                      assert np.allclose(
                          conved_data_lps, philips_img.dataobj
                      )
      with InTemporaryDirectory():
          # Test some options
          dti_par = pjoin(BALLS, "PARREC", "DTI.PAR")
          run_command(["parrec2nii", dti_par])
          assert exists("DTI.nii")
          assert not exists("DTI.bvals")
          assert not exists("DTI.bvecs")
          # Does not overwrite unless option given
          code, stdout, stderr = run_command(
              ["parrec2nii", dti_par], check_code=False
          )
          assert code == 1
          # Writes bvals, bvecs files if asked
          run_command(
              [
                  "parrec2nii",
                  "--overwrite",
                  "--keep-trace",
                  "--bvs",
                  dti_par,
              ]
          )
          bvecs_trace = np.loadtxt("DTI.bvecs").T
          bvals_trace = np.loadtxt("DTI.bvals")
          assert_almost_equal(bvals_trace, DTI_PAR_BVALS)
          img = load("DTI.nii")
          data = img.get_fdata()
          del img
          # Bvecs in header, transposed from PSL to LPS
          bvecs_LPS = DTI_PAR_BVECS[:, [2, 0, 1]]
          # Adjust for output flip of Y axis in data and bvecs
          bvecs_LAS = bvecs_LPS * [1, -1, 1]
          assert_almost_equal(np.loadtxt("DTI.bvecs"), bvecs_LAS.T)
          # Dwell time
          assert not exists("DTI.dwell_time")
          # Need field strength if requesting dwell time
          (
              code,
              _,
              _,
          ) = run_command(
              ["parrec2nii", "--overwrite", "--dwell-time", dti_par],
              check_code=False,
          )
          assert code == 1
          run_command(
              [
                  "parrec2nii",
                  "--overwrite",
                  "--dwell-time",
                  "--field-strength",
                  "3",
                  dti_par,
              ]
          )
          exp_dwell = (26 * 9.087) / (42.576 * 3.4 * 3 * 28)
          with open("DTI.dwell_time") as fobj:
              contents = fobj.read().strip()
          assert_almost_equal(float(contents), exp_dwell)
          # ensure trace is removed by default
          run_command(["parrec2nii", "--overwrite", "--bvs", dti_par])
          assert exists("DTI.bvals")
          assert exists("DTI.bvecs")
          img = load("DTI.nii")
          bvecs_notrace = np.loadtxt("DTI.bvecs").T
          bvals_notrace = np.loadtxt("DTI.bvals")
          data_notrace = img.get_fdata()
          assert data_notrace.shape[-1] == len(bvecs_notrace)
          del img
          # ensure correct volume was removed
          good_mask = np.logical_or(
              (bvecs_trace != 0).any(axis=1), bvals_trace == 0
          )
          assert_almost_equal(data_notrace, data[..., good_mask])
          assert_almost_equal(
              bvals_notrace, np.array(DTI_PAR_BVALS)[good_mask]
          )
          assert_almost_equal(bvecs_notrace, bvecs_LAS[good_mask])
          # test --strict-sort
          run_command(
              [
                  "parrec2nii",
                  "--overwrite",
                  "--keep-trace",
                  "--bvs",
                  "--strict-sort",
                  dti_par,
              ]
          )
          # strict-sort: bvals should be in ascending order
          assert_almost_equal(
              np.loadtxt("DTI.bvals"), np.sort(DTI_PAR_BVALS)
          )
          img = load("DTI.nii")
          data_sorted = img.get_fdata()
          assert_almost_equal(
              data[..., np.argsort(DTI_PAR_BVALS, kind="stable")],
              data_sorted,
          )
          del img
  
          # Writes .ordering.csv if requested
          run_command(
              ["parrec2nii", "--overwrite", "--volume-info", dti_par]
          )
          assert exists("DTI.ordering.csv")
          with open("DTI.ordering.csv") as csvfile:
              csvreader = csv.reader(csvfile, delimiter=",")
              csv_keys = next(csvreader)  # header row
              nlines = 0  # count number of non-header rows
              for line in csvreader:
                  nlines += 1
  
          assert sorted(csv_keys) == [
              "diffusion b value number",
              "gradient orientation number",
          ]
          assert nlines == 8
  
imports: import csv
  import os
  import shutil
  import sys
  import unittest
  from glob import glob
  from os.path import abspath, basename, dirname, exists
  from os.path import join
  from os.path import splitext
  import numpy
  import pytest
  from numpy.testing import assert_almost_equal
  import nibabel
  from ..loadsave import load
  from ..orientations import aff2axcodes, inv_ornt_aff
  from ..testing import assert_data_similar, assert_dt_equal, assert_re_in
  from ..tmpdirs import InTemporaryDirectory
  from .nibabel_data import needs_nibabel_data
  from .scriptrunner import ScriptRunner
  from .test_parrec import DTI_PAR_BVALS, DTI_PAR_BVECS
  from .test_parrec import EXAMPLE_IMAGES
  from .test_parrec_data import AFF_OFF, BALLS
label_desc: Use image node_id 14 for calling a global function with example usage: test_parrec2nii_with_data() without return types