def plot_heatmapSingle(probabilityArray, fsds, skip, figsize = (5,2.5), threshold = 0.5, smoothing = 20, vmin = 0.1, vmax = 1.1, cmapName = "Blues"): fig, ax = plt.subplots(1,1, figsize=figsize, dpi =300) windows, nchan = probabilityArray.shape w = int(smoothing*fsds/skip) mvgAvg = np.zeros(shape = (windows - w + 1, nchan)) for c in range(nchan): mvgAvg[:,c] = echobase.movingaverage(probabilityArray[:,c], w) cmap = plt.cm.get_cmap(cmapName) sns.heatmap( mvgAvg.T , cmap = cmap , ax = ax, cbar = False, xticklabels = False, yticklabels = False, vmin = vmin, vmax = vmax)
def prob_threshold_moving_avg(prob_array, fsds, skip, threshold = 0.9, smoothing = 20): nchan = prob_array.shape[1] w = int(smoothing*fsds/skip) probability_arr_movingAvg = np.zeros(shape = (windows - w + 1, nchan)) for c in range(nchan): probability_arr_movingAvg[:,c] = echobase.movingaverage(prob_array[:,c], w) probability_arr_threshold = copy.deepcopy(probability_arr_movingAvg) probability_arr_threshold[probability_arr_threshold > threshold] = 1 probability_arr_threshold[probability_arr_threshold <= threshold] = 0 return probability_arr_movingAvg, probability_arr_threshold
def plot_probheatmaps(probabilityArray, fsds, skip, threshold = 0.5, smoothing = 20, channel1 = 0, channel2 = 1, vmin = 0, vmax = 1, center=0.5): fig, ax = plt.subplots(3,2, figsize=(10,10), dpi =300) windows, nchan = probabilityArray.shape thresholded = copy.deepcopy(probabilityArray) thresholded[thresholded>threshold] = 1; thresholded[thresholded<=threshold] = 0 w = int(smoothing*fsds/skip) mvgAvg = np.zeros(shape = (windows - w + 1, nchan)) for c in range(nchan): mvgAvg[:,c] = echobase.movingaverage(probabilityArray[:,c], w) mvgAvgThreshold = copy.deepcopy(mvgAvg) mvgAvgThreshold[mvgAvgThreshold>threshold] = 1 mvgAvgThreshold[mvgAvgThreshold<=threshold] = 0 sns.lineplot( x = range(windows), y= probabilityArray[:,channel1], ci=None, ax = ax[0,0]) sns.lineplot( x = range(windows), y= probabilityArray[:,channel2], ci=None, ax = ax[0,1]) sns.heatmap( probabilityArray.T , ax = ax[1,0], vmin = vmin, vmax = vmax, center=center) sns.heatmap( thresholded.T, ax = ax[1,1] ) sns.heatmap( mvgAvg.T , ax = ax[2,0] , vmin = vmin, vmax = vmax, center=center) sns.heatmap( mvgAvgThreshold.T, ax = ax[2,1] )
def plot_singleProbability(probabilityArray, fsds, skip, channel=1, startInd = 0, stopInd = None, smoothing = 20, vmin = -0.2, vmax = 1.2): windows, nchan = probabilityArray.shape if stopInd == None: stopInd = windows smoothing = smoothing w = int(smoothing*fsds/skip) mvgAvg = np.zeros(shape = (windows - w + 1, nchan)) for c in range(nchan): mvgAvg[:,c] = echobase.movingaverage(probabilityArray[:,c], w) y= mvgAvg[startInd:stopInd,channel] x = range(len(y)) y2 = -0.05 fig, ax = plt.subplots(1,1, figsize=(10,2), dpi =300) sns.lineplot( x = x, y= y, ci=None, ax = ax, alpha = 0) interpolate = interp1d(np.array(x),np.array(x)) xnew = interpolate(np.arange( 0, len(x)-1, 1 )) interpolate = interp1d(np.array(x),y) ynew = interpolate(np.arange( 0, len(x)-1, 1 )) Z = np.linspace(0, 1, len(xnew)) normalize = colors.Normalize(vmin=vmin, vmax=vmax) cmap = plt.cm.get_cmap('Blues') df = pd.DataFrame({'x': xnew, 'y1': ynew, 'z': Z}) xcolor = df['x'].values y1color = df['y1'].values zcolor = df['z'].values for ii in range(len(df['x'].values)-1): plt.fill_between( x = [ xcolor[ii] , xcolor[(ii+1)]] , y1 = [ y1color[ii], y1color[ii+1] ], y2=y2, color=cmap(normalize(zcolor[ii])) ) ax.set(yticks=[]) ax.set(xticks=[]) ax.set_ylim([y2,1]) sns.despine(bottom=True, left=True)
for ch in range(len(c)): print(ch) ch_pred = data_scalerDSPlot_X[:,:,ch].reshape(windows, time_step, 1 ) probWN[:,ch] = modelWN.predict(ch_pred, verbose=1)[:,1] probCNN[:,ch] = modelCNN.predict(ch_pred, verbose=1)[:,1] probLSTM[:,ch] = modelLSTM.predict(ch_pred, verbose=1)[:,1] smoothing = 20 w = int(smoothing*fsds/skip) mvgAvg = np.zeros(shape = (windows - w + 1, len(c))) for ch in range(len(c)): mvgAvg[:,ch] = echobase.movingaverage(probWN[:,ch], w) #Calculate Raw Features #linelength, linelengthNorm = echobase.lineLengthOfArray(data_scalerDSPlot_X) #%% generate example EEG fnameFig = join(pathFigureOverview, "exampleEEGspread.png") DataJson.plot_eeg(data_scaler[int(ictalStartIndex-1*fs_plot):int(ictalStopIndex+2*fs_plot),c], fs_plot, nchan = None, dpi = 300, aspect=30, height=0.5, hspace=-0.87, lw = 0.8, savefig = False, pathFig = fnameFig) #%making pattern
def plot_probheatmaps(probabilityArray, fsds, skip, threshold = 0.5, smoothing = 20, channel1 = 0, channel2 = 1, vmin = 0, vmax = 1, center=0.5, title = None, title_channel1 = None, title_channel2 = None, verticalline = 1800, channel_names = ""): fig, ax = plt.subplots(3,2, figsize=(16,16), dpi =300) #printing channel names channel_names_string = " ".join(channel_names) names_len = len(channel_names_string) max_char = 160 #max characters on single line text_string = [] new_string = copy.deepcopy(channel_names_string) s=0 while s < names_len: spaces = np.array([i for i in range(len(new_string)) if new_string.startswith(" ", i)]) where_spaces_are_less_than_max = np.where(spaces < max_char)[0] loc = spaces[where_spaces_are_less_than_max[-1]] text_string.append( new_string[0:loc] ) s = s+loc+1 new_string = new_string[loc+1:] if s+max_char >names_len: text_string.append( new_string ) s = s + max_char text = "\n".join(text_string) fig.text(0.1,0, text, size = 9, wrap=True, ha='left', va = "bottom") windows, nchan = probabilityArray.shape thresholded = copy.deepcopy(probabilityArray) thresholded[thresholded>threshold] = 1; thresholded[thresholded<=threshold] = 0 w = int(smoothing*fsds/skip) mvgAvg = np.zeros(shape = (windows - w + 1, nchan)) for c in range(nchan): mvgAvg[:,c] = echobase.movingaverage(probabilityArray[:,c], w) mvgAvgThreshold = copy.deepcopy(mvgAvg) mvgAvgThreshold[mvgAvgThreshold>threshold] = 1 mvgAvgThreshold[mvgAvgThreshold<=threshold] = 0 #plot non-smoothed #sns.lineplot( x = range(windows), y= probabilityArray[:,channel1], ci=None, ax = ax[0,0]) #sns.lineplot( x = range(windows), y= probabilityArray[:,channel2], ci=None, ax = ax[0,1]) #plot smoothed nan_add = windows - mvgAvg.shape[0] lineplot1 = np.concatenate([ mvgAvg[:,channel1], np.repeat(np.nan, nan_add)]) lineplot2 = np.concatenate([ mvgAvg[:,channel2], np.repeat(np.nan, nan_add)]) np.array(range(windows)).shape[0] sns.lineplot( x = range(windows), y= lineplot1, ci=None, ax = ax[0,0]) sns.lineplot( x = range(windows), y= lineplot2, ci=None, ax = ax[0,1]) ax[0,0].axhline(y=threshold, color="#562686", linestyle='-', lw = 3) ax[0,1].axhline(y=threshold, color="#562686", linestyle='-', lw = 3) ax[0,0].axvline(x=verticalline, c = "#aa2222", lw = 3 ) ax[0,1].axvline(x=verticalline, c = "#aa2222", lw = 3 ) sns.heatmap( probabilityArray.T , ax = ax[1,0], vmin = vmin, vmax = vmax, center=center) sns.heatmap( thresholded.T, ax = ax[1,1] ) sns.heatmap( mvgAvg.T , ax = ax[2,0] , vmin = vmin, vmax = vmax, center=center) sns.heatmap( mvgAvgThreshold.T, ax = ax[2,1] ) if title == None: title = "Seizure Probabilities" if title_channel1 == None: title_channel1 = 'Channel 0' if title_channel2 == None: title_channel2 = 'Channel 1' fig.suptitle(title, fontsize=28, y= 0.93) ax[0,0].set_xlabel("time (0.1 sec)") ax[0,1].set_xlabel("time (0.1 sec)") ax[0,0].set_ylabel("seizure probability") ax[0,0].title.set_text(title_channel1) ax[0,1].title.set_text(title_channel2) ax[1,0].set_ylabel("Channel #") ax[2,0].set_ylabel("Channel #") ax[2,0].set_xlabel("time (0.1 sec)") ax[2,1].set_xlabel("time (0.1 sec)") ax[1,0].set_xticklabels(labels = ax[2,0].get_xticklabels(), fontsize = 7) ax[1,1].set_xticklabels(labels = ax[2,0].get_xticklabels(), fontsize = 7) ax[2,0].set_xticklabels(labels = ax[2,0].get_xticklabels(), fontsize = 7) ax[2,1].set_xticklabels(labels = ax[2,0].get_xticklabels(), fontsize = 7)
SMOOTHING = 20 #in seconds lineLength_arr_threshold = copy.deepcopy(lineLength_arr) lineLength_arr_threshold[lineLength_arr_threshold>THRESHOLD_LL] = 1 lineLength_arr_threshold[lineLength_arr_threshold<=THRESHOLD_LL] = 0 sns.heatmap( lineLength_arr_threshold.T, ax = ax[1,1] ) w = int(SMOOTHING*fsds/skip) lineLength_arr_movingAvg = np.zeros(shape = (windows - w + 1, nchan)) for c in range(nchan): lineLength_arr_movingAvg[:,c] = echobase.movingaverage(lineLength_arr[:,c], w) sns.heatmap( lineLength_arr_movingAvg.T , ax = ax[2,0] ) lineLength_arr_movingAvg_threshold = copy.deepcopy(lineLength_arr_movingAvg) lineLength_arr_movingAvg_threshold[lineLength_arr_movingAvg_threshold>THRESHOLD_LL] = 1 lineLength_arr_movingAvg_threshold[lineLength_arr_movingAvg_threshold<=THRESHOLD_LL] = 0 sns.heatmap( lineLength_arr_movingAvg_threshold.T, ax = ax[2,1] ) #sns.heatmap(SC, square=True)