-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdp_node_identify_sequences.m
executable file
·146 lines (101 loc) · 4.37 KB
/
dp_node_identify_sequences.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
classdef dp_node_identify_sequences < dp_node
% searches for files according to pattern in input.nii_path
%
% pattern is given as { {field_name, this_pattern} } where
% this_pattern is an expression going into 'dir'
% xxx: improvements needed, errors here leads to a drop of the
% subject in the reporting, check this
properties
patterns;
multiple_hit_strategy = 'error'; % alt: first, last
name_db = {};
end
methods
function obj = dp_node_identify_sequences(patterns)
if (~iscell(patterns)) || (~iscell(patterns{1}))
error('expected input: {{field_name, pattern}_i}');
end
obj.patterns = patterns;
end
function obj = update(obj, opt, mode)
obj = update@dp_node(obj, opt, mode);
% clear name db at start of each execution
obj.name_db = {};
end
function output = i2o(obj, input)
% make a pass-through to keep all info
output = input;
if (~exist(input.nii_path, 'dir'))
error('nii path does not exist (%s) in node %s', input.nii_path, obj.name);
end
% save all nii files to a db in this node
d = dir(fullfile(input.nii_path, '*.nii.gz'));
for c = 1:numel(d)
obj.name_db{end+1} = d(c).name;
end
f = obj.patterns;
for c = 1:numel(f)
me = [];
try
tmp = msf_find_fns(input.nii_path, f{c}{2}, 1);
% Eliminate files starting with '.'
ind = zeros(size(tmp));
for c2 = 1:numel(tmp)
[~,fn] = fileparts(tmp{c2});
if (numel(fn) == 0), continue; end
ind(c2) = fn(1) ~= '.';
end
tmp = tmp(ind == 1);
if (numel(tmp) == 0)
error('No file found');
elseif (numel(tmp) == 1) %#ok<ISCL>
tmp = tmp{1};
else
switch (obj.multiple_hit_strategy)
case 'error'
error('Multiple files found');
case 'first'
tmp = tmp{1};
case 'last'
tmp = tmp{end};
otherwise
error('Undefined multiple hit strategy');
end
end
catch me
tmp = [];
end
output.(f{c}{1}) = tmp;
% Report helpful information if nothing was found
if (isempty(output.(f{c}{1})))
obj.log(1, '%s: File not found for field %s pattern %s', ...
input.id, ...
f{c}{1}, formattedDisplayText(f{c}{2}));
obj.log(1, '%s: Was searching in: %s', input.id, input.nii_path);
if (~isempty(me))
obj.log(1, '%s: Search error: %s', input.id, me.message);
obj.log(1, '%s: Listing nii files in %s', input.id, input.nii_path);
d2 = dir(fullfile(input.nii_path, '*.nii*'));
for c2 = 1:numel(d2)
obj.log(1, '%s: %s', input.id, d2(c2).name);
end
end
% allow some granularity here, sometimes ok not to
% find data
error('file not found');
end
end
end
function test_pattern(obj, pattern)
% Convert the wildcard to a regular expression.
% This simple conversion replaces '.' with '\.' and '*' with '.*'
rx = ['^', strrep(strrep(pattern, '.', '\.'), '*', '.*'), '$'];
% Test each string in the list
matches = find(~cellfun(@isempty, regexp(obj.name_db, rx)));
% Print them
for c = 1:numel(matches)
fprintf('%i: %s\n', c, obj.name_db{matches(c)});
end
end
end
end