Skip to content

Commit

Permalink
Attempt to migrate segment_all.sh Bash shellscript to Ruby script
Browse files Browse the repository at this point in the history
  • Loading branch information
CppPhil committed Oct 11, 2020
1 parent 60359ea commit 9400c87
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 167 deletions.
33 changes: 0 additions & 33 deletions TODO_LIST.txt

This file was deleted.

2 changes: 1 addition & 1 deletion preprocessed_segment.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cd "%dir%"

call "%goto_ruby%" "%dir%\ruby\preprocessed_segment.rb" %*
if errorlevel 1 (
cd %prev_dir%
cd "%prev_dir%"
exit /B 1
)

Expand Down
130 changes: 130 additions & 0 deletions ruby/segment_all.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
require 'optparse'
require_relative 'modules/system'

options = {}

OptionParser.new do |opt|
opt.on('--skip_existing=BOOLEAN',
'Whether to skip already existing log files') do |o|
options[:skip_existing] = (o.casecmp 'true').zero?
end

opt.on_tail('-h', '--help', 'Show this message') do
STDERR.puts opt
exit(0)
end
end.parse!

skip_existing = if options[:skip_existing].nil?
true
else
options[:skip_existing]
end

RESOURCE_DIR = 'resources'.freeze
PREPROCESSED_DIR = "#{RESOURCE_DIR}/preprocessed".freeze

CSV_FILES = [
"#{PREPROCESSED_DIR}/2020-07-02_11.17.39_Belly.csv",
"#{PREPROCESSED_DIR}/2020-07-02_11.17.39_Chest.csv",
"#{PREPROCESSED_DIR}/2020-07-02_11.17.39_LeftArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_11.17.39_RightArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_12.50.00_Belly.csv",
"#{PREPROCESSED_DIR}/2020-07-02_12.50.00_Chest.csv",
"#{PREPROCESSED_DIR}/2020-07-02_12.50.00_LeftArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_12.50.00_RightArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_13.00.09_Belly.csv",
"#{PREPROCESSED_DIR}/2020-07-02_13.00.09_Chest.csv",
"#{PREPROCESSED_DIR}/2020-07-02_13.00.09_LeftArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_13.00.09_RightArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.07.33_Belly.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.07.33_Chest.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.07.33_LeftArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.07.33_RightArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.14.32_Belly.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.14.32_Chest.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.14.32_LeftArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.14.32_RightArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.20.28_Belly.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.20.28_Chest.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.20.28_LeftArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.20.28_RightArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.38.40 (FalschausfБhrung)_Belly.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.38.40 (FalschausfБhrung)_Chest.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.38.40 (FalschausfБhrung)_LeftArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.38.40 (FalschausfБhrung)_RightArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.59.59_Belly.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.59.59_Chest.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.59.59_LeftArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_14.59.59_RightArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.13.22_Belly.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.13.22_Chest.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.13.22_LeftArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.13.22_RightArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.31.36_Belly.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.31.36_Chest.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.31.36_LeftArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.31.36_RightArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.39.22 (FalschausfБhrung)_Belly.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.39.22 (FalschausfБhrung)_Chest.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.39.22 (FalschausfБhrung)_LeftArm.csv",
"#{PREPROCESSED_DIR}/2020-07-02_15.39.22 (FalschausfБhrung)_RightArm.csv"
].freeze

SKIP_WINDOW_OPTIONS = %w[false true].freeze
DELETE_TOO_CLOSE_OPTIONS = %w[false true].freeze
DELETE_LOW_VARIANCE_OPTIONS = %w[false true].freeze
SEGMENTATION_KINDS = %w[both max min].freeze
WINDOW_SIZES = %w[101 151 201 251 301 351 401 451 501 51 551].freeze
FILTERS = %w[average butterworth].freeze

LOG_DIR = 'segmentation_comparison/logs'.freeze

def script
if System.os == :windows
'./preprocessed_segment.sh'
else
'preprocessed_segment.bat'
end
end

SKIP_WINDOW_OPTIONS.each do |skip_window_option|
DELETE_TOO_CLOSE_OPTIONS.each do |delete_too_close_option|
DELETE_LOW_VARIANCE_OPTIONS.each do |delete_low_variance_option|
SEGMENTATION_KINDS.each do |segmentation_kind|
WINDOW_SIZES.each do |window_size|
FILTERS.each do |filter|
file = "#{LOG_DIR}/skip_window-#{skip_window_option}_delete_too_"\
"close-#{delete_too_close_option}_delete_low_variance-"\
"#{delete_low_variance_option}_kind-#{segmentation_kind}_"\
"window-#{window_size}_filter-#{filter}.log"

next if skip_existing && File.file?(file)
File.delete(file)

CSV_FILES.each do |csv_file|
next if system(
"#{script} "\
"--skip_window=#{skip_window_option} "\
"--delete_too_close=#{delete_too_close_option} "\
"--delete_low_variance=#{delete_low_variance_option} "\
'--image_format=png '\
"--csv_file_path=#{csv_file} "\
'--imu=accelerometer '\
"--segmentation_kind=#{segmentation_kind} "\
"--window_size=#{window_size} "\
"--filter=#{filter} "\
">> #{file}"
)
STDERR.puts("Failure invoking #{script}!")
exit(1)
end
end
end
end
end
end
end

puts('Done.')
exit(0)
2 changes: 1 addition & 1 deletion segment.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cd "%dir%"

call "%goto_ruby%" "%dir%\ruby\segment.rb" %*
if errorlevel 1 (
cd %prev_dir%
cd "%prev_dir%"
exit /B 1
)

Expand Down
17 changes: 17 additions & 0 deletions segment_all.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@echo off

set dir=%~dp0
set prev_dir=%CD%
set goto_ruby=%dir%\external\goto-ruby\goto-ruby.bat

cd "%dir%"

call "%goto_ruby%" "%dir%\ruby\segment_all.rb" %*
if errorlevel 1 (
cd "%prev_dir%"
exit /B 1
)

cd "%prev_dir%"
exit /B 0

134 changes: 2 additions & 132 deletions segment_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,141 +12,11 @@ readonly DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

readonly PREV_DIR=$(pwd)

readonly RESOURCE_DIR="resources"

readonly PREPROCESSED_DIR="$RESOURCE_DIR/preprocessed"

readonly CSV_FILES=(
"$PREPROCESSED_DIR/2020-07-02_11.17.39_Belly.csv"
"$PREPROCESSED_DIR/2020-07-02_11.17.39_Chest.csv"
"$PREPROCESSED_DIR/2020-07-02_11.17.39_LeftArm.csv"
"$PREPROCESSED_DIR/2020-07-02_11.17.39_RightArm.csv"
"$PREPROCESSED_DIR/2020-07-02_12.50.00_Belly.csv"
"$PREPROCESSED_DIR/2020-07-02_12.50.00_Chest.csv"
"$PREPROCESSED_DIR/2020-07-02_12.50.00_LeftArm.csv"
"$PREPROCESSED_DIR/2020-07-02_12.50.00_RightArm.csv"
"$PREPROCESSED_DIR/2020-07-02_13.00.09_Belly.csv"
"$PREPROCESSED_DIR/2020-07-02_13.00.09_Chest.csv"
"$PREPROCESSED_DIR/2020-07-02_13.00.09_LeftArm.csv"
"$PREPROCESSED_DIR/2020-07-02_13.00.09_RightArm.csv"
"$PREPROCESSED_DIR/2020-07-02_14.07.33_Belly.csv"
"$PREPROCESSED_DIR/2020-07-02_14.07.33_Chest.csv"
"$PREPROCESSED_DIR/2020-07-02_14.07.33_LeftArm.csv"
"$PREPROCESSED_DIR/2020-07-02_14.07.33_RightArm.csv"
"$PREPROCESSED_DIR/2020-07-02_14.14.32_Belly.csv"
"$PREPROCESSED_DIR/2020-07-02_14.14.32_Chest.csv"
"$PREPROCESSED_DIR/2020-07-02_14.14.32_LeftArm.csv"
"$PREPROCESSED_DIR/2020-07-02_14.14.32_RightArm.csv"
"$PREPROCESSED_DIR/2020-07-02_14.20.28_Belly.csv"
"$PREPROCESSED_DIR/2020-07-02_14.20.28_Chest.csv"
"$PREPROCESSED_DIR/2020-07-02_14.20.28_LeftArm.csv"
"$PREPROCESSED_DIR/2020-07-02_14.20.28_RightArm.csv"
"$PREPROCESSED_DIR/2020-07-02_14.38.40 (FalschausfБhrung)_Belly.csv"
"$PREPROCESSED_DIR/2020-07-02_14.38.40 (FalschausfБhrung)_Chest.csv"
"$PREPROCESSED_DIR/2020-07-02_14.38.40 (FalschausfБhrung)_LeftArm.csv"
"$PREPROCESSED_DIR/2020-07-02_14.38.40 (FalschausfБhrung)_RightArm.csv"
"$PREPROCESSED_DIR/2020-07-02_14.59.59_Belly.csv"
"$PREPROCESSED_DIR/2020-07-02_14.59.59_Chest.csv"
"$PREPROCESSED_DIR/2020-07-02_14.59.59_LeftArm.csv"
"$PREPROCESSED_DIR/2020-07-02_14.59.59_RightArm.csv"
"$PREPROCESSED_DIR/2020-07-02_15.13.22_Belly.csv"
"$PREPROCESSED_DIR/2020-07-02_15.13.22_Chest.csv"
"$PREPROCESSED_DIR/2020-07-02_15.13.22_LeftArm.csv"
"$PREPROCESSED_DIR/2020-07-02_15.13.22_RightArm.csv"
"$PREPROCESSED_DIR/2020-07-02_15.31.36_Belly.csv"
"$PREPROCESSED_DIR/2020-07-02_15.31.36_Chest.csv"
"$PREPROCESSED_DIR/2020-07-02_15.31.36_LeftArm.csv"
"$PREPROCESSED_DIR/2020-07-02_15.31.36_RightArm.csv"
"$PREPROCESSED_DIR/2020-07-02_15.39.22 (FalschausfБhrung)_Belly.csv"
"$PREPROCESSED_DIR/2020-07-02_15.39.22 (FalschausfБhrung)_Chest.csv"
"$PREPROCESSED_DIR/2020-07-02_15.39.22 (FalschausfБhrung)_LeftArm.csv"
"$PREPROCESSED_DIR/2020-07-02_15.39.22 (FalschausfБhrung)_RightArm.csv"
)

readonly SKIP_WINDOW_OPTIONS=(
"false"
"true"
)

readonly DELETE_TOO_CLOSE_OPTIONS=(
"false"
"true"
)

readonly DELETE_LOW_VARIANCE_OPTIONS=(
"false"
"true"
)

readonly SEGMENTATION_KINDS=(
"both"
"max"
"min"
)

readonly WINDOW_SIZES=(
"101"
"151"
"201"
"251"
"301"
"351"
"401"
"451"
"501"
"51"
"551"
)

readonly FILTERS=(
"average"
"butterworth"
)

readonly skip_existing=true
readonly GOTO_RUBY="$DIR/external/goto-ruby/goto-ruby.sh"

cd "$DIR"

readonly LOG_DIR="$DIR/segmentation_comparison/logs"

for skip_window_option in "${SKIP_WINDOW_OPTIONS[@]}"; do
for delete_too_close_option in "${DELETE_TOO_CLOSE_OPTIONS[@]}"; do
for delete_low_variance_option in "${DELETE_LOW_VARIANCE_OPTIONS[@]}"; do
for segmentation_kind in "${SEGMENTATION_KINDS[@]}"; do
for window_size in "${WINDOW_SIZES[@]}"; do
for filter in "${FILTERS[@]}"; do

file="$LOG_DIR/skip_window-${skip_window_option}_delete_too_close-${delete_too_close_option}_delete_low_variance-${delete_low_variance_option}_kind-${segmentation_kind}_window-${window_size}_filter-${filter}.log"

if [ "$skip_existing" = true ]; then
if [ -e "$file" ]; then
continue
fi
fi

rm -f $file

for csv_file in "${CSV_FILES[@]}"; do
./preprocessed_segment.sh \
--skip_window="$skip_window_option" \
--delete_too_close="$delete_too_close_option" \
--delete_low_variance="$delete_low_variance_option" \
--image_format=png \
--csv_file_path="$csv_file" \
--imu=accelerometer \
--segmentation_kind="$segmentation_kind" \
--window_size="$window_size" \
--filter="$filter" \
>> $file
done
done
done
done
done
done
done

echo "Done."
"$GOTO_RUBY" "$DIR/ruby/segment_all.rb" "$@"

cd "$PREV_DIR"
exit 0
Expand Down

0 comments on commit 9400c87

Please sign in to comment.