wiki:SecondQuery

Version 4 (modified by detwiler, 12 years ago) (diff)

--

return to XbrainEvaluation

import module namespace math = "http://sig.biostr.washington.edu/dxq/math/" at "http://bursa:8080/XQueryLibraries/math.xq";

declare function  local:distinct-deep($nodes as node()*)  as node()* 
{     
   for $node at $x in $nodes
   return $node[fn:not(some $otherNode in fn:subsequence($nodes, $x + 1)
                    satisfies fn:deep-equal($otherNode, $node))]
} ;

let $patients_with_semantic_errors :=
	dxq:csm("
		<results>
		<TransformServerParam coord='site' source='0' target=''/>
		{
			let $codes := ('2','6')
			for $p in $pv/patient
			where $p/surgery/csmstudy/trial[stimulated='Y']/trialcode/term[type='CSM error code']/abbrev/text()
				=$codes
			return 
				<patient>
					{$p/pnum}
					{$p/sex}
					{
						for $trial in $p/surgery/csmstudy/trial[stimulated='Y']
						where $trial/trialcode/term[type='CSM error code']/abbrev/text()=$codes
						return
							<trial>
								{$trial/_oid}
								<trialcodes>
								{
									for $code in $trial/trialcode/term[type='CSM error code']/abbrev
									return
										<trialcode>{$code}</trialcode>
								}
								</trialcodes>
								{
									for $sitemap_elt in 
										$p/surgery/sitetoanatomymap[preference='1']/
											sitetoanatomymapelement[stimsite/_oid = $trial/stimsite1/_oid]
									return
										<site x0='{$sitemap_elt/right_coord}' 
											y0='{$sitemap_elt/sup_coord}' z0='{$sitemap_elt/ant_coord}'
											space0='{concat('P',$p/pnum)}' space='MNI'/>
								}
							</trial>
					}
				</patient>
		} 
		</results>
	")
	
(: normalize coordinates :)
let $norm_patients_with_semantic_errors := dxq:trans($patients_with_semantic_errors)

	
(: create point objects for all sites for each patient :)
let $patient_points :=
	for $patient in $norm_patients_with_semantic_errors//patient
	let $sites := $patient//site
	return
		if(exists($sites))
		then
			<patient>
				{$patient/pnum}
				{$patient/sex}
				<sites>
				{
					for $site in $sites
					return
						if(string(number($site/@x))!='NaN' and
							string(number($site/@y))!='NaN' and
							string(number($site/@z))!='NaN')
						then
							<point>
								<x>{data($site/@x)}</x>
								<y>{data($site/@y)}</y>
								<z>{data($site/@z)}</z>
							</point>
						else()
				}
				</sites>
			</patient>
		else()
			
(: determine standard deviation in pair distances (intra-patient) :)
let $male_patient_stnd_devs :=
	for $patient in $patient_points
	let $unique_points := local:distinct-deep($patient[sex='M']//point)
	return
		if(count($unique_points)>1)
		then
			math:standard_deviation($unique_points)
		else()
		
let $female_patient_stnd_devs :=
	for $patient in $patient_points
	let $unique_points := local:distinct-deep($patient[sex='F']//point)
	return
		if(count($unique_points)>1)
		then
			math:standard_deviation($unique_points)
		else()
		
let $avg_male_dev := avg($male_patient_stnd_devs)
let $avg_female_dev := avg($female_patient_stnd_devs)
	
return
	<results>
		<male>
			<avg_standard_deviation>{$avg_male_dev}</avg_standard_deviation>
		</male>
		<female>
			<avg_standard_deviation>{$avg_female_dev}</avg_standard_deviation>
		</female>
	</results>