Example #1
0
def pytest_sessionstart():
    """Perform actions that must be done prior to test collection."""
    # Use a non-interactive backend so that no GUI plots will interrupt the test suite.
    # (NB: We do this here to ensure it is set before `matplotlib` is first imported.)
    if 'MPLBACKEND' not in os.environ:
        os.environ["MPLBACKEND"] = 'Agg'

    # Download sct_testing_data prior to test collection
    if not os.path.exists(sct_test_path()):
        logger.info("Downloading sct test data")
        downloader.main(['-d', 'sct_testing_data', '-o', sct_test_path()])
Example #2
0
def check_testing_data_integrity(files_checksums: Mapping[os.PathLike, str]):
    changed = []
    new = []
    missing = []

    after = []

    for root, _, files in os.walk(sct_test_path()):
        for f in files:
            fname = os.path.join(root, f)
            chksum = checksum(fname)
            after.append(fname)

            if fname not in files_checksums:
                logger.warning(
                    f"Discovered new file in sct_testing_data that didn't exist before: {(fname, chksum)}"
                )
                new.append((fname, chksum))

            elif files_checksums[fname] != chksum:
                logger.error(
                    f"Checksum mismatch for test data: {fname}. Got {chksum} instead of {files_checksums[fname]}"
                )
                changed.append((fname, chksum))

    for fname, chksum in files_checksums.items():
        if fname not in after:
            logger.error(f"Test data missing after test:a: {fname}")
            missing.append((fname, chksum))

    assert not changed
    # assert not new
    assert not missing
Example #3
0
def run_in_sct_testing_data_dir():
    """Temporarily change the working directory to 'sct_testing_data'. This replicates the behavior of the old
    `sct_testing`, and is needed to prevent tests from cluttering the working directory with output files."""
    cwd = os.getcwd()
    os.chdir(sct_test_path())
    yield
    os.chdir(cwd)
Example #4
0
def test_data_integrity(request):
    files_checksums = dict()
    for root, _, files in os.walk(sct_test_path()):
        for f in files:
            fname = os.path.join(root, f)
            chksum = checksum(fname)
            files_checksums[fname] = chksum

    request.addfinalizer(lambda: check_testing_data_integrity(files_checksums))
def main():

    # Initialization
    fname_input = ''
    fname_segmentation = ''

    if param.debug:
        printv('\n*** WARNING: DEBUG MODE ON ***\n')
        fname_input = ''
        fname_segmentation = sct_test_path('t2', 't2_seg.nii.gz')
    else:
        # Check input param
        try:
            opts, args = getopt.getopt(sys.argv[1:], 'hi:t:')
        except getopt.GetoptError as err:
            logger.error(str(err))
            usage()
        for opt, arg in opts:
            if opt == '-h':
                usage()
            elif opt in ('-i'):
                fname_input = arg
            elif opt in ('-t'):
                fname_segmentation = arg

    # display usage if a mandatory argument is not provided
    if fname_segmentation == '' or fname_input == '':
        usage()

    # check existence of input files
    check_file_exist(fname_input)
    check_file_exist(fname_segmentation)

    # read nifti input file
    img = nibabel.load(fname_input)
    # 3d array for each x y z voxel values for the input nifti image
    data = img.get_data()

    # read nifti input file
    img_seg = nibabel.load(fname_segmentation)
    # 3d array for each x y z voxel values for the input nifti image
    data_seg = img_seg.get_data()

    X, Y, Z = (data > 0).nonzero()
    status = 0
    for i in range(0, len(X)):
        if data_seg[X[i], Y[i], Z[i]] == 0:
            status = 1
            break

    if status is not 0:
        printv('ERROR: detected point is not in segmentation', 1, 'warning')
    else:
        printv('OK: detected point is in segmentation')

    sys.exit(status)
Example #6
0
def test_non_executable_task():
    data_path = sct_test_path()
    with \
        NamedTemporaryFile('w', suffix='.sh') as script,\
            TemporaryDirectory() as out:

        script_text = """
        #!/bin/bash
        echo $SUBJECT
        """
        script.write(
            dedent(script_text)[1:])  #indexing removes beginning newline
        script.flush()

        assert not os.access(script.name, os.X_OK), "Script already executable"

        sct_run_batch.main([
            '-include', '^t.*', '-subject-prefix', '', '-path-data', data_path,
            '-path-out', out, '-script', script.name, '-continue-on-error', 0
        ])
Example #7
0
def pytest_sessionstart():
    """ Download sct_testing_data prior to test collection. """
    logger.info("Downloading sct test data")
    downloader.main(['-d', 'sct_testing_data', '-o', sct_test_path()])
Example #8
0
def test_sct_image_display_warp_check_output_exists():
    """Run the CLI script and check that the warp image file was created."""
    fname_in = 'warp_template2anat.nii.gz'
    fname_out = 'grid_3_resample_' + fname_in
    sct_image.main(argv=['-i', sct_test_path('t2', fname_in), '-display-warp'])
    assert os.path.exists(sct_test_path('t2', fname_out))