<commit_before>#!/bin/bash

# <NAME> 2015
# Common functions for QUITK Tests

# Check for presence of FSL
if [ -n "$(which fslmaths)" ]; then
	HAVE_FSL="1"
	echo "FSL detected."
	export FSLOUTPUTTYPE=NIFTI
else
	HAVE_FSL=""
	echo "FSL not detected. Cannot run comparison tests."
fi

# Simple test function
SILENCE_TESTS=0
function run_test {
	# $1 is test name, remainder is command to run
	NAME="$1"
	shift
	printf "Starting test $NAME..."
	if [ "$SILENCE_TESTS" -eq "1" ]; then
		"$@" > "$NAME.log"
	else
		"$@"
	fi
	local STATUS=$?
	if [ $STATUS -ne 0 ]; then
		printf "Failed.\n" >&2
		exit $STATUS
	else
		printf "Passed.\n" >&1
	fi
	return $STATUS
}

# Use FSL to compare 2 images and check if the average difference is below the tolerance
function compare_test {
	# $1 test name
	# $2 reference, $3 test image, $4 tolerance
	NAME="$1"
	REF=$2
	TEST=$3
	TOL=$4
	DIFF=${REF%.nii}_${TEST%.nii}
	if [ "$HAVE_FSL" -eq "1" ]; then
		fslmaths $REF -sub $TEST $DIFF
		MEAN=$( fslstats $DIFF -M )
		STD=$( fslstats $DIFF -s )
		ABSMEAN=$(echo $MEAN | awk ' { print sqrt($1*$1) } ' )
		TEST=$(echo "$ABSMEAN $TOL" | awk ' { if($1<=$2) { print 1 } else { print 0 }}')
		if [ "$TEST" -eq "1" ]; then
			echo "Comparison test $NAME passed, mean diff was $ABSMEAN tolerance $TOL (std was $STD)"
		else
			echo "Comparison test $NAME failed, mean diff was $ABSMEAN tolerance $TOL (std was $STD)"
			exit 1
		fi
	else
		echo "FSL not present, skipping test $NAME"
	fi
	return 0
}

# Setup environment
QUITDIR=$PWD/../
export QUIT_EXT=NIFTI
#QUITVER=$(cat ../src/version | sed -e 's/^"//'  -e 's/"$//')<commit_msg>Added a check for tests returning nan/inf etc.<commit_after>#!/bin/bash

# <NAME> 2015
# Common functions for QUITK Tests

# Check for presence of FSL
if [ -n "$(which fslmaths)" ]; then
	HAVE_FSL="1"
	echo "FSL detected."
	export FSLOUTPUTTYPE=NIFTI
else
	HAVE_FSL=""
	echo "FSL not detected. Cannot run comparison tests."
fi

# Simple test function
SILENCE_TESTS=0
function run_test {
	# $1 is test name, remainder is command to run
	NAME="$1"
	shift
	printf "Starting test $NAME..."
	if [ "$SILENCE_TESTS" -eq "1" ]; then
		"$@" > "$NAME.log"
	else
		"$@"
	fi
	local STATUS=$?
	if [ $STATUS -ne 0 ]; then
		printf "Failed.\n" >&2
		exit $STATUS
	else
		printf "Passed.\n" >&1
	fi
	return $STATUS
}

# Use FSL to compare 2 images and check if the average difference is below the tolerance
function compare_test {
	# $1 test name
	# $2 reference, $3 test image, $4 tolerance
	NAME="$1"
	REF=$2
	TEST=$3
	TOL=$4
	DIFF=${REF%.nii}_${TEST%.nii}
	if [ "$HAVE_FSL" -eq "1" ]; then
		fslmaths $REF -sub $TEST $DIFF
		MEAN=$( fslstats $DIFF -M )
		STD=$( fslstats $DIFF -s )
		ABSMEAN=$(echo $MEAN | awk ' { print sqrt($1*$1) } ' )
		# Check for nan/inf/etc. because on some platforms awk will treat these as 0
		REGEXP='^-?[0-9]+([.][0-9]+)?$'
		if ! [[ $ABSMEAN =~ $REGEXP ]] ; then
			echo "Comparison test $NAME failed, mean diff is not a valid number"
			exit 1
		fi
		# Now do the tolerance test
		TEST=$(echo "$ABSMEAN $TOL" | awk ' { if(strtonum($1)<=strtonum($2)) { print 1 } else { print 0 }}')
		if [ "$TEST" -eq "1" ]; then
			echo "Comparison test $NAME passed, mean diff was $ABSMEAN tolerance $TOL (std was $STD)"
		else
			echo "Comparison test $NAME failed, mean diff was $ABSMEAN tolerance $TOL (std was $STD)"
			exit 1
		fi
	else
		echo "FSL not present, skipping test $NAME"
	fi
	return 0
}

# Setup environment
QUITDIR=$PWD/../
export QUIT_EXT=NIFTI
#QUITVER=$(cat ../src/version | sed -e 's/^"//'  -e 's/"$//')
{'fraction_non_alphanumeric': 0.1580288870008496, 'fraction_numerical': 0.019258000566411783, 'mean_word_length': 3.426065162907268, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5067889', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1362, 'n_words': 464}