Skip to content

Commit 93ea052

Browse files
committed
Final version, with code and illustrations
1 parent fd70b8d commit 93ea052

File tree

98 files changed

+43047
-1
lines changed

Some content is hidden

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

98 files changed

+43047
-1
lines changed
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2+
% Desarrollado por | Developed by: %
3+
% University Carlos III of Madrid PhD Researchers %
4+
% Daniel Amigo Herrero mailto: [email protected] %
5+
% David Sanchez Pedroche mailto: [email protected] %
6+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7+
8+
%% Cuenta cuantos ficheros, lineas y tipos de barco / maniobra hay en cada carpeta dentro de Data
9+
% It counts how many files, entries and ship type / maneuver type are on each subfolder within Data
10+
function [] = countAllFolders()
11+
12+
fileWrite = "folderAnalysis.csv";
13+
fid = fopen(fileWrite, 'w'); % w clears the file (if it exists). Open the file.
14+
fprintf(fid, strcat('numFiles', '; ', 'numEntries', '; ', "Cargo", '; ', "Fishing", '; ', "Passenger", '; ', "Tanker", '; ', "Undefined", '; ', "Other", '; ', ...
15+
"EngagedFishing", '; ', "Restricted", '; ', "Sailing", '; ', "Engine", '; ', "Unknown value", '; ', "Other", '; ', ...
16+
'completePath', '; ', 'pathPart1', '; ', 'pathPart2', '; ', 'pathPart3', '; ', 'pathPart4'));
17+
fprintf(fid, "\n");
18+
countAllFoldersRecursive("Data", fileWrite);
19+
end
20+
21+
22+
%% Abre de forma recursiva todas las sub-carpetas. Apunta en fileWrite el nombre de carpeta, numero de ficheros, entradas, y cada tipo de barco y maniobra
23+
% Open recursively all subfolders. Write at the fileWrite the folder name, number of files, entries and each shiptype and maneuvertype.
24+
function [] = countAllFoldersRecursive(folder, fileWrite)
25+
26+
allFiles = dir(strcat(folder, "/*.*"));
27+
folders = allFiles([allFiles.isdir]); % getting all folders in the folder
28+
folders(1:2) = []; % Deleting . and .. folders
29+
30+
31+
for k=1:length(folders)
32+
thisPath = strcat(folder, "/", folders(k).name);
33+
allFiles2 = dir(strcat(thisPath, "/*.*"));
34+
allFiles2(1:2) = []; % Deleting . and .. folders
35+
if sum([allFiles2(:).isdir]) > 0 % If it still have folders to analyze, keep on the recursive searching
36+
%disp("Not a final folder");
37+
countAllFoldersRecursive(thisPath, fileWrite);
38+
39+
else % If its a final folder, all its files are analyzed, writing the results in a single line
40+
[numFiles, numEntries, numManeuverType, numShipType] = countFolder(thisPath);
41+
disp("Soy carpeta: "+thisPath);
42+
fid = fopen(fileWrite, 'a');
43+
fprintf(fid, strcat(num2str(numFiles), '; ', num2str(numEntries), '; '));
44+
for i=1:length(numShipType)
45+
fprintf(fid, strcat(num2str(numShipType(i)), '; '));
46+
end
47+
for i=1:length(numManeuverType)
48+
fprintf(fid, strcat(num2str(numManeuverType(i)), '; '));
49+
end
50+
fprintf(fid, strcat(thisPath, '; '));
51+
a = split(thisPath, '/');
52+
for i=1:length(a)
53+
fprintf(fid, strcat(a(i), '; ') );
54+
end
55+
fprintf(fid, "\n");
56+
57+
fclose(fid);
58+
end
59+
end
60+
61+
end
+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2+
% Desarrollado por | Developed by: %
3+
% University Carlos III of Madrid PhD Researchers %
4+
% Daniel Amigo Herrero mailto: [email protected] %
5+
% David Sanchez Pedroche mailto: [email protected] %
6+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7+
8+
%% Recibe una carpeta, la abre y apunta el numero de ficheros y las lineas totales
9+
% Receives a folder, opens it and writes down the number of files, the total lines and the number of ships per type
10+
function [numFiles, numEntries, countPerManeuver, countPerShiptype] = countFolder(folderRead)
11+
12+
% Specify the major types to count per class
13+
classesShipType = ["Cargo", "Fishing", "Passenger", "Tanker", "Undefined"];
14+
classesManeuver = ["Engaged in fishing", "Restricted maneuverability", "Under way sailing", "Under way using engine", "Unknown value"];
15+
16+
allFiles = dir(strcat(folderRead, "/*.csv")); % Get all CSV files
17+
allFiles([allFiles.isdir]) = []; % skip directories
18+
19+
% auxiliar variables
20+
numFiles = length(allFiles);
21+
countPerFile = zeros(numFiles, 1);
22+
% each ship type counter
23+
isCargo = zeros(numFiles, 1);
24+
isFishing = zeros(numFiles, 1);
25+
isPassenger = zeros(numFiles, 1);
26+
isTanker = zeros(numFiles, 1);
27+
isUndefined = zeros(numFiles, 1);
28+
isOtherShip = zeros(numFiles, 1);
29+
% each maneuver type counter
30+
isEngaged = zeros(numFiles, 1);
31+
isRestricted = zeros(numFiles, 1);
32+
isSailing = zeros(numFiles, 1);
33+
isEngine = zeros(numFiles, 1);
34+
isUnknown = zeros(numFiles, 1);
35+
isOtherMane = zeros(numFiles, 1);
36+
37+
38+
parfor k=1:numFiles
39+
nameFileRead = allFiles(k).name;
40+
fullFileRead = strcat(folderRead,'/',nameFileRead); % Path de lectura
41+
42+
fid = fopen(fullFileRead);
43+
fgetl(fid); % Reading the header
44+
firstLine = fgetl(fid); % Reading the first line
45+
if firstLine ~= -1 % error prevention. If the file only has a header
46+
splitted = split(firstLine, ";");
47+
48+
a = find(ismember(classesShipType, splitted{15})); % Get the shiptype
49+
if a == 1
50+
isCargo(k) = isCargo(k) + 1;
51+
elseif a == 2
52+
isFishing(k) = isFishing(k) + 1;
53+
elseif a == 3
54+
isPassenger(k) = isPassenger(k) + 1;
55+
elseif a == 4
56+
isTanker(k) = isTanker(k) + 1;
57+
elseif a == 5
58+
isUndefined(k) = isUndefined(k) + 1;
59+
else
60+
isOtherShip(k) = isOtherShip(k) + 1;
61+
end
62+
63+
a = find(ismember(classesManeuver, splitted{7})); % Get the maneuver
64+
if a == 1
65+
isEngaged(k) = isEngaged(k) + 1;
66+
elseif a == 2
67+
isRestricted(k) = isRestricted(k) + 1;
68+
elseif a == 3
69+
isSailing(k) = isSailing(k) + 1;
70+
elseif a == 4
71+
isEngine(k) = isEngine(k) + 1;
72+
elseif a == 5
73+
isUnknown(k) = isUnknown(k) + 1;
74+
else
75+
isOtherMane(k) = isOtherMane(k) + 1;
76+
end
77+
78+
% Read the rest of the file to count the number of lines
79+
% if the classes change within the file, this is wrong
80+
while true
81+
if ~ischar( fgetl(fid) ); break; end
82+
countPerFile(k) = countPerFile(k) + 1;
83+
end
84+
fclose(fid);
85+
end
86+
87+
end
88+
89+
% Not counting the last line
90+
countPerFile(:) = countPerFile(:) - 1;
91+
numEntries = sum(countPerFile);
92+
93+
% Final results per class
94+
countPerManeuver = zeros(length(classesManeuver)+1, 1);
95+
for i=1:length(classesManeuver)+1
96+
if i == 1
97+
countPerManeuver(i) = sum(isEngaged(:));
98+
elseif i == 2
99+
countPerManeuver(i) = sum(isRestricted(:));
100+
elseif i == 3
101+
countPerManeuver(i) = sum(isSailing(:));
102+
elseif i == 4
103+
countPerManeuver(i) = sum(isEngine(:));
104+
elseif i == 5
105+
countPerManeuver(i) = sum(isUnknown(:));
106+
else
107+
countPerManeuver(i) = sum(isOtherMane(:));
108+
end
109+
end
110+
countPerShiptype = zeros(length(classesShipType)+1, 1);
111+
for i=1:length(classesShipType)+1
112+
if i == 1
113+
countPerShiptype(i) = sum(isCargo(:));
114+
elseif i == 2
115+
countPerShiptype(i) = sum(isFishing(:));
116+
elseif i == 3
117+
countPerShiptype(i) = sum(isPassenger(:));
118+
elseif i == 4
119+
countPerShiptype(i) = sum(isTanker(:));
120+
elseif i == 5
121+
countPerShiptype(i) = sum(isUndefined(:));
122+
else
123+
countPerShiptype(i) = sum(isOtherShip(:));
124+
end
125+
end
126+
127+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2+
% Desarrollado por | Developed by: %
3+
% University Carlos III of Madrid PhD Researchers %
4+
% Daniel Amigo Herrero mailto: [email protected] %
5+
% David Sanchez Pedroche mailto: [email protected] %
6+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7+
8+
%% Recibe una carpeta con muchos ficheros resultado del proceso featureExtraction. Sobre cada fichero, apunta el numero de trayectorias, las lineas totales, numero de cada tipo de clase...
9+
% Receives a folder with some results of the featureExtraction process. On each process, opens it and writes down the number of files, the total lines and the number of ships per type
10+
function [numFiles, numEntries] = countPreclassifier(folderRead)
11+
12+
% Get all files of the folder
13+
allFiles = dir(strcat(folderRead, "/*.*"));
14+
allFiles([allFiles.isdir]) = []; %skip directories
15+
16+
% Specify the major types to count per class
17+
classesShipType = ["Cargo", "Fishing", "Passenger", "Tanker", "Undefined"];
18+
classesManeuver = ["Engaged in fishing", "Restricted maneuverability", "Under way sailing", "Under way using engine", "Unknown value"];
19+
20+
numFiles = length(allFiles);
21+
countPerFile = zeros(numFiles, 1);
22+
% Shiptypes
23+
summaryCargo = zeros(numFiles, 1);
24+
summaryFishing = zeros(numFiles, 1);
25+
summaryPassenger = zeros(numFiles, 1);
26+
summaryTanker = zeros(numFiles, 1);
27+
summaryUndefined = zeros(numFiles, 1);
28+
summaryOtherShip = zeros(numFiles, 1);
29+
30+
parfor k=1:numFiles
31+
nameFileRead = allFiles(k).name;
32+
fullFileRead = strcat(folderRead,'/',nameFileRead); % Path de lectura
33+
34+
fid = fopen(fullFileRead);
35+
line = fgetl(fid); % Reading the header
36+
while true
37+
line = fgetl(fid);
38+
if ~ischar( line ); break; end
39+
40+
splittedLine = split(line, ";");
41+
a = find(ismember(classesShipType, splittedLine{5}));
42+
if a == 1
43+
summaryCargo(k) = summaryCargo(k) + 1;
44+
elseif a == 2
45+
summaryFishing(k) = summaryFishing(k) + 1;
46+
elseif a == 3
47+
summaryPassenger(k) = summaryPassenger(k) + 1;
48+
elseif a == 4
49+
summaryTanker(k) = summaryTanker(k) + 1;
50+
elseif a == 5
51+
summaryUndefined(k) = summaryUndefined(k) + 1;
52+
else
53+
summaryOtherShip(k) = summaryOtherShip(k) + 1;
54+
end
55+
countPerFile(k) = countPerFile(k) + 1;
56+
end
57+
fclose(fid);
58+
end
59+
60+
% One file for all featureExtraction outputs
61+
fileWrite = strcat(folderRead, "\", "metrics.csv");
62+
fid = fopen(fileWrite, 'a');
63+
% Write the header
64+
fprintf(fid, strcat("Preclassifier", '; '));
65+
fprintf(fid, strcat("NumEntries", '; '));
66+
fprintf(fid, strcat("NumCargo", '; '));
67+
fprintf(fid, strcat("NumFishing", '; '));
68+
fprintf(fid, strcat("NumPassenger", '; '));
69+
fprintf(fid, strcat("NumTanker", '; '));
70+
fprintf(fid, strcat("NumUndefined", '; '));
71+
fprintf(fid, strcat("NumOther", '; '));
72+
fprintf(fid, "\n");
73+
74+
% Results
75+
for k=1:numFiles
76+
fprintf(fid, strcat(allFiles(k).name, '; '));
77+
fprintf(fid, strcat(num2str(countPerFile(k)), '; '));
78+
fprintf(fid, strcat(num2str(summaryCargo(k)), '; '));
79+
fprintf(fid, strcat(num2str(summaryFishing(k)), '; '));
80+
fprintf(fid, strcat(num2str(summaryPassenger(k)), '; '));
81+
fprintf(fid, strcat(num2str(summaryTanker(k)), '; '));
82+
fprintf(fid, strcat(num2str(summaryUndefined(k)), '; '));
83+
fprintf(fid, strcat(num2str(summaryOtherShip(k)), '; '));
84+
fprintf(fid, "\n");
85+
end
86+
87+
fclose(fid);
88+
89+
end

0 commit comments

Comments
 (0)