|
a |
|
b/functions/functions_DBProc/balanceDB.m |
|
|
1 |
function [filesProc, errorC] = balanceDB(files, maxNumInd, maxNumSampleInd) |
|
|
2 |
|
|
|
3 |
%init |
|
|
4 |
errorC = 0; |
|
|
5 |
filesProc = files; |
|
|
6 |
|
|
|
7 |
%loop on files to compute num of samples for each ind |
|
|
8 |
numSamplePerInd = zeros(numel(filesProc), 1); %larger init to be sure |
|
|
9 |
for i = 1 : numel(filesProc) |
|
|
10 |
filename = filesProc(i).name; |
|
|
11 |
|
|
|
12 |
%class |
|
|
13 |
[C, ~] = strsplit(filename, {'_', '.'}); |
|
|
14 |
ind = str2double(C{2}) + 1; |
|
|
15 |
|
|
|
16 |
numSamplePerInd(ind) = numSamplePerInd(ind) + 1; |
|
|
17 |
end %for i |
|
|
18 |
|
|
|
19 |
%remove excess |
|
|
20 |
im = find(numSamplePerInd, 1, 'last'); |
|
|
21 |
numSamplePerInd(im+1 : end) = []; |
|
|
22 |
|
|
|
23 |
%remove individual without minimum number of samples |
|
|
24 |
indexrem = []; |
|
|
25 |
for i = 1 : numel(filesProc) |
|
|
26 |
filename = filesProc(i).name; |
|
|
27 |
|
|
|
28 |
%class |
|
|
29 |
[C, ~] = strsplit(filename, {'_', '.'}); |
|
|
30 |
ind = str2double(C{2}) + 1; |
|
|
31 |
|
|
|
32 |
if numSamplePerInd(ind) < maxNumSampleInd |
|
|
33 |
indexrem = [indexrem i]; |
|
|
34 |
end %if numSamplePerInd(ind) == 1 |
|
|
35 |
end %for i |
|
|
36 |
filesProc(indexrem) = []; |
|
|
37 |
|
|
|
38 |
%check if there are enough |
|
|
39 |
if numel(filesProc) < maxNumInd |
|
|
40 |
errorC = -1; |
|
|
41 |
return; |
|
|
42 |
end %if numel(filesProc) < maxNumInd |
|
|
43 |
|
|
|
44 |
%remove excess individuals |
|
|
45 |
indexrem = []; |
|
|
46 |
allInd = []; |
|
|
47 |
for i = 1 : numel(filesProc) |
|
|
48 |
filename = filesProc(i).name; |
|
|
49 |
|
|
|
50 |
%class |
|
|
51 |
[C, ~] = strsplit(filename, {'_', '.'}); |
|
|
52 |
ind = str2double(C{2}) + 1; |
|
|
53 |
|
|
|
54 |
if numel(find(allInd == ind)) == 0 |
|
|
55 |
allInd = [allInd ind]; |
|
|
56 |
end %if numel |
|
|
57 |
|
|
|
58 |
if numel(allInd) > maxNumInd |
|
|
59 |
%we can remove the others |
|
|
60 |
allInd(end) = []; |
|
|
61 |
indexrem = i : numel(filesProc); |
|
|
62 |
break; |
|
|
63 |
end %if numInd > maxNumInd |
|
|
64 |
end %for i |
|
|
65 |
filesProc(indexrem) = []; |
|
|
66 |
|
|
|
67 |
%discard samples in excess |
|
|
68 |
indexrem = []; |
|
|
69 |
numSamplePerInd = zeros(numel(filesProc), 1); %larger init to be sure |
|
|
70 |
for i = 1 : numel(filesProc) |
|
|
71 |
filename = filesProc(i).name; |
|
|
72 |
|
|
|
73 |
%class |
|
|
74 |
[C, ~] = strsplit(filename, {'_', '.'}); |
|
|
75 |
ind = str2double(C{2}) + 1; |
|
|
76 |
|
|
|
77 |
numSamplePerInd(ind) = numSamplePerInd(ind) + 1; |
|
|
78 |
if numSamplePerInd(ind) > maxNumSampleInd |
|
|
79 |
indexrem = [indexrem i]; |
|
|
80 |
end %if numSamplePerInd(ind) > maxNumSampleInd |
|
|
81 |
end %for i |
|
|
82 |
filesProc(indexrem) = []; |
|
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
|