Exemple #1
0
def test_save_dicoms_realtime(tmp_path, dd=data_dict):

    dd = dd.copy()
    start_time = time.time()

    dd['save_dicom'] = True
    dd['save_realtime'] = True

    # test when ROI files are not set
    dd['ROI_A_file'] = None
    dd['ROI_B_file'] = None
    dd['template_path'] = None
    dd['noise_dict_file'] = None

    # Run the simulation
    gen.generate_data(str(tmp_path), dd)

    end_time = time.time()

    # Check it took 2s per TR
    assert (end_time - start_time) > 60, 'Realtime ran fast'

    # Check correct file number
    file_path = str(tmp_path / '*.dcm')
    assert len(glob.glob(file_path)) == 30, "Wrong dicom file num"
Exemple #2
0
def test_signal_size(tmp_path, dd=data_dict):

    dd = dd.copy()

    # Change it to only use ROI A
    dd['different_ROIs'] = False

    # Make the signal large
    dd['scale_percentage'] = 100

    # Run the simulation
    gen.generate_data(str(tmp_path), dd)

    # Load in the ROI masks
    ROI_A = dd['ROI_A_file']
    ROI_B = dd['ROI_B_file']

    # Load in the data just simulated
    ROI_A_mean = []
    ROI_B_mean = []
    for TR_counter in range(dd['numTRs']):

        # Load the data
        vol_name = 'rt_%03d.npy' % TR_counter
        vol = np.load(tmp_path / vol_name)

        # Mask the data
        ROI_A_mean += [np.mean(vol[ROI_A == 1])]
        ROI_B_mean += [np.mean(vol[ROI_B == 1])]

    assert np.std(ROI_A_mean) > np.std(ROI_B_mean), 'Signal not scaling'
Exemple #3
0
def test_save_dicoms_realtime(tmp_path, dd=data_dict):

    dd['save_dicom'] = True
    dd['save_realtime'] = True

    start_time = time.time()

    # Run the simulation
    gen.generate_data(str(tmp_path), dd)

    end_time = time.time()

    # Check it took 2s per TR
    assert (end_time - start_time) > 60, 'Realtime ran fast'

    # Check correct file number
    file_path = str(tmp_path / '*.dcm')
    assert len(glob.glob(file_path)) == 30, "Wrong dicom file num"
Exemple #4
0
def test_default(tmp_path, dd=data_dict):

    # Run the simulation
    gen.generate_data(str(tmp_path), dd)

    # Check that there are 32 files where there should be (30 plus label and
    # mask)
    assert len(os.listdir(str(tmp_path))) == 32, "Incorrect file number"

    # Check that the data is the right shape
    input_template = dd['template_path']
    input_shape = input_template.shape
    output_vol = np.load(tmp_path / 'rt_000.npy')
    output_shape = output_vol.shape
    assert input_shape == output_shape, 'Output shape is incorrect'

    # Check the labels have the correct count
    labels = np.load(tmp_path / 'labels.npy')

    assert np.sum(labels > 0) == 9, 'Incorrect number of events'
Exemple #5
0
def test_multivariate(tmp_path, dd=data_dict):

    dd['multivariate_pattern'] = True
    dd['different_ROIs'] = False

    # Make the signal large
    dd['scale_percentage'] = 100

    # Run the simulation
    gen.generate_data(str(tmp_path), dd)

    # Load in the ROI masks
    ROI_A = dd['ROI_A_file']
    ROI_B = dd['ROI_B_file']

    # Test this volume
    vol = np.load(str(tmp_path / 'rt_007.npy'))

    ROI_A_std = np.std(vol[ROI_A == 1])
    ROI_B_std = np.std(vol[ROI_B == 1])

    assert ROI_A_std > ROI_B_std, 'Multivariate not making variable signal'
Exemple #6
0
 Authors: Cameron Ellis (Princeton) 2020
"""
import numpy as np
from brainiak.utils import fmrisim_real_time_generator as gen
import pytest
import os
import time
import glob
from pkg_resources import resource_stream
from typing import Dict
from nibabel.nifti1 import Nifti1Image
import gzip

# Test that it crashes without inputs
with pytest.raises(TypeError):
    gen.generate_data()  # type: ignore

data_dict = {}  # type: Dict
vol = resource_stream(gen.__name__, "sim_parameters/ROI_A.nii.gz").read()
data_dict['ROI_A_file'] = Nifti1Image.from_bytes(
    gzip.decompress(vol)).get_data()
vol = resource_stream(gen.__name__, "sim_parameters/ROI_B.nii.gz").read()
data_dict['ROI_B_file'] = Nifti1Image.from_bytes(
    gzip.decompress(vol)).get_data()
vol = resource_stream(gen.__name__,
                      "sim_parameters/sub_template.nii.gz").read()
data_dict['template_path'] = Nifti1Image.from_bytes(
    gzip.decompress(vol)).get_data()
noise_dict_file = resource_stream(gen.__name__,
                                  "sim_parameters/sub_noise_dict.txt").read()
data_dict['noise_dict_file'] = noise_dict_file
Exemple #7
0
    if type(args.allowedDirs) is str:
        args.allowedDirs = args.allowedDirs.split(',')

    if type(args.allowedFileTypes) is str:
        args.allowedFileTypes = args.allowedFileTypes.split(',')

    addr, port = args.server.split(':')
    # Check if the ssl certificate is valid for this server address
    if checkSSLCertAltName(certFile, addr) is False:
        # Addr not listed in sslCert, recreate ssl Cert
        makeSSLCertFile(addr)

    # if generate synthetic data
    if args.synthetic_data:
        # check if the dicoms are already created
        if not os.path.exists("/tmp/synthetic_dicom/rt_199.dcm"):
            datagen.generate_data("/tmp/synthetic_dicom", {'save_dicom': True})

    print("Server: {}, interval {}".format(args.server, args.interval))
    print("Allowed file types {}".format(args.allowedFileTypes))
    print("Allowed directories {}".format(args.allowedDirs))

    WsFileWatcher.runFileWatcher(args.server,
                                 retryInterval=args.interval,
                                 allowedDirs=args.allowedDirs,
                                 allowedTypes=args.allowedFileTypes,
                                 username=args.username,
                                 password=args.password,
                                 testMode=args.test)