%% This script serves as a working example to loading data described in
%   "A Machine Learning Dataset of Artificial Inner Ring Damages on
%   Cylindrical Roller Bearings Measured Under Varying
%   Cross-Influences" 
%   available at "https://doi.org/10.5281/zenodo.11108503".
%
%   *****CAUTION*****
%   DO NOT MOVE THIS SCRIPT AND THE PROVIDED FILES AWAY FROM EACH OTHER.
%   THE SCRIPT AND FUNCTIONS ASSUME TO BE IN THE SAME PARENT DIRECTORY
%   INCLUDING DATA, METADATA AND INFO.
%   *****************
%
%   The script selectively loads and processes data from the provided 
%   directory structure and info table.
%   Users can specify criteria to filter measurements they wish to load.
%   Optionally, the data can be reshaped for further analysis.
%
%   The script uses control flags and variables to enable customization.
%   Flags(boolean, true/false):
%   - 'SAVE_DATA':    save the processed data to 'OUT_PATH/OUT_FILE.mat'.
%   - 'RESHAPE_DATA': reshape the data into NUM_SEGMENTS segments.
%                     This option also alters the output data size.
%   Variables:
%   - 'OUT_PATH': file system path where the output file should be stored 
%                 default=current working directory
%   - 'OUT_FILE': file name for the ourput file
%                 default='dataset'
%   - 'NUM_SEGMENTS: number of segments the data is split into when 
%                    reshaping data
%                    default=8
%   
%   The process of selective loading is controlled by indexing of the 
%   provided info table that has a 1-to-1 relation to all measurements.
%   The selection is achieved by picking a subset of the state of 
%   variable influences that were altered during experimentation via the 
%   structure SELECTION. Its default state selects all measurements.
%
%   *****CAUTION*****
%   THE DEFAULT FULL SELECTION RESULTS IN A HUGE DATASET.
%   LIMITING THE SELECTION IS HIGHLY RECOMMENDED TO AVOID RAM OVERFLOW.
%   *****************
%
%   Influence values can only be chosen from a fixed, discrete collection.
%   See functions.fullSelection or script section 1 for allowed values.
%   Selecting multiple values for the same influence will result in the 
%   disjuntion (logical OR) of states being included in the final dataset.
%   Selecting multiple influences with one or multiple values will result 
%   in the conjunction (logical AND) of per influence states being included
%   in the final dataset.
%   
%   The info table contains additional meta information that will 
%   be output with the final dataset. However the recorded values were 
%   not deliberately altered during experimentation and are therefore 
%   not recommended for measurement selections.

%% Section 0: Setup control flags and variables

% Flags
SAVE_DATA    = true;
RESHAPE_DATA = false;

% Variables
OUT_PATH = pwd;
OUT_FILE = "dataset";
NUM_SEGMENTS = 8; % Default of 8 returns at least 10 rotations per segment 
                  % due to minimum RPM of 85 and a measurement duration of 
                  % 1 minute

%% Section 1: Setup selective loading by picking influence values

% Get the defaults for selecting all measurements.
% The full set covers the influence values commented below.
SELECTION = functions.fullSelection(); 
% SELECTION.("Bearing")     = [10 20 30];
% SELECTION.("DamageState") = [0 1];
% SELECTION.("Run")         = [1 2 3];
% SELECTION.("Position")    = [1 2 3 4];
% SELECTION.("ForceLevel")  = [0 1 2 3];
% SELECTION.("SpeedTarget") = [706 969 85 392];

% Adapt to select measurements based on a subset of influence values.
% Leave the other influences at their default values.
SELECTION.("ForceLevel")  = 0;
SELECTION.("SpeedTarget") = 969;
SELECTION.("Position")    = 1; 

%% Section 2: Read the specified dataset from disk
[data, target] = functions.readFromDisk(SELECTION, ...
                                        "ReshapeData",RESHAPE_DATA, ...
                                        "NumSegments",NUM_SEGMENTS);

%% Section 3: Save dataset at specified path
if SAVE_DATA
    fprintf("Saving dataset to %s\n", fullfile(OUT_PATH, OUT_FILE + ".mat"))
    save(fullfile(OUT_PATH, OUT_FILE + ".mat"), "data","target", "-v7.3")
    fprintf(" Done saving.\n")
end