aboutsummaryrefslogtreecommitdiff
path: root/utils/processTobiiRecords.sh
blob: aca15436b5aafb838ece9225ca9d589adb1196f5 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#! /bin/bash
#
# Look for all Tobii record segments into input folder ($1) to execute an ArGaze context file ($2) on each.
#
# For each Tobii record segment found a temporary context and pipeline files are created:
# - temporary context "segment" field is modified to bind on it,
# - temporary context "pipeline" field is modified to bind on a temporary pipeline file,
# - temporary pipeline "projection_cache" field is added to create or bind a "layers_projection.csv" file in segment folder,
#	— if a "patch.json" file exist in  the Tobii record segment, it merged to the temporary pipeline file,
# - then, ArGaze executes the temporary context from output folder ($3) to export all records into it,
# - finally, temporary context and pipeline files are deleted.
#
# Arguments:
# $1: folder from where to look for Tobii records
# $2: ArGaze context file
# $3: folder where to export processing outputs
# $4+: any other arguments are passed to "argaze load" command
#######################################

# Parse mandatory arguments
input_folder="$(realpath "$1")"
context_file="$(realpath "$2")"
output_folder="$(realpath "$3")"

# Parse any other arguments to pass them to "argaze load" command
if [ $# -ge 4 ]; then

	argaze_args=$4

  while shift && [ -n "$4" ]; do
    argaze_args="${argaze_args},$4"
  done

fi

# Check requirements
if brew ls --versions jq > /dev/null; then
 	: # jq installed
else
	echo "*** Installing jq package"
 	brew install jq
fi

ctx_folder="$(dirname "$context_file")"

#######################################
# Process Tobii segment folder
# Arguments:
#   $1: Path to Tobii segment
#######################################
function process_segment() {

	local rec_id=$1
	local seg_folder=$2
	local seg_id=$(basename $seg_folder)
	local seg_length=$(jq .seg_length $seg_folder/segment.json)

	local ca_state=$(jq -r .ca_state $seg_folder/calibration.json)

	echo "- Segment $seg_id:"
	echo "	- Lenght: $seg_length"
	echo "	- Calibration: $ca_state"

	# Move to context folder
	cd $ctx_folder

	# Create temporary context file
	temp_context_file=".$rec_id-$seg_id.context.json"
	yes | cp -f $context_file $temp_context_file
	ctx_class=$(jq "keys[0]" $temp_context_file)
	ctx_name=$(jq .$ctx_class.name $temp_context_file)
	ctx_pipeline=$(jq -r .$ctx_class.pipeline $temp_context_file)

	echo "- Context $(basename $temp_context_file):"
	echo "	- Name: $ctx_name"

	# Create temporary pipeline file 
	temp_pipeline_file=".$rec_id-$seg_id.pipeline.json"
	yes | cp -f $ctx_pipeline $temp_pipeline_file
	ppl_class=$(jq "keys[0]" $temp_pipeline_file)
	ppl_name=$(jq .$ppl_class.name $temp_pipeline_file)

	echo "- Pipeline: $(basename $temp_pipeline_file)"
	echo "	- Name: $ppl_name"

	# Modify temporary context segment
	echo "$(jq --tab ".$ctx_class.segment = \"$seg_folder\"" $temp_context_file)" > "$ctx_folder/$temp_context_file"

	# Modify temporary context pipeline
	echo "$(jq --tab ".$ctx_class.pipeline = \"$temp_pipeline_file\"" $temp_context_file)" > "$ctx_folder/$temp_context_file"

	# Modify temporary pipeline projection_cache
	echo "$(jq --tab ".$ppl_class.projection_cache = \"$seg_folder/layers_projection.csv\"" $temp_pipeline_file)" > "$ctx_folder/$temp_pipeline_file"

	# Check patch
	patch_file="$seg_folder/patch.json"
	if [ -f "$patch_file" ]; then
		
		echo "	+ Patch:"
		echo "$(jq . $patch_file)"

		# Merge patch with temporary pipeline
		echo "$(jq --tab -s ".[0] * .[1]" $temp_pipeline_file $patch_file)" > "$ctx_folder/$temp_pipeline_file"
	fi

	# Create segment output folder
	seg_output=$output_folder/$rec_id/segments/$seg_id
	mkdir -p $seg_output
	cd $seg_output

	# Launch modified context with argaze load command 
	echo "*** ArGaze starts context"

	python -m argaze load "$ctx_folder/$temp_context_file" $argaze_args

	echo "*** ArGaze ends context"

	# Move back to context folder
	cd $ctx_folder

	# Delete temporary context file
	rm "$ctx_folder/$temp_context_file"

	# Delete temporary pipeline file
	rm "$ctx_folder/$temp_pipeline_file"
}

#######################################
# Process Tobii record folder
# Arguments:
#   $1: Path to Tobii record
#######################################
function process_record() {

	local rec_folder=$1
	local rec_id=$(basename $rec_folder)
	local rec_name=$(jq .rec_info.Name $rec_folder/recording.json)

	local pa_id=$(jq .pa_id $rec_folder/participant.json)
	local pa_name=$(jq .pa_info.Name $rec_folder/participant.json)

	echo "*** Loading record $rec_id:"
	echo "- Name: $rec_name"
	echo "- Participant:"
	echo "	- Id: $pa_id"
	echo "	- Name: $pa_name"

	for segment in $rec_folder/segments/*; do
		process_segment $rec_id "$rec_folder/segments/$(basename $segment)"
	done
}

echo "*** Looking for Tobii records into $1 folder"

# Check if the path is directly a path to a record segment
if [ -f "$input_folder/livedata.json.gz" ]; then

	process_record "$(dirname "$(dirname "$input_folder")")"

# Otherwise, look for all Tobii record segment
else

	for tobii_data in $input_folder/**/segments/*/livedata.json.gz; do
	    process_record "$(dirname "$(dirname "$(dirname "$tobii_data")")")"
	done

fi