-
Notifications
You must be signed in to change notification settings - Fork 2
/
pointmatch_task.m
160 lines (141 loc) · 5.71 KB
/
pointmatch_task.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
function varargout = pointmatch_task(filename,from,to,numcores,exitcode)
%% deploys pointmatch function on array task
if nargin==0
brain = '2017-10-31';tag='';
runlocal = 0;
deployment(brain,runlocal);
elseif nargin==1
disp('Creating *sh file for cluster. File will be here (might take upto 5min to create the file):')
brain = filename;
runlocal = 0;
deployment(brain,runlocal);
return
elseif nargin==2 % run on local machine
disp('RUNNING ON LOCAL MACHINE< MIGHT TAKE A WHILE')
brain = filename;
runlocal = from;
deployment(brain,runlocal);
return
end
if ischar(from);from = eval(from);end
if ischar(to);to = eval(to);end
if ischar(numcores);numcores = eval(numcores);end
if ischar(exitcode);exitcode = eval(exitcode); end
varargout{1} = exitcode;
% first get file list
fid = fopen(filename);
targetlist = textscan(fid,'%s %s %s %s %s %f %f %f %s %f');
fclose(fid);
parpool(numcores)
parfor idx=from:to
tile1 = targetlist{1}{idx};
tile2 = targetlist{2}{idx};
acqusitionfolder1 = targetlist{3}{idx};
acqusitionfolder2 = targetlist{4}{idx};
outfold =targetlist{5}{idx};
pixshift = [targetlist{6}(idx) targetlist{7}(idx) targetlist{8}(idx)];
ch = targetlist{9}{idx};
maxnumofdesc = targetlist{10}(idx);
pointmatch(tile1,tile2,acqusitionfolder1,acqusitionfolder2,outfold,pixshift,ch,maxnumofdesc,exitcode)
end
delete(gcp('nocreate'))
end
function deployment(brain,runlocal)
%%
addpath(genpath('./functions'))
% addpath(genpath('./common'))
matlab_cluster_path = '/usr/local/matlab-2017a';
compiledfunc = '/groups/mousebrainmicro/home/base/CODE/MATLAB/compiledfunctions/pointmatch_task/pointmatch_task';
[mypath,mycomp] = fileparts(compiledfunc);
taskwrapper = fullfile(pwd,'cluster2.sh');
taskscript = fullfile(mypath,sprintf('run_%s.sh',mycomp));
if ~exist(fileparts(compiledfunc),'dir');
mkdir(fileparts(compiledfunc));
mfilename_ = mfilename('fullpath');
unix(sprintf('mcc -m -v -R -I %s -d %s -a %s',mfilename_,fileparts(compiledfunc),fullfile(fileparts(mfilename_),'functions')))
%,fullfile(fileparts(mfilename_),'common')
end
inputfolder = sprintf('/groups/mousebrainmicro/mousebrainmicro/data/%s/Tiling',brain);
experimentfolder = sprintf('/nrs/mouselight/cluster/classifierOutputs/%s',brain);
descriptorfolder = fullfile(experimentfolder,'classifier_output');
matfolder = fullfile(experimentfolder,'matfiles/');
scopefile = fullfile(matfolder,'scopeloc.mat');
if ~exist(matfolder,'dir')
mkdir(matfolder)
end
if exist(scopefile,'file')
load(scopefile,'scopeloc')
else
newdash = 1; % set this to 1 for datasets acquired after 160404
[scopeloc] = getScopeCoordinates(inputfolder,newdash);% parse from acqusition files
save(fullfile(matfolder,'scopeloc.mat'),'neighbors','scopeloc','imsize_um','experimentfolder','inputfolder')
end
[neighbors] = buildNeighbor(scopeloc.gridix(:,1:3)); %[id -x -y +x +y -z +z] format
directionMap = containers.Map({'-X','-Y','X','Y','-Z','Z'},[ 2, 3, 4, 5, 6, 7]);
%%
directions = 'Z';
ch='1';
maxnumofdesc = 10e3;
checkversion=1;
if 0
pixinit = zeros(size(neighbors,1),3);
nummatches = zeros(size(neighbors,1),1);
else
% load finished tile matches. find badly matched or missing tile pairs
[regpts,featmap] = loadMatchedFeatures(scopeloc,descriptorfolder,directions,checkversion);
% initalize missing tiles based on knn
numthr = 50;
[pixinit,nummatches] = initTiles(featmap,directions,scopeloc,numthr);
sum(nummatches)
end
badtiles = nummatches<numthr & ~isnan(neighbors(:,directionMap(directions)));
%%
% generate a txt file with all tile pairs to be matched with optional shift
% values
% (filename,from,to,numcores,exitcode)
outlistfile = fullfile(pwd,'shfiles',sprintf('outlistfile_%s_%s.txt',brain,date));
if ~runlocal;fid = fopen(outlistfile,'w');end
parfor ii = 1:length(badtiles)
if ~badtiles(ii)
continue
end
tile1 = fullfile(descriptorfolder,scopeloc.relativepaths{ii});
acqusitionfolder1 = fileparts(scopeloc.filepath{ii});
iineig = neighbors(ii,directionMap(directions));
tile2 = fullfile(descriptorfolder,scopeloc.relativepaths{iineig});
acqusitionfolder2 = fileparts(scopeloc.filepath{iineig});
outfold =tile1;
if runlocal
pointmatch(tile1,tile2,acqusitionfolder1,acqusitionfolder2,outfold,pixinit(ii,:),ch,maxnumofdesc,0);
else
fprintf(fid,'%s %s %s %s %s %f %f %f %s %f\n',tile1,tile2,acqusitionfolder1,acqusitionfolder2,outfold,pixinit(ii,:),ch,maxnumofdesc);
end
end
if ~runlocal;fclose(fid);end
%%
if ~runlocal
%% task script (filename,from,to,numcores,exitcode)
%find number of random characters to choose from and %specify length of random string to generate
s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';numRands = length(s);sLength = 10;
%-o /dev/null
esttime = 30*60;
numcores = 16;
exitcode = 0;
tottasks = sum(badtiles(:));
intervals = round(linspace(0,tottasks,round(tottasks/numcores)));
mkdir(fullfile(pwd,'shfiles'))
myfile = fullfile(pwd,'shfiles',sprintf('featmatchrun_%s_%s.sh',brain,date))
if ~runlocal; fid = fopen(myfile,'w'); end
for ii = 1:length(intervals)-1
from = intervals(ii)+1;
to = intervals(ii+1);
cmd = sprintf('''%s %s %s %s %d %d %d %d''',taskwrapper,taskscript,matlab_cluster_path,outlistfile,from,to,numcores,exitcode);
%generate random string
randString = s( ceil(rand(1,sLength)*numRands) );
name = sprintf('zm_%05d-%s',ii,randString);
mysub = sprintf('bsub -J %s -n%d -R"affinity[core(1)]" -We %d -o /dev/null %s\n',name,numcores,esttime/60,cmd);
fwrite(fid,mysub);
end
if ~runlocal; fclose(fid); unix(sprintf('chmod +x %s',myfile)); disp(myfile);end
end
end