def test_run_plot_GLM_topo(): raw_intensity = _load_dataset() raw_intensity.crop(450, 600) # Keep the test fast design_matrix = make_first_level_design_matrix(raw_intensity, drift_order=1, drift_model='polynomial') raw_od = mne.preprocessing.nirs.optical_density(raw_intensity) raw_haemo = mne.preprocessing.nirs.beer_lambert_law(raw_od, ppf=0.1) glm_estimates = run_glm(raw_haemo, design_matrix) fig = plot_glm_topo(raw_haemo, glm_estimates.data, design_matrix) # 5 conditions (A,B,C,Drift,Constant) * two chroma + 2xcolorbar assert len(fig.axes) == 12 # Two conditions * two chroma + 2 x colorbar fig = plot_glm_topo(raw_haemo, glm_estimates.data, design_matrix, requested_conditions=['A', 'B']) assert len(fig.axes) == 6 # Two conditions * one chroma + 1 x colorbar with pytest.warns(RuntimeWarning, match='Reducing GLM results'): fig = plot_glm_topo(raw_haemo.copy().pick(picks="hbo"), glm_estimates.data, design_matrix, requested_conditions=['A', 'B']) assert len(fig.axes) == 3 # One conditions * two chroma + 2 x colorbar fig = plot_glm_topo(raw_haemo, glm_estimates.data, design_matrix, requested_conditions=['A']) assert len(fig.axes) == 4 # One conditions * one chroma + 1 x colorbar with pytest.warns(RuntimeWarning, match='Reducing GLM results'): fig = plot_glm_topo(raw_haemo.copy().pick(picks="hbo"), glm_estimates.data, design_matrix, requested_conditions=['A']) assert len(fig.axes) == 2 # One conditions * one chroma + 0 x colorbar with pytest.warns(RuntimeWarning, match='Reducing GLM results'): fig = plot_glm_topo(raw_haemo.copy().pick(picks="hbo"), glm_estimates.data, design_matrix, colorbar=False, requested_conditions=['A']) assert len(fig.axes) == 1 # Ensure warning thrown if glm estimates is missing channels from raw glm_estimates_subset = { a: glm_estimates.data[a] for a in raw_haemo.ch_names[0:3] } with pytest.raises(RuntimeError, match="does not match regression"): plot_glm_topo(raw_haemo, glm_estimates_subset, design_matrix)
def individual_analysis(bids_path): raw_intensity = read_raw_bids(bids_path=bids_path, verbose=False) raw_intensity.pick(picks=range(20)).crop(200).resample(0.3) # Reduce load raw_haemo = beer_lambert_law(optical_density(raw_intensity), ppf=0.1) design_matrix = make_first_level_design_matrix(raw_haemo) glm_est = run_glm(raw_haemo, design_matrix) return glm_est
def test_run_GLM_order(): raw = simulate_nirs_raw(sig_dur=200, stim_dur=5., sfreq=3) design_matrix = make_first_level_design_matrix(raw, stim_dur=5., drift_order=1, drift_model='polynomial') # Default should be first order AR glm_estimates = run_glm(raw, design_matrix) assert glm_estimates.pick("Simulated").model()[0].order == 1 # Default should be first order AR glm_estimates = run_glm(raw, design_matrix, noise_model='ar2') assert glm_estimates.pick("Simulated").model()[0].order == 2 glm_estimates = run_glm(raw, design_matrix, noise_model='ar7') assert glm_estimates.pick("Simulated").model()[0].order == 7 # Auto should be 4 times sample rate cov = Covariance(np.ones(1) * 1e-11, raw.ch_names, raw.info['bads'], raw.info['projs'], nfree=0) raw = add_noise(raw, cov, iir_filter=iir_filter) glm_estimates = run_glm(raw, design_matrix, noise_model='auto') assert glm_estimates.pick("Simulated").model()[0].order == 3 * 4 raw = simulate_nirs_raw(sig_dur=10, stim_dur=5., sfreq=2) cov = Covariance(np.ones(1) * 1e-11, raw.ch_names, raw.info['bads'], raw.info['projs'], nfree=0) raw = add_noise(raw, cov, iir_filter=iir_filter) design_matrix = make_first_level_design_matrix(raw, stim_dur=5., drift_order=1, drift_model='polynomial') # Auto should be 4 times sample rate glm_estimates = run_glm(raw, design_matrix, noise_model='auto') assert glm_estimates.pick("Simulated").model()[0].order == 2 * 4
def individual_analysis(bids_path): raw_intensity = read_raw_bids(bids_path=bids_path, verbose=False) # Delete annotation labeled 15, as these just signify the start and end of experiment. raw_intensity.annotations.delete( raw_intensity.annotations.description == '15.0') raw_intensity.pick(picks=range(20)).crop(200).resample(0.3) # Reduce load raw_haemo = beer_lambert_law(optical_density(raw_intensity), ppf=0.1) design_matrix = make_first_level_design_matrix(raw_haemo) glm_est = run_glm(raw_haemo, design_matrix) return glm_est
def _get_glm_contrast_result(tmin=60, tmax=400): raw = _get_minimal_haemo_data(tmin=tmin, tmax=tmax) design_matrix = make_first_level_design_matrix(raw, stim_dur=5., drift_order=1, drift_model='polynomial') glm_est = run_glm(raw, design_matrix) contrast_matrix = np.eye(design_matrix.shape[1]) basic_conts = dict([(column, contrast_matrix[i]) for i, column in enumerate(design_matrix.columns)]) assert 'e1p' in basic_conts, sorted(basic_conts) contrast_LvR = basic_conts['e1p'] - basic_conts['e2p'] return glm_est.compute_contrast(contrast_LvR)
def analysis(fname, ID): raw_intensity = read_raw_bids(bids_path=fname, verbose=False) # Delete annotation labeled 15, as these just signify the start and end of experiment. raw_intensity.annotations.delete( raw_intensity.annotations.description == '15.0') # sanitize event names raw_intensity.annotations.description[:] = [ d.replace('/', '_') for d in raw_intensity.annotations.description ] # Convert signal to haemoglobin and just keep hbo raw_od = optical_density(raw_intensity) raw_haemo = beer_lambert_law(raw_od, ppf=0.1) raw_haemo.resample(0.5, npad="auto") # Cut out just the short channels for creating a GLM regressor short_chans = get_short_channels(raw_haemo) raw_haemo = get_long_channels(raw_haemo) # Create a design matrix design_matrix = make_first_level_design_matrix(raw_haemo, hrf_model='fir', stim_dur=1.0, fir_delays=range(10), drift_model='cosine', high_pass=0.01, oversampling=1) # Add short channels as regressor in GLM for chan in range(len(short_chans.ch_names)): design_matrix[f"short_{chan}"] = short_chans.get_data(chan).T # Run GLM glm_est = run_glm(raw_haemo, design_matrix) # Create a single ROI that includes all channels for example rois = dict(AllChannels=range(len(raw_haemo.ch_names))) # Calculate ROI for all conditions conditions = design_matrix.columns # Compute output metrics by ROI df_ind = glm_est.to_dataframe_region_of_interest(rois, conditions) df_ind["ID"] = ID df_ind["theta"] = [t * 1.e6 for t in df_ind["theta"]] return df_ind, raw_haemo, design_matrix
def individual_analysis(bids_path, ID): raw_intensity = read_raw_bids(bids_path=bids_path, verbose=False) raw_intensity.annotations.delete( raw_intensity.annotations.description == '15.0') # sanitize event names raw_intensity.annotations.description[:] = [ d.replace('/', '_') for d in raw_intensity.annotations.description ] # Convert signal to haemoglobin and resample raw_od = optical_density(raw_intensity) raw_haemo = beer_lambert_law(raw_od, ppf=0.1) raw_haemo.resample(0.3) # Cut out just the short channels for creating a GLM repressor sht_chans = get_short_channels(raw_haemo) raw_haemo = get_long_channels(raw_haemo) # Create a design matrix design_matrix = make_first_level_design_matrix(raw_haemo, stim_dur=5.0) # Append short channels mean to design matrix design_matrix["ShortHbO"] = np.mean( sht_chans.copy().pick(picks="hbo").get_data(), axis=0) design_matrix["ShortHbR"] = np.mean( sht_chans.copy().pick(picks="hbr").get_data(), axis=0) # Run GLM glm_est = run_glm(raw_haemo, design_matrix) # Extract channel metrics cha = glm_est.to_dataframe() # Add the participant ID to the dataframes cha["ID"] = ID # Convert to uM for nicer plotting below. cha["theta"] = [t * 1.e6 for t in cha["theta"]] return raw_haemo, cha
def test_run_plot_GLM_contrast_topo(): raw_intensity = _load_dataset() raw_intensity.crop(450, 600) # Keep the test fast design_matrix = make_first_level_design_matrix(raw_intensity, drift_order=1, drift_model='polynomial') raw_od = mne.preprocessing.nirs.optical_density(raw_intensity) raw_haemo = mne.preprocessing.nirs.beer_lambert_law(raw_od, ppf=0.1) glm_est = run_glm(raw_haemo, design_matrix) contrast_matrix = np.eye(design_matrix.shape[1]) basic_conts = dict([(column, contrast_matrix[i]) for i, column in enumerate(design_matrix.columns)]) contrast_LvR = basic_conts['A'] - basic_conts['B'] with pytest.deprecated_call(match='comprehensive GLM'): contrast = mne_nirs.statistics.compute_contrast( glm_est.data, contrast_LvR) with pytest.deprecated_call(match='comprehensive GLM'): fig = mne_nirs.visualisation.plot_glm_contrast_topo( raw_haemo, contrast) assert len(fig.axes) == 3
def test_statsmodel_to_df(func): func = getattr(smf, func) np.random.seed(0) amplitude = 1.432 df_cha = pd.DataFrame() for n in range(5): raw = simulate_nirs_raw(sfreq=3., amplitude=amplitude, sig_dur=300., stim_dur=5., isi_min=15., isi_max=45.) raw._data += np.random.normal(0, np.sqrt(1e-12), raw._data.shape) design_matrix = make_first_level_design_matrix(raw, stim_dur=5.0) glm_est = run_glm(raw, design_matrix) with pytest.warns(RuntimeWarning, match='Non standard source detect'): cha = glm_est.to_dataframe() cha["ID"] = '%02d' % n df_cha = pd.concat([df_cha, cha], ignore_index=True) df_cha["theta"] = df_cha["theta"] * 1.0e6 roi_model = func("theta ~ -1 + Condition", df_cha, groups=df_cha["ID"]).fit() df = statsmodels_to_results(roi_model) assert type(df) == pd.DataFrame assert_allclose(df["Coef."]["Condition[A]"], amplitude, rtol=0.1) assert df["Significant"]["Condition[A]"] assert df.shape == (8, 8) roi_model = smf.rlm("theta ~ -1 + Condition", df_cha, groups=df_cha["ID"]).fit() df = statsmodels_to_results(roi_model) assert type(df) == pd.DataFrame assert_allclose(df["Coef."]["Condition[A]"], amplitude, rtol=0.1) assert df["Significant"]["Condition[A]"] assert df.shape == (8, 8)
def test_run_GLM(): raw = simulate_nirs_raw(sig_dur=200, stim_dur=5.) design_matrix = make_first_level_design_matrix(raw, stim_dur=5., drift_order=1, drift_model='polynomial') glm_estimates = run_glm(raw, design_matrix) # Test backwards compatibility with pytest.deprecated_call(match='more comprehensive'): old_res = run_GLM(raw, design_matrix) assert old_res.keys() == glm_estimates.data.keys() assert (old_res["Simulated"].theta == glm_estimates.data["Simulated"].theta ).all() assert len(glm_estimates) == len(raw.ch_names) # Check the estimate is correct within 10% error assert abs(glm_estimates.pick("Simulated").theta()[0][0] - 1.e-6) < 0.1e-6 # ensure we return the same type as nilearn to encourage compatibility _, ni_est = nilearn.glm.first_level.run_glm( raw.get_data(0).T, design_matrix.values) assert isinstance(glm_estimates._data, type(ni_est))
def test_run_plot_GLM_projection(requires_pyvista): raw_intensity = _load_dataset() raw_intensity.crop(450, 600) # Keep the test fast design_matrix = make_first_level_design_matrix(raw_intensity, drift_order=1, drift_model='polynomial') raw_od = mne.preprocessing.nirs.optical_density(raw_intensity) raw_haemo = mne.preprocessing.nirs.beer_lambert_law(raw_od, ppf=0.1) glm_estimates = run_glm(raw_haemo, design_matrix) df = glm_to_tidy(raw_haemo, glm_estimates.data, design_matrix) df = df.query("Chroma in 'hbo'") df = df.query("Condition in 'A'") brain = plot_glm_surface_projection(raw_haemo.copy().pick("hbo"), df, clim='auto', view='dorsal', colorbar=True, size=(800, 700), value="theta", surface='white', subjects_dir=subjects_dir) assert type(brain) == mne.viz._brain.Brain
# This can be done by manually specifying the weights used in the region of # interest function call. # The details of the GLM analysis will not be described here, instead view the # :ref:`fNIRS GLM tutorial <tut-fnirs-hrf>`. Instead, comments are provided # for the weighted region of interest function call. # Basic pipeline, simplified for example raw_od = optical_density(raw) raw_haemo = beer_lambert_law(raw_od) raw_haemo.resample(0.3).pick("hbo") # Speed increase for web server sht_chans = get_short_channels(raw_haemo) raw_haemo = get_long_channels(raw_haemo) design_matrix = make_first_level_design_matrix(raw_haemo, stim_dur=13.0) design_matrix["ShortHbO"] = np.mean( sht_chans.copy().pick(picks="hbo").get_data(), axis=0) glm_est = run_glm(raw_haemo, design_matrix) # First we create a dictionary for each region of interest. # Here we include all channels in each ROI, as we will later be applying # weights based on their specificity to the brain regions of interest. rois = dict() rois["Audio_weighted"] = range(len(glm_est.ch_names)) rois["Visual_weighted"] = range(len(glm_est.ch_names)) # Next we compute the specificity for each channel to the auditory and visual cortex. spec_aud = fold_landmark_specificity( raw_haemo, '42 - Primary and Auditory Association Cortex', atlas="Brodmann") spec_vis = fold_landmark_specificity(raw_haemo, '17 - Primary Visual Cortex (V1)',
drift_model='polynomial') fig, ax1 = plt.subplots(figsize=(10, 6), nrows=1, ncols=1) fig = plot_design_matrix(design_matrix, ax=ax1) # %% # Estimate response on clean data # ------------------------------- # # Here we run the GLM analysis on the clean data. # The design matrix had three columns, so we get an estimate for our simulated # event, the first order drift, and the constant. # We see that the estimate of the first component is 4e-6 (4 uM), # which was the amplitude we used in the simulation. # We also see that the mean square error of the model fit is close to zero. glm_est = run_glm(raw, design_matrix) def print_results(glm_est, truth): """Function to print the results of GLM estimate""" print("Estimate:", glm_est.theta()[0][0], " MSE:", glm_est.MSE()[0], " Error (uM):", 1e6 * (glm_est.theta()[0][0] - truth * 1e-6)) print_results(glm_est, amp) # %% # Simulate noisy NIRS data (white) # --------------------------------
def test_io(): num_chans = 6 fnirs_data_folder = mne.datasets.fnirs_motor.data_path() fnirs_raw_dir = os.path.join(fnirs_data_folder, 'Participant-1') raw_intensity = mne.io.read_raw_nirx(fnirs_raw_dir).load_data() raw_intensity.resample(0.2) raw_intensity.annotations.description[:] = [ 'e' + d.replace('.', 'p') for d in raw_intensity.annotations.description ] raw_od = mne.preprocessing.nirs.optical_density(raw_intensity) raw_haemo = mne.preprocessing.nirs.beer_lambert_law(raw_od, ppf=0.1) raw_haemo = mne_nirs.channels.get_long_channels(raw_haemo) raw_haemo.pick(picks=range(num_chans)) design_matrix = make_first_level_design_matrix(raw_intensity, hrf_model='spm', stim_dur=5.0, drift_order=3, drift_model='polynomial') glm_est = run_glm(raw_haemo, design_matrix) df = glm_to_tidy(raw_haemo, glm_est.data, design_matrix) assert df.shape == (48, 12) assert set(df.columns) == { 'ch_name', 'Condition', 'df', 'mse', 'p_value', 't', 'theta', 'Source', 'Detector', 'Chroma', 'Significant', 'se' } num_conds = 8 # triggers (1, 2, 3, 15) + 3 drifts + constant assert df.shape[0] == num_chans * num_conds assert len(df["se"]) == 48 assert sum(df["se"]) > 0 # Check isn't nan assert len(df["df"]) == 48 assert sum(df["df"]) > 0 # Check isn't nan assert len(df["p_value"]) == 48 assert sum(df["p_value"]) > 0 # Check isn't nan assert len(df["theta"]) == 48 assert sum(df["theta"]) > 0 # Check isn't nan assert len(df["t"]) == 48 assert sum(df["t"]) > -99999 # Check isn't nan contrast_matrix = np.eye(design_matrix.shape[1]) basic_conts = dict([(column, contrast_matrix[i]) for i, column in enumerate(design_matrix.columns)]) contrast_LvR = basic_conts['e2p0'] - basic_conts['e3p0'] contrast = mne_nirs.statistics.compute_contrast(glm_est.data, contrast_LvR) df = glm_to_tidy(raw_haemo, contrast, design_matrix) assert df.shape == (6, 10) assert set(df.columns) == { 'ch_name', 'ContrastType', 'z_score', 'stat', 'p_value', 'effect', 'Source', 'Detector', 'Chroma', 'Significant' } contrast = mne_nirs.statistics.compute_contrast(glm_est.data, contrast_LvR, contrast_type='F') df = glm_to_tidy(raw_haemo, contrast, design_matrix, wide=False) df = _tidy_long_to_wide(df) assert df.shape == (6, 10) assert set(df.columns) == { 'ch_name', 'ContrastType', 'z_score', 'stat', 'p_value', 'effect', 'Source', 'Detector', 'Chroma', 'Significant' } with pytest.raises(TypeError, match="Unknown statistic type"): glm_to_tidy(raw_haemo, [1, 2, 3], design_matrix, wide=False)
def individual_analysis(bids_path, ID): raw_intensity = read_raw_bids(bids_path=bids_path, verbose=False) # Delete annotation labeled 15, as these just signify the start and end of experiment. raw_intensity.annotations.delete(raw_intensity.annotations.description == '15.0') # sanitize event names raw_intensity.annotations.description[:] = [ d.replace('/', '_') for d in raw_intensity.annotations.description] # Convert signal to haemoglobin and resample raw_od = optical_density(raw_intensity) raw_haemo = beer_lambert_law(raw_od, ppf=0.1) raw_haemo.resample(0.3) # Cut out just the short channels for creating a GLM repressor sht_chans = get_short_channels(raw_haemo) raw_haemo = get_long_channels(raw_haemo) # Create a design matrix design_matrix = make_first_level_design_matrix(raw_haemo, stim_dur=5.0) # Append short channels mean to design matrix design_matrix["ShortHbO"] = np.mean(sht_chans.copy().pick(picks="hbo").get_data(), axis=0) design_matrix["ShortHbR"] = np.mean(sht_chans.copy().pick(picks="hbr").get_data(), axis=0) # Run GLM glm_est = run_glm(raw_haemo, design_matrix) # Define channels in each region of interest # List the channel pairs manually left = [[4, 3], [1, 3], [3, 3], [1, 2], [2, 3], [1, 1]] right = [[8, 7], [5, 7], [7, 7], [5, 6], [6, 7], [5, 5]] # Then generate the correct indices for each pair groups = dict( Left_Hemisphere=picks_pair_to_idx(raw_haemo, left, on_missing='ignore'), Right_Hemisphere=picks_pair_to_idx(raw_haemo, right, on_missing='ignore')) # Extract channel metrics cha = glm_est.to_dataframe() # Compute region of interest results from channel data roi = glm_est.to_dataframe_region_of_interest(groups, design_matrix.columns, demographic_info=True) # Define left vs right tapping contrast contrast_matrix = np.eye(design_matrix.shape[1]) basic_conts = dict([(column, contrast_matrix[i]) for i, column in enumerate(design_matrix.columns)]) contrast_LvR = basic_conts['Tapping_Left'] - basic_conts['Tapping_Right'] # Compute defined contrast contrast = glm_est.compute_contrast(contrast_LvR) con = contrast.to_dataframe() # Add the participant ID to the dataframes roi["ID"] = cha["ID"] = con["ID"] = ID # Convert to uM for nicer plotting below. cha["theta"] = [t * 1.e6 for t in cha["theta"]] roi["theta"] = [t * 1.e6 for t in roi["theta"]] con["effect"] = [t * 1.e6 for t in con["effect"]] return raw_haemo, roi, cha, con
def _get_glm_result(tmax=60, tmin=0, noise_model='ar1'): raw = _get_minimal_haemo_data(tmin=tmin, tmax=tmax) design_matrix = make_first_level_design_matrix(raw, stim_dur=5., drift_order=1, drift_model='polynomial') return run_glm(raw, design_matrix, noise_model=noise_model)
# # Fit GLM to subset of data and estimate response for each experimental condition # ------------------------------------------------------------------------------- # # .. sidebar:: Relevant literature # # Huppert TJ. Commentary on the statistical properties of noise and its # implication on general linear models in functional near-infrared # spectroscopy. Neurophotonics. 2016;3(1) # # We run a GLM fit for the data and experiment matrix. # First we analyse just the first two channels which correspond to HbO and HbR # of a single source detector pair. data_subset = raw_haemo.copy().pick(picks=range(2)) glm_est = run_glm(data_subset, design_matrix) # %% # # This returns a GLM regression estimate for each channel. # This data is stored in a dedicated type. # You can view an overview of the estimates by addressing the variable: glm_est # %% # # As with other MNE types you can use the `pick` function. # To query the mean square error of a single channel you would call. # # Note: as we wish to retain both channels for further the analysis below,