-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdz_runFirst.m
More file actions
142 lines (112 loc) · 3.62 KB
/
dz_runFirst.m
File metadata and controls
142 lines (112 loc) · 3.62 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
function dz_runFirst(curdir)
%%By Diksha Zutshi
%run first: to create a SpikeCleaner folder with the required files.
rootdir=fileparts(mfilename('fullpath'));
addpath(genpath(rootdir));
if nargin < 1 || isempty(curdir)
curdir=pwd;
end
requiredFiles={
'amplitudes.npy'
'channel_map.npy'
'channel_positions.npy'
'params.py'
'spike_clusters.npy'
'pc_features.npy'
'spike_times.npy'
'templates.npy'
'spike_templates.npy'
'whitening_mat.npy'
'whitening_mat_inv.npy'
'similar_templates.npy'
'templates_ind.npy'
'channel_shanks.npy'
'pc_feature_ind.npy'
};
%%create SpikeCleaner folder
outputDir=fullfile(curdir,'SpikeCleaner');
if ~isfolder(outputDir)
mkdir(outputDir);
end
%%check for requiredFiles in curdir and copy to SpikeCleaner folder
missing = {};
for i = 1:numel(requiredFiles)
if ~isfile(fullfile(curdir, requiredFiles{i}))
missing{end+1} = requiredFiles{i};
end
end
if isempty(missing)
for i=1:numel(requiredFiles)
copyfile(fullfile(curdir,requiredFiles{i}), fullfile(outputDir, requiredFiles{i}));
end
disp('All files have been copied to SpikeCleaner');
end
%%if not present look for them in all present subfolders
for i=1:numel(requiredFiles)
fname=requiredFiles{i};
hits=dir(fullfile(curdir,'**',fname)); %%recursively looking inside subfolders
if isempty(hits)
warning('Couldnt find these files under current directory:%s',fname);
end
%%if multiple matches are found then keep the latest
[~,idx]=max([hits.datenum]);
src=fullfile(hits(idx).folder,hits(idx).name);
%%copy these files to SpikeCleaner folder
copyfile(src,fullfile(outputDir,fname));
fprintf('Copied %s from %s /n',fname,hits(idx).folder);
end
%%copy the dat file as well SpikeCleaner:: just get the path
datfile = dir(fullfile(curdir, '*.dat'));
[~,basename]=fileparts(curdir);
matchIdx=find(strcmp({datfile.name}, [basename '.dat']),1);
fullname=[basename '.dat'];
if isempty(matchIdx)
matchIdx = find(strcmp({datfile.name}, 'temp_wh.dat'), 1);
end
if isempty(matchIdx)
warning('No matching .dat file found.');
%check subfolders
hits=dir(fullfile(curdir,'**',fullname)); %%recursively looking inside subfolders
if isempty(hits)
warning('Couldnt find these files under current directory:%s',fullname);
else
%%copy file to SpikeCleaner folder
src=fullfile(hits.folder,hits.name);
end
else
src=fullfile(curdir, datfile(matchIdx).name);
end
disp("All files have been copied");
%%make a parameters.mat file similar to a rez.mat
%needs sampling rate,num of channels,name of the animal from basename
%
parameters=struct();
parameterFile=fullfile(outputDir,'parameters.mat');
params=fullfile(outputDir,'params.py');
contents=fileread(params);
%Renaming the dat file
newPath=src;
lines = regexp(contents, '\r?\n', 'split')';
for i=1:length(lines)
if contains(lines{i},'dat_path')
lines{i}=sprintf("dat_path= '%s' ",newPath);
lines{i} = sprintf("dat_path = '%s'", newPath);
end
end
%parsing animal name
parameters.animal=basename;
%parsing channels
chanMatch=regexp(contents,'n_channels_dat\s*=\s*(\d+)','tokens','once');
parameters.nChannels=str2double(chanMatch{1});
%parsing sampling rate
fsmatch=regexp(contents,'sample_rate\s*=\s*([0-9\.]+)','tokens','once');
parameters.samplingRate=str2double(fsmatch{1});
%%writing back to params.py
fid=fopen(params,'w');
for i = 1:length(lines)
fprintf(fid, '%s\n', lines{i});
end
fclose(fid);
save(parameterFile,'parameters');%saving the structure
disp('parameters.mat created in SpikeCleaner directory in the animal folder');
end