Changeset 2572
- Timestamp:
- 11/07/08 12:16:01 (2 months ago)
- Files:
-
- trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/META-INF/MANIFEST.MF (modified) (1 diff)
- trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/AttributeAdder.java (modified) (1 diff)
- trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/AttributeExtractor.java (modified) (3 diffs)
- trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/GetMetadataAndCounts.java (added)
- trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/Helper.java (modified) (4 diffs)
- trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/NWBIntegrator.java (modified) (1 diff)
- trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/NWBSimplifier.java (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/META-INF/MANIFEST.MF
r2564 r2572 17 17 org.osgi.service.prefs;version="1.1.0" 18 18 X-AutoStart: true 19 Service-Component: OSGI-INF/component.xml20 19 Export-Package: edu.iu.nwb.templates.staticexecutable.nwb trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/AttributeAdder.java
r2564 r2572 38 38 updatedAttributes.put(this.attributeNames[ii], this.attributeExtractors[ii].nextValue()); 39 39 } 40 return attributes;40 return updatedAttributes; 41 41 } 42 42 trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/AttributeExtractor.java
r2564 r2572 6 6 import java.io.FileReader; 7 7 import java.io.IOException; 8 import java.text.DecimalFormat; 9 import java.text.NumberFormat; 10 import java.text.ParseException; 8 11 import java.util.HashMap; 9 12 import java.util.Map; … … 12 15 13 16 private BufferedReader reader; 17 private NumberFormat format = new DecimalFormat(); 14 18 15 19 public AttributeExtractor(File nodeAttributeFile) throws FileNotFoundException { … … 18 22 19 23 20 public Double nextValue() { 24 public Number nextValue() { 25 String numberString; 21 26 try { 22 return Double.valueOf(this.reader.readLine().trim()); 23 } catch (NumberFormatException e) { 24 throw new IllegalArgumentException("This algorithm behaved incorrectly. Please report it to the developers.", e); 27 numberString = this.reader.readLine().trim(); 25 28 } catch (IOException e) { 26 29 throw new IllegalArgumentException("This algorithm is unable to read a file it needs to continue.", e); 30 31 } 32 try { 33 return Double.valueOf(numberString); 34 } catch (NumberFormatException e) { 35 try { 36 return format.parse(numberString); 37 } catch (ParseException e1) { 38 throw new IllegalArgumentException("This algorithm behaved incorrectly. Please report it to the developers.", e); 39 } 27 40 } 28 41 } trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/Helper.java
r2564 r2572 39 39 public Data[] execute() throws AlgorithmExecutionException { 40 40 File nwbFile = (File) this.data[0].getData(); 41 GetNWBFileMetadata handler = new GetNWBFileMetadata(); 42 41 GetMetadataAndCounts handler = new GetMetadataAndCounts(); 42 String weightAttribute = (String) this.parameters.get("weightAttribute"); 43 43 44 try { 44 45 NWBFileParser parser = new NWBFileParser(nwbFile); … … 59 60 "Consider transforming the network into one that is all directed or all undirected, perhaps with symmetrize."); 60 61 } 62 63 if(undirectedEdgeCount == 0 && directedEdgeCount == 0) { 64 throw new AlgorithmExecutionException("This network has no edges. " + 65 "This algorithm only works on networks with edges."); 66 } 67 61 68 try { 62 69 File simpleFormat = File.createTempFile("nwb-", ".simple"); 63 70 FileOutputStream simpleOutputStream = new FileOutputStream(simpleFormat); 64 NWBFileParserHandler simplifier = new NWBSimplifier(simpleOutputStream, nodeCount, undirectedEdgeCount + directedEdgeCount ); //one of the edge counts is guaranteed to be zero71 NWBFileParserHandler simplifier = new NWBSimplifier(simpleOutputStream, nodeCount, undirectedEdgeCount + directedEdgeCount, weightAttribute); //one of the edge counts is guaranteed to be zero 65 72 new NWBFileParser(nwbFile).parse(simplifier); 73 simpleOutputStream.flush(); 66 74 simpleOutputStream.close(); 67 75 … … 73 81 List forEdges = new ArrayList(); 74 82 Data[] output = realAlgorithm.execute(); //let any exceptions bubble up, they're already real exceptions 83 System.err.println("SEA Length is: " + output.length); 75 84 Data firstAttributeData = null; 76 85 for(int ii = 0; ii < output.length; ii++) { 77 86 Data outputData = output[ii]; 78 String label = (String) outputData.getMetadata().get(DataProperty.LABEL);79 if( label.endsWith(".nodes")) {87 String fileName = ((File) outputData.getData()).getName(); 88 if(fileName.endsWith(".nodes")) { 80 89 forNodes.add(outputData.getData()); //always a file object 81 90 if(firstAttributeData == null) { 82 91 firstAttributeData = outputData; 83 92 } 84 } else if( label.endsWith(".edges")) {93 } else if(fileName.endsWith(".edges")) { 85 94 forEdges.add(outputData.getData()); //ditto 86 95 if(firstAttributeData == null) { … … 91 100 } 92 101 } 93 File realFormat = File.createTempFile("nwb-", ".nwb"); 94 FileOutputStream realOutputStream = new FileOutputStream(realFormat); 95 try { 96 NWBFileParserHandler integrator = new NWBIntegrator(realOutputStream, forNodes, forEdges); 97 new NWBFileParser(nwbFile).parse(integrator); 98 } catch(IllegalArgumentException e) { 99 //quite awful, but the handler methods can't have throws added 100 throw new AlgorithmExecutionException(e.getMessage(), e.getCause()); 102 103 if(firstAttributeData != null) { 104 File realFormat = File.createTempFile("nwb-", ".nwb"); 105 FileOutputStream realOutputStream = new FileOutputStream(realFormat); 106 try { 107 NWBFileParserHandler integrator = new NWBIntegrator(realOutputStream, forNodes, forEdges); 108 new NWBFileParser(nwbFile).parse(integrator); 109 } catch(IllegalArgumentException e) { 110 //quite awful, but the handler methods can't have throws added 111 throw new AlgorithmExecutionException(e.getMessage(), e.getCause()); 112 } 113 realOutputStream.close(); 114 //make data of realFormat, stick at beginning of transformedOutput, turn into array, and give it back 115 116 Data nwbOutput = new BasicData(firstAttributeData.getMetadata(), realFormat, "text/nwb"); 117 118 transformedOutput.add(0, nwbOutput); 101 119 } 102 realOutputStream.close();103 //make data of realFormat, stick at beginning of transformedOutput, turn into array, and give it back104 105 Data nwbOutput = new BasicData(firstAttributeData.getMetadata(), realFormat, "text/nwb");106 107 transformedOutput.add(0, nwbOutput);108 109 120 return (Data[]) transformedOutput.toArray(new Data[]{}); 110 121 trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/NWBIntegrator.java
r2564 r2572 24 24 25 25 this.nodeAttributeAdder = new AttributeAdder(forNodes, ".nodes"); 26 System.err.println("New attributes for nodes: " + forNodes.size()); 26 27 this.edgeAttributeAdder = new AttributeAdder(forEdges, ".edges"); 27 28 trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/NWBSimplifier.java
r2564 r2572 13 13 private PrintWriter output; 14 14 private Map nodeIds = new HashMap(); 15 private String weightAttribute; 15 16 16 public NWBSimplifier(OutputStream outputStream, int numberOfNodes, int numberOfEdges ) {17 this.output = new PrintWriter(outputStream );17 public NWBSimplifier(OutputStream outputStream, int numberOfNodes, int numberOfEdges, String weightAttribute) { 18 this.output = new PrintWriter(outputStream, true); 18 19 this.writeHeader(this.output, numberOfNodes, numberOfEdges); 20 this.weightAttribute = weightAttribute; 19 21 } 20 22 … … 25 27 26 28 private void writeHeader(PrintWriter output, int numberOfNodes, int numberOfEdges) { 27 output.println(numberOfNodes);28 output.println(numberOfEdges);29 this.output.println(numberOfNodes); 30 this.output.println(numberOfEdges); 29 31 } 30 32 31 private void writeEdge(PrintWriter output, int source, int target) { 32 output.print(source); 33 output.print(" "); 34 output.println(target); 33 private void writeEdge(PrintWriter output, int source, int target, double value) { 34 this.output.print(source); 35 this.output.print(" "); 36 this.output.print(target); 37 this.output.print(" "); 38 this.output.println(value); 35 39 } 36 40 37 private void addEdge(int source, int target ) {41 private void addEdge(int source, int target, double value) { 38 42 int fakeSource = ((Integer) this.nodeIds.get(new Integer(source))).intValue(); 39 43 int fakeTarget = ((Integer) this.nodeIds.get(new Integer(target))).intValue(); 40 this.writeEdge(output, fakeSource, fakeTarget );44 this.writeEdge(output, fakeSource, fakeTarget, value); 41 45 } 42 46 43 47 public void addDirectedEdge(int sourceNode, int targetNode, Map attributes) { 44 addEdge(sourceNode, targetNode); 48 double weight = ((Number) attributes.get(weightAttribute)).doubleValue(); 49 addEdge(sourceNode, targetNode, weight); 45 50 } 46 51 47 52 public void addUndirectedEdge(int node1, int node2, Map attributes) { 48 add Edge(node1, node2);53 addDirectedEdge(node1, node2, attributes); 49 54 } 50 55 }
