Example #1
0
def test_mask_brain():

    # Inputs for generate_signal
    dimensions = np.array([10, 10, 10])  # What is the size of the brain
    feature_size = [2]
    feature_type = ['cube']
    feature_coordinates = np.array(
        [[4, 4, 4]])
    signal_magnitude = [30]

    # Generate a volume representing the location and quality of the signal
    volume = sim.generate_signal(dimensions=dimensions,
                                 feature_coordinates=feature_coordinates,
                                 feature_type=feature_type,
                                 feature_size=feature_size,
                                 signal_magnitude=signal_magnitude,
                                 )

    # Mask the volume to be the same shape as a brain
    mask, _ = sim.mask_brain(dimensions, mask_self=None,)
    brain = volume * mask

    assert np.sum(brain != 0) == np.sum(volume != 0), "Masking did not work"
    assert brain[0, 0, 0] == 0, "Masking did not work"
    assert brain[4, 4, 4] != 0, "Masking did not work"

    feature_coordinates = np.array(
        [[1, 1, 1]])

    volume = sim.generate_signal(dimensions=dimensions,
                                 feature_coordinates=feature_coordinates,
                                 feature_type=feature_type,
                                 feature_size=feature_size,
                                 signal_magnitude=signal_magnitude,
                                 )

    # Mask the volume to be the same shape as a brain
    mask, _ = sim.mask_brain(dimensions, mask_self=None, )
    brain = volume * mask

    assert np.sum(brain != 0) < np.sum(volume != 0), "Masking did not work"

    # Test that you can load the default
    dimensions = np.array([100, 100, 100])
    mask, template = sim.mask_brain(dimensions, mask_self=False)

    assert mask[20, 80, 50] == 0, 'Masking didn''t work'
    assert mask[25, 80, 50] == 1, 'Masking didn''t work'
    assert int(template[25, 80, 50] * 100) == 57, 'Template not correct'

    # Check that you can mask self
    mask_self, template_self = sim.mask_brain(template, mask_self=True)

    assert (template_self - template).sum() < 1e2, 'Mask self error'
    assert (mask_self - mask).sum() == 0, 'Mask self error'
Example #2
0
def test_calc_noise():

    # Inputs for functions
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 200
    tr_number = int(np.floor(duration / tr_duration))
    dimensions_tr = np.array([10, 10, 10, tr_number])

    # Preset the noise dict
    nd_orig = {
        'auto_reg_sigma': 0.6,
        'drift_sigma': 0.4,
        'snr': 30,
        'sfnr': 30,
        'max_activity': 1000,
        'fwhm': 4,
    }

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(
        onsets=onsets,
        event_durations=event_durations,
        total_time=duration,
    )

    # Mask the volume to be the same shape as a brain
    mask, template = sim.mask_brain(dimensions_tr, mask_threshold=0.2)
    stimfunction_tr = stimfunction[::int(tr_duration * 100)]
    noise = sim.generate_noise(
        dimensions=dimensions_tr[0:3],
        stimfunction_tr=stimfunction_tr,
        tr_duration=tr_duration,
        template=template,
        mask=mask,
        noise_dict=nd_orig,
    )

    # Check that noise_system is being calculated correctly
    spatial_sd = 5
    temporal_sd = 5
    noise_system = sim._generate_noise_system(dimensions_tr, spatial_sd,
                                              temporal_sd)

    precision = abs(noise_system[0, 0, 0, :].std() - spatial_sd)
    assert precision < spatial_sd, 'noise_system calculated incorrectly'

    precision = abs(noise_system[:, :, :, 0].std() - temporal_sd)
    assert precision < spatial_sd, 'noise_system calculated incorrectly'

    # Calculate the noise
    nd_calc = sim.calc_noise(volume=noise, mask=mask)

    # How precise are these estimates
    precision = abs(nd_calc['snr'] - nd_orig['snr'])
    assert precision < nd_orig['snr'], 'snr calculated incorrectly'

    precision = abs(nd_calc['sfnr'] - nd_orig['sfnr'])
    assert precision < nd_orig['sfnr'], 'sfnr calculated incorrectly'
Example #3
0
def test_calc_noise():

    # Inputs for functions
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 200
    tr_number = int(np.floor(duration / tr_duration))
    dimensions_tr = np.array([10, 10, 10, tr_number])

    # Preset the noise dict
    nd_orig = {'auto_reg_sigma': 0.6,
               'drift_sigma': 0.4,
               'snr': 30,
               'sfnr': 30,
               'max_activity': 1000,
               'fwhm': 4,
               }

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(onsets=onsets,
                                             event_durations=event_durations,
                                             total_time=duration,
                                             )

    # Mask the volume to be the same shape as a brain
    mask, template = sim.mask_brain(dimensions_tr, mask_threshold=0.2)
    stimfunction_tr = stimfunction[::int(tr_duration * 100)]
    noise = sim.generate_noise(dimensions=dimensions_tr[0:3],
                               stimfunction_tr=stimfunction_tr,
                               tr_duration=tr_duration,
                               template=template,
                               mask=mask,
                               noise_dict=nd_orig,
                               )

    # Check that noise_system is being calculated correctly
    spatial_sd = 5
    temporal_sd = 5
    noise_system = sim._generate_noise_system(dimensions_tr,
                                              spatial_sd,
                                              temporal_sd)

    precision = abs(noise_system[0, 0, 0, :].std() - spatial_sd)
    assert precision < spatial_sd, 'noise_system calculated incorrectly'

    precision = abs(noise_system[:, :, :, 0].std() - temporal_sd)
    assert precision < spatial_sd, 'noise_system calculated incorrectly'

    # Calculate the noise
    nd_calc = sim.calc_noise(volume=noise,
                             mask=mask)

    # How precise are these estimates
    precision = abs(nd_calc['snr'] - nd_orig['snr'])
    assert precision < nd_orig['snr'], 'snr calculated incorrectly'

    precision = abs(nd_calc['sfnr'] - nd_orig['sfnr'])
    assert precision < nd_orig['sfnr'], 'sfnr calculated incorrectly'
Example #4
0
def test_mask_brain():

    # Inputs for generate_signal
    dimensions = np.array([10, 10, 10])  # What is the size of the brain
    feature_size = [2]
    feature_type = ['cube']
    feature_coordinates = np.array([[4, 4, 4]])
    signal_magnitude = [30]

    # Generate a volume representing the location and quality of the signal
    volume = sim.generate_signal(
        dimensions=dimensions,
        feature_coordinates=feature_coordinates,
        feature_type=feature_type,
        feature_size=feature_size,
        signal_magnitude=signal_magnitude,
    )

    # Mask the volume to be the same shape as a brain
    mask = sim.mask_brain(volume)[:, :, :, 0]
    brain = volume * (mask > 0)

    assert np.sum(brain != 0) == np.sum(volume != 0), "Masking did not work"
    assert brain[0, 0, 0] == 0, "Masking did not work"
    assert brain[4, 4, 4] != 0, "Masking did not work"

    feature_coordinates = np.array([[1, 1, 1]])

    volume = sim.generate_signal(
        dimensions=dimensions,
        feature_coordinates=feature_coordinates,
        feature_type=feature_type,
        feature_size=feature_size,
        signal_magnitude=signal_magnitude,
    )

    # Mask the volume to be the same shape as a brain
    mask = sim.mask_brain(volume)[:, :, :, 0]
    brain = volume * (mask > 0)

    assert np.sum(brain != 0) < np.sum(volume != 0), "Masking did not work"
Example #5
0
def test_mask_brain():

    # Inputs for generate_signal
    dimensions = np.array([10, 10, 10])  # What is the size of the brain
    feature_size = [2]
    feature_type = ['cube']
    feature_coordinates = np.array(
        [[4, 4, 4]])
    signal_magnitude = [30]

    # Generate a volume representing the location and quality of the signal
    volume = sim.generate_signal(dimensions=dimensions,
                                 feature_coordinates=feature_coordinates,
                                 feature_type=feature_type,
                                 feature_size=feature_size,
                                 signal_magnitude=signal_magnitude,
                                 )

    # Mask the volume to be the same shape as a brain
    mask, _ = sim.mask_brain(volume)
    brain = volume * mask

    assert np.sum(brain != 0) == np.sum(volume != 0), "Masking did not work"
    assert brain[0, 0, 0] == 0, "Masking did not work"
    assert brain[4, 4, 4] != 0, "Masking did not work"

    feature_coordinates = np.array(
        [[1, 1, 1]])

    volume = sim.generate_signal(dimensions=dimensions,
                                 feature_coordinates=feature_coordinates,
                                 feature_type=feature_type,
                                 feature_size=feature_size,
                                 signal_magnitude=signal_magnitude,
                                 )

    # Mask the volume to be the same shape as a brain
    mask, _ = sim.mask_brain(volume)
    brain = volume * mask

    assert np.sum(brain != 0) < np.sum(volume != 0), "Masking did not work"
Example #6
0
def test_calc_noise():

    # Inputs for functions
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 100
    tr_number = int(np.floor(duration / tr_duration))
    dimensions_tr = np.array([10, 10, 10, tr_number])

    # Preset the noise dict
    nd_orig = {
        'auto_reg_sigma': 1,
        'drift_sigma': 0.5,
        'overall': 0.1,
        'snr': 30,
        'spatial_sigma': 0.15,
        'system_sigma': 1,
    }

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(
        onsets=onsets,
        event_durations=event_durations,
        total_time=duration,
    )

    # Mask the volume to be the same shape as a brain
    mask = sim.mask_brain(dimensions_tr)
    noise = sim.generate_noise(
        dimensions=dimensions_tr[0:3],
        stimfunction=stimfunction,
        tr_duration=tr_duration,
        mask=mask,
        noise_dict=nd_orig,
    )

    # Calculate the noise
    nd_calc = sim.calc_noise(noise, mask)

    assert abs(nd_calc['overall'] - nd_orig['overall']) < 0.1, 'overall ' \
                                                               'calculated ' \
                                                               'incorrectly'

    assert abs(nd_calc['snr'] - nd_orig['snr']) < 10, 'snr calculated ' \
                                                      'incorrectly'

    assert abs(nd_calc['system_sigma'] - nd_orig['system_sigma']) < 1, \
        'snr calculated incorrectly'
Example #7
0
def test_calc_noise():

    # Inputs for functions
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 100
    tr_number = int(np.floor(duration / tr_duration))
    dimensions_tr = np.array([10, 10, 10, tr_number])

    # Preset the noise dict
    nd_orig = {
        'auto_reg_sigma': 0.6,
        'drift_sigma': 0.4,
        'temporal_noise': 5,
        'sfnr': 30,
        'max_activity': 1000,
        'fwhm': 4,
    }

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(
        onsets=onsets,
        event_durations=event_durations,
        total_time=duration,
    )

    # Mask the volume to be the same shape as a brain
    mask = sim.mask_brain(dimensions_tr)
    stimfunction_tr = stimfunction[::int(tr_duration * 1000)]
    noise = sim.generate_noise(
        dimensions=dimensions_tr[0:3],
        stimfunction_tr=stimfunction_tr,
        tr_duration=tr_duration,
        mask=mask,
        noise_dict=nd_orig,
    )

    # Calculate the noise
    nd_calc = sim.calc_noise(noise, mask)

    # How precise are these estimates
    precision = abs(nd_calc['temporal_noise'] - nd_orig['temporal_noise'])
    assert precision < 1, 'temporal_noise calculated incorrectly'

    precision = abs(nd_calc['sfnr'] - nd_orig['sfnr'])
    assert precision < 5, 'sfnr calculated incorrectly'
Example #8
0
def test_calc_noise():

    # Inputs for functions
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 100
    tr_number = int(np.floor(duration / tr_duration))
    dimensions_tr = np.array([10, 10, 10, tr_number])

    # Preset the noise dict
    nd_orig = {'auto_reg_sigma': 1,
               'drift_sigma': 0.5,
               'overall': 0.1,
               'snr': 30,
               'spatial_sigma': 0.15,
               'system_sigma': 1,
               }

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(onsets=onsets,
                                             event_durations=event_durations,
                                             total_time=duration,
                                             )

    # Mask the volume to be the same shape as a brain
    mask = sim.mask_brain(dimensions_tr)
    noise = sim.generate_noise(dimensions=dimensions_tr[0:3],
                               stimfunction=stimfunction,
                               tr_duration=tr_duration,
                               mask=mask,
                               noise_dict=nd_orig,
                               )

    # Calculate the noise
    nd_calc = sim.calc_noise(noise, mask)

    assert abs(nd_calc['overall'] - nd_orig['overall']) < 0.1, 'overall ' \
                                                               'calculated ' \
                                                               'incorrectly'

    assert abs(nd_calc['snr'] - nd_orig['snr']) < 10, 'snr calculated ' \
                                                      'incorrectly'

    assert abs(nd_calc['system_sigma'] - nd_orig['system_sigma']) < 1, \
        'snr calculated incorrectly'
Example #9
0
def get_MNI152_template(dim_x, dim_y, dim_z):
    """get MNI152 template used in fmrisim
    Parameters
    ----------
    dim_x: int
    dim_y: int
    dim_z: int
        - dims set the size of the volume we want to create
    
    Return
    -------
    MNI_152_template: 3d array (dim_x, dim_y, dim_z)
    """
    # Import the fmrisim from BrainIAK
    import brainiak.utils.fmrisim as sim
    # Make a grey matter mask into a 3d volume of a given size
    dimensions = np.asarray([dim_x, dim_y, dim_z])
    _, MNI_152_template = sim.mask_brain(dimensions)
    return MNI_152_template
Example #10
0
def test_generate_noise():

    dimensions = np.array([10, 10, 10])  # What is the size of the brain
    feature_size = [2]
    feature_type = ['cube']
    feature_coordinates = np.array(
        [[5, 5, 5]])
    signal_magnitude = [1]

    # Generate a volume representing the location and quality of the signal
    volume = sim.generate_signal(dimensions=dimensions,
                                 feature_coordinates=feature_coordinates,
                                 feature_type=feature_type,
                                 feature_size=feature_size,
                                 signal_magnitude=signal_magnitude,
                                 )

    # Inputs for generate_stimfunction
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 200

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(onsets=onsets,
                                             event_durations=event_durations,
                                             total_time=duration,
                                             )

    signal_function = sim.convolve_hrf(stimfunction=stimfunction,
                                       tr_duration=tr_duration,
                                       )

    # Convolve the HRF with the stimulus sequence
    signal = sim.apply_signal(signal_function=signal_function,
                              volume_signal=volume,
                              )

    # Generate the mask of the signal
    mask, template = sim.mask_brain(signal, mask_threshold=0.1)

    assert min(mask[mask > 0]) > 0.1, "Mask thresholding did not work"
    assert len(np.unique(template) > 2), "Template creation did not work"

    stimfunction_tr = stimfunction[::int(tr_duration * 100)]
    # Create the noise volumes (using the default parameters)
    noise = sim.generate_noise(dimensions=dimensions,
                               stimfunction_tr=stimfunction_tr,
                               tr_duration=tr_duration,
                               template=template,
                               mask=mask,
                               )

    assert signal.shape == noise.shape, "The dimensions of signal and noise " \
                                        "the same"

    assert np.std(signal) < np.std(noise), "Noise was not created"

    noise = sim.generate_noise(dimensions=dimensions,
                               stimfunction_tr=stimfunction_tr,
                               tr_duration=tr_duration,
                               template=template,
                               mask=mask,
                               noise_dict={'sfnr': 10000, 'snr': 10000},
                               )

    system_noise = np.std(noise[mask > 0], 1).mean()

    assert system_noise <= 0.1, "Noise strength could not be manipulated"
Example #11
0
# Set up stimulus event time course parameters
event_duration = 15  # How long is each event
isi = 5  # What is the time between each event

# Specify signal magnitude parameters
signal_change = 10  # How much change is there in intensity for the max of the patterns across participants
multivariate_pattern = 1  # Do you want the signal to be a z scored pattern across voxels (1) or a univariate increase (0)

print('Load template of average voxel value')
template_nii = image.load_img(os.path.join(dat_dir, 'sub_template.nii.gz'))
template = template_nii.get_fdata()
dimensions = np.array(template.shape[0:3])

print('Create binary mask and normalize the template range')
mask, template = sim.mask_brain(volume=template, mask_self=True)
mask_cherry = image.load_img(
    os.path.join(dat_dir, 'cherry_pick_brain_mask.nii.gz')).get_fdata()

# Load the noise dictionary
print('Loading noise parameters')
with open(os.path.join(dat_dir, 'sub_noise_dict.txt'), 'r') as f:
    noise_dict = f.read()
noise_dict = eval(noise_dict)
noise_dict['matched'] = 0

# ------------------------
# Cherry pick 1000 voxels
# ------------------------
#brain_index = np.where(mask == 1)
#brain_coordinates = np.array([[x for x in brain_index[0]], [x for x in brain_index[1]], [x for x in brain_index[2]]])
Example #12
0
    # Multiply the HRF timecourse with the signal
    signal_cond = sim.apply_signal(signal_function=signal_function,
                                   volume_static=volume_static,
                                   )

    # Concatenate all the signal and function files
    if cond == 0:
        stimfunction = stimfunction_cond
        signal = signal_cond
    else:
        stimfunction = list(np.add(stimfunction, stimfunction_cond))
        signal += signal_cond

# Generate the mask of the signal
mask = sim.mask_brain(signal)

# Mask the signal to the shape of a brain (does not attenuate signal according
# to grey matter likelihood)
signal *= mask > 0

# Iterate through the participants and store participants
epochs = []
for participantcounter in range(1, participants + 1):

    # Add the epoch cube
    epochs += [epoch]

    # Save a file name
    savename = directory + 'p' + str(participantcounter) + '.nii'
Example #13
0
def generate_data(cfgFile):
    cfg = loadConfigFile(cfgFile)
    frame = inspect.currentframe()
    moduleFile = typing.cast(str, frame.f_code.co_filename)  # type: ignore
    moduleDir = os.path.dirname(moduleFile)
    cfgDate = parser.parse(cfg.session.date).strftime("%Y%m%d")
    dataDir = os.path.join(
        cfg.session.dataDir, "subject{}/day{}".format(cfg.session.subjectNum,
                                                      cfg.session.subjectDay))
    imgDir = os.path.join(
        cfg.session.imgDir, "{}.{}.{}".format(cfgDate, cfg.session.subjectName,
                                              cfg.session.subjectName))
    if os.path.exists(dataDir) and os.path.exists(imgDir):
        print(
            "output data and imgage directory already exist, skippig data generation"
        )
        return
    runPatterns = [
        'patternsdesign_1_20180101T000000.mat',
        'patternsdesign_2_20180101T000000.mat',
        'patternsdesign_3_20180101T000000.mat'
    ]
    template_filename = os.path.join(moduleDir, 'sub_template.nii.gz')
    noise_dict_filename = os.path.join(moduleDir, 'sub_noise_dict.txt')
    roiA_filename = os.path.join(moduleDir, 'ROI_A.nii.gz')
    roiB_filename = os.path.join(moduleDir, 'ROI_B.nii.gz')
    output_file_pattern = '001_0000{}_000{}.mat'
    if not os.path.exists(imgDir):
        os.makedirs(imgDir)
    if not os.path.exists(dataDir):
        os.makedirs(dataDir)

    print('Load data')
    template_nii = nibabel.load(template_filename)
    template = template_nii.get_data()
    # dimsize = template_nii.header.get_zooms()

    roiA_nii = nibabel.load(roiA_filename)
    roiB_nii = nibabel.load(roiB_filename)
    roiA = roiA_nii.get_data()
    roiB = roiB_nii.get_data()

    dimensions = np.array(template.shape[0:3])  # What is the size of the brain

    print('Create mask')
    # Generate the continuous mask from the voxels
    mask, template = sim.mask_brain(
        volume=template,
        mask_self=True,
    )
    # Write out the mask as matlab
    mask_uint8 = mask.astype(np.uint8)
    maskfilename = os.path.join(
        dataDir, 'mask_{}_{}.mat'.format(cfg.session.subjectNum,
                                         cfg.session.subjectDay))
    sio.savemat(maskfilename, {'mask': mask_uint8})

    # Load the noise dictionary
    with open(noise_dict_filename, 'r') as f:
        noise_dict = f.read()

    print('Loading ' + noise_dict_filename)
    noise_dict = eval(noise_dict)
    noise_dict['matched'] = 0

    runNum = 1
    scanNum = 0
    for patfile in runPatterns:
        fullPatfile = os.path.join(moduleDir, patfile)
        # make dataDir run directory
        runDir = os.path.join(dataDir, "run{}".format(runNum))
        if not os.path.exists(runDir):
            os.makedirs(runDir)
        shutil.copy(fullPatfile, runDir)
        runNum += 1

        pat = sio.loadmat(fullPatfile)
        scanNum += 1
        # shifted labels are in regressor field
        shiftedLabels = pat['patterns']['regressor'][0][0]
        # non-shifted labels are in attCateg field and whether stimulus applied in the stim field
        nsLabels = pat['patterns']['attCateg'][0][0] * pat['patterns']['stim'][
            0][0]
        labels_A = (nsLabels == 1).astype(int)
        labels_B = (nsLabels == 2).astype(int)

        # trialType = pat['patterns']['type'][0][0]
        tr_duration = pat['TR'][0][0]
        disdaqs = pat['disdaqs'][0][0]
        begTrOffset = disdaqs // tr_duration
        nTRs = pat['nTRs'][0][0]
        # nTestTRs = np.count_nonzero(trialType == 2)

        # Preset some of the parameters
        total_trs = nTRs + begTrOffset  # How many time points are there?

        print('Generating data')
        start = time.time()
        noiseVols = sim.generate_noise(
            dimensions=dimensions,
            stimfunction_tr=np.zeros((total_trs, 1)),
            tr_duration=int(tr_duration),
            template=template,
            mask=mask,
            noise_dict=noise_dict,
        )
        print("Time: generate noise vols {} sec".format(time.time() - start))

        nVoxelsA = int(roiA.sum())
        nVoxelsB = int(roiB.sum())
        # Multiply each pattern by each voxel time course
        weights_A = np.tile(labels_A.reshape(-1, 1), nVoxelsA)
        weights_B = np.tile(labels_B.reshape(-1, 1), nVoxelsB)

        print('Creating signal time course')
        signal_func_A = sim.convolve_hrf(
            stimfunction=weights_A,
            tr_duration=tr_duration,
            temporal_resolution=(1 / tr_duration),
            scale_function=1,
        )

        signal_func_B = sim.convolve_hrf(
            stimfunction=weights_B,
            tr_duration=tr_duration,
            temporal_resolution=(1 / tr_duration),
            scale_function=1,
        )

        max_activity = noise_dict['max_activity']
        signal_change = 10  # .01 * max_activity
        signal_func_A *= signal_change
        signal_func_B *= signal_change

        # Combine the signal time course with the signal volume
        print('Creating signal volumes')
        signal_A = sim.apply_signal(
            signal_func_A,
            roiA,
        )

        signal_B = sim.apply_signal(
            signal_func_B,
            roiB,
        )
        # Combine the two signal timecourses
        signal = signal_A + signal_B

        # testTrId = 0
        numVols = noiseVols.shape[3]
        for idx in range(numVols):
            start = time.time()
            brain = noiseVols[:, :, :, idx]
            if idx >= begTrOffset:
                # some initial scans are skipped as only instructions and not stimulus are shown
                signalIdx = idx - begTrOffset
                brain += signal[:, :, :, signalIdx]

            # TODO: how to create a varying combined percentage of A and B signals
            #     if trialType[0][idx] == 1:
            #         # training TR, so create pure A or B signal
            #         if labels_A[idx] != 0:
            #             brain = brain + roiA
            #         elif labels_B[idx] != 0:
            #             brain = brain + roiB
            #     elif trialType[0][idx] == 2:
            #         # testing TR, so create a mixture of A and B signal
            #         testTrId += 1
            #         testPercent = testTrId / nTestTRs
            #         brain = brain + testPercent * roiA + (1-testPercent) * roiB

            # Save the volume as a matlab file
            filenum = idx + 1
            filename = output_file_pattern.format(
                str(scanNum).zfill(2),
                str(filenum).zfill(3))
            outputfile = os.path.join(imgDir, filename)
            brain_float32 = brain.astype(np.float32)
            sio.savemat(outputfile, {'vol': brain_float32})
            print("Time: generate vol {}: {} sec".format(
                filenum,
                time.time() - start))
Example #14
0
def generate_data(outputDir, user_settings):
    """Generate simulated fMRI data
    Use a few parameters that might be relevant for real time analysis

    Parameters
    ----------

    outputDir : str
        Specify output data dir where the data should be saved

    user_settings : dict
        A dictionary to specify the parameters used for making data,
        specifying the following keys
        numTRs - int - Specify the number of time points
        multivariate_patterns - bool - Is the difference between conditions
        univariate (0) or multivariate (1)
        different_ROIs - bool - Are there different ROIs for each condition (
        1) or is it in the same ROI (0). If it is the same ROI and you are
        using univariate differences, the second condition will have a
        smaller evoked response than the other.
        event_duration - int - How long, in seconds, is each event
        scale_percentage - float - What is the percent signal change
        trDuration - float - How many seconds per volume
        save_dicom - bool - Save to data as a dicom (1) or numpy (0)
        save_realtime - bool - Do you want to save the data in real time (1)
        or as fast as possible (0)?
        isi - float - What is the time between each event (in seconds)
        burn_in - int - How long before the first event (in seconds)

    """
    data_dict = default_settings.copy()
    data_dict.update(user_settings)

    # If the folder doesn't exist then make it
    os.system('mkdir -p %s' % outputDir)

    logger.info('Load template of average voxel value')

    # Get the file names needed for loading in the data
    ROI_A_file, ROI_B_file, template_path, noise_dict_file = \
        _get_input_names(data_dict)

    # Load in the template data (it may already be loaded if doing a test)
    if isinstance(template_path, str):
        template_nii = nibabel.load(template_path)
        template = template_nii.get_data()
    else:
        template = template_path

    dimensions = np.array(template.shape[0:3])

    logger.info('Create binary mask and normalize the template range')
    mask, template = sim.mask_brain(
        volume=template,
        mask_self=True,
    )

    # Write out the mask as a numpy file
    outFile = os.path.join(outputDir, 'mask.npy')
    np.save(outFile, mask.astype(np.uint8))

    # Load the noise dictionary
    logger.info('Loading noise parameters')

    # If this isn't a string, assume it is a resource stream file
    if type(noise_dict_file) is str:
        with open(noise_dict_file, 'r') as f:
            noise_dict = f.read()
    else:
        # Read the resource stream object
        noise_dict = noise_dict_file.decode()

    noise_dict = eval(noise_dict)
    noise_dict['matched'] = 0  # Increases processing time

    # Add it here for easy access
    data_dict['noise_dict'] = noise_dict

    logger.info('Generating noise')
    temp_stimfunction = np.zeros((data_dict['numTRs'], 1))
    noise = sim.generate_noise(
        dimensions=dimensions,
        stimfunction_tr=temp_stimfunction,
        tr_duration=int(data_dict['trDuration']),
        template=template,
        mask=mask,
        noise_dict=noise_dict,
    )

    # Create the stimulus time course of the conditions
    total_time = int(data_dict['numTRs'] * data_dict['trDuration'])
    onsets_A = []
    onsets_B = []
    curr_time = data_dict['burn_in']
    while curr_time < (total_time - data_dict['event_duration']):

        # Flip a coin for each epoch to determine whether it is A or B
        if np.random.randint(0, 2) == 1:
            onsets_A.append(curr_time)
        else:
            onsets_B.append(curr_time)

        # Increment the current time
        curr_time += data_dict['event_duration'] + data_dict['isi']

    # How many timepoints per second of the stim function are to be generated?
    temporal_res = 1 / data_dict['trDuration']

    # Create a time course of events
    event_durations = [data_dict['event_duration']]
    stimfunc_A = sim.generate_stimfunction(
        onsets=onsets_A,
        event_durations=event_durations,
        total_time=total_time,
        temporal_resolution=temporal_res,
    )

    stimfunc_B = sim.generate_stimfunction(
        onsets=onsets_B,
        event_durations=event_durations,
        total_time=total_time,
        temporal_resolution=temporal_res,
    )

    # Create a labels timecourse
    outFile = os.path.join(outputDir, 'labels.npy')
    np.save(outFile, (stimfunc_A + (stimfunc_B * 2)))

    # How is the signal implemented in the different ROIs
    signal_A = _generate_ROIs(ROI_A_file, stimfunc_A, noise,
                              data_dict['scale_percentage'], data_dict)
    if data_dict['different_ROIs'] is True:

        signal_B = _generate_ROIs(ROI_B_file, stimfunc_B, noise,
                                  data_dict['scale_percentage'], data_dict)

    else:

        # Halve the evoked response if these effects are both expected in the
        #  same ROI
        if data_dict['multivariate_pattern'] is False:
            signal_B = _generate_ROIs(ROI_A_file, stimfunc_B, noise,
                                      data_dict['scale_percentage'] * 0.5,
                                      data_dict)
        else:
            signal_B = _generate_ROIs(ROI_A_file, stimfunc_B, noise,
                                      data_dict['scale_percentage'], data_dict)

    # Combine the two signal timecourses
    signal = signal_A + signal_B

    logger.info('Generating TRs in real time')
    for idx in range(data_dict['numTRs']):

        #  Create the brain volume on this TR
        brain = noise[:, :, :, idx] + signal[:, :, :, idx]

        # Convert file to integers to mimic what you get from MR
        brain_int32 = brain.astype(np.int32)

        # Store as dicom or nifti?
        if data_dict['save_dicom'] is True:
            # Save the volume as a DICOM file, with each TR as its own file
            output_file = os.path.join(outputDir,
                                       'rt_' + format(idx, '03d') + '.dcm')
            _write_dicom(output_file, brain_int32, idx + 1)
        else:
            # Save the volume as a numpy file, with each TR as its own file
            output_file = os.path.join(outputDir,
                                       'rt_' + format(idx, '03d') + '.npy')
            np.save(output_file, brain_int32)

        logger.info("Generate {}".format(output_file))

        # Sleep until next TR
        if data_dict['save_realtime'] == 1:
            time.sleep(data_dict['trDuration'])
Example #15
0
signal_A = sim.apply_signal(signal_function=signal_function_A,
                            volume_static=volume_static_A,
                            )

signal_B = sim.apply_signal(signal_function=signal_function_B,
                            volume_static=volume_static_B,
                            )

# Combine the signals from the two conditions
signal = signal_A + signal_B

# Combine the stim functions
stimfunction = list(np.add(stimfunction_A, stimfunction_B))

# Generate the mask of the signal
mask = sim.mask_brain(signal)

# Mask the signal to the shape of a brain (attenuates signal according to grey
# matter likelihood)
signal *= mask

# Create the noise volumes (using the default parameters
noise = sim.generate_noise(dimensions=dimensions,
                           stimfunction=stimfunction,
                           tr_duration=tr_duration,
                           mask=mask,
                           )

# Combine the signal and the noise
brain = signal + noise
Example #16
0
                            volume_signal=volume_signal_A,
                            )

signal_B = sim.apply_signal(signal_function=signal_function_B,
                            volume_signal=volume_signal_B,
                            )

# Combine the signals from the two conditions
signal = signal_A + signal_B

# Combine the stim functions
stimfunction = list(np.add(stimfunction_A, stimfunction_B))
stimfunction_tr = stimfunction[::int(tr_duration * temporal_res)]

# Generate the mask of the signal
mask, template = sim.mask_brain(signal, mask_threshold=0.2)

# Mask the signal to the shape of a brain (attenuates signal according to grey
# matter likelihood)
signal *= mask.reshape(dimensions[0], dimensions[1], dimensions[2], 1)

# Generate original noise dict for comparison later
orig_noise_dict = sim._noise_dict_update({})

# Create the noise volumes (using the default parameters
noise = sim.generate_noise(dimensions=dimensions,
                           stimfunction_tr=stimfunction_tr,
                           tr_duration=tr_duration,
                           mask=mask,
                           template=template,
                           noise_dict=orig_noise_dict,
Example #17
0
def test_generate_noise():

    dimensions = np.array([10, 10, 10])  # What is the size of the brain
    feature_size = [2]
    feature_type = ['cube']
    feature_coordinates = np.array(
        [[5, 5, 5]])
    signal_magnitude = [1]

    # Generate a volume representing the location and quality of the signal
    volume = sim.generate_signal(dimensions=dimensions,
                                 feature_coordinates=feature_coordinates,
                                 feature_type=feature_type,
                                 feature_size=feature_size,
                                 signal_magnitude=signal_magnitude,
                                 )

    # Inputs for generate_stimfunction
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 100

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(onsets=onsets,
                                             event_durations=event_durations,
                                             total_time=duration,
                                             )

    signal_function = sim.double_gamma_hrf(stimfunction=stimfunction,
                                           tr_duration=tr_duration,
                                           )

    # Convolve the HRF with the stimulus sequence
    signal = sim.apply_signal(signal_function=signal_function,
                              volume_static=volume,
                              )

    # Generate the mask of the signal
    mask = sim.mask_brain(signal, mask_threshold=0.1)

    assert min(mask[mask > 0]) > 0.1, "Mask thresholding did not work"

    # Create the noise volumes (using the default parameters)
    noise = sim.generate_noise(dimensions=dimensions,
                               stimfunction=stimfunction,
                               tr_duration=tr_duration,
                               mask=mask,
                               )

    assert signal.shape == noise.shape, "The dimensions of signal and noise " \
                                        "the same"

    assert np.std(signal) < np.std(noise), "Noise was not created"

    noise = sim.generate_noise(dimensions=dimensions,
                               stimfunction=stimfunction,
                               tr_duration=tr_duration,
                               mask=mask,
                               noise_dict={'overall': 0},
                               )

    assert np.sum(noise) == 0, "Noise strength could not be manipulated"
    assert np.std(noise) == 0, "Noise strength could not be manipulated"
Example #18
0
def test_calc_noise():

    # Inputs for functions
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 200
    temporal_res = 100
    tr_number = int(np.floor(duration / tr_duration))
    dimensions_tr = np.array([10, 10, 10, tr_number])

    # Preset the noise dict
    nd_orig = sim._noise_dict_update({})

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(
        onsets=onsets,
        event_durations=event_durations,
        total_time=duration,
        temporal_resolution=temporal_res,
    )

    # Mask the volume to be the same shape as a brain
    mask, template = sim.mask_brain(dimensions_tr, mask_self=None)
    stimfunction_tr = stimfunction[::int(tr_duration * temporal_res)]

    nd_orig['matched'] = 0
    noise = sim.generate_noise(
        dimensions=dimensions_tr[0:3],
        stimfunction_tr=stimfunction_tr,
        tr_duration=tr_duration,
        template=template,
        mask=mask,
        noise_dict=nd_orig,
    )

    # Check the spatial noise match
    nd_orig['matched'] = 1
    noise_matched = sim.generate_noise(dimensions=dimensions_tr[0:3],
                                       stimfunction_tr=stimfunction_tr,
                                       tr_duration=tr_duration,
                                       template=template,
                                       mask=mask,
                                       noise_dict=nd_orig,
                                       iterations=[50, 0])

    # Calculate the noise parameters from this newly generated volume
    nd_new = sim.calc_noise(noise, mask, template)
    nd_matched = sim.calc_noise(noise_matched, mask, template)

    # Check the values are reasonable"
    assert nd_new['snr'] > 0, 'snr out of range'
    assert nd_new['sfnr'] > 0, 'sfnr out of range'
    assert nd_new['auto_reg_rho'][0] > 0, 'ar out of range'

    # Check that the dilation increases SNR
    no_dilation_snr = sim._calc_snr(
        noise_matched,
        mask,
        dilation=0,
        reference_tr=tr_duration,
    )

    assert nd_new['snr'] > no_dilation_snr, "Dilation did not increase SNR"

    # Check that template size is in bounds
    with pytest.raises(ValueError):
        sim.calc_noise(noise, mask, template * 2)

    # Check that Mask is set is checked
    with pytest.raises(ValueError):
        sim.calc_noise(noise, None, template)

    # Check that it can deal with missing noise parameters
    temp_nd = sim.calc_noise(noise, mask, template, noise_dict={})
    assert temp_nd['voxel_size'][0] == 1, 'Default voxel size not set'

    temp_nd = sim.calc_noise(noise, mask, template, noise_dict=None)
    assert temp_nd['voxel_size'][0] == 1, 'Default voxel size not set'

    # Check that the fitting worked
    snr_diff = abs(nd_orig['snr'] - nd_new['snr'])
    snr_diff_match = abs(nd_orig['snr'] - nd_matched['snr'])
    assert snr_diff > snr_diff_match, 'snr fit incorrectly'

    # Test that you can generate rician and exponential noise
    sim._generate_noise_system(
        dimensions_tr,
        1,
        1,
        spatial_noise_type='exponential',
        temporal_noise_type='rician',
    )

    # Check the temporal noise match
    nd_orig['matched'] = 1
    noise_matched = sim.generate_noise(dimensions=dimensions_tr[0:3],
                                       stimfunction_tr=stimfunction_tr,
                                       tr_duration=tr_duration,
                                       template=template,
                                       mask=mask,
                                       noise_dict=nd_orig,
                                       iterations=[0, 50])

    nd_matched = sim.calc_noise(noise_matched, mask, template)

    sfnr_diff = abs(nd_orig['sfnr'] - nd_new['sfnr'])
    sfnr_diff_match = abs(nd_orig['sfnr'] - nd_matched['sfnr'])
    assert sfnr_diff > sfnr_diff_match, 'sfnr fit incorrectly'

    ar1_diff = abs(nd_orig['auto_reg_rho'][0] - nd_new['auto_reg_rho'][0])
    ar1_diff_match = abs(nd_orig['auto_reg_rho'][0] -
                         nd_matched['auto_reg_rho'][0])
    assert ar1_diff > ar1_diff_match, 'AR1 fit incorrectly'

    # Check that you can calculate ARMA for a single voxel
    vox = noise[5, 5, 5, :]
    arma = sim._calc_ARMA_noise(
        vox,
        None,
        sample_num=2,
    )
    assert len(arma) == 2, "Two outputs not given by ARMA"
Example #19
0
def test_calc_noise():

    # Inputs for functions
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 200
    temporal_res = 100
    tr_number = int(np.floor(duration / tr_duration))
    dimensions_tr = np.array([10, 10, 10, tr_number])

    # Preset the noise dict
    nd_orig = sim._noise_dict_update({})

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(onsets=onsets,
                                             event_durations=event_durations,
                                             total_time=duration,
                                             temporal_resolution=temporal_res,
                                             )

    # Mask the volume to be the same shape as a brain
    mask, template = sim.mask_brain(dimensions_tr, mask_self=None)
    stimfunction_tr = stimfunction[::int(tr_duration * temporal_res)]

    nd_orig['matched'] = 0
    noise = sim.generate_noise(dimensions=dimensions_tr[0:3],
                               stimfunction_tr=stimfunction_tr,
                               tr_duration=tr_duration,
                               template=template,
                               mask=mask,
                               noise_dict=nd_orig,
                               )

    # Check the spatial noise match
    nd_orig['matched'] = 1
    noise_matched = sim.generate_noise(dimensions=dimensions_tr[0:3],
                                       stimfunction_tr=stimfunction_tr,
                                       tr_duration=tr_duration,
                                       template=template,
                                       mask=mask,
                                       noise_dict=nd_orig,
                                       iterations=[50, 0]
                                       )

    # Calculate the noise parameters from this newly generated volume
    nd_new = sim.calc_noise(noise, mask, template)
    nd_matched = sim.calc_noise(noise_matched, mask, template)

    # Check the values are reasonable"
    assert nd_new['snr'] > 0, 'snr out of range'
    assert nd_new['sfnr'] > 0, 'sfnr out of range'
    assert nd_new['auto_reg_rho'][0] > 0, 'ar out of range'

    # Check that the dilation increases SNR
    no_dilation_snr = sim._calc_snr(noise_matched,
                                    mask,
                                    dilation=0,
                                    reference_tr=tr_duration,
                                    )

    assert nd_new['snr'] > no_dilation_snr, "Dilation did not increase SNR"

    # Check that template size is in bounds
    with pytest.raises(ValueError):
        sim.calc_noise(noise, mask, template * 2)

    # Check that Mask is set is checked
    with pytest.raises(ValueError):
        sim.calc_noise(noise, None, template)

    # Check that it can deal with missing noise parameters
    temp_nd = sim.calc_noise(noise, mask, template, noise_dict={})
    assert temp_nd['voxel_size'][0] == 1, 'Default voxel size not set'

    temp_nd = sim.calc_noise(noise, mask, template, noise_dict=None)
    assert temp_nd['voxel_size'][0] == 1, 'Default voxel size not set'

    # Check that the fitting worked
    snr_diff = abs(nd_orig['snr'] - nd_new['snr'])
    snr_diff_match = abs(nd_orig['snr'] - nd_matched['snr'])
    assert snr_diff > snr_diff_match, 'snr fit incorrectly'

    # Test that you can generate rician and exponential noise
    sim._generate_noise_system(dimensions_tr,
                               1,
                               1,
                               spatial_noise_type='exponential',
                               temporal_noise_type='rician',
                               )

    # Check the temporal noise match
    nd_orig['matched'] = 1
    noise_matched = sim.generate_noise(dimensions=dimensions_tr[0:3],
                                       stimfunction_tr=stimfunction_tr,
                                       tr_duration=tr_duration,
                                       template=template,
                                       mask=mask,
                                       noise_dict=nd_orig,
                                       iterations=[0, 50]
                                       )

    nd_matched = sim.calc_noise(noise_matched, mask, template)

    sfnr_diff = abs(nd_orig['sfnr'] - nd_new['sfnr'])
    sfnr_diff_match = abs(nd_orig['sfnr'] - nd_matched['sfnr'])
    assert sfnr_diff > sfnr_diff_match, 'sfnr fit incorrectly'

    ar1_diff = abs(nd_orig['auto_reg_rho'][0] - nd_new['auto_reg_rho'][0])
    ar1_diff_match = abs(nd_orig['auto_reg_rho'][0] - nd_matched[
        'auto_reg_rho'][0])
    assert ar1_diff > ar1_diff_match, 'AR1 fit incorrectly'

    # Check that you can calculate ARMA for a single voxel
    vox = noise[5, 5, 5, :]
    arma = sim._calc_ARMA_noise(vox,
                                None,
                                sample_num=2,
                                )
    assert len(arma) == 2, "Two outputs not given by ARMA"
Example #20
0
def test_apply_signal():

    dimensions = np.array([10, 10, 10])  # What is the size of the brain
    feature_size = [2]
    feature_type = ['cube']
    feature_coordinates = np.array([[5, 5, 5]])
    signal_magnitude = [30]

    # Generate a volume representing the location and quality of the signal
    volume = sim.generate_signal(
        dimensions=dimensions,
        feature_coordinates=feature_coordinates,
        feature_type=feature_type,
        feature_size=feature_size,
        signal_magnitude=signal_magnitude,
    )

    # Inputs for generate_stimfunction
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 100

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(
        onsets=onsets,
        event_durations=event_durations,
        total_time=duration,
    )

    signal_function = sim.convolve_hrf(
        stimfunction=stimfunction,
        tr_duration=tr_duration,
    )

    # Check that you can compute signal change appropriately
    # Preset a bunch of things
    stimfunction_tr = stimfunction[::int(tr_duration * 100)]
    mask, template = sim.mask_brain(dimensions, mask_self=False)
    noise_dict = sim._noise_dict_update({})
    noise = sim.generate_noise(dimensions=dimensions,
                               stimfunction_tr=stimfunction_tr,
                               tr_duration=tr_duration,
                               template=template,
                               mask=mask,
                               noise_dict=noise_dict,
                               iterations=[0, 0])
    coords = feature_coordinates[0]
    noise_function_a = noise[coords[0], coords[1], coords[2], :]
    noise_function_a = noise_function_a.reshape(duration // tr_duration, 1)

    noise_function_b = noise[coords[0] + 1, coords[1], coords[2], :]
    noise_function_b = noise_function_b.reshape(duration // tr_duration, 1)

    # Create the calibrated signal with PSC
    method = 'PSC'
    sig_a = sim.compute_signal_change(
        signal_function,
        noise_function_a,
        noise_dict,
        [0.5],
        method,
    )
    sig_b = sim.compute_signal_change(
        signal_function,
        noise_function_a,
        noise_dict,
        [1.0],
        method,
    )

    assert sig_b.max() / sig_a.max() == 2, 'PSC modulation failed'

    # Create the calibrated signal with SFNR
    method = 'SFNR'
    sig_a = sim.compute_signal_change(
        signal_function,
        noise_function_a,
        noise_dict,
        [0.5],
        method,
    )
    scaled_a = sig_a / (noise_function_a.mean() / noise_dict['sfnr'])
    sig_b = sim.compute_signal_change(
        signal_function,
        noise_function_b,
        noise_dict,
        [1.0],
        method,
    )
    scaled_b = sig_b / (noise_function_b.mean() / noise_dict['sfnr'])

    assert scaled_b.max() / scaled_a.max() == 2, 'SFNR modulation failed'

    # Create the calibrated signal with CNR_Amp/Noise-SD
    method = 'CNR_Amp/Noise-SD'
    sig_a = sim.compute_signal_change(
        signal_function,
        noise_function_a,
        noise_dict,
        [0.5],
        method,
    )
    scaled_a = sig_a / noise_function_a.std()
    sig_b = sim.compute_signal_change(
        signal_function,
        noise_function_b,
        noise_dict,
        [1.0],
        method,
    )
    scaled_b = sig_b / noise_function_b.std()

    assert scaled_b.max() / scaled_a.max() == 2, 'CNR_Amp modulation failed'

    # Create the calibrated signal with CNR_Amp/Noise-Var_dB
    method = 'CNR_Amp2/Noise-Var_dB'
    sig_a = sim.compute_signal_change(
        signal_function,
        noise_function_a,
        noise_dict,
        [0.5],
        method,
    )
    scaled_a = np.log(sig_a.max() / noise_function_a.std())
    sig_b = sim.compute_signal_change(
        signal_function,
        noise_function_b,
        noise_dict,
        [1.0],
        method,
    )
    scaled_b = np.log(sig_b.max() / noise_function_b.std())

    assert np.round(scaled_b / scaled_a) == 2, 'CNR_Amp dB modulation failed'

    # Create the calibrated signal with CNR_Signal-SD/Noise-SD
    method = 'CNR_Signal-SD/Noise-SD'
    sig_a = sim.compute_signal_change(
        signal_function,
        noise_function_a,
        noise_dict,
        [0.5],
        method,
    )
    scaled_a = sig_a.std() / noise_function_a.std()
    sig_b = sim.compute_signal_change(
        signal_function,
        noise_function_a,
        noise_dict,
        [1.0],
        method,
    )
    scaled_b = sig_b.std() / noise_function_a.std()

    assert (scaled_b / scaled_a) == 2, 'CNR signal modulation failed'

    # Create the calibrated signal with CNR_Amp/Noise-Var_dB
    method = 'CNR_Signal-Var/Noise-Var_dB'
    sig_a = sim.compute_signal_change(
        signal_function,
        noise_function_a,
        noise_dict,
        [0.5],
        method,
    )

    scaled_a = np.log(sig_a.std() / noise_function_a.std())
    sig_b = sim.compute_signal_change(
        signal_function,
        noise_function_b,
        noise_dict,
        [1.0],
        method,
    )
    scaled_b = np.log(sig_b.std() / noise_function_b.std())

    assert np.round(scaled_b / scaled_a) == 2, 'CNR signal dB modulation ' \
                                               'failed'

    # Convolve the HRF with the stimulus sequence
    signal = sim.apply_signal(
        signal_function=signal_function,
        volume_signal=volume,
    )

    assert signal.shape == (dimensions[0], dimensions[1], dimensions[2],
                            duration / tr_duration), "The output is the " \
                                                     "wrong size"

    signal = sim.apply_signal(
        signal_function=stimfunction,
        volume_signal=volume,
    )

    assert np.any(signal == signal_magnitude), "The stimfunction is not binary"

    # Check that there is an error if the number of signal voxels doesn't
    # match the number of non zero brain voxels
    with pytest.raises(IndexError):
        sig_vox = (volume > 0).sum()
        vox_pattern = np.tile(stimfunction, (1, sig_vox - 1))
        sim.apply_signal(
            signal_function=vox_pattern,
            volume_signal=volume,
        )
Example #21
0
# Multiply the HRF timecourse with the signal
signal_A = sim.apply_signal(signal_function=signal_function_A, volume_static=volume_static_A)

signal_B = sim.apply_signal(signal_function=signal_function_B, volume_static=volume_static_B)

# Combine the signals from the two conditions
signal = signal_A + signal_B

# Combine the stim functions
stimfunction = list(np.add(stimfunction_A, stimfunction_B))

# Create the noise volumes (using the default parameters
noise = sim.generate_noise(dimensions=dimensions, stimfunction=stimfunction, tr_duration=tr_duration)

# Combine the signal and the noise
volume = signal + noise

# Mask the volume to be the same shape as a brain
brain = sim.mask_brain(volume)

# Display the brain
fig = plt.figure()
for tr_counter in list(range(0, brain.shape[3])):

    # Get the axis to be plotted
    ax = sim.plot_brain(fig, brain[:, :, :, tr_counter], percentile=99.9)

    # Wait for an input
    logging.info(tr_counter)
    plt.pause(0.5)
Example #22
0
def test_generate_noise():

    dimensions = np.array([10, 10, 10])  # What is the size of the brain
    feature_size = [2]
    feature_type = ['cube']
    feature_coordinates = np.array(
        [[5, 5, 5]])
    signal_magnitude = [1]

    # Generate a volume representing the location and quality of the signal
    volume = sim.generate_signal(dimensions=dimensions,
                                 feature_coordinates=feature_coordinates,
                                 feature_type=feature_type,
                                 feature_size=feature_size,
                                 signal_magnitude=signal_magnitude,
                                 )

    # Inputs for generate_stimfunction
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 200

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(onsets=onsets,
                                             event_durations=event_durations,
                                             total_time=duration,
                                             )

    signal_function = sim.convolve_hrf(stimfunction=stimfunction,
                                       tr_duration=tr_duration,
                                       )

    # Convolve the HRF with the stimulus sequence
    signal = sim.apply_signal(signal_function=signal_function,
                              volume_signal=volume,
                              )

    # Generate the mask of the signal
    mask, template = sim.mask_brain(signal,
                                    mask_self=None)

    assert min(mask[mask > 0]) > 0.1, "Mask thresholding did not work"
    assert len(np.unique(template) > 2), "Template creation did not work"

    stimfunction_tr = stimfunction[::int(tr_duration * 100)]

    # Create the noise volumes (using the default parameters)
    noise = sim.generate_noise(dimensions=dimensions,
                               stimfunction_tr=stimfunction_tr,
                               tr_duration=tr_duration,
                               template=template,
                               mask=mask,
                               iterations=[1, 0],
                               )

    assert signal.shape == noise.shape, "The dimensions of signal and noise " \
                                        "the same"

    noise_high = sim.generate_noise(dimensions=dimensions,
                                    stimfunction_tr=stimfunction_tr,
                                    tr_duration=tr_duration,
                                    template=template,
                                    mask=mask,
                                    noise_dict={'sfnr': 50, 'snr': 25},
                                    iterations=[1, 0],
                                    )

    noise_low = sim.generate_noise(dimensions=dimensions,
                                   stimfunction_tr=stimfunction_tr,
                                   tr_duration=tr_duration,
                                   template=template,
                                   mask=mask,
                                   noise_dict={'sfnr': 100, 'snr': 25},
                                   iterations=[1, 0],
                                   )

    system_high = np.std(noise_high[mask > 0], 1).mean()
    system_low = np.std(noise_low[mask > 0], 1).mean()

    assert system_low < system_high, "SFNR noise could not be manipulated"

    # Check that you check for the appropriate template values
    with pytest.raises(ValueError):
        sim.generate_noise(dimensions=dimensions,
                           stimfunction_tr=stimfunction_tr,
                           tr_duration=tr_duration,
                           template=template * 2,
                           mask=mask,
                           noise_dict={},
                           )

    # Check that iterations does what it should
    sim.generate_noise(dimensions=dimensions,
                       stimfunction_tr=stimfunction_tr,
                       tr_duration=tr_duration,
                       template=template,
                       mask=mask,
                       noise_dict={},
                       iterations=[0, 0],
                       )

    sim.generate_noise(dimensions=dimensions,
                       stimfunction_tr=stimfunction_tr,
                       tr_duration=tr_duration,
                       template=template,
                       mask=mask,
                       noise_dict={},
                       iterations=None,
                       )

    # Test drift noise
    trs = 1000
    period = 100
    drift = sim._generate_noise_temporal_drift(trs,
                                               tr_duration,
                                               'sine',
                                               period,
                                               )

    # Check that the max frequency is the appropriate frequency
    power = abs(np.fft.fft(drift))[1:trs // 2]
    freq = np.linspace(1, trs // 2 - 1, trs // 2 - 1) / trs
    period_freq = np.where(freq == 1 / (period // tr_duration))
    max_freq = np.argmax(power)

    assert period_freq == max_freq, 'Max frequency is not where it should be'

    # Do the same but now with cosine basis functions, answer should be close
    drift = sim._generate_noise_temporal_drift(trs,
                                               tr_duration,
                                               'discrete_cos',
                                               period,
                                               )

    # Check that the appropriate frequency is peaky (may not be the max)
    power = abs(np.fft.fft(drift))[1:trs // 2]
    freq = np.linspace(1, trs // 2 - 1, trs // 2 - 1) / trs
    period_freq = np.where(freq == 1 / (period // tr_duration))[0][0]

    assert power[period_freq] > power[period_freq + 1], 'Power is low'
    assert power[period_freq] > power[period_freq - 1], 'Power is low'

    # Check it gives a warning if the duration is too short
    drift = sim._generate_noise_temporal_drift(50,
                                               tr_duration,
                                               'discrete_cos',
                                               period,
                                               )

    # Test physiological noise (using unrealistic parameters so that it's easy)
    timepoints = list(np.linspace(0, (trs - 1) * tr_duration, trs))
    resp_freq = 0.2
    heart_freq = 1.17
    phys = sim._generate_noise_temporal_phys(timepoints,
                                             resp_freq,
                                             heart_freq,
                                             )

    # Check that the max frequency is the appropriate frequency
    power = abs(np.fft.fft(phys))[1:trs // 2]
    freq = np.linspace(1, trs // 2 - 1, trs // 2 - 1) / (trs * tr_duration)
    peaks = (power > (power.mean() + power.std()))  # Where are the peaks
    peak_freqs = freq[peaks]

    assert np.any(resp_freq == peak_freqs), 'Resp frequency not found'
    assert len(peak_freqs) == 2, 'Two peaks not found'

    # Test task noise
    sim._generate_noise_temporal_task(stimfunction_tr,
                                      motion_noise='gaussian',
                                      )
    sim._generate_noise_temporal_task(stimfunction_tr,
                                      motion_noise='rician',
                                      )

    # Test ARMA noise
    with pytest.raises(ValueError):
        noise_dict = {'fwhm': 4, 'auto_reg_rho': [1], 'ma_rho': [1, 1]}
        sim._generate_noise_temporal_autoregression(stimfunction_tr,
                                                    noise_dict,
                                                    dimensions,
                                                    mask,
                                                    )

    # Generate spatial noise
    vol = sim._generate_noise_spatial(np.array([10, 10, 10, trs]))
    assert len(vol.shape) == 3, 'Volume was not reshaped to ignore TRs'

    # Switch some of the noise types on
    noise_dict = dict(physiological_sigma=1, drift_sigma=1, task_sigma=1,
                      auto_reg_sigma=0)
    sim.generate_noise(dimensions=dimensions,
                       stimfunction_tr=stimfunction_tr,
                       tr_duration=tr_duration,
                       template=template,
                       mask=mask,
                       noise_dict=noise_dict,
                       iterations=[0, 0],
                       )
Example #23
0
def test_apply_signal():

    dimensions = np.array([10, 10, 10])  # What is the size of the brain
    feature_size = [2]
    feature_type = ['cube']
    feature_coordinates = np.array(
        [[5, 5, 5]])
    signal_magnitude = [30]

    # Generate a volume representing the location and quality of the signal
    volume = sim.generate_signal(dimensions=dimensions,
                                 feature_coordinates=feature_coordinates,
                                 feature_type=feature_type,
                                 feature_size=feature_size,
                                 signal_magnitude=signal_magnitude,
                                 )

    # Inputs for generate_stimfunction
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 100

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(onsets=onsets,
                                             event_durations=event_durations,
                                             total_time=duration,
                                             )

    signal_function = sim.convolve_hrf(stimfunction=stimfunction,
                                       tr_duration=tr_duration,
                                       )

    # Check that you can compute signal change appropriately
    # Preset a bunch of things
    stimfunction_tr = stimfunction[::int(tr_duration * 100)]
    mask, template = sim.mask_brain(dimensions, mask_self=False)
    noise_dict = sim._noise_dict_update({})
    noise = sim.generate_noise(dimensions=dimensions,
                               stimfunction_tr=stimfunction_tr,
                               tr_duration=tr_duration,
                               template=template,
                               mask=mask,
                               noise_dict=noise_dict,
                               iterations=[0, 0]
                               )
    coords = feature_coordinates[0]
    noise_function_a = noise[coords[0], coords[1], coords[2], :]
    noise_function_a = noise_function_a.reshape(duration // tr_duration, 1)

    noise_function_b = noise[coords[0] + 1, coords[1], coords[2], :]
    noise_function_b = noise_function_b.reshape(duration // tr_duration, 1)

    # Create the calibrated signal with PSC
    method = 'PSC'
    sig_a = sim.compute_signal_change(signal_function,
                                      noise_function_a,
                                      noise_dict,
                                      [0.5],
                                      method,
                                      )
    sig_b = sim.compute_signal_change(signal_function,
                                      noise_function_a,
                                      noise_dict,
                                      [1.0],
                                      method,
                                      )

    assert sig_b.max() / sig_a.max() == 2, 'PSC modulation failed'

    # Create the calibrated signal with SFNR
    method = 'SFNR'
    sig_a = sim.compute_signal_change(signal_function,
                                      noise_function_a,
                                      noise_dict,
                                      [0.5],
                                      method,
                                      )
    scaled_a = sig_a / (noise_function_a.mean() / noise_dict['sfnr'])
    sig_b = sim.compute_signal_change(signal_function,
                                      noise_function_b,
                                      noise_dict,
                                      [1.0],
                                      method,
                                      )
    scaled_b = sig_b / (noise_function_b.mean() / noise_dict['sfnr'])

    assert scaled_b.max() / scaled_a.max() == 2, 'SFNR modulation failed'

    # Create the calibrated signal with CNR_Amp/Noise-SD
    method = 'CNR_Amp/Noise-SD'
    sig_a = sim.compute_signal_change(signal_function,
                                      noise_function_a,
                                      noise_dict,
                                      [0.5],
                                      method,
                                      )
    scaled_a = sig_a / noise_function_a.std()
    sig_b = sim.compute_signal_change(signal_function,
                                      noise_function_b,
                                      noise_dict,
                                      [1.0],
                                      method,
                                      )
    scaled_b = sig_b / noise_function_b.std()

    assert scaled_b.max() / scaled_a.max() == 2, 'CNR_Amp modulation failed'

    # Create the calibrated signal with CNR_Amp/Noise-Var_dB
    method = 'CNR_Amp2/Noise-Var_dB'
    sig_a = sim.compute_signal_change(signal_function,
                                      noise_function_a,
                                      noise_dict,
                                      [0.5],
                                      method,
                                      )
    scaled_a = np.log(sig_a.max() / noise_function_a.std())
    sig_b = sim.compute_signal_change(signal_function,
                                      noise_function_b,
                                      noise_dict,
                                      [1.0],
                                      method,
                                      )
    scaled_b = np.log(sig_b.max() / noise_function_b.std())

    assert np.round(scaled_b / scaled_a) == 2, 'CNR_Amp dB modulation failed'

    # Create the calibrated signal with CNR_Signal-SD/Noise-SD
    method = 'CNR_Signal-SD/Noise-SD'
    sig_a = sim.compute_signal_change(signal_function,
                                      noise_function_a,
                                      noise_dict,
                                      [0.5],
                                      method,
                                      )
    scaled_a = sig_a.std() / noise_function_a.std()
    sig_b = sim.compute_signal_change(signal_function,
                                      noise_function_a,
                                      noise_dict,
                                      [1.0],
                                      method,
                                      )
    scaled_b = sig_b.std() / noise_function_a.std()

    assert (scaled_b / scaled_a) == 2, 'CNR signal modulation failed'

    # Create the calibrated signal with CNR_Amp/Noise-Var_dB
    method = 'CNR_Signal-Var/Noise-Var_dB'
    sig_a = sim.compute_signal_change(signal_function,
                                      noise_function_a,
                                      noise_dict,
                                      [0.5],
                                      method,
                                      )

    scaled_a = np.log(sig_a.std() / noise_function_a.std())
    sig_b = sim.compute_signal_change(signal_function,
                                      noise_function_b,
                                      noise_dict,
                                      [1.0],
                                      method,
                                      )
    scaled_b = np.log(sig_b.std() / noise_function_b.std())

    assert np.round(scaled_b / scaled_a) == 2, 'CNR signal dB modulation ' \
                                               'failed'

    # Convolve the HRF with the stimulus sequence
    signal = sim.apply_signal(signal_function=signal_function,
                              volume_signal=volume,
                              )

    assert signal.shape == (dimensions[0], dimensions[1], dimensions[2],
                            duration / tr_duration), "The output is the " \
                                                     "wrong size"

    signal = sim.apply_signal(signal_function=stimfunction,
                              volume_signal=volume,
                              )

    assert np.any(signal == signal_magnitude), "The stimfunction is not binary"

    # Check that there is an error if the number of signal voxels doesn't
    # match the number of non zero brain voxels
    with pytest.raises(IndexError):
        sig_vox = (volume > 0).sum()
        vox_pattern = np.tile(stimfunction, (1, sig_vox - 1))
        sim.apply_signal(signal_function=vox_pattern,
                         volume_signal=volume,
                         )
Example #24
0
    # Multiply the HRF timecourse with the signal
    signal_cond = sim.apply_signal(signal_function=signal_function,
                                   volume_signal=volume_signal,
                                   )

    # Concatenate all the signal and function files
    if cond == 0:
        stimfunction = stimfunction_cond
        signal = signal_cond
    else:
        stimfunction = list(np.add(stimfunction, stimfunction_cond))
        signal += signal_cond

# Generate the mask of the signal
mask, template = sim.mask_brain(signal)

# Mask the signal to the shape of a brain (does not attenuate signal according
# to grey matter likelihood)
signal *= mask.reshape(dimensions[0], dimensions[1], dimensions[2], 1)

# Downsample the stimulus function to generate it in TR time
stimfunction_tr = stimfunction[::int(tr_duration * 1000)]

# Iterate through the participants and store participants
epochs = []
for participantcounter in range(1, participants + 1):

    # Add the epoch cube
    epochs += [epoch]
Example #25
0
def test_generate_noise():

    dimensions = np.array([10, 10, 10])  # What is the size of the brain
    feature_size = [2]
    feature_type = ['cube']
    feature_coordinates = np.array([[5, 5, 5]])
    signal_magnitude = [1]

    # Generate a volume representing the location and quality of the signal
    volume = sim.generate_signal(
        dimensions=dimensions,
        feature_coordinates=feature_coordinates,
        feature_type=feature_type,
        feature_size=feature_size,
        signal_magnitude=signal_magnitude,
    )

    # Inputs for generate_stimfunction
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 100

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(
        onsets=onsets,
        event_durations=event_durations,
        total_time=duration,
    )

    signal_function = sim.double_gamma_hrf(
        stimfunction=stimfunction,
        tr_duration=tr_duration,
    )

    # Convolve the HRF with the stimulus sequence
    signal = sim.apply_signal(
        signal_function=signal_function,
        volume_static=volume,
    )

    # Generate the mask of the signal
    mask = sim.mask_brain(signal, mask_threshold=0.1)

    assert min(mask[mask > 0]) > 0.1, "Mask thresholding did not work"

    stimfunction_tr = stimfunction[::int(tr_duration * 1000)]
    # Create the noise volumes (using the default parameters)
    noise = sim.generate_noise(
        dimensions=dimensions,
        stimfunction_tr=stimfunction_tr,
        tr_duration=tr_duration,
        mask=mask,
    )

    assert signal.shape == noise.shape, "The dimensions of signal and noise " \
                                        "the same"

    assert np.std(signal) < np.std(noise), "Noise was not created"

    noise = sim.generate_noise(
        dimensions=dimensions,
        stimfunction_tr=stimfunction_tr,
        tr_duration=tr_duration,
        mask=mask,
        noise_dict={
            'temporal_noise': 0,
            'sfnr': 10000
        },
    )

    temporal_noise = np.std(noise[mask[:, :, :, 0] > 0], 1).mean()

    assert temporal_noise <= 0.1, "Noise strength could not be manipulated"
Example #26
0
)

signal_B = sim.apply_signal(
    signal_function=signal_function_B,
    volume_signal=volume_signal_B,
)

# Combine the signals from the two conditions
signal = signal_A + signal_B

# Combine the stim functions
stimfunction = list(np.add(stimfunction_A, stimfunction_B))
stimfunction_tr = stimfunction[::int(tr_duration * temporal_res)]

# Generate the mask of the signal
mask, template = sim.mask_brain(signal, mask_threshold=0.2)

# Mask the signal to the shape of a brain (attenuates signal according to grey
# matter likelihood)
signal *= mask.reshape(dimensions[0], dimensions[1], dimensions[2], 1)

# Generate original noise dict for comparison later
orig_noise_dict = sim._noise_dict_update({})

# Create the noise volumes (using the default parameters
noise = sim.generate_noise(
    dimensions=dimensions,
    stimfunction_tr=stimfunction_tr,
    tr_duration=tr_duration,
    mask=mask,
    template=template,
Example #27
0
)

# Combine the signals from the two conditions
signal = signal_A + signal_B

# Combine the stim functions
stimfunction = list(np.add(stimfunction_A, stimfunction_B))

# Create the noise volumes (using the default parameters
noise = sim.generate_noise(
    dimensions=dimensions,
    stimfunction=stimfunction,
    tr_duration=tr_duration,
)

# Combine the signal and the noise
volume = signal + noise

# Mask the volume to be the same shape as a brain
brain = sim.mask_brain(volume)

# Display the brain
fig = plt.figure()
for tr_counter in list(range(0, brain.shape[3])):

    # Get the axis to be plotted
    ax = sim.plot_brain(fig, brain[:, :, :, tr_counter], percentile=99.9)

    # Wait for an input
    logging.info(tr_counter)
    plt.pause(0.5)
    template_name = parameters_path + 'template/' + participant + '_r' + \
                    str(run_counter) + '.nii.gz'
    noise_dict_name = parameters_path + 'noise_dict/' + participant + '_r' + str(run_counter) + \
                      '.txt'
    nifti_save = simulated_data_path + 'nifti/' + participant + '_r' + str(run_counter)\
                 + effect_name + '.nii.gz'
    signal_func_save = './community_structure/plots/' + participant + '_r' +\
                       str(run_counter) + effect_name + '.eps'

    # Load the template (not yet scaled
    nii = nibabel.load(template_name)
    template = nii.get_data()  # Takes a while

    # Create the mask and rescale the template
    mask, template = sim.mask_brain(template,
                                    mask_self=True,
                                    )

    # Pull out the onsets for this participant (copy it so you don't alter it)
    onsets = copy.deepcopy(onsets_runs[run_counter - 1])

    # What is the original max duration of the onsets
    max_duration_orig = np.max([np.max(onsets[x]) for x in range(onsets.size)])
    max_duration_orig += 10 # Add some wiggle room

    # Do you want to randomise the onsets (so that the events do not have a
    # fixed order)
    if randomise_timing == 1:
        onsets = utils.randomise_timing(onsets,
                                        )
Example #29
0
    volume_static=volume_static_A,
)

signal_B = sim.apply_signal(
    signal_function=signal_function_B,
    volume_static=volume_static_B,
)

# Combine the signals from the two conditions
signal = signal_A + signal_B

# Combine the stim functions
stimfunction = list(np.add(stimfunction_A, stimfunction_B))

# Generate the mask of the signal
mask = sim.mask_brain(signal)

# Mask the signal to the shape of a brain (attenuates signal according to grey
# matter likelihood)
signal *= mask

# Create the noise volumes (using the default parameters
noise = sim.generate_noise(
    dimensions=dimensions,
    stimfunction=stimfunction,
    tr_duration=tr_duration,
    mask=mask,
)

# Combine the signal and the noise
brain = signal + noise
def generate_data(inputDir, outputDir, data_dict):
    # Generate simulated fMRI data with a few parameters that might be
    # relevant for real time analysis
    # inputDir - Specify input data dir where the parameters for fmrisim are
    # outputDir - Specify output data dir where the data should be saved
    # data_dict contains:
    #     numTRs - Specify the number of time points
    #     multivariate_patterns - Is the difference between conditions
    # univariate (0) or multivariate (1)
    #     different_ROIs - Are there different ROIs for each condition (1) or
    #  is it in the same ROI (0). If it is the same ROI and you are using
    # univariate differences, the second condition will have a smaller evoked
    #  response than the other.
    #     event_duration - How long, in seconds, is each event
    #     scale_percentage - What is the percent signal change
    #     trDuration - How many seconds per volume
    #     save_dicom - Do you want to save data as a dicom (1) or numpy (0)
    #     save_realtime - Do you want to save the data in real time (1) or as
    #  fast as possible (0)?
    #     isi - What is the time between each event (in seconds)
    #     burn_in - How long before the first event (in seconds)

    # If the folder doesn't exist then make it
    if os.path.isdir(outputDir) is False:
        os.makedirs(outputDir, exist_ok=True)

    print('Load template of average voxel value')
    templateFile = os.path.join(inputDir, 'sub_template.nii.gz')
    template_nii = nibabel.load(templateFile)
    template = template_nii.get_data()

    dimensions = np.array(template.shape[0:3])

    print('Create binary mask and normalize the template range')
    mask, template = sim.mask_brain(
        volume=template,
        mask_self=True,
    )

    # Write out the mask as a numpy file
    outFile = os.path.join(outputDir, 'mask.npy')
    np.save(outFile, mask.astype(np.uint8))

    # Load the noise dictionary
    print('Loading noise parameters')
    noiseFile = os.path.join(inputDir, 'sub_noise_dict.txt')
    with open(noiseFile, 'r') as f:
        noise_dict = f.read()
    noise_dict = eval(noise_dict)
    noise_dict['matched'] = 0  # Increases processing time

    # Add it here for easy access
    data_dict['noise_dict'] = data_dict

    print('Generating noise')
    temp_stimfunction = np.zeros((data_dict['numTRs'], 1))
    noise = sim.generate_noise(
        dimensions=dimensions,
        stimfunction_tr=temp_stimfunction,
        tr_duration=int(data_dict['trDuration']),
        template=template,
        mask=mask,
        noise_dict=noise_dict,
    )

    # Create the stimulus time course of the conditions
    total_time = int(data_dict['numTRs'] * data_dict['trDuration'])
    onsets_A = []
    onsets_B = []
    curr_time = data_dict['burn_in']
    while curr_time < (total_time - data_dict['event_duration']):

        # Flip a coin for each epoch to determine whether it is A or B
        if np.random.randint(0, 2) == 1:
            onsets_A.append(curr_time)
        else:
            onsets_B.append(curr_time)

        # Increment the current time
        curr_time += data_dict['event_duration'] + data_dict['isi']

    # How many timepoints per second of the stim function are to be generated?
    temporal_res = 1 / data_dict['trDuration']

    # Create a time course of events
    event_durations = [data_dict['event_duration']]
    stimfunc_A = sim.generate_stimfunction(
        onsets=onsets_A,
        event_durations=event_durations,
        total_time=total_time,
        temporal_resolution=temporal_res,
    )

    stimfunc_B = sim.generate_stimfunction(
        onsets=onsets_B,
        event_durations=event_durations,
        total_time=total_time,
        temporal_resolution=temporal_res,
    )

    # Create a labels timecourse
    outFile = os.path.join(outputDir, 'labels.npy')
    np.save(outFile, (stimfunc_A + (stimfunc_B * 2)))

    roiA_file = os.path.join(inputDir, 'ROI_A.nii.gz')
    roiB_file = os.path.join(inputDir, 'ROI_B.nii.gz')

    # How is the signal implemented in the different ROIs
    signal_A = generate_ROIs(roiA_file, stimfunc_A, noise,
                             data_dict['scale_percentage'], data_dict)
    if data_dict['different_ROIs'] is True:

        signal_B = generate_ROIs(roiB_file, stimfunc_B, noise,
                                 data_dict['scale_percentage'], data_dict)

    else:

        # Halve the evoked response if these effects are both expected in the same ROI
        if data_dict['multivariate_pattern'] is False:
            signal_B = generate_ROIs(roiA_file, stimfunc_B, noise,
                                     data_dict['scale_percentage'] * 0.5,
                                     data_dict)
        else:
            signal_B = generate_ROIs(roiA_file, stimfunc_B, noise,
                                     data_dict['scale_percentage'], data_dict)

    # Combine the two signal timecourses
    signal = signal_A + signal_B

    print('Generating TRs in real time')
    for idx in range(data_dict['numTRs']):

        #  Create the brain volume on this TR
        brain = noise[:, :, :, idx] + signal[:, :, :, idx]

        # Convert file to integers to mimic what you get from MR
        brain_int32 = brain.astype(np.int32)

        # Store as dicom or nifti?
        if data_dict['save_dicom'] is True:
            # Save the volume as a DICOM file, with each TR as its own file
            output_file = os.path.join(outputDir,
                                       'rt_' + format(idx, '03d') + '.dcm')
            write_dicom(output_file, brain_int32, idx + 1)
        else:
            # Save the volume as a numpy file, with each TR as its own file
            output_file = os.path.join(outputDir,
                                       'rt_' + format(idx, '03d') + '.npy')
            np.save(output_file, brain_int32)

        print("Generate {}".format(output_file))

        # Sleep until next TR
        if data_dict['save_realtime'] == 1:
            time.sleep(data_dict['trDuration'])
Example #31
0
    output_noise_dict_name = None
if output_name == 'None' or output_name == '':
    output_name = None

# How many TRs are there?
nii = nibabel.load(input_name)
dimsize = nii.header.get_zooms()
tr_duration = dimsize[3]
trs = nii.shape[3]
real_brain = nii.get_data()

dimensions = np.array(real_brain.shape[0:3])  # What is the size of the brain

# Generate the continuous mask from the voxels
mask, template = sim.mask_brain(
    volume=real_brain,
    mask_self=True,
)

# Save the mask brain
nii = nibabel.Nifti1Image(mask.astype('int16'), nii.affine)
hdr = nii.header
hdr.set_zooms((dimsize[0], dimsize[1], dimsize[2]))

# Save the mask if the name is given
if output_mask_name is not None:
    print('Saving ' + output_mask_name)
    nibabel.save(nii, output_mask_name)

# Calculate the noise parameters
if input_noise_dict_name is None or path.exists(input_noise_dict_name) == \
        False:
Example #32
0
def test_generate_noise():

    dimensions = np.array([10, 10, 10])  # What is the size of the brain
    feature_size = [2]
    feature_type = ['cube']
    feature_coordinates = np.array([[5, 5, 5]])
    signal_magnitude = [1]

    # Generate a volume representing the location and quality of the signal
    volume = sim.generate_signal(
        dimensions=dimensions,
        feature_coordinates=feature_coordinates,
        feature_type=feature_type,
        feature_size=feature_size,
        signal_magnitude=signal_magnitude,
    )

    # Inputs for generate_stimfunction
    onsets = [10, 30, 50, 70, 90]
    event_durations = [6]
    tr_duration = 2
    duration = 200

    # Create the time course for the signal to be generated
    stimfunction = sim.generate_stimfunction(
        onsets=onsets,
        event_durations=event_durations,
        total_time=duration,
    )

    signal_function = sim.convolve_hrf(
        stimfunction=stimfunction,
        tr_duration=tr_duration,
    )

    # Convolve the HRF with the stimulus sequence
    signal = sim.apply_signal(
        signal_function=signal_function,
        volume_signal=volume,
    )

    # Generate the mask of the signal
    mask, template = sim.mask_brain(signal, mask_self=None)

    assert min(mask[mask > 0]) > 0.1, "Mask thresholding did not work"
    assert len(np.unique(template) > 2), "Template creation did not work"

    stimfunction_tr = stimfunction[::int(tr_duration * 100)]

    # Create the noise volumes (using the default parameters)
    noise = sim.generate_noise(
        dimensions=dimensions,
        stimfunction_tr=stimfunction_tr,
        tr_duration=tr_duration,
        template=template,
        mask=mask,
        iterations=[1, 0],
    )

    assert signal.shape == noise.shape, "The dimensions of signal and noise " \
                                        "the same"

    noise_high = sim.generate_noise(
        dimensions=dimensions,
        stimfunction_tr=stimfunction_tr,
        tr_duration=tr_duration,
        template=template,
        mask=mask,
        noise_dict={
            'sfnr': 50,
            'snr': 25
        },
        iterations=[1, 0],
    )

    noise_low = sim.generate_noise(
        dimensions=dimensions,
        stimfunction_tr=stimfunction_tr,
        tr_duration=tr_duration,
        template=template,
        mask=mask,
        noise_dict={
            'sfnr': 100,
            'snr': 25
        },
        iterations=[1, 0],
    )

    system_high = np.std(noise_high[mask > 0], 1).mean()
    system_low = np.std(noise_low[mask > 0], 1).mean()

    assert system_low < system_high, "SFNR noise could not be manipulated"

    # Check that you check for the appropriate template values
    with pytest.raises(ValueError):
        sim.generate_noise(
            dimensions=dimensions,
            stimfunction_tr=stimfunction_tr,
            tr_duration=tr_duration,
            template=template * 2,
            mask=mask,
            noise_dict={},
        )

    # Check that iterations does what it should
    sim.generate_noise(
        dimensions=dimensions,
        stimfunction_tr=stimfunction_tr,
        tr_duration=tr_duration,
        template=template,
        mask=mask,
        noise_dict={},
        iterations=[0, 0],
    )

    sim.generate_noise(
        dimensions=dimensions,
        stimfunction_tr=stimfunction_tr,
        tr_duration=tr_duration,
        template=template,
        mask=mask,
        noise_dict={},
        iterations=None,
    )

    # Test drift noise
    trs = 1000
    period = 100
    drift = sim._generate_noise_temporal_drift(
        trs,
        tr_duration,
        'sine',
        period,
    )

    # Check that the max frequency is the appropriate frequency
    power = abs(np.fft.fft(drift))[1:trs // 2]
    freq = np.linspace(1, trs // 2 - 1, trs // 2 - 1) / trs
    period_freq = np.where(freq == 1 / (period // tr_duration))
    max_freq = np.argmax(power)

    assert period_freq == max_freq, 'Max frequency is not where it should be'

    # Do the same but now with cosine basis functions, answer should be close
    drift = sim._generate_noise_temporal_drift(
        trs,
        tr_duration,
        'discrete_cos',
        period,
    )

    # Check that the appropriate frequency is peaky (may not be the max)
    power = abs(np.fft.fft(drift))[1:trs // 2]
    freq = np.linspace(1, trs // 2 - 1, trs // 2 - 1) / trs
    period_freq = np.where(freq == 1 / (period // tr_duration))[0][0]

    assert power[period_freq] > power[period_freq + 1], 'Power is low'
    assert power[period_freq] > power[period_freq - 1], 'Power is low'

    # Check it runs fine
    drift = sim._generate_noise_temporal_drift(
        50,
        tr_duration,
        'discrete_cos',
        period,
    )

    # Check it runs fine
    drift = sim._generate_noise_temporal_drift(
        300,
        tr_duration,
        'cos_power_drop',
        period,
    )

    # Check that when the TR is greater than the period it errors
    with pytest.raises(ValueError):
        sim._generate_noise_temporal_drift(30, 10, 'cos_power_drop', 5)

    # Test physiological noise (using unrealistic parameters so that it's easy)
    timepoints = list(np.linspace(0, (trs - 1) * tr_duration, trs))
    resp_freq = 0.2
    heart_freq = 1.17
    phys = sim._generate_noise_temporal_phys(
        timepoints,
        resp_freq,
        heart_freq,
    )

    # Check that the max frequency is the appropriate frequency
    power = abs(np.fft.fft(phys))[1:trs // 2]
    freq = np.linspace(1, trs // 2 - 1, trs // 2 - 1) / (trs * tr_duration)
    peaks = (power > (power.mean() + power.std()))  # Where are the peaks
    peak_freqs = freq[peaks]

    assert np.any(resp_freq == peak_freqs), 'Resp frequency not found'
    assert len(peak_freqs) == 2, 'Two peaks not found'

    # Test task noise
    sim._generate_noise_temporal_task(
        stimfunction_tr,
        motion_noise='gaussian',
    )
    sim._generate_noise_temporal_task(
        stimfunction_tr,
        motion_noise='rician',
    )

    # Test ARMA noise
    with pytest.raises(ValueError):
        noise_dict = {'fwhm': 4, 'auto_reg_rho': [1], 'ma_rho': [1, 1]}
        sim._generate_noise_temporal_autoregression(
            stimfunction_tr,
            noise_dict,
            dimensions,
            mask,
        )

    # Generate spatial noise
    vol = sim._generate_noise_spatial(np.array([10, 10, 10, trs]))
    assert len(vol.shape) == 3, 'Volume was not reshaped to ignore TRs'

    # Switch some of the noise types on
    noise_dict = dict(physiological_sigma=1,
                      drift_sigma=1,
                      task_sigma=1,
                      auto_reg_sigma=0)
    sim.generate_noise(
        dimensions=dimensions,
        stimfunction_tr=stimfunction_tr,
        tr_duration=tr_duration,
        template=template,
        mask=mask,
        noise_dict=noise_dict,
        iterations=[0, 0],
    )
Example #33
0
)

signal_B = sim.apply_signal(
    signal_function=signal_function_B,
    volume_static=volume_static_B,
)

# Combine the signals from the two conditions
signal = signal_A + signal_B

# Combine the stim functions
stimfunction = list(np.add(stimfunction_A, stimfunction_B))
stimfunction_tr = stimfunction[::int(tr_duration * temporal_res)]

# Generate the mask of the signal
mask = sim.mask_brain(signal)

# Mask the signal to the shape of a brain (attenuates signal according to grey
# matter likelihood)
signal *= mask

# Generate original noise dict for comparison later
orig_noise_dict = sim._noise_dict_update({})

# Create the noise volumes (using the default parameters
noise = sim.generate_noise(
    dimensions=dimensions,
    stimfunction_tr=stimfunction_tr,
    tr_duration=tr_duration,
    mask=mask,
    noise_dict=orig_noise_dict,