Recombination DNA Technology (Nucleic Acid Hybridization )
Python Code Training sample1
1. Data Analysis Instruction
for EEG (g.tec), eye-tracking
(Tobii) and motion capture
(Perception Neuron)
H. Wagatsuma@Kyutech
Instruction meeting for EEG team in Wagatsuma Lab@Kyutech
1
Started: 2020/1/6
Revised:2020/1/26
3. Protocols on EEG:
1. EEG data confirmation→ ch pos, time, subjects and so on
2. Epoch segmentations depending on events
• Tobii apps → movie with gaze maker → Human operation (get event
timing as H:M:S: ms)
• EEG verification with Tobii trigger (1/512; sampling rate)
• Estimate Tobii start timing (to get the subtraction time with g.tec
00:00:00 starting time
3. Noise removal (EEG lab or MCA)
4. Data analysis 1(averaging with all segments; ERP )
5. Data analysis 2(FFT or others)
3
5. 1. EEG data confirmation
• → ch pos, time, subjects and so on
AutoCarExp_sub1_4122019_04_12_2019_13_44_02_0000.mat
AutoDrivingExpDay2_sub1_5122019_05_12_2019_13_36_09_0000.mat
AutoDrivingExpDay2_sub2_5122019_05_12_2019_14_43_34_0000.mat
AutomatedDrivingExp_sub1_4122019_04_12_2019_13_49_59_0000.mat
AutomatedDriving_sub2_4122019_04_12_2019_14_39_41_0000.mat
AutomatedDriving_sub2_4122019_04_12_2019_14_39_41_0001.mat
DriveSim_Subj2_26112019_26_11_2019_16_54_35_0000.mat
DriveSim_Subj2_26112019_26_11_2019_16_54_35_0001.mat
DriveSim_Subj2_26112019_26_11_2019_16_54_35_0002.mat
DriveSim_sub1_26112019_26_11_2019_12_54_33_0000.mat
DriveSim_sub1_26112019_26_11_2019_12_54_33_0001.mat
currentCFG_g.SCOPE_f47fe32382a59a51e350d6b98bbc5d17_0.cfg
AutoCarExp_sub1_4122019_
AutoDrivingExpDay2_sub1_5122019_
Data name should be
defined uniquely and
systematically for reading
with programming codes
Next page
5
11. 11
>> b={}
b =
0×0 empty cell array
>> b{1}='abc'; b{2}='dbc';
>> b{3}='kdjkasd'
b =
1×3 cell array
{'abc'} {'dbc'} {'kdjkasd'}
>> b
b =
2×3 char array
'abc'
'dbc'
>> b{3}='kdjkasd'
Unable to perform assignment because brace indexing is not
supported for variables of
this type.
NG matrix
OK matrix
Filenameの扱いにはcellを使う
Point!
cell is better for treating Filename(string )
rather than matrix.
12. How to connect g.tec recording files
12
AutoDrivingExp_Day1_Subj1_4122019_04_12_2019_13_44_02_0000.mat
AutoDrivingExp_Day1_Subj1_4122019_04_12_2019_13_49_59_0000.mat
AutoDrivingExp_Day1_Subj2_4122019_04_12_2019_14_39_41_0000.mat
AutoDrivingExp_Day1_Subj2_4122019_04_12_2019_14_39_41_0001.mat
AutoDrivingExp_Day2_Subj1_5122019_05_12_2019_13_36_09_0000.mat
AutoDrivingExp_Day2_Subj2_5122019_05_12_2019_14_43_34_0000.mat
DriveSim_Day1_Subj1_26112019_26_11_2019_12_54_33_0000.mat
DriveSim_Day1_Subj1_26112019_26_11_2019_12_54_33_0001.mat
DriveSim_Day1_Subj2_26112019_26_11_2019_16_54_35_0000.mat
DriveSim_Day1_Subj2_26112019_26_11_2019_16_54_35_0001.mat
DriveSim_Day1_Subj2_26112019_26_11_2019_16_54_35_0002.mat
AutoDrivingExp_Day1_Subj2_4122019_04_12_2019_14_39_41_0000.mat
13. How to get a file list: fread_Plus.m (1/2)
13
defaultPath=pwd;
DataFolderPath='/Volumes/Elements/Automated_Driving_System_Sato_Lab_
20191204_05_DataAnalysis/EEG';
fnlist=struct2cell(dir);
merge_f='merged';
cd(fullfile(DataFolderPath,merge_f));
close all;
flist = dir; rfields = {'date','bytes','isdir','datenum'};
flist=rmfield(flist,rfields);
tag=cell(size({flist(:).name})); tag(:)={'mat'};
flag=cellfun(@strfind,{flist(:).name},tag,'UniformOutput', false);
flist2 = struct2cell(flist(~cellfun(@isempty,flag)));
flist3=flist2(1,:);
Set of the Default Path to return the same place later
Extract
filenames
with ”.mat”
See detail explanation in the next page
24. 24
flag4ConnectE=flag4Connect-eye(size(flag4Connect));
checkList=sum(flag4Connect);
checkListE=sum(flag4ConnectE);
for i=1:length(checkList)
newDataName0=makefname(flist5{i}(1:10));
newDataName=[newDataName0,'.',tag{1}];
if checkListE(i)==0
disp(['one file: ',num2str(i)]);
load(fullfile(DataFolderPath,flist3{i}));
ynew=permute(y,[1,3,2]);
size(ynew)
save(fullfile(DataFolderPath,merge_f,newDataName),'ynew','SR');
ynew=[];
elseif checkListE(i)>0
disp(['multi files: ',num2str(i)]);
disp(checkListE);
y2new=[];
cID=find(flag4Connect(i,:)>0);
for j=1:checkList(i)
fnum=j-1; snum=num2str(fnum);
s2num=[repmat('0',[1,gtecLen-length(snum)]),snum];
fname_tmp=[newDataName0,'_',s2num,'.',tag{1}];
load(fullfile(DataFolderPath,fname_tmp));
display(fname_tmp)
display(flist3{cID(j)});
if ~strcmp(fname_tmp,flist3{cID(j)}); disp('error!!'); end
checkListE(cID(j))=-1;
y2new=[y2new,permute(y,[1,3,2])];
end
ynew=y2new;
size(y2new);
save(fullfile(DataFolderPath,merge_f,newDataName),'ynew','SR');
ynew=[];
end
end
Files to be
connected
This is
single file
31. 31
format long; diff(temp)
ans =
Columns 1 through 4
0.001953125000000 0.001953125000000 0.001953125000000 0.001953125000000
Columns 5 through 8
0.001953125000000 0.001953125000000 0.001953125000000 0.001953125000000
Columns 9 through 10
0.001953125000000 0.001953125000000
>> 1/0.001953125000000
ans =
512
32. Result of the data analysis
32
7 8
matrix matrix
time
Connected Matrix
7: 'DriveSim_Day1_Subj1_26112019_26_11_2019_12_54_33_0000.mat’
8: 'DriveSim_Day1_Subj1_26112019_26_11_2019_12_54_33_0001.mat'
0 1 2 0 1 2
0 1 2 0 1 2
0 1 2 3 4 5
Check this
(wrong)
(right)
>> ynew1(1,1)
ans =
0
>> ynew2(1,1)
ans =
0
Data is same or different?
0 1 2
0 1 2
0 1 2
0 1 2
7
8
Problem①
start “0” or “1”?
Problem②
data is same or not?
Problem③
what time difference?
d_t
Answer①
start “0”
34. 34
>> > timestep_first=ynew1(1,:); d_t=diff(timestep_first);
>> min(d_t)
ans =
0.001953125000000
>> max(d_t)
ans =
0.001953125000000
>> max(d_t)-min(d_t)
ans =
0
>> mean(d_t)
ans =
0.001953125000000
0 1 2
0 1 2
Problem③
what time difference?
d_t
No fluctuation
It is okay to set d_t as one sample or mean
43. How to read json data: Tobii data analysis
43
References:
https://qiita.com/kikuchiTakuya/items/53990fca06fb9ba1d8a7
Last login: Sun Jan 26 15:32:37 on ttys005
[HiroWaga-Retina-MacBook-Pro-2:~]
hiro% cd /Volumes/Elements2/Automated_Driving_System_Sato_Lab_20191204_05_DataAnalysis/Tobii
[HiroWaga-Retina-MacBook-Pro-
2:Elements2/Automated_Driving_System_Sato_Lab_20191204_05_DataAnalysis/Tobii]
hiro% Jupyter Notebook
[I 15:34:22.711 NotebookApp] Serving notebooks from local directory:
/Volumes/Elements2/Automated_Driving_System_Sato_Lab_20191204_05_DataAnalysis/Tobii
[I 15:34:22.711 NotebookApp] The Jupyter Notebook is running at:
[I 15:34:22.711 NotebookApp]
http://localhost:8888/?token=c48f7978b541eada07ddadd2bd206b24e32ee62270f33d1b
[I 15:34:22.712 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice
to skip confirmation).
[C 15:34:22.723 NotebookApp]
Copy/paste this URL into your browser when you connect for the first time,
to login with a token:
http://localhost:8888/?token=c48f7978b541eada07ddadd2bd206b24e32ee62270f33d1b
[I 15:34:23.052 NotebookApp] Accepting one-time-token-authenticated connection from ::1
49. 49
[HiroWaga-Retina-MacBook-Pro-2:a6nbbtq/segments/1] hiro% pip3 install jsonlib-python3
Collecting jsonlib-python3
Downloading
https://files.pythonhosted.org/packages/f2/ec/f632f40dc465923baa25f81f4497e22bf801ba1e48
5b4ff09055a49c7ed4/jsonlib-python3-1.6.1.tar.gz (43kB)
|████████████████████████████████| 51kB 922kB/s
Building wheels for collected packages: jsonlib-python3
Building wheel for jsonlib-python3 (setup.py) ... done
Created wheel for jsonlib-python3: filename=jsonlib_python3-1.6.1-cp37-cp37m-
macosx_10_13_x86_64.whl size=36475
sha256=976657ea6d0f8a459b17d5f8c8ddefb26240b13f5234c3557387e504f3b83464
Stored in directory:
/Users/hiro/Library/Caches/pip/wheels/05/32/40/4816ec2597eb6584df06e7a5700647af098056db7
f3bc32536
Successfully built jsonlib-python3
Installing collected packages: jsonlib-python3
Successfully installed jsonlib-python3-1.6.1
[HiroWaga-Retina-MacBook-Pro-2:a6nbbtq/segments/1] hiro%
Python json module install
50. 50
Last login: Sun Jan 26 15:32:37 on ttys005
[HiroWaga-Retina-MacBook-Pro-2:~] hiro% cd
/Volumes/Elements2/Automated_Driving_System_Sato_Lab_20191
204_05_DataAnalysis/Tobii
[HiroWaga-Retina-MacBook-Pro-
2:Elements2/Automated_Driving_System_Sato_Lab_20191204_05_
DataAnalysis/Tobii]
hiro% Jupyter Notebook
55. 55
It means that “livedata.json” is not single json file, and every single line
has to be loaded as individual json file.
結論:
Tobiiの生成したjson file (livedata.json)は1ファイルで1つのjsonファ
イルでなく、1行が1ファイルと同じ構成になっている。
→ つまり、1行ずつ読み込んで, json変換する必要がある。
→ json.load(ファイルポインタ)でなく、json.loads(文字列)を用いる
It requires that json.loads(json strings) not json.load(file pointer)
65. 65
Python If ... Else
❮ PreviousNext ❯
Python Conditions and If statements
Python supports the usual logical conditions from
mathematics:
•Equals: a == b
•Not Equals: a != b
•Less than: a < b
•Less than or equal to: a <= b
•Greater than: a > b
•Greater than or equal to: a >= b
These conditions can be used in several ways, most
commonly in "if statements" and loops.
An "if statement" is written by using
the if keyword.
Example
If statement:
a = 33
b = 200
if b > a:
print("b is greater than a")
https://www.w3schools.com/python/pyt
hon_conditions.asp
66. 66
import csv
try:
# 書き込み UTF-8
with open('tobii_gp.csv', 'w') as csvfile:
writer = csv.writer(csvfile, lineterminator='¥n')
writer.writerow(['ts', 'gp', 'l', 's'])
f = open('livedata_a6nbbtq_rep2.json', 'r')
line = f.readline()
while line:
# print(line.strip())
line = f.readline()
if len(line)>0:
data = json.loads(line)
if "gp" in data.keys():
val1 = data["ts"]
val2 = data["gp"]
val3 = data["l"]
val4 = data["s"]
writer.writerow([val1,val2,val3,val4])
# print(data)
f.close()
# 起こりそうな例外をキャッチ
except FileNotFoundError as e:
print(e)
except csv.Error as e:
print(e)
print("successfully completed!!")