Skip to content

Commit 6395a94

Browse files
author
Daisuke
committed
copy all files
1 parent 2739051 commit 6395a94

File tree

133 files changed

+16854
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+16854
-0
lines changed

2pAnalysis/LoadSyncEpisodeFunction.m

+390
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,390 @@
1+
%% Load ThorSync Episode HDF5 file:
2+
% - all will be exported to base workspace with unique names.
3+
% - Attributes can be any combination of the following:
4+
% [syncDataOut=LoadSyncEpisode(fnam) -- Load the data from the h5 file at the fully qualified path 'fnam'
5+
% -- syncDataOut - Structure containing all of the data sets in the Thorsync dataset
6+
% Note: This version does not allow opening of only part of the sync episode. Any other arguments besides the file name are ignored.
7+
% This is modified for use as a function from LoadSyncFunction.m (c) 2014
8+
% --- Copyright (c) 2018, Thorlabs, Inc. All rights reserved. ---
9+
10+
%% Copyright (c) 2018, Thorlabs, Inc. All rights reserved.
11+
%
12+
% Redistribution and use in source and binary forms, with or without
13+
% modification, are permitted provided that the following conditions are
14+
% met:
15+
%
16+
% * Redistributions of source code must retain the above copyright
17+
% notice, this list of conditions and the following disclaimer.
18+
% * Redistributions in binary form must reproduce the above copyright
19+
% notice, this list of conditions and the following disclaimer in the
20+
% documentation and/or other materials provided with the distribution
21+
%
22+
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23+
% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24+
% THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25+
% PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26+
% CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27+
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28+
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29+
% PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30+
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31+
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32+
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
34+
function [syncDataOut]=LoadSyncEpisodeFunction(fnam, lineNames)
35+
%[syncDataOut]=LoadSyncEpisodeFunction(fnam)
36+
% loads all Thorsync data in a .h5 file specified as fnam
37+
%
38+
% [syncDataOut]=LoadSyncEpisodeFunction(fnam, lineNames)
39+
% loads a subset of Thorsync data specified in lineNames
40+
%
41+
%
42+
% 17/7/20 DS added 2nd input
43+
%% Select one h5 file:
44+
45+
% [filename, pathname] = uigetfile('*.h5', 'Pick a Sync Episode file');
46+
% if isequal(filename,0) || isequal(pathname,0)
47+
% disp('User pressed cancel')
48+
% return
49+
% else
50+
% disp(['User selected ', fullfile(pathname, filename)])
51+
% end
52+
53+
if nargin < 2
54+
lineNames = [];
55+
end
56+
57+
[pathname,name,ext]=fileparts(fnam);
58+
filename=[name ext];
59+
pathname=[pathname filesep];
60+
61+
%% Load params from XML:
62+
clockRate = 20000000;
63+
sampleRate = LoadSyncXML(pathname);
64+
65+
%% Start loading HDF5:
66+
pathandfilename = strcat(pathname,filename);
67+
info = h5info(pathandfilename);
68+
69+
%% Parse input:
70+
props = {'start','length','interval'};
71+
data = {[1,1],[1 Inf],[1 1]};
72+
73+
% if(~isempty(varargin))
74+
% assert(rem(length(varargin),2)==0 && iscellstr(varargin(1:2:end)), 'Inputs failed to conform to expected string-value pair format, eg> ''start'',1');
75+
% %foundProps = intersect(varargin(1:2:end), props);
76+
% IdxCell = cellfun(@(x) strcmpi(x,props),varargin(1:2:end),'UniformOutput',false);
77+
% val = double(cell2mat(varargin(2:2:end)))*sampleRate;
78+
% for i=1:length(val)
79+
% data{cell2mat(IdxCell(i))>0} = [1 val(i)];
80+
% end
81+
% end
82+
83+
%% Read HDF5:
84+
85+
for j=1:length(info.Groups)
86+
for k = 1:length(info.Groups(j).Datasets)
87+
datasetPath = strcat(info.Groups(j).Name,'/',info.Groups(j).Datasets(k).Name);
88+
datasetName = info.Groups(j).Datasets(k).Name;
89+
90+
if ~(strcmp(info.Groups(j).Name,'/Global')) && ~isempty(lineNames) ...
91+
&& ~ismember(info.Groups(j).Datasets(k).Name, lineNames)
92+
continue;
93+
end
94+
datasetName(isspace(datasetName))='_';
95+
datasetValue = h5read(pathandfilename,datasetPath,data{1},data{2},data{3})';
96+
%h5read is inherently slow...particularly when reading over network
97+
98+
% load digital line in binary:
99+
if(strcmp(info.Groups(j).Name,'/DI'))
100+
datasetValue(datasetValue>0) = 1;
101+
end
102+
% create time variable out of gCtr,
103+
% account for 20MHz sample rate:
104+
if(strcmp(info.Groups(j).Name,'/Global'))
105+
datasetValue = double(datasetValue)./clockRate;
106+
datasetName = 'time';
107+
end
108+
assignStr = UniqueName(datasetName);
109+
syncDataOut.(assignStr)=datasetValue;
110+
% assignin('base',assignStr,datasetValue);
111+
end
112+
end
113+
114+
end
115+
116+
117+
function outStr = UniqueName(str)
118+
%% Generate unique name for variable to be exported.
119+
120+
cellfind = @(string)(@(cell_contents)(strcmp(string,cell_contents)));
121+
vars = evalin('base','who');
122+
index = 1;
123+
unique = false;
124+
cmpStr = str;
125+
126+
while (~unique)
127+
ret = cellfun(cellfind(cmpStr),vars);
128+
if(~any(ret))
129+
outStr = cmpStr;
130+
unique = true;
131+
else
132+
cmpStr = strcat(str,num2str(index,'%03d'));
133+
index=index+1;
134+
end
135+
end
136+
137+
end
138+
139+
%% Load ThorSync XML file:
140+
% - input file path, output sample rate.
141+
% --- Copyright (c) 2014, Thorlabs, Inc. All rights reserved. ---
142+
143+
%% Copyright (c) 2014, Thorlabs, Inc. All rights reserved.
144+
%
145+
% Redistribution and use in source and binary forms, with or without
146+
% modification, are permitted provided that the following conditions are
147+
% met:
148+
%
149+
% * Redistributions of source code must retain the above copyright
150+
% notice, this list of conditions and the following disclaimer.
151+
% * Redistributions in binary form must reproduce the above copyright
152+
% notice, this list of conditions and the following disclaimer in the
153+
% documentation and/or other materials provided with the distribution
154+
%
155+
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
156+
% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
157+
% THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
158+
% PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
159+
% CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
160+
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
161+
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
162+
% PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
163+
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
164+
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
165+
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
166+
167+
function sampleRate = LoadSyncXML(varargin)
168+
%% Load dll:
169+
xmlFile = strcat(varargin{1}, 'ThorRealTimeDataSettings.xml');
170+
assert(exist(xmlFile,'file')>0,'ThorRealTimeDataSettings.xml was not found. ');
171+
dataStruct = xml2struct(xmlFile);
172+
173+
if(~isempty(dataStruct))
174+
BrdID = cellfun(@(x) strcmpi(x.Attributes.active,'1'),dataStruct.RealTimeDataSettings.DaqDevices.AcquireBoard);
175+
sampleID = cellfun(@(x) strcmpi(x.Attributes.enable,'1'),dataStruct.RealTimeDataSettings.DaqDevices.AcquireBoard{BrdID}.SampleRate);
176+
sampleRate = dataStruct.RealTimeDataSettings.DaqDevices.AcquireBoard{BrdID}.SampleRate{sampleID>0}.Attributes.rate;
177+
end
178+
179+
end
180+
181+
182+
%% Convert xml file into a MATLAB structure
183+
% [ s ] = xml2struct( file )
184+
%
185+
% A file containing:
186+
% <XMLname attrib1="Some value">
187+
% <Element>Some text</Element>
188+
% <DifferentElement attrib2="2">Some more text</Element>
189+
% <DifferentElement attrib3="2" attrib4="1">Even more text</DifferentElement>
190+
% </XMLname>
191+
%
192+
% Will produce:
193+
% s.XMLname.Attributes.attrib1 = "Some value";
194+
% s.XMLname.Element.Text = "Some text";
195+
% s.XMLname.DifferentElement{1}.Attributes.attrib2 = "2";
196+
% s.XMLname.DifferentElement{1}.Text = "Some more text";
197+
% s.XMLname.DifferentElement{2}.Attributes.attrib3 = "2";
198+
% s.XMLname.DifferentElement{2}.Attributes.attrib4 = "1";
199+
% s.XMLname.DifferentElement{2}.Text = "Even more text";
200+
%
201+
% Please note that the following characters are substituted
202+
% '-' by '_dash_', ':' by '_colon_' and '.' by '_dot_'
203+
%
204+
% Written by W. Falkena, ASTI, TUDelft, 21-08-2010
205+
% Attribute parsing speed increased by 40% by A. Wanner, 14-6-2011
206+
% Added CDATA support by I. Smirnov, 20-3-2012
207+
%
208+
% Modified by X. Mo, University of Wisconsin, 12-5-2012
209+
210+
%% Copyright (c) 2010, Wouter Falkena All rights reserved.
211+
%
212+
% Redistribution and use in source and binary forms, with or without
213+
% modification, are permitted provided that the following conditions are
214+
% met:
215+
%
216+
% * Redistributions of source code must retain the above copyright
217+
% notice, this list of conditions and the following disclaimer.
218+
% * Redistributions in binary form must reproduce the above copyright
219+
% notice, this list of conditions and the following disclaimer in the
220+
% documentation and/or other materials provided with the distribution
221+
%
222+
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
223+
% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
224+
% THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
225+
% PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
226+
% CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
227+
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
228+
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
229+
% PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
230+
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
231+
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
232+
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
233+
234+
function [ s ] = xml2struct( file )
235+
%% Function:
236+
if (nargin < 1)
237+
clc;
238+
help xml2struct
239+
return
240+
end
241+
242+
if isa(file, 'org.apache.xerces.dom.DeferredDocumentImpl') || isa(file, 'org.apache.xerces.dom.DeferredElementImpl')
243+
% input is a java xml object
244+
xDoc = file;
245+
else
246+
%check for existance
247+
if (exist(file,'file') == 0)
248+
%Perhaps the xml extension was omitted from the file name. Add the
249+
%extension and try again.
250+
if (isempty(strfind(file,'.xml')))
251+
file = [file '.xml'];
252+
end
253+
254+
if (exist(file,'file') == 0)
255+
error(['The file ' file ' could not be found']);
256+
end
257+
end
258+
%read the xml file
259+
xDoc = xmlread(file);
260+
end
261+
262+
%parse xDoc into a MATLAB structure
263+
s = parseChildNodes(xDoc);
264+
265+
end
266+
267+
% ----- Subfunction parseChildNodes -----
268+
function [children,ptext,textflag] = parseChildNodes(theNode)
269+
% Recurse over node children.
270+
children = struct;
271+
ptext = struct; textflag = 'Text';
272+
if hasChildNodes(theNode)
273+
childNodes = getChildNodes(theNode);
274+
numChildNodes = getLength(childNodes);
275+
276+
for count = 1:numChildNodes
277+
theChild = item(childNodes,count-1);
278+
[text,name,attr,childs,textflag] = getNodeData(theChild);
279+
280+
if (~strcmp(name,'#text') && ~strcmp(name,'#comment') && ~strcmp(name,'#cdata_dash_section'))
281+
%XML allows the same elements to be defined multiple times,
282+
%put each in a different cell
283+
if (isfield(children,name))
284+
if (~iscell(children.(name)))
285+
%put existsing element into cell format
286+
children.(name) = {children.(name)};
287+
end
288+
index = length(children.(name))+1;
289+
%add new element
290+
children.(name){index} = childs;
291+
if(~isempty(fieldnames(text)))
292+
children.(name){index} = text;
293+
end
294+
if(~isempty(attr))
295+
children.(name){index}.('Attributes') = attr;
296+
end
297+
else
298+
%add previously unknown (new) element to the structure
299+
children.(name) = childs;
300+
if(~isempty(text) && ~isempty(fieldnames(text)))
301+
children.(name) = text;
302+
end
303+
if(~isempty(attr))
304+
children.(name).('Attributes') = attr;
305+
end
306+
end
307+
else
308+
ptextflag = 'Text';
309+
if (strcmp(name, '#cdata_dash_section'))
310+
ptextflag = 'CDATA';
311+
elseif (strcmp(name, '#comment'))
312+
ptextflag = 'Comment';
313+
end
314+
315+
%this is the text in an element (i.e., the parentNode)
316+
if (~isempty(regexprep(text.(textflag),'[\s]*','')))
317+
if (~isfield(ptext,ptextflag) || isempty(ptext.(ptextflag)))
318+
ptext.(ptextflag) = text.(textflag);
319+
else
320+
%what to do when element data is as follows:
321+
%<element>Text <!--Comment--> More text</element>
322+
323+
%put the text in different cells:
324+
% if (~iscell(ptext)) ptext = {ptext}; end
325+
% ptext{length(ptext)+1} = text;
326+
327+
%just append the text
328+
ptext.(ptextflag) = [ptext.(ptextflag) text.(textflag)];
329+
end
330+
end
331+
end
332+
333+
end
334+
end
335+
end
336+
337+
% ----- Subfunction getNodeData -----
338+
function [text,name,attr,childs,textflag] = getNodeData(theNode)
339+
% Create structure of node info.
340+
341+
%make sure name is allowed as structure name
342+
name = toCharArray(getNodeName(theNode))';
343+
name = strrep(name, '-', '_dash_');
344+
name = strrep(name, ':', '_colon_');
345+
name = strrep(name, '.', '_dot_');
346+
347+
attr = parseAttributes(theNode);
348+
if (isempty(fieldnames(attr)))
349+
attr = [];
350+
end
351+
352+
%parse child nodes
353+
[childs,text,textflag] = parseChildNodes(theNode);
354+
355+
if (isempty(fieldnames(childs)) && isempty(fieldnames(text)))
356+
%get the data of any childless nodes
357+
% faster than if any(strcmp(methods(theNode), 'getData'))
358+
% no need to try-catch (?)
359+
% faster than text = char(getData(theNode));
360+
text.(textflag) = toCharArray(getTextContent(theNode))';
361+
end
362+
363+
end
364+
365+
% ----- Subfunction parseAttributes -----
366+
function attributes = parseAttributes(theNode)
367+
% Create attributes structure.
368+
369+
attributes = struct;
370+
if hasAttributes(theNode)
371+
theAttributes = getAttributes(theNode);
372+
numAttributes = getLength(theAttributes);
373+
374+
for count = 1:numAttributes
375+
%attrib = item(theAttributes,count-1);
376+
%attr_name = regexprep(char(getName(attrib)),'[-:.]','_');
377+
%attributes.(attr_name) = char(getValue(attrib));
378+
379+
%Suggestion of Adrian Wanner
380+
str = toCharArray(toString(item(theAttributes,count-1)))';
381+
k = strfind(str,'=');
382+
attr_name = str(1:(k(1)-1));
383+
attr_name = strrep(attr_name, '-', '_dash_');
384+
attr_name = strrep(attr_name, ':', '_colon_');
385+
attr_name = strrep(attr_name, '.', '_dot_');
386+
attributes.(attr_name) = str((k(1)+2):(end-1));
387+
end
388+
end
389+
end
390+

0 commit comments

Comments
 (0)