forked from nubs01/TrodesExtractionGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRN_createAllMatclustFiles.m
More file actions
executable file
·190 lines (159 loc) · 5.43 KB
/
RN_createAllMatclustFiles.m
File metadata and controls
executable file
·190 lines (159 loc) · 5.43 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
function RN_createAllMatclustFiles(mjc)
%createAllMatclustFiles()
%This function will create matclust files for all ntrodes in one recording
%session. It assumes that there is one '*.spikes' folder in the current working directory
%that contains the extracted spike information for all ntrodes. Use
%'extractSpikeBinaryFiles.m' to create these files from the raw .rec file.
currDir = pwd;
filesInDir = dir;
spikesFolder = [];
for i=3:length(filesInDir)
if filesInDir(i).isdir && ~isempty(strfind(filesInDir(i).name,'.spikes'))
spikesFolder = filesInDir(i).name;
break;
end
end
dotLoc = strfind(spikesFolder,'.');
baseName = spikesFolder(1:dotLoc(end)-1);
if isempty(spikesFolder)
error('Spikes folder not found in this directory.');
end
ranges = getEpochs(1); %assumes that there is at least a 1-second gap in data between epochs
names{1} = '1 All points';
for r = 1:31
if (r <= size(ranges,1))
st = timetrans(ranges(r,1),1,1);
st = st{1};
et = timetrans(ranges(r,2),1,1);
et = et{1};
names{r+1,1} = [num2str(r+1),' e',num2str(r),' ', st,'-', et];
else
names{r+1,1} = num2str(r+1);
end
end
ranges = [[ranges(1,1) ranges(end,2)];ranges];
cd(spikesFolder);
datFiles = dir('*.spikes_*.dat');
if (isempty(datFiles))
cd(currDir);
error('No spike binary files found in spikes folder.');
end
cd(currDir);
matclustDir = [baseName,'.matclust'];
mkdir(matclustDir);
matclustDir = fullfile(currDir,matclustDir);
% Here we set a flag if the user has a file indicative of owning the
% parallel computing toolbox ... matclust file creation happens much faster
% with one matlab instance per core
if exist('parfeval.m','file')
use_paralleltoolbox = true;
job_count = 0;
if ~exist('mjc','var')
mjc = '';
end
while isempty(mjc)
mjc = inputdlg('How many parallel jobs would you like to run while generating matclust files? [1-8]','Max Job Count',1,{'8'});
mjc = str2num(mjc);
if ~isempty(mjc)
if ~(mjc>=1 && mjc<=8 && fix(mjc)==mjc)
h = msgbox('Please choose a valid integer between 1 and 8.');
waitfor(h);
mjc = '';
end
end
end
max_job_count = mjc;
poolStartup;
p=gcp;
else
use_paralleltoolbox = false;
end
cd(spikesFolder);
datFileInd = 0;
while datFileInd < length(datFiles)
datFileInd = datFileInd + 1;
disp(datFiles(datFileInd).name);
if use_paralleltoolbox
try
job_count = job_count + 1;
jobs(job_count) = ...
parfeval(p,@createMatclustFile,0,datFiles(datFileInd).name,matclustDir);
if mod(job_count,max_job_count) == 0
fprintf('Waiting for %d parallel matlab workers to finish processing ...\n',max_job_count);
wait(jobs(job_count-max_job_count+1:end));
delete(jobs(job_count-max_job_count+1:end));
lastValid = datFileInd;
end
catch exception
warning('Likely ran out of memory ... lowering max job count');
for j = 1:numel(jobs)
if ~isempty(jobs) && jobs(j).isvalid && ...
isequal(jobs(j).State,'finished')
lastValid = j; delete(jobs(j));
end
end
job_count = 0;
datFileInd = max(datFileInd - max_job_count,lastValid);
max_job_count = ceil(max_job_count/2);
clear('jobs'); p=gcp;
end
else
createMatclustFile(datFiles(datFileInd).name,matclustDir);
end
end
try
wait(jobs(length(jobs)),'finished');
end
cd(matclustDir);
save times ranges names;
cd(currDir);
function out = timetrans(times,UnitsPerSec,dir)
%out = timestrans(times, UnitsPerSec, dir)
%transforms time inputs from seconds to hours:minutes:seconds or vice versa
%TIMES is times that you want to transform, ie [60 3600] seconds or
%[0:01:00 1:00:00] in hours:min:secs
%UnitsPerSec is units in each sec
%dir is 1 if going from seconds to hours:min:sec, 2 if going from
%hrs:min:sec to sec
if (dir==1)
for i = 1:length(times)
if (times(i) > 0)
hours = floor(times(i)/(60*60*UnitsPerSec));
minutes = floor(times(i)/(60*UnitsPerSec))-(hours*60);
seconds = floor(times(i)/(UnitsPerSec))-(hours*60*60)-(minutes*60);
else
hours = abs(ceil(times(i)/(60*60*UnitsPerSec)));
minutes = abs(ceil(times(i)/(60*UnitsPerSec))+(hours*60));
seconds = abs(ceil(times(i)/(UnitsPerSec))+(hours*60*60)+(minutes*60));
end
if (minutes<10)
tempmin = ['0',num2str(minutes)];
else
tempmin = [num2str(minutes)];
end
if (seconds<10)
tempseconds = ['0',num2str(seconds)];
else
tempseconds = [num2str(seconds)];
end
out{i,1} = [num2str(hours),':',tempmin,':',tempseconds];
if (times(i)<0)
out{i,1} = ['-',out{i,1}];
end
end
elseif (dir==2)
for i = 1:length(times)
t = [0 0 0 0 0 0];
temptime = times{i};
colons = findstr(temptime,':');
startind = length(temptime);
count = 6;
for j = length(colons):-1:1
t(count) = str2num(temptime((colons(j)+1):startind));
startind = colons(j)-1;
count = count-1;
end
t(count) = str2num(temptime(1:colons(1)-1));
out(i,1) = etime(t,[0 0 0 0 0 0])*UnitsPerSec;
end
end