-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildExptComments.m
executable file
·177 lines (161 loc) · 4.93 KB
/
BuildExptComments.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
function [Comments, ExptList, CellInfo] = BuildExptComments(path, varargin)
%BuildExptComments(path, varargin) Extracts comment lines from
% Spike2.mat files and puts them in path/ExptComments.mat
% By default does nothing in ExptComments.mat exists.
rebuild = 0;
showcomments = 0;
CellInfo = [];
j = 1;
while j <=length(varargin)
if strncmpi(varargin{j},'rebuild',4)
rebuild = 1;
elseif strncmpi(varargin{j},'show',4)
showcomments = 1;
end
j = j+1;
end
if ~isdir(path) && exist(path,'file')
[path, filename] = fileparts(path);
end
outname = [path '/ExptComments.mat'];
if exist(outname) && ~rebuild
load(outname);
else
Comments = [];
ExptList = [];
if ~isdir(path)
fprintf('No Folder %s\n',path);
return;
end
d = mydir([path '/*idx.mat']);
nc = 1;
ne = 1;
pes = [];
allxy = [];
for j = 1:length(d)
clear Expt;
load(d(j).name);
if exist('Expt','var') && isfield(Expt,'Comments')
Expt.Header.builddate = double(d(j).datenum);
if isfield(Expt.Comments,'Peninfo') && isfield(Expt.Comments.Peninfo,'user')
user = Expt.Comments.Peninfo.user;
else
user = '??';
end
for k = 1:length(Expt.Comments.text)
if strncmp(Expt.Comments.text{k},' Electrode',7)
elseif strncmp(Expt.Comments.text{k},'Electrode',7)
elseif strncmp(Expt.Comments.text{k},'Experimenter Not Set',7)
elseif strncmp(Expt.Comments.text{k},'rf=',3)
elseif strncmp(Expt.Comments.text{k},'cm=NotSet',9)
elseif strncmp(Expt.Comments.text{k},'cm=back=',8)
else
Comments(nc).text = Expt.Comments.text{k};
Comments(nc).time = ConvertTime(Expt, Expt.Comments.times(k));
Comments(nc).depth = FindElectrodeDepth(Expt, Expt.Comments.times(k));
Comments(nc).user = user;
nc = nc+1;
end
end
if ~exist('ExptList','var') && isfield(Expt,'ExptList')
Expts = ReadExptDir(path);
for k = 1:length(Expts)
ExptList(k).expname = Expt2Name(Expts{k});
ExptList(k).start = Expts{k}.Header.trange(1);
ExptList(k).end = Expts{k}.Header.trange(2);
end
end
for k = 1:length(ExptList)
ExptsList(ne).name = ExptList(k).expname;
ExptsList(ne).start = ConvertTime(Expt, ExptList(k).start);
ExptsList(ne).end = ConvertTime(Expt, ExptList(k).end);
ExptsList(ne).depth(1) = FindElectrodeDepth(Expt, ExptList(k).start);
ExptsList(ne).depth(2) = FindElectrodeDepth(Expt, ExptList(k).end);
ExptsList(ne).suffix = GetExptNumber(Expt);
ExptsList(ne).ntrials = CountTrials(Expt, [ExptList(k).start ExptList(k).end]);
ne = ne+1;
end
if isfield(Expt.Trials,'rf') && size(Expt.Trials.rf,2) > 5
pes(j) = median(Expt.Trials.rf(:,6));
if size(Expt.Trials.rf,2) > 7
allxy(j,1) = median(Expt.Trials.rf(:,7));
allxy(j,2) = median(Expt.Trials.rf(:,8));
end
end
end
end
if ~isempty(pes)
pe = median(pes);
penxy = median(allxy);
logfile = sprintf('/b/bgc/anal/%s/pens/pen%d.log',GetMonkeyName(path),pe);
if ~exist(logfile)
logfile = sprintf('/b/data/%s/pens/pen%d.log',GetMonkeyName(path),pe);
end
pen = ReadPen(logfile,'noplot');
else
pen = [];
pe = 0;
penxy = [NaN NaN];
end
if isempty(Comments)
exptcomments = {};
else
exptcomments = {Comments.text};
end
if isfield(pen, 'comments')
for j = 1:length(pen.comments)
if ~strncmp(pen.comments{j},'NotSet',6) && isempty(strcmp(pen.comments{j},exptcomments))
Comments(end+1).text = pen.comments{j};
Comments(end).time = pen.date(pen.cmtime(j));
Comments(end).depth = pen.depths(pen.cmtime(j));
end
end
end
if ~isempty(Comments)
[a, b] = sort([Comments.time]);
Comments = Comments(b);
end
ExptList = ExptsList;
if ~isempty(ExptList)
[a, b] = sort([ExptList.start]);
ExptList = ExptList(b);
end
[a,b,c,d] = GetMonkeyName(path);
txtfile = [path '/' a c 'Add.txt'];
d = dir(txtfile);
if ~isempty(d)
txt = scanlines(txtfile);
id = find(strncmp('-2',txt,2));
for j = 1:length(id)
Comments(end+1).text = txt{id(j)}(3:end);
Comments(end).user = 'txt';
Comments(end).time = d.datenum;
end
end
CellInfo.pen =[pe penxy];
save(outname,'Comments','ExptList','CellInfo');
end
function truet = ConvertTime(Expt, t)
%t in seconds
t = double(t);
if isfield(Expt.Header,'CreationDate')
truet = Expt.Header.CreationDate + t ./(24 .* 60 .* 60 .* 10000);
else
truet = Expt.Header.builddate + t ./(24 .* 60 .* 60 .* 10000);
end
function d = FindElectrodeDepth(Expt, t)
bid = find(Expt.Trials.Start < t);
aid = find(Expt.Trials.Start > t);
if isempty(aid) && isempty(bid)
id = 1;
elseif isempty(aid)
id = bid(end);
elseif isempty(bid)
id = aid(1);
else
id = bid(end);
end
d = Expt.Trials.ed(id);
function nt = CountTrials(Expt, t)
bid = find(Expt.Trials.Start > t(1) & Expt.Trials.Start < t(2));
nt = sum(Expt.Trials.Result(bid) > 0);