c=dict(crop=(0.35, 0.45), center=0.4), d=dict(crop=(0.45, 0.55), center=0.5), e=dict(crop=(0.55, 0.65), center=0.6), f=dict(crop=(0.65, 0.75), center=0.7), ) COLORS_272 = np.load(os.path.join(os.path.dirname(__file__), '..', 'tools', 'colors_272.npy')) RE_COMPUTE_ENTROPY = False NUM_BINS = 10 TMP_DIR = os.path.join('.', 'tmp_mutual') if not os.path.exists(TMP_DIR): os.mkdir(TMP_DIR) DRAWER = Drawer() SHOW = False # %% Tools def hist(x, y, num_bins=NUM_BINS): _range = (min((min(x), min(y))), max((max(x), max(y)))) x_hist, edges = np.histogram(x, bins=num_bins, range=_range) y_hist, _ = np.histogram(y, bins=edges) xy_hist, _, _ = np.histogram2d(x, y, bins=(edges, edges), range=_range) return (x_hist / num_bins, y_hist / num_bins, np.ravel(xy_hist) / num_bins)
# numpy import numpy as np # sklearn from sklearn import metrics # Plotting -------------------------------------- import matplotlib.pyplot as plt # Local Settings -------------------------------------- # Tools sys.path.append(os.path.join('..', 'tools')) # noqa from figure_tools import Drawer # Drawer DRAWER = Drawer() # Init results_folder RESULTS_FOLDER = os.path.join('.', 'results_baseline') RESULTS_NAME = os.path.join('.', 'results_baseline.json') def fuck_report(report): # I don't know why report has to be 2-layers dictionary, # this method is to make it human useable. fucked_report = dict() for key1 in report: if isinstance(report[key1], dict): for key2 in report[key1]: fucked_report[f'{key1}-{key2}'] = report[key1][key2] else:
# Plotting import tqdm import matplotlib.pyplot as plt # Local tools sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'tools')) # noqa from MEG_worker import MEG_Worker from figure_tools import Drawer # Settings BAND_NAME = 'U07' COLORS_272 = np.load( os.path.join(os.path.dirname(__file__), '..', 'tools', 'colors_272.npy')) DRAWER = Drawer() SHOW = False # %% idx = 3 # Setting ------------------------------------------- running_name = f'MEG_S{idx:02d}' plt.style.use('ggplot') # Worker pipeline ----------------------------------- worker = MEG_Worker(running_name=running_name) worker.pipeline(band_name=BAND_NAME) # %%
# numpy import numpy as np # sklearn from sklearn import metrics # Plotting -------------------------------------- import matplotlib.pyplot as plt # Local Settings -------------------------------------- # Tools sys.path.append(os.path.join('..', 'tools')) # noqa from figure_tools import Drawer # Drawer DRAWER = Drawer() # Init results_folder RESULTS_FOLDER = os.path.join('.', 'results') TMP_FOLDER = os.path.join('.', 'tmp') RE_COMPUTE = False def fuck_report(report): # I don't know why report has to be 2-layers dictionary, # this method is to make it human useable. fucked_report = dict() for key1 in report: if isinstance(report[key1], dict):
def __init__(self, title_prefix='', show=False): self.epochs = None self.drawer = Drawer() self.title_prefix = title_prefix self.show = show
class Visualizer(): def __init__(self, title_prefix='', show=False): self.epochs = None self.drawer = Drawer() self.title_prefix = title_prefix self.show = show def _title(self, title): # Built-in title method prefix = self.title_prefix return f'{prefix}-{title}' def load_epochs(self, epochs): # Load epochs for ploting self.epochs = epochs print(f'New epochs are loaded: {epochs}') def plot_lags(self, paired_lags_timelines, title=None): # Methods for visualizing the lags and timelines of button effect # Make sure we have a title if title is None: title = 'Lags vs. Timeline' # Prepare times, _lags, _timelines, # _lags: the lags of behavior button response to target picture, # _timelines: the estimated time line of the button effect. times = self.epochs.times _lags = paired_lags_timelines['sorted_lags'] _timelines = paired_lags_timelines['sorted_timelines'] # Filter too long lags _timelines = _timelines[_lags < 0.8] _lags = _lags[_lags < 0.8] # Compute number of samples num_samples = _lags.shape[0] # Compute corr between _lags and peak _timelines def _corr(_lags, _timelines, num_samples): def _smooth_max(a, b=np.ones(10)): new_a = np.convolve(a, b, mode='same') return np.where(new_a == max(new_a))[0][0] _idx_peaks = np.array([_smooth_max(e) for e in _timelines]) return np.corrcoef(_lags, _idx_peaks)[0][1] _corrcoef = _corr(_lags, _timelines, num_samples) title = f'{title} - {_corrcoef}' # Plot in two layers, # Bottom is the _timelines matrix, # Top is the _lags curve. fig, ax = plt.subplots(1, 1) ax.plot(_lags, c='red', alpha=0.7, linewidth=3) ax.set_ylim([min(times), max(times)]) im = ax.imshow(_timelines.transpose(), extent=(0, num_samples - 1, min(times), max(times)), aspect=200, origin='lower') ax.set_title(title) fig.colorbar(im, ax=ax) # Save fig into drawer self.drawer.fig = fig def plot_joint(self, event_id, title=None, times='peaks'): # Plot joint # Make sure we have a title if title is None: title = event_id # Compute evoked evoked = self.epochs[event_id].average() # Plot evoked in joint plot self.drawer.fig = evoked.plot_joint(show=self.show, times=times, title=self._title(title)) def save_figs(self, path): # Save plotted figures into [path] self.drawer.save(path) self.drawer.clear_figures()
# numpy import numpy as np # sklearn from sklearn import metrics # Plotting -------------------------------------- import matplotlib.pyplot as plt # Local Settings -------------------------------------- # Tools sys.path.append(os.path.join('..', 'tools')) # noqa from figure_tools import Drawer # Drawer DRAWER = Drawer() # Init results_folder RESULTS_FOLDER = os.path.join('.', 'results') def fuck_report(report): # I don't know why report has to be 2-layers dictionary, # this method is to make it human useable. fucked_report = dict() for key1 in report: if isinstance(report[key1], dict): for key2 in report[key1]: fucked_report[f'{key1}-{key2}'] = report[key1][key2] else: fucked_report[key1] = report[key1]