[1422d3]: / functions / functions_DBProc / balanceDB.m

Download this file

86 lines (66 with data), 2.1 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
function [filesProc, errorC] = balanceDB(files, maxNumInd, maxNumSampleInd)
%init
errorC = 0;
filesProc = files;
%loop on files to compute num of samples for each ind
numSamplePerInd = zeros(numel(filesProc), 1); %larger init to be sure
for i = 1 : numel(filesProc)
filename = filesProc(i).name;
%class
[C, ~] = strsplit(filename, {'_', '.'});
ind = str2double(C{2}) + 1;
numSamplePerInd(ind) = numSamplePerInd(ind) + 1;
end %for i
%remove excess
im = find(numSamplePerInd, 1, 'last');
numSamplePerInd(im+1 : end) = [];
%remove individual without minimum number of samples
indexrem = [];
for i = 1 : numel(filesProc)
filename = filesProc(i).name;
%class
[C, ~] = strsplit(filename, {'_', '.'});
ind = str2double(C{2}) + 1;
if numSamplePerInd(ind) < maxNumSampleInd
indexrem = [indexrem i];
end %if numSamplePerInd(ind) == 1
end %for i
filesProc(indexrem) = [];
%check if there are enough
if numel(filesProc) < maxNumInd
errorC = -1;
return;
end %if numel(filesProc) < maxNumInd
%remove excess individuals
indexrem = [];
allInd = [];
for i = 1 : numel(filesProc)
filename = filesProc(i).name;
%class
[C, ~] = strsplit(filename, {'_', '.'});
ind = str2double(C{2}) + 1;
if numel(find(allInd == ind)) == 0
allInd = [allInd ind];
end %if numel
if numel(allInd) > maxNumInd
%we can remove the others
allInd(end) = [];
indexrem = i : numel(filesProc);
break;
end %if numInd > maxNumInd
end %for i
filesProc(indexrem) = [];
%discard samples in excess
indexrem = [];
numSamplePerInd = zeros(numel(filesProc), 1); %larger init to be sure
for i = 1 : numel(filesProc)
filename = filesProc(i).name;
%class
[C, ~] = strsplit(filename, {'_', '.'});
ind = str2double(C{2}) + 1;
numSamplePerInd(ind) = numSamplePerInd(ind) + 1;
if numSamplePerInd(ind) > maxNumSampleInd
indexrem = [indexrem i];
end %if numSamplePerInd(ind) > maxNumSampleInd
end %for i
filesProc(indexrem) = [];