# SPATIAL CADENCE - Create database for analysis

# This code creates a database with the following columns:
# 1- Participants: 41
# 2- ExpertiseType: 3 (Type of participants) - "amateurs" (1), "performers" (2), "composers" (3)
# 3- Test: 8 tests (two sets)
# 4- Set: 2 sets (4 tests and 4 types of stimulus each)
# 5- StymulusType: 4 types of stimuli
# 6- TrajectoryType: 25 trajectories (for each test)
# 7- Item: Trajectories are grouped by their type of closure. For example, closure=1 has 6 items (1,2,3,4,5,6), closure=2 has 7 items (1,2,3,4,5,6,7), etc.
# 8- Cadence_Group: 2 (splitting closures into narrow and wide closures)
# 9- Closure: 4 closures (Distance between the pernultimate impulse location and the last one - this is what we are investigating)
# 10- Relation_FirstLast: 4 (this parameters indicates the distance between the first impulse location and the last one - analogous to Closure)
# 11- LastPosition: 8 (this represents the last loudspeaker sounding in which the stimulus ends - randomised)
# 12- Score: Participant rating on a likert-scale from 1 to 5

# Reset
setwd("/home/luca/WorksInProgress/Perceptual-test-(2019)/Perceptual-test/Perceptual-Test-Analysis/")    #go to the working directory
datasheet = read.csv(file = "Data.csv")    #read the data structure
summary(datasheet)    #show result

datasheet$Participant[nchar(datasheet$Participant)==0]=NA    #fill empty cells with NA
install.packages("zoo")
library(zoo)    #for the na.locf() function

# CREATE COMPLETE DATABASE
# We create a column to group the four types of cadences in 2 groups 
if (exists("column_c_groups")) { rm(column_c_groups) }
column_c_groups <- vector()
for (relation_PL in datasheet$Relation_PenultimateLast) {	
	if (relation_PL <= 2) {
		column_c_groups <- c(column_c_groups, 1)
	}
	else {
		column_c_groups <- c(column_c_groups, 2)
	}
}

# We create a column for our database in order to separate the first from the second set of tests.
length(datasheet$Participant)
if (exists("set")) { rm(set) }
set <- vector()
for (fragmentType in na.locf(datasheet$FragmentType)) {	
	if (fragmentType <= 4) {
		set <- c(set, 1)
	}
	else {
		set <- c(set, 2)
	}
}

# We create a column to group the types of stimuli.
length(datasheet$Participant)
if (exists("stimulusTypes")) { rm(stimulusTypes) }
stimulusTypes <- vector()
for (fragmentType in na.locf(datasheet$FragmentType)) {	
	if (fragmentType <= 4) {
		stimulusTypes <- c(stimulusTypes, fragmentType)
	}
	else {
		stimulusTypes <- c(stimulusTypes, fragmentType-4)
	}
}

# We create our items, by grouping trajectories by their types of cadences.
# For each type of cadence, we have a number of items.
# Specifically, for cadence "type1" we have 6 items, for cadence "type2" we have 7 items, for cadence "type3" we have 3 items, and for "type4" we have 9 items.
if (exists("item")) { rm(item) }
item <- vector()
for (trj in na.locf(datasheet$StimulusLocationNumber)) {	
	#--- 1	
	if (trj == 3) {
		item <- c(item, 1)
	}
	else if (trj == 5) {
		item <- c(item, 2)
	}
	else if (trj == 7) {
		item <- c(item, 3)
	}
	else if (trj == 10) {
		item <- c(item, 4)
	}
	else if (trj == 15) {
		item <- c(item, 5)
	}
	else if (trj == 17) {
		item <- c(item, 6)
	}
	#--- 2
	else if (trj == 2) {
		item <- c(item, 1)
	}
	else if (trj == 6) {
		item <- c(item, 2)
	}
	else if (trj == 8) {
		item <- c(item, 3)
	}
	else if (trj == 13) {
		item <- c(item, 4)
	}
	else if (trj == 19) {
		item <- c(item, 5)
	}
	else if (trj == 22) {
		item <- c(item, 6)
	}
	else if (trj == 24) {
		item <- c(item, 7)
	}
	#--- 3
	else if (trj == 12) {
		item <- c(item, 1)
	}
	else if (trj == 18) {
		item <- c(item, 2)
	}
	else if (trj == 20) {
		item <- c(item, 3)
	}
	#--- 4
	else if (trj == 1) {
		item <- c(item, 1)
	}
	else if (trj == 4) {
		item <- c(item, 2)
	}
	else if (trj == 9) {
		item <- c(item, 3)
	}
	else if (trj == 11) {
		item <- c(item, 4)
	}
	else if (trj == 14) {
		item <- c(item, 5)
	}
	else if (trj == 16) {
		item <- c(item, 6)
	}
	else if (trj == 21) {
		item <- c(item, 7)
	}
	else if (trj == 23) {
		item <- c(item, 8)
	}
	else if (trj == 25) {
		item <- c(item, 9)
	}
}


# We create a column to plot the front/back contrast.
if (exists("fb")) { rm(fb) }
fb <- vector()
for (lp in na.locf(datasheet$LastPosition)) {	
	#--- 1	
	if (lp == 1 || lp == 2 || lp == 3 || lp == 8) {
		fb <- c(fb, "front")
	}
	else {
		fb <- c(fb, "back")
	}
}

if (exists("lr")) { rm(lr) }
lr <- vector()
for (lp in na.locf(datasheet$LastPosition)) {	
	#--- 1	
	if (lp == 1 || lp == 6 || lp == 7 || lp == 8) {
		lr <- c(lr, "left")
	}
	else {
		lr <- c(lr, "right")
	}
}

# Front / back+sides
if (exists("fbs")) { rm(fbs) }
fbs <- vector()
for (lp in na.locf(datasheet$LastPosition)) {	
	#--- 1	
	if (lp == 1 || lp == 2) {
		fbs <- c(fbs, "front")
	}
	else {
		fbs <- c(fbs, "backsides")
	}
}



# We add our new comulns to our database
datasheet.complete <- data.frame(	
				Participant = na.locf(datasheet$Participant),
				ExpertiseType = factor(na.locf(datasheet$ExpertiseType)),
				Test = factor(na.locf(datasheet$FragmentType)),
				Set = factor(set),				
				StimulusType = factor(stimulusTypes),
				TrajectoryType = datasheet$StimulusLocationNumber,
				Item = item,
#				Cadence_Group = column_c_groups,
				Closure = factor(datasheet$Relation_PenultimateLast),
#				Relation_FirstLast = datasheet$Relation_FirstLast,
				LastPosition = factor(datasheet$LastPosition),
				Score = datasheet$Score,
				FrontBack = factor(fb),
				LeftRight = factor(lr),
				FrontBackSides = factor(fbs)
)
summary(datasheet.complete)    #show result



