unit Score_utilities_unit;interface	uses		Globals_unit, Utilities_unit, dialog_unit;	const		trim = 1000;   {number of milleseconds allowed for reaction time - greater than trim rts are not scored}		lower_trim = 100;   {faster than this is counted as an anticipation}	var{Data: RT_type;         -- found by the Read_data procedure  - declared in RT-variables}{fixed_free, }		long_output, quantiles, reciprocal, very_old_data, old_data, just_one: boolean;  {parameters set in the menus and fixed in set_params}		medium_output, short_output, reliability, special_read: boolean;{cutoff, }		lower_cutoff: real;   {number of milleseconds allowed for reaction time  (converted to ticks if not MS timer)}		blocks: integer;		maximum_items_to_score: integer;  {normally maxint, but can be set by menu selection}		problem_number: array[1..20] of boolean;		rt_score_options: rt_score_options_rectype;	procedure Read_data (old_data_name: string; var number_of_trials: integer);  {replace with your read data procedure}                          {data are read and stored in global variable: Data:RT_Type}                          {the number_of_trials is the length of the Data vector}	procedure Set_parameters;    {convert the Menu items to parameters}	procedure sort (count: integer; var vector: vectortype);	procedure write_means (count: integer; vector: vectortype);  {of each bin separately}implementation	procedure Read_Normal_data (old_data_name: string; var number_of_trials: integer);		var			loop, dummy: integer;			dummy_line: string;	begin		IOCheck(false);		reset(datafile, old_data_name);		if IOResult <> 0 then			Error_messages(IOResult);		number_of_trials := 0;		Showtext;		if rt_score_options.version = 4 then     {clear out the 3 lines of identification information}			begin				readln(datafile, dummy_line);				readln(datafile, dummy_line);				readln(datafile, dummy_line);			end;		loop := 1;		if shMenu.seven[1] then  {only if requested do we write out these summary labels}			begin				writeln('Trial  type location  semantic    response  correct anticip  miss  rt    flash');				writeln(outfile, 'Trial  type   location   semantic    response    correct   anticipated    missed   rt   flash');			end;		repeat			with RT_Data[loop] do   {read the data file in three different formats, depending on how old the data are}				begin					case rt_score_options.version of						1:  {old style data, without anticipations and misses}							begin								readln(datafile, dummy, problem, word, semantic, ISI, response, correct, rt);								if IOResult <> 0 then									Error_messages(IOResult);								anticipated := 0;   {fill in the missing values}								missed := 0;								if response = 99 then									response := 0;   {some of these very old data counted a mouse as a 99}							end;						2:{newer style data, with anticipations and misses  -- the 92-93 style}							begin								readln(datafile, dummy, problem, word, semantic, ISI, response, correct, anticipated, missed, rt);								if IOResult <> 0 then									Error_messages(IOResult);								if special_read and (anticipated > 0) then									anticipated := anticipated - 1   {at least in FIAT, anticipations were not recorded correctly}								else if (response = 255) and false then  {skip this part except for the Rob experiments}									begin										missed := missed + 1;{at least for ROB1/2, missings and rts were messed up}										rt := 0;									end							end;  {old_data}						3, 4: {the default case for post 93 data}							readln(datafile, dummy, problem, word, semantic, ISI, response, correct, anticipated, missed, rt, flash);  {the 93- style, the new default}					end;  {case of version}					if very_old_data or old_data then						begin							flash := false;  {not actually recorded}							if word = 2 then								flash := true							else								flash := false;						end; {very_old_data or old_data}					if problem = 1 then						if response <> 255 then   {corrects were not scored correctly in old data}							correct := true  {a button was pressed, but could have been too slow}						else							begin								correct := false;      {this was a failure to respond}								rt := 0;   {the subject failed to respond and thus has no rt}                                {note that rt's on missed trials are stored (incorrectly) as the rt from the previous trial}							end;					if response <> opt_q then   {the option q trial should be ignored -- this was the last trial and the experiment was stopping}						loop := loop + 1;					if ShMenu.seven[1] then   {show the raw data}{$R-}						begin							writeln(dummy : 5, problem : 5, word : 5, semantic : 10, response : 10, correct : 10, anticipated : 5, missed : 5, rt * ticks : 10 : 0, flash : 10);{datafile,}							writeln(outfile, dummy : 5, tab, problem : 5, tab, word : 5, tab, semantic : 10, tab, response : 10, tab, correct : 10, tab, anticipated : 5, tab, missed : 5, tab, rt * ticks : 10 : 0, tab, flash);						end;{$R+}				end;{of with data[loop]}		until (eof(datafile) or (loop > maximum_items_to_score) or ((dummy = RT_Data[loop].word) and (RT_Data[loop].word = 0)));{normally the eof condition}		number_of_trials := loop;        {how many trials worth of data did we read?}		close(datafile);		IOCheck(true);	end;{Read_data}	procedure Read_special_data (old_data_name: string; var number_of_trials: integer);  {this can be replaced with any special read set of commands}{This particular one reads data for Kathy Nugent's experiments}{and compensates for some awkward output by the earlier program}		var			loop, stimulus, dummy, dummy2, isi2: integer;			dummy1: real;			dummy_str: string;			feedback: boolean;			response2: integer;			str1, str2, str3, str4, str5: char;	begin		IOCheck(false);		reset(datafile, old_data_name);		if IOResult <> 0 then			Error_messages(IOResult);		number_of_trials := 0;		Showtext;		for loop := 1 to 5 do    {first, get rid of identification stuff}			begin				readln(datafile, dummy_str);				if IOResult <> 0 then					Error_messages(IOResult);			end;		loop := 1;		if shMenu.seven[1] then  {only if requested do we write out these summary labels}			begin				writeln('Trial  type location  semantic    response  correct   rt    feedback');				writeln(outfile, 'Trial  type location  semantic    response  correct   rt    feedback');			end;		number_of_trials := 228;        {how many trials worth of data should we read?}		for loop := 1 to number_of_trials do			begin				with RT_Data[loop] do   {read the data file }					begin						problem := 4;   {a problem with stimulus-response left and right compatibility}						readln(datafile, dummy1, stimulus, str1, str2, rt, str3, str4, isi, feedback, isi2);  {read in the response, which unfortunately is a tab followed by a character}						if IOResult <> 0 then							Error_messages(IOResult);						case stimulus of							1, 2:    {1 is left arrow on left, 2 is right arrow on right}								semantic := true;							3, 4: {3  right arrow on left  , 4 left arrow on right}								semantic := false;						end;  {of case}						response := ord(str2);						response2 := ord(str4);						case stimulus of							1, 4: 								begin									word := 1;									if response = z then										correct := true									else										correct := false;								end;							2, 3: 								begin									word := 2;									if response = slash then										correct := true									else										correct := false;								end;						end; {of case}						if feedback = true or ((feedback = false) and correct) then							flash := true						else							flash := false;						if ShMenu.seven[1] then   {show the raw data}{$R-}							begin								writeln(dummy1 : 5 : 0, problem : 5, word : 5, semantic : 10, response : 10, correct : 10, rt * ticks : 10 : 0, flash : 10);{datafile,}								writeln(outfile, dummy1 : 5 : 0, tab, problem : 5, tab, word : 5, tab, semantic : 10, tab, response : 10, tab, correct : 10, tab, rt * ticks : 10 : 0, tab, flash);{$R+}							end;					end;{of with loop}			end; {of repeat loop}		close(datafile);		IOCheck(True);	end;{Read_special_data}	procedure Read_data;  {(old_data_name: string; var number_of_trials: integer); }	begin		if special_read then			Read_special_data(old_data_name, number_of_trials)		else			read_normal_data(old_data_name, number_of_trials);	end;	procedure Set_parameters;		var			i, loop, dummy1: integer;			dummy: boolean;	begin		for loop := 1 to 20 do			problem_number[loop] := false;		with rt_options do			begin				if simple then					problem_number[1] := true;				if directional then					problem_number[2] := true;				if choice then					problem_number[3] := true;				if semantic then					problem_number[4] := true;				if vertical then					problem_number[8] := true;				if yes_no then					problem_number[9] := true;				if affective then					problem_number[10] := true;			end;		with rt_score_options do {used in the RT programs to set parameters for scoring}			begin				version := 4;   {1 is pre92, 2 is 92-93, 3 is 93-94, 4 is late 94}				trim := true;				trim_value := 1000;				reliability := false;				quantiles := false;				reciprocals := false;				ntrials := true;				mean := true;				sd := true;				r := true;				slope := false;				n_miss := false;				n_anticipated := false;				dprime := false;				beta := false;				aprime := false;				bdoubleprime := false;				output := 1;  {short}				one_at_a_time := false;				special_read := false;			end;		if not summarized then			dummy := get_parameters_RT_scores(140, rt_score_options);		if shMenu.seven[4] then    {should we use millisecond timing  (Mac plus or earlier}			MacPlus := true		else			MacPlus := false;		if ShMenu.nine[5] then  {default is to trim at 1000 ms}			cutoff := 99999		else if MacPlus then    {if using ms timer option}			begin				ticks := 1.0;				cutoff := trim;			end		else			begin             {default is to trim at 1000 ms but converted from ticks}				ticks := 1000 / 60;				cutoff := trim / ticks;				lower_cutoff := lower_trim / ticks;			end;		blocks := 3;		very_old_data := false;		old_data := false;		case rt_score_options.version of			1: 				very_old_data := true;			2: 				old_data := true;			3, 4: 				;		end;		if rt_score_options.quantiles then			quantiles := true		else			quantiles := false;		if rt_score_options.one_at_a_time then			just_one := true		else			just_one := false;		long_output := false;		medium_output := false;		short_output := false;		case rt_score_options.output of			0: 				;			1: 				short_output := true;			2: 				medium_output := true;			3: 				long_output := true;		end;		if rt_score_options.reliability then			reliability := true		else			reliability := false;		if rt_score_options.special_read then			special_read := true		else			special_read := false;		if rt_score_options.reciprocals then			reciprocal := true		else			reciprocal := false;		maximum_items_to_score := maxint; {normally read until eof on the data}		for loop := 1 to num_options do			begin				if ShMenu.four[loop] then					begin						i := loop;						case i of							1: 								maximum_items_to_score := 10;							2: 								maximum_items_to_score := 20;							3: 								maximum_items_to_score := 30;							4: 								maximum_items_to_score := 40;							5: 								maximum_items_to_score := 60;							6: 								maximum_items_to_score := 120;							7: 								maximum_items_to_score := 144;							8: 								maximum_items_to_score := 180;							9: 								maximum_items_to_score := 240;						end;					end;			end; {finding out the maximum number of items to score}		for loop := 1 to num_options do {set the parameters from ShMenu.nine}			begin				if ShMenu.nine[loop] then					begin						i := loop;						case i of							1: 								very_old_data := true;							2: 								old_data := true;							3: 								long_output := true;							4: 								quantiles := true;							6: 								just_one := true;							7: 								medium_output := true;							8: 								short_output := true;							9: 								reliability := true;							10: 								special_read := true;							11: 								reciprocal := true;						end;					end;			end;	end;  {set parameters}	procedure sort;  {(count: integer; var vector: vectortype );}		var			min, max, loop, loop1, temp, maxloc, minloc: integer;	begin		for loop1 := 1 to count do			begin				min := maxint;				for loop := loop1 to count do					begin						if vector[loop] < min then							begin								min := vector[loop];								minloc := loop;							end;					end; {end loop}				temp := vector[minloc];        {swap minimum values}				vector[minloc] := vector[loop1];				vector[loop1] := temp;			end; {end loop1}	end; {sort}	procedure sort1 (count: integer; var vector: vectortype);   {doesn't quite work, replaced with sort}		var                                                      {should be faster, but confuses maximum values}			min, max, loop, loop1, temp, maxloc, minloc: integer;	begin		for loop1 := 1 to count div 2 do			begin				min := maxint;				max := -min;				for loop := loop1 to count - loop1 + 1 do					begin						if vector[loop] < min then							begin								min := vector[loop];								minloc := loop;							end						else if vector[loop] > max then							begin								max := vector[loop];								maxloc := loop;							end;					end; {loop}				temp := vector[minloc];        {swap minimum values}				vector[minloc] := vector[loop1];				vector[loop1] := temp;                                                         {swap maximum values}				if maxloc = loop1 then      {first check for problematic case}					temp := vector[minloc]    {this is the value that had been in vector[loop1] before the minimum was switched}				else					temp := vector[maxloc];                                                         {swap values and store maximum value}				vector[maxloc] := vector[count - loop1 + 1];				vector[count - loop1 + 1] := temp;			end; {loop1}		writeln;		for loop1 := 15 to count do			write(vector[loop1] : 4);		writeln;	end; {sort}	procedure write_means; {(count: integer; vector: vectortype);  of each bin separately}		var			bins, loop, loop1, base, bin_size_int: integer;			sum, means, bin_size: real;	begin		bin_size := count div max_bins;   {how many elements per bin}		bin_size_int := round(bin_size);		base := 0;		for bins := 1 to max_bins do			begin				sum := 0.0;				for loop := 1 to bin_size_int do					sum := sum + vector[base + loop];				means := sum / bin_size;				write(means : 5);				write(outfile, tab, means : 5);				base := base + bin_size_int;			end; {bins}		writeln;	end; {procedure write_means}end.