Exemple #1
0
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 stimfunction.shape[0] == duration * 100, "stimfunc incorrect length"
    eventNumber = np.sum(event_durations * len(onsets)) * 100
    assert np.sum(stimfunction) == eventNumber, "Event number"

    # Create the signal function
    signal_function = sim.convolve_hrf(
        stimfunction=stimfunction,
        tr_duration=tr_duration,
    )

    stim_dur = stimfunction.shape[0] / (tr_duration * 100)
    assert signal_function.shape[0] == stim_dur, "The length did not change"

    # Test
    onsets = [0]
    tr_duration = 1
    event_durations = [1]
    stimfunction = sim.generate_stimfunction(
        onsets=onsets,
        event_durations=event_durations,
        total_time=duration,
    )

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

    max_response = np.where(signal_function != 0)[0].max()
    assert 25 < max_response <= 30, "HRF has the incorrect length"
    assert np.sum(signal_function < 0) > 0, "No values below zero"

    # Export a stimfunction
    sim.export_3_column(
        stimfunction,
        'temp.txt',
    )

    # Load in the stimfunction
    stimfunc_new = sim.generate_stimfunction(
        onsets=None,
        event_durations=None,
        total_time=duration,
        timing_file='temp.txt',
    )

    assert np.all(stimfunc_new == stimfunction), "Export/import failed"

    # Break the timing precision of the generation
    stimfunc_new = sim.generate_stimfunction(
        onsets=None,
        event_durations=None,
        total_time=duration,
        timing_file='temp.txt',
        temporal_resolution=0.5,
    )

    assert stimfunc_new.sum() == 0, "Temporal resolution not working right"

    # Set the duration to be too short so you should get an error
    onsets = [10, 30, 50, 70, 90]
    event_durations = [5]
    with pytest.raises(ValueError):
        sim.generate_stimfunction(
            onsets=onsets,
            event_durations=event_durations,
            total_time=89,
        )

    # Clip the event offset
    stimfunc_new = sim.generate_stimfunction(
        onsets=onsets,
        event_durations=event_durations,
        total_time=95,
    )
    assert stimfunc_new[-1] == 1, 'Event offset was not clipped'

    # Test exporting a group of participants to an epoch file
    cond_a = sim.generate_stimfunction(
        onsets=onsets,
        event_durations=event_durations,
        total_time=110,
    )

    cond_b = sim.generate_stimfunction(
        onsets=[x + 5 for x in onsets],
        event_durations=event_durations,
        total_time=110,
    )

    stimfunction_group = [np.hstack((cond_a, cond_b))] * 2
    sim.export_epoch_file(
        stimfunction_group,
        'temp.txt',
        tr_duration,
    )

    # Check that convolve throws a warning when the shape is wrong
    sim.convolve_hrf(
        stimfunction=np.hstack((cond_a, cond_b)).T,
        tr_duration=tr_duration,
        temporal_resolution=1,
    )
Exemple #2
0
    signal_B = sim.apply_signal(signal_func_B, ROI_B)

    # Combine the two signal timecourses
    signal = signal_A + signal_B

    # spare the true noise.
    #print('Generating noise')
    #noise = sim.generate_noise(dimensions=dimensions,
    #                           stimfunction_tr=np.zeros((numTRs, 1)),
    #                           tr_duration=int(trDuration),
    #                           template=template,
    #                           mask=mask_cherry,
    #                           noise_dict=noise_dict,
    #                           temporal_proportion = 0.5)

    brain = signal  #+ noise
    brain_nii = image.new_img_like(template_nii, brain, template_nii.affine)
    out_path = os.path.join(dat_dir, '../simulated_data')
    if os.path.isdir(out_path) is False:
        os.makedirs(out_path, exist_ok=True)
    brain_nii.to_filename(os.path.join(out_path, f"sub_{sid}_sim_dat.nii.gz"))

# write out the simulated epoch file
sim.export_epoch_file(
    stimfunction=stimfunc_all,
    filename=os.path.join(dat_dir, '../simulated_data/sim_epoch_file.npy'),
    tr_duration=1.0,
    temporal_resolution=1.0,
)
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 stimfunction.shape[0] == duration * 100, "stimfunc incorrect length"
    eventNumber = np.sum(event_durations * len(onsets)) * 100
    assert np.sum(stimfunction) == eventNumber, "Event number"

    # Create the signal function
    signal_function = sim.convolve_hrf(stimfunction=stimfunction,
                                       tr_duration=tr_duration,
                                       )

    stim_dur = stimfunction.shape[0] / (tr_duration * 100)
    assert signal_function.shape[0] == stim_dur, "The length did not change"

    # Test
    onsets = [0]
    tr_duration = 1
    event_durations = [1]
    stimfunction = sim.generate_stimfunction(onsets=onsets,
                                             event_durations=event_durations,
                                             total_time=duration,
                                             )

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

    max_response = np.where(signal_function != 0)[0].max()
    assert 25 < max_response <= 30, "HRF has the incorrect length"
    assert np.sum(signal_function < 0) > 0, "No values below zero"

    # Export a stimfunction
    sim.export_3_column(stimfunction,
                        'temp.txt',
                        )

    # Load in the stimfunction
    stimfunc_new = sim.generate_stimfunction(onsets=None,
                                             event_durations=None,
                                             total_time=duration,
                                             timing_file='temp.txt',
                                             )

    assert np.all(stimfunc_new == stimfunction), "Export/import failed"

    # Break the timing precision of the generation
    stimfunc_new = sim.generate_stimfunction(onsets=None,
                                             event_durations=None,
                                             total_time=duration,
                                             timing_file='temp.txt',
                                             temporal_resolution=0.5,
                                             )

    assert stimfunc_new.sum() == 0, "Temporal resolution not working right"

    # Set the duration to be too short so you should get an error
    onsets = [10, 30, 50, 70, 90]
    event_durations = [5]
    with pytest.raises(ValueError):
        sim.generate_stimfunction(onsets=onsets,
                                  event_durations=event_durations,
                                  total_time=89,
                                  )

    # Clip the event offset
    stimfunc_new = sim.generate_stimfunction(onsets=onsets,
                                             event_durations=event_durations,
                                             total_time=95,
                                             )
    assert stimfunc_new[-1] == 1, 'Event offset was not clipped'

    # Test exporting a group of participants to an epoch file
    cond_a = sim.generate_stimfunction(onsets=onsets,
                                       event_durations=event_durations,
                                       total_time=110,
                                       )

    cond_b = sim.generate_stimfunction(onsets=[x + 5 for x in onsets],
                                       event_durations=event_durations,
                                       total_time=110,
                                       )

    stimfunction_group = [np.hstack((cond_a, cond_b))] * 2
    sim.export_epoch_file(stimfunction_group,
                          'temp.txt',
                          tr_duration,
                          )

    # Check that convolve throws a warning when the shape is wrong
    sim.convolve_hrf(stimfunction=np.hstack((cond_a, cond_b)).T,
                     tr_duration=tr_duration,
                     temporal_resolution=1,
                     )