wiki:SecondQuery

Version 3 (modified by anonymous, 12 years ago) (diff)

--

return to XbrainEvaluation

import module namespace math = "http://sig.biostr.washington.edu/dxq/math/" at "http://localhost: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>
		{
			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>
											{$sitemap_elt/right_coord}
											{$sitemap_elt/sup_coord}
											{$sitemap_elt/ant_coord}
										</site>
								}
							</trial>
					}
				</patient>
		} 
		</results>
	")
	
(: create point objects for all sites for each patient :)
let $patient_points :=
	for $patient in $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
						<point>
							<x>{$site/right_coord/text()}</x>
							<y>{$site/sup_coord/text()}</y>
							<z>{$site/ant_coord/text()}</z>
						</point>
				}
				</sites>
			</patient>
		else()
			
(: determine standard deviation in from centroid (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>