import os
import numpy as np
import matplotlib.pyplot as plt
import mne
from scipy.stats import ttest_1samp
def get_data(fnirs_data, d_type, t_tup):
# return the hbo data belong to the time interval specified by the tuple
return fnirs_data.pick_types(fnirs=d_type).crop(tmin=t_tup[0], tmax=t_tup[1],
include_tmax=True).get_data()
def get_diff_talking_base_data(raw, t, t_b=10, t_a=20):
# This function takes the raw data, time instant, duration before event (t_a), duration after event
# (t_a) as input. It will collect the data t_b sec before and t_a sec after the specified time instant.
# Then take the average of these data before and after time instance specified by t.
# Then compute the difference of this two averages.
d_type='hbo' # type of fNIRS data (hbo/hbr)
base_data=get_data(raw.copy(), d_type, (t-t_b, t)).mean(axis=1)
event2_data=get_data(raw.copy(), d_type, (t, t+t_a)).mean(axis=1)
diff=event2_data-base_data
return diff[:, np.newaxis]
baseFolder='path for data'
run_2='xxxx' # folder name
data_path=os.path.join(baseFolder, run_2)
# Reading fNIRS data
fnirs_data=mne.io.read_raw_nirx(data_path, preload=True, verbose=None)
### converting raw intensity to optical intensity
fnirs_data_od=mne.preprocessing.nirs.optical_density(fnirs_data)
### Converting from optical density to haemoglobin
fnirs_data_haemo=mne.preprocessing.nirs.beer_lambert_law(fnirs_data_od)
## Removing heart rate from signal
l_freq=0.01 # lower cut off frequency
h_freq=0.1 # higher cut off frequency
fnirs_filt= fnirs_data_haemo.filter(l_freq=l_freq, h_freq=h_freq, method='fir', phase='zero-double')
## Get the difference data from the particular intervals
time_inst=[399.68, 466.54, 772.73, 1018.89, 1102.38] ## Specify the time instants where you want to compare
t_b=10 # time duration before an event you want to average (for event_1)
t_a=20 # Time duration after an event you want to average (for event_2)
diff_data=[]
diff_data=[get_diff_talking_base_data(fnirs_filt.copy(), t, t_b, t_a) for t in time_inst] # Get the difference between average data from two events
diff_data=np.hstack(diff_data) # list to 2-D array
## Compute t-static of the difference values
info_data=fnirs_filt.pick_types(fnirs='hbo').info
t_diff, p_diff=ttest_1samp(diff_data, 0, axis=1)
fig, ax=plt.subplots(1,1)
mne.viz.plot_topomap(t_diff, info_data, colorbar=True, sensors=True, vmin=-5, vmax=5, outlines='head', contours=0)
ax.set_title('T-statitstic between Talking time and Before Talking intervals', fontweight='bold')
### End of 2D plotting t-values
data_path='/data2/pdhara/mne_data/MNE-sample-data'
subjects_dir = data_path + '/subjects'
## This function is working for plotting source and detector pairs (106 channels) in 3D head model
fig = mne.viz.create_3d_figure(size=(800, 600), bgcolor='white')
fig = mne.viz.plot_alignment(fnirs_filt.info, show_axes=True,
subject='fsaverage', coord_frame='mri',
trans='fsaverage', surfaces=['brain'],
fnirs=['channels', 'pairs',
'sources', 'detectors'],
subjects_dir=subjects_dir, fig=fig)
mne.viz.set_3d_view(figure=fig, azimuth=20, elevation=60, distance=0.4,
focalpoint=(0., -0.01, 0.02))
### This function is not working ***
mne_nirs.visualisation.plot_nirs_source_detector(t_diff, info_data, radius=0.001, subject='fsaverage', subjects_dir=subjects_dir, surfaces='head', cmap=True)
I have attached the code above. It took longer a bit as I need to eliminate unnecessary stuffs. Anyways,
I tried to plot source and detector pairs in head, and it is working fine (second last). But, I tried to plot t-values as a data matrix ([106x1]), but not working.