ctakes-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jtgr...@apache.org
Subject svn commit: r1585156 [13/13] - in /ctakes/sandbox: ./ CollapsableDendrogram/ CollapsableDendrogram/d3/ Data/ Dendrogram/ NodeLinkTree/ ZoomablePack/
Date Sat, 05 Apr 2014 18:32:43 GMT
Added: ctakes/sandbox/ZoomablePack/index.html
URL: http://svn.apache.org/viewvc/ctakes/sandbox/ZoomablePack/index.html?rev=1585156&view=auto
==============================================================================
--- ctakes/sandbox/ZoomablePack/index.html (added)
+++ ctakes/sandbox/ZoomablePack/index.html Sat Apr  5 18:32:42 2014
@@ -0,0 +1,117 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<style>
+
+.node {
+  cursor: pointer;
+}
+
+.node:hover {
+  stroke: #000;
+  stroke-width: 1.5px;
+}
+
+.node--root {
+  stroke: #777;
+  stroke-width: 2px;
+}
+
+.node--leaf {
+  fill: white;
+}
+
+.label {
+  font: 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
+  text-anchor: middle;
+  text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff, 0 -1px 0 #fff;
+}
+
+.label,
+.node--root,
+.node--leaf {
+  pointer-events: none;
+}
+
+</style>
+<body>
+<script src="http://d3js.org/d3.v3.min.js"></script>
+<script>
+
+var margin = 10,
+    outerDiameter = 960,
+    innerDiameter = outerDiameter - margin - margin;
+
+var x = d3.scale.linear()
+    .range([0, innerDiameter]);
+
+var y = d3.scale.linear()
+    .range([0, innerDiameter]);
+
+var color = d3.scale.linear()
+    .domain([-1, 5])
+    .range(["hsl(152,80%,80%)", "hsl(228,30%,40%)"])
+    .interpolate(d3.interpolateHcl);
+
+var pack = d3.layout.pack()
+    .padding(2)
+    .size([innerDiameter, innerDiameter])
+    .value(function(d) { return d.size; })
+
+var svg = d3.select("body").append("svg")
+    .attr("width", outerDiameter)
+    .attr("height", outerDiameter)
+  .append("g")
+    .attr("transform", "translate(" + margin + "," + margin + ")");
+
+d3.json("flare.json", function(error, root) {
+  var focus = root,
+      nodes = pack.nodes(root);
+
+  svg.append("g").selectAll("circle")
+      .data(nodes)
+    .enter().append("circle")
+      .attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf"
: "node node--root"; })
+      .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
+      .attr("r", function(d) { return d.r; })
+      .style("fill", function(d) { return d.children ? color(d.depth) : null; })
+      .on("click", function(d) { return zoom(focus == d ? root : d); });
+
+  svg.append("g").selectAll("text")
+      .data(nodes)
+    .enter().append("text")
+      .attr("class", "label")
+      .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
+      .style("fill-opacity", function(d) { return d.parent === root ? 1 : 0; })
+      .style("display", function(d) { return d.parent === root ? null : "none"; })
+      .text(function(d) { return d.name; });
+
+  d3.select(window)
+      .on("click", function() { zoom(root); });
+
+  function zoom(d, i) {
+    var focus0 = focus;
+    focus = d;
+
+    var k = innerDiameter / d.r / 2;
+    x.domain([d.x - d.r, d.x + d.r]);
+    y.domain([d.y - d.r, d.y + d.r]);
+    d3.event.stopPropagation();
+
+    var transition = d3.selectAll("text,circle").transition()
+        .duration(d3.event.altKey ? 7500 : 750)
+        .attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")";
});
+
+    transition.filter("circle")
+        .attr("r", function(d) { return k * d.r; });
+
+    transition.filter("text")
+      .filter(function(d) { return d.parent === focus || d.parent === focus0; })
+        .style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; })
+        .each("start", function(d) { if (d.parent === focus) this.style.display = "inline";
})
+        .each("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
+  }
+});
+
+d3.select(self.frameElement).style("height", outerDiameter + "px");
+
+</script>

Added: ctakes/sandbox/cTakes_XML_to_JSON.py
URL: http://svn.apache.org/viewvc/ctakes/sandbox/cTakes_XML_to_JSON.py?rev=1585156&view=auto
==============================================================================
--- ctakes/sandbox/cTakes_XML_to_JSON.py (added)
+++ ctakes/sandbox/cTakes_XML_to_JSON.py Sat Apr  5 18:32:42 2014
@@ -0,0 +1,131 @@
+#!/usr/bin/python
+
+# Written by John T Green (people.apache.org/~jtgreen/) for Apache cTakes. 
+
+# Import
+import json
+from lxml import etree
+
+# Hardcoded input for now
+CASE = "./Data/GenSurg_ColonCancer_1.rtf.xml"
+JSON_OUT = "./CollapsableDendrogram/flare.json"
+
+
+# Parser with data handlers
+class parseCTakesXML:
+    
+    # Setup our starting conditions
+    def __init__(self, caseFile):
+
+        # Set up type system (tags)
+        self.cTakesTypes = ["org.apache.ctakes.typesystem.type.textsem.SignSymptomMention",
+                            "org.apache.ctakes.typesystem.type.textsem.ProcedureMention",
+                            "org.apache.ctakes.typesystem.type.textsem.AnatomicalSiteMention",
+                            "org.apache.ctakes.typesystem.type.textsem.MedicationMention",
+                            "org.apache.ctakes.typesystem.type.textsem.DiseaseDisorderMention",
+                            #"org.apache.ctakes.typesystem.type.textsem.SemanticArgument",
+                            #"org.apache.ctakes.assertion.medfacts.types.Concept"
+                            ]
+        
+        # Set values in types-tags that we want to record
+        self.notableTypeAttributes = ["historyOf",
+                                    "polarity",
+                                    "subject"
+                                    ]
+                       
+        self.noteData = {}
+        
+        self.jsonOut = {} 
+
+        # Read the xml
+        self.tree = etree.parse(caseFile)
+
+    def getConceptId(self, tag):
+        # Attribute is a (attribute, value) tuple
+        for attribute in tag.items():
+            
+            # If attribute has a case specific trigger to grab other data ...
+            if attribute[0] == "_id":
+                return attribute[1]
+               
+    # Not elegant, but for now, pass in all iterated items in current loops, then update
class variable noteData
+    def grabConceptText(self, conceptId, cTakesType):
+
+        # Technically returns a list, for now we're assuming one element list
+        concept = self.tree.xpath('descendant::*[@originalEntityExternalId="%s"]' % conceptId)
+        
+        # Make sure we grabbed info. Could be cTakesType did not have a corresponding Concept
value (like SemanticArgument for example)
+        if concept != []:
+            for conceptItem in concept[0].items():
+            
+                conceptReturn = []            
+                if conceptItem[0] == "conceptText":
+                    
+                    return conceptItem[1]
+    
+    # Add the node to json out, various checks on structure
+    # This is very case specific
+    def addToNoteData(self, cTakesType, conceptText, attribute):
+        if cTakesType.split(".")[-1:][0] in self.noteData:
+            if conceptText in self.noteData[cTakesType.split(".")[-1:][0]]:
+                self.noteData[cTakesType.split(".")[-1:][0]][conceptText].append(attribute)
+            else:
+                self.noteData[cTakesType.split(".")[-1:][0]][conceptText] = []
+                self.noteData[cTakesType.split(".")[-1:][0]][conceptText].append(attribute)
+        else:
+            self.noteData[cTakesType.split(".")[-1:][0]] = {}
+            self.noteData[cTakesType.split(".")[-1:][0]][conceptText] = []
+            self.noteData[cTakesType.split(".")[-1:][0]][conceptText].append(attribute)
+    
+    # Apply json specific rules to our noteData structure
+    def makeJson(self):
+        self.jsonOut = {"name":"patient", "children":[] }
+        
+        for k,v in self.noteData.iteritems():
+            self.jsonOut["children"].append( { "name":k, "children":[] } )
+            
+            for i,j in v.iteritems():
+                self.jsonOut["children"][-1:][0]["children"].append( { "name":i, "children":[]
} )
+                for x in j:
+                    self.jsonOut["children"][-1:][0]["children"][-1:][0]["children"].append(
{ "name": str(x[0] + " : " + x[1]) })
+            
+            
+    # Parse the XML
+    def parse(self):
+
+        for cTakesType in self.cTakesTypes:
+            
+            # Find all instances in tree of type
+            tags = self.tree.findall(cTakesType)
+            
+            for tag in tags:
+                
+                conceptText = self.grabConceptText( self.getConceptId(tag), cTakesType )
+                
+                # Attribute is a (attribute, value) tuple
+                attribs = []
+                for attribute in tag.items():
+                    
+                    # Else, if it's notable, grab type specific values, store ...
+
+                    if attribute[0] in self.notableTypeAttributes:
+                        
+                        #attribs.append( (attribute[0], attribute[1]) )
+                        self.addToNoteData(cTakesType, conceptText, attribute)
+        
+        self.makeJson()
+
+# MAIN
+parser = parseCTakesXML(CASE)
+parser.parse()
+
+# Json
+jsonFile = open(JSON_OUT, "w")
+
+# Output the JSON
+json.dump(parser.jsonOut, jsonFile, sort_keys=True, indent=4, separators=(',', ': '))
+
+# Clean up
+jsonFile.close()
+
+print "Done."

Propchange: ctakes/sandbox/cTakes_XML_to_JSON.py
------------------------------------------------------------------------------
    svn:executable = 



Mime
View raw message