Skip to content

Instantly share code, notes, and snippets.

@cosacog
cosacog / detect_emg_onset.py
Last active December 20, 2015 00:40
emg(筋電図)の立ち上が(onset)りを適当なチャンネル(e.g. EEG063)から決めていくfunctionいろいろ. MRCFを想定しています。要 mne python , numpy, matplotlib. 下の方のコメント欄にも説明書いてます
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# この中で実際に使うのは
# 1. マウスクリックなどで決めたトリガーを基に取り出す
# detect_emg_onset_1ch_by_stim_ch(raw, ch_emg, stim_channel, event_id)
# 2. 視察で筋電図から大まかにピークを取り出して、次に再度各区間で立ち上がり時刻を正確に決める
# detect_emg_onset_1ch_vis_inspect(raw, ch_emg)
# 3. 閾値で筋電図からピークを検出して、次に再度各区間で立ち上がり時刻を正確に決める
# detect_emg_onset_1ch_by_thr(raw, ch_emg, win_mv) # e.g. win_mv=0.3
# のどれかです.
@cosacog
cosacog / epoch_ica_plot_component.py
Last active January 26, 2016 16:09
epochでicaをかけた後、各componentのマップと加算平均波形を表示するfunction: plot_comp_map_and_evoked のスクリプト
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# function設定(componentの任意の条件(neutral/centerとか)とcomponentの番号から
# mapとevoked(componentから作成)のプロット
def plot_comp_map_and_evoked(comps, cnd, idx_comp, is_peak_need):
"""
component (icaの)を各条件、component番号で加算平均波形をプロットする。
comps: icaで出てくるcomponentのデータ. e.g.comps=ica.get_sources(epochs)
@cosacog
cosacog / func_make_movie.py
Last active January 26, 2016 16:08
mne pythonで作ったcon (connectivityのデータ)とlabelからconnectivityの図の動画を作るfunction
#!/usr/bin/env python
# _*_ coding: utf-8 -*-
def make_movie_circle_plot(con,n_seed_label,labels, times, con_method, vmin, vmax):
"""
mne pythonで作ったcon(connectivity)のデータから時系列でfigureを作る
# all-to-allかseed basedのデータにするか
#n_seed_label = 9 # number or None: e.g. labels[9].name=entorhinal-rh
#n_seed_label = None
"""
@cosacog
cosacog / mri_processing_basics.sh
Last active January 27, 2016 01:38
mneで使うmriの処理のよくあるパターン(dicom から変換していろいろファイルがmriとかbemとかsurfのディレクトリの中にできて・・・)をまとめました。下の方にコメント入れてますので参考にしてください。
## 下準備(環境変数の設定)
export SUBJECTS_DIR=/data_wo_matometa_drive/MRI_wo_matomeru_directory #"/"を最後つけない
export SUBJECT=hikenja_no_namae #ここの最後に"/"をつけない
export DICOM_DIR=/dicom_no_aru_directory # 例:/net/raid0/mnt/share/hikenja/dicom
# 以下基本変更なし
# データを入れるディレクトリをまとめて作る(mriとかT1とかbemとかsurfとかorigとか)
mksubjdirs $SUBJECTS_DIR/$SUBJECT # freesurferのコマンド
@cosacog
cosacog / func_raw_list2stc.py
Last active February 2, 2016 13:03
raw.fifのリストを入力して(1)周波数フィルタ (2) epochにして保存 (3) mneでstcに変換して保存する作業をまとめた関数. 下の方に説明をちょっぴりとサンプルスクリプト追加してます
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def filter_raw_list(list_fname, bp_freq):
u"""
filter raw files. save file name with "l_freq"_"h_freq".fif
Arguments
list_fname: list of raw.fif
bp_freq: band pass frequency. e.g (0,40)
return
@cosacog
cosacog / hrv_analysis_xlsx.R
Last active July 5, 2016 12:58
HRV解析用Rスクリプト。xlsxファイルから読み込み。要RHRV, XLConnect
# ## データ読み込み
# setwd('C:/directory/to/xlsx/file')
# rrdata_pre<-read.table('before.txt',sep='/t',header=F)
# rrdata_pst<-read.table('after.txt',sep='/t',header=F)
# time_onset <- sum(rrdata_pre)/1000 # ここで前後を区切る
# doPlot<-TRUE
# names(rrdata_pre) <- names(rrdata_pst) <- 'Time'
# rrdata <- rbind(rrdata_pre,rrdata_pst)
## define function
@cosacog
cosacog / mne_stc_misc.py
Last active July 7, 2016 11:37
labelをいじるfunction、label内の時系列データのうち活動の多い方から10%のvertexの平均をプロットするfunction
def half_ydir_label(label_orig):
'''
要mne等
labelをy軸方向に半分にするスクリプト
params: label_orig. 元のlabel
return: label_half. y軸方向に半分になったlabel
'''
ypos_label = label_orig.pos[:,1]
idx_half_label = np.where(ypos_label >= np.median(ypos_label)) # example: y positions are anterior
DetectOnset <- function(data, intvl_thr){
# dataからオンセットを取り出す
# params:
# data: 1chのデータ
# intvl_thr: これ以下の間隔の時は1つながりのトリガーとする
thr <- (median(data) + max(data))*0.5
bin_data <- data > thr
bin_onset <- which((tail(bin_data,-1) == 1) & (head(bin_data,-1) == 0)) + 1
bin_ofset <- which((tail(bin_data,-1) == 0) & (head(bin_data,-1) == 1))
# exclude
@cosacog
cosacog / trigger_by_gray_marker_moveup_opencv.py
Last active July 20, 2016 11:49
psychopy: detect finger extension using openCV and send trigger
# -*- coding: utf-8 -*-
from psychopy import visual, core, event, parallel
import numpy as np
import cv2
# define
K_ESC = 27
K_ARROW_UP = 2490368 # arrow up
K_ARROW_DOWN = 2621440 # arrow down
K_RIGHT = 114 # "r"
K_LEFT = 108 # "l"
@cosacog
cosacog / mep_single_pulse.py
Last active July 17, 2018 05:17
psychopy for single pulse TMS
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
## settings
prt_num = int('00001111',2) # port number(digit number) indicates port 8,7,6,5,4,3,2,1
t_range = [5, 7] # time range for TMS (sec)
t_init_wait = 3 # time for start TMS (sec)
n_rep_tms = 12 # TMS repetition frequency (times)
p_prt_address = '0xCFF8'