Changeset 2572

Show
Ignore:
Timestamp:
11/07/08 12:16:01 (2 months ago)
Author:
rduhon
Message:

Bring this into shape.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/META-INF/MANIFEST.MF

    r2564 r2572  
    1717 org.osgi.service.prefs;version="1.1.0" 
    1818X-AutoStart: true 
    19 Service-Component: OSGI-INF/component.xml 
    2019Export-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  
    3838                        updatedAttributes.put(this.attributeNames[ii], this.attributeExtractors[ii].nextValue()); 
    3939                } 
    40                 return attributes; 
     40                return updatedAttributes; 
    4141        } 
    4242 
  • trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/AttributeExtractor.java

    r2564 r2572  
    66import java.io.FileReader; 
    77import java.io.IOException; 
     8import java.text.DecimalFormat; 
     9import java.text.NumberFormat; 
     10import java.text.ParseException; 
    811import java.util.HashMap; 
    912import java.util.Map; 
     
    1215         
    1316        private BufferedReader reader; 
     17        private NumberFormat format = new DecimalFormat(); 
    1418 
    1519        public AttributeExtractor(File nodeAttributeFile) throws FileNotFoundException { 
     
    1822 
    1923 
    20         public Double nextValue() { 
     24        public Number nextValue() { 
     25                String numberString; 
    2126                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(); 
    2528                } catch (IOException e) { 
    2629                        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                                } 
    2740                } 
    2841        } 
  • trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/Helper.java

    r2564 r2572  
    3939        public Data[] execute() throws AlgorithmExecutionException { 
    4040                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                 
    4344                try { 
    4445                        NWBFileParser parser = new NWBFileParser(nwbFile); 
     
    5960                        "Consider transforming the network into one that is all directed or all undirected, perhaps with symmetrize."); 
    6061                } 
     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                 
    6168                try { 
    6269                        File simpleFormat = File.createTempFile("nwb-", ".simple"); 
    6370                        FileOutputStream simpleOutputStream = new FileOutputStream(simpleFormat); 
    64                         NWBFileParserHandler simplifier = new NWBSimplifier(simpleOutputStream, nodeCount, undirectedEdgeCount + directedEdgeCount); //one of the edge counts is guaranteed to be zero 
     71                        NWBFileParserHandler simplifier = new NWBSimplifier(simpleOutputStream, nodeCount, undirectedEdgeCount + directedEdgeCount, weightAttribute); //one of the edge counts is guaranteed to be zero 
    6572                        new NWBFileParser(nwbFile).parse(simplifier); 
     73                        simpleOutputStream.flush(); 
    6674                        simpleOutputStream.close(); 
    6775 
     
    7381                        List forEdges = new ArrayList(); 
    7482                        Data[] output = realAlgorithm.execute(); //let any exceptions bubble up, they're already real exceptions 
     83                        System.err.println("SEA Length is: " + output.length); 
    7584                        Data firstAttributeData = null; 
    7685                        for(int ii = 0; ii < output.length; ii++) { 
    7786                                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")) { 
    8089                                        forNodes.add(outputData.getData()); //always a file object 
    8190                                        if(firstAttributeData == null) { 
    8291                                                firstAttributeData = outputData; 
    8392                                        } 
    84                                 } else if(label.endsWith(".edges")) { 
     93                                } else if(fileName.endsWith(".edges")) { 
    8594                                        forEdges.add(outputData.getData()); //ditto 
    8695                                        if(firstAttributeData == null) { 
     
    91100                                } 
    92101                        } 
    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); 
    101119                        } 
    102                         realOutputStream.close(); 
    103                         //make data of realFormat, stick at beginning of transformedOutput, turn into array, and give it back 
    104                          
    105                         Data nwbOutput = new BasicData(firstAttributeData.getMetadata(), realFormat, "text/nwb"); 
    106                          
    107                         transformedOutput.add(0, nwbOutput); 
    108                          
    109120                        return (Data[]) transformedOutput.toArray(new Data[]{}); 
    110121 
  • trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/NWBIntegrator.java

    r2564 r2572  
    2424                 
    2525                this.nodeAttributeAdder = new AttributeAdder(forNodes, ".nodes"); 
     26                System.err.println("New attributes for nodes: " + forNodes.size()); 
    2627                this.edgeAttributeAdder = new AttributeAdder(forEdges, ".edges"); 
    2728                 
  • trunk/plugins/shared/edu.iu.nwb.templates.staticexecutable.nwb/src/edu/iu/nwb/templates/staticexecutable/nwb/NWBSimplifier.java

    r2564 r2572  
    1313        private PrintWriter output; 
    1414        private Map nodeIds = new HashMap(); 
     15        private String weightAttribute; 
    1516         
    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); 
    1819                this.writeHeader(this.output, numberOfNodes, numberOfEdges); 
     20                this.weightAttribute = weightAttribute; 
    1921        } 
    2022         
     
    2527         
    2628        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); 
    2931        } 
    3032         
    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); 
    3539        } 
    3640         
    37         private void addEdge(int source, int target) { 
     41        private void addEdge(int source, int target, double value) { 
    3842                int fakeSource = ((Integer) this.nodeIds.get(new Integer(source))).intValue(); 
    3943                int fakeTarget = ((Integer) this.nodeIds.get(new Integer(target))).intValue(); 
    40                 this.writeEdge(output, fakeSource, fakeTarget); 
     44                this.writeEdge(output, fakeSource, fakeTarget, value); 
    4145        } 
    4246         
    4347        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); 
    4550        } 
    4651         
    4752        public void addUndirectedEdge(int node1, int node2, Map attributes) { 
    48                 addEdge(node1, node2); 
     53                addDirectedEdge(node1, node2, attributes); 
    4954        } 
    5055}