def test_generate_stimfunction(): # 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, ) assert len(stimfunction) == duration * 1000, "stimfunction incorrect " \ "length" eventNumber = np.sum(event_durations * len(onsets)) * 1000 assert np.sum(stimfunction) == eventNumber, "Event number" # Create the signal function signal_function = sim.double_gamma_hrf( stimfunction=stimfunction, tr_duration=tr_duration, ) assert len(signal_function) == len(stimfunction) / (tr_duration * 1000), \ "The length did not change" onsets = [10] 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, ) assert np.sum(signal_function < 0) > 0, "No values below zero"
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.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, ) 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_static=volume, ) assert np.any(signal == signal_magnitude), "The stimfunction is not binary"
def test_generate_stimfunction(): # 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, tr_duration=tr_duration, ) assert len(stimfunction) == duration / tr_duration, "stimfunction incorrect " \ "length" assert np.sum(stimfunction) == np.sum(event_durations * len(onsets)) / \ tr_duration, "Event number" # Create the signal function signal_function = sim.double_gamma_hrf(stimfunction=stimfunction, ) assert len(signal_function) == len(stimfunction), "The length did not change" onsets = [10] stimfunction = sim.generate_stimfunction(onsets=onsets, event_durations=event_durations, total_time=duration, tr_duration=tr_duration, ) signal_function = sim.double_gamma_hrf(stimfunction=stimfunction, ) assert np.sum(signal_function < 0) > 0, "No values below zero"
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.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, ) 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_static=volume, ) assert np.any(signal == signal_magnitude), "The stimfunction is not binary"
def test_generate_noise(): dimensions = np.array([64, 64, 36]) # What is the size of the brain feature_size = [2] feature_type = ['cube'] feature_coordinates = np.array([[32, 32, 18]]) signal_magnitude = [30] # Generate a volume representing the location and quality of the signal volume_static = 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, tr_duration=tr_duration, ) signal_function = sim.double_gamma_hrf(stimfunction=stimfunction, ) # Convolve the HRF with the stimulus sequence signal = sim.apply_signal( signal_function=signal_function, volume_static=volume_static, ) # Create the noise volumes (using the default parameters) noise = sim.generate_noise( dimensions=dimensions, stimfunction=stimfunction, tr_duration=tr_duration, ) assert signal.shape == noise.shape, "The dimensions of signal " \ "and noise the same" Z_noise = sim._generate_noise_temporal(stimfunction, tr_duration, 1) noise = sim._generate_noise_temporal(stimfunction, tr_duration, 0) assert np.std(Z_noise) < np.std(noise), "Z scoring is not working" # Combine the signal and the noise volume = signal + noise assert np.std(signal) < np.std(noise), "Noise was not created" noise = sim.generate_noise(dimensions=dimensions, stimfunction=stimfunction, tr_duration=tr_duration, noise_strength=[0, 0, 0]) assert np.sum(noise) == 0, "Noise strength could not be manipulated" assert np.std(noise) == 0, "Noise strength could not be manipulated"
plt.show() # Create the time course for the signal to be generated stimfunction_A = sim.generate_stimfunction(onsets=onsets_A, event_durations=event_durations, total_time=duration, ) stimfunction_B = sim.generate_stimfunction(onsets=onsets_B, event_durations=event_durations, total_time=duration, ) # Convolve the HRF with the stimulus sequence signal_function_A = sim.double_gamma_hrf(stimfunction=stimfunction_A, tr_duration=tr_duration, ) signal_function_B = sim.double_gamma_hrf(stimfunction=stimfunction_B, tr_duration=tr_duration, ) # 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, )
feature_type=feature_type, feature_size=feature_size, signal_magnitude=signal_magnitude, ) # Create the time course for the signal to be generated stimfunction_cond = sim.generate_stimfunction(onsets=onsets[cond], event_durations= event_durations, total_time=duration, weights=weights[cond], ) # Convolve the HRF with the stimulus sequence signal_function = sim.double_gamma_hrf(stimfunction=stimfunction_cond, tr_duration=tr_duration, ) # 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
def test_generate_noise(): dimensions = np.array([64, 64, 36]) # What is the size of the brain feature_size = [2] feature_type = ['cube'] feature_coordinates = np.array( [[32, 32, 18]]) signal_magnitude = [30] # Generate a volume representing the location and quality of the signal volume_static = 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, tr_duration=tr_duration, ) signal_function = sim.double_gamma_hrf(stimfunction=stimfunction, ) # Convolve the HRF with the stimulus sequence signal = sim.apply_signal(signal_function=signal_function, volume_static=volume_static, ) # Create the noise volumes (using the default parameters) noise = sim.generate_noise(dimensions=dimensions, stimfunction=stimfunction, tr_duration=tr_duration, ) assert signal.shape == noise.shape, "The dimensions of signal " \ "and noise the same" Z_noise = sim._generate_noise_temporal(stimfunction, tr_duration, 1) noise = sim._generate_noise_temporal(stimfunction, tr_duration, 0) assert np.std(Z_noise) < np.std(noise), "Z scoring is not working" # Combine the signal and the noise volume = signal + noise assert np.std(signal) < np.std(noise), "Noise was not created" noise = sim.generate_noise(dimensions=dimensions, stimfunction=stimfunction, tr_duration=tr_duration, noise_strength=[0, 0, 0] ) assert np.sum(noise) == 0, "Noise strength could not be manipulated" assert np.std(noise) == 0, "Noise strength could not be manipulated"
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"
event_durations=event_durations, total_time=duration, temporal_resolution=temporal_res, ) stimfunction_B = sim.generate_stimfunction( onsets=onsets_B, event_durations=event_durations, total_time=duration, temporal_resolution=temporal_res, ) # Convolve the HRF with the stimulus sequence signal_function_A = sim.double_gamma_hrf( stimfunction=stimfunction_A, tr_duration=tr_duration, temporal_resolution=temporal_res, ) signal_function_B = sim.double_gamma_hrf( stimfunction=stimfunction_B, tr_duration=tr_duration, temporal_resolution=temporal_res, ) # Multiply the HRF timecourse with the signal signal_A = sim.apply_signal( signal_function=signal_function_A, volume_static=volume_static_A, )
# Visualize the signal that was generated for condition A fig = plt.figure() sim.plot_brain(fig, volume_static_A) plt.show() # Create the time course for the signal to be generated stimfunction_A = sim.generate_stimfunction( onsets=onsets_A, event_durations=event_durations, total_time=duration, tr_duration=tr_duration ) stimfunction_B = sim.generate_stimfunction( onsets=onsets_B, event_durations=event_durations, total_time=duration, tr_duration=tr_duration ) # Convolve the HRF with the stimulus sequence signal_function_A = sim.double_gamma_hrf(stimfunction=stimfunction_A) signal_function_B = sim.double_gamma_hrf(stimfunction=stimfunction_B) # 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
stimfunction_A = sim.generate_stimfunction( onsets=onsets_A, event_durations=event_durations, total_time=duration, tr_duration=tr_duration, ) stimfunction_B = sim.generate_stimfunction( onsets=onsets_B, event_durations=event_durations, total_time=duration, tr_duration=tr_duration, ) # Convolve the HRF with the stimulus sequence signal_function_A = sim.double_gamma_hrf(stimfunction=stimfunction_A, ) signal_function_B = sim.double_gamma_hrf(stimfunction=stimfunction_B, ) # 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
# Create the time course for the signal to be generated stimfunction_A = sim.generate_stimfunction( onsets=onsets_A, event_durations=event_durations, total_time=duration, ) stimfunction_B = sim.generate_stimfunction( onsets=onsets_B, event_durations=event_durations, total_time=duration, ) # Convolve the HRF with the stimulus sequence signal_function_A = sim.double_gamma_hrf( stimfunction=stimfunction_A, tr_duration=tr_duration, ) signal_function_B = sim.double_gamma_hrf( stimfunction=stimfunction_B, tr_duration=tr_duration, ) # 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,
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"