Changeset 2403

Show
Ignore:
Timestamp:
08/12/08 14:06:14 (3 months ago)
Author:
kelleyt
Message:

added support for calculating the structural robustness of a state space graph, also removed much of the memory dependency for the algorithm, can now handle much larger graphs.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plugins/analysis/edu.iu.nwb.analysis.extractattractors/OSGI-INF/algorithm.properties

    r2380 r2403  
    22label=Extract and Annotate Attractors 
    33description=Evaluates a set of functions defining the dynamics of a discrete network, creates the dependency graphs, and generates the state space graph 
    4 in_data=prefuse.data.Graph,prefuse.data.Table 
     4in_data=file:text/nwb,prefuse.data.Table 
    55out_data=file:text/nwb,prefuse.data.Table 
    66service.pid=edu.iu.nwb.analysis.extractattractors.algorithms.ExtractAttractorsAlgorithm 
  • trunk/plugins/analysis/edu.iu.nwb.analysis.extractattractors/src/edu/iu/nwb/analysis/extractattractors/algorithms/ExtractAttractorsAlgorithm.java

    r2167 r2403  
    11package edu.iu.nwb.analysis.extractattractors.algorithms; 
    22 
     3import java.io.BufferedReader; 
    34import java.io.File; 
     5import java.io.FileReader; 
     6import java.io.IOException; 
    47import java.util.Dictionary; 
    58 
     
    1417import org.osgi.service.log.LogService; 
    1518 
    16 import prefuse.data.Graph; 
    1719import prefuse.data.Table; 
    1820import edu.iu.nwb.analysis.extractattractors.components.ExtractAttractorBasins; 
     21import edu.iu.nwb.analysis.extractattractors.components.SimpleStateSpaceGraph; 
    1922 
    2023 
     
    4447        public Data[] execute() throws AlgorithmExecutionException{ 
    4548                Table nameTable; 
    46                 Graph stateSpace; 
     49                //Graph stateSpace; 
     50                File stateSpace; 
     51                SimpleStateSpaceGraph sssg; 
    4752                Object[] data = new Object[2]; 
    4853                int graphIndex = getInputData(data); 
    49                  
    50                 //dependencyGraph = (Graph)data[0].getData(); 
    51                  
    52 /* 
    53                 String nodeStates = this.getParameter(ParameterDefinitions.NODESTATES); 
     54 
     55                String nodeStates = this.getParameter("noStates"); 
    5456 
    5557                int numberOfStates = new Integer(nodeStates).intValue(); 
     
    5759                        throw new AlgorithmExecutionException("There must be at least two states to correctly analyze these expressions.\n"); 
    5860                }        
    59                  
    60                  
    61 */ 
    62                  
     61 
     62 
     63 
    6364                String labelColumn = this.parameters.get("labelColumn").toString(); 
    64                  
     65 
    6566                if(graphIndex > -1){ 
    66                         stateSpace = (Graph)data[0]; 
     67                         
     68                        stateSpace = (File)data[0]; 
    6769                        nameTable = (Table)data[1]; 
    68                 try{ 
     70                        try{ 
     71                                sssg=getStateSpaceGraph(stateSpace); 
    6972                         
    70                         monitor.start(ProgressMonitor.WORK_TRACKABLE, stateSpace.getNodeCount()); 
    71                         ExtractAttractorBasins eab = new ExtractAttractorBasins(); 
    72                         eab.extractAttractorBasins(stateSpace, 0, true,nameTable,labelColumn,this.monitor); 
    73                         File[] attractorBasins = eab.getBasins(); 
    74                         File[] attractorTables = eab.getAttractors(); 
    75                         int[] attractorSizes = eab.getBasinSizes(); 
    76                         Data[] returnData = new Data[2*attractorBasins.length]; 
    77                         for(int i = 0; i < attractorBasins.length; i++){ 
    78                                 returnData[2*i] = constructData(this.data[0],attractorBasins[i],"file:text/nwb",DataProperty.NETWORK_TYPE,"Attractor Basin " + (i+1) + " of " + attractorSizes[i] + " nodes"); 
    79                                 returnData[(2*i)+1] = constructData(this.data[0],attractorTables[i],"file:text/csv",DataProperty.MATRIX_TYPE,"Table view of Attractor " + (i+1)); 
     73                                monitor.start(ProgressMonitor.WORK_TRACKABLE, sssg.getSize()); 
     74                                ExtractAttractorBasins eab = new ExtractAttractorBasins(monitor); 
     75                         
     76                                eab.weakComponentCalculation(sssg, nameTable, labelColumn, numberOfStates); 
     77                                File[] attractorBasins = eab.getBasins(); 
     78                                File[] attractorTables = eab.getAttractors(); 
     79                                File attractorRobustness = eab.getRobustnessFile(); 
     80                                int[] attractorSizes = eab.getBasinSizes(); 
     81                                Data[] returnData = new Data[(2*attractorBasins.length)+1]; 
     82                                returnData[0] = constructData(this.data[0],attractorRobustness,"file:text/csv",DataProperty.TABLE_TYPE,"Table of the attractors coherency statistics"); 
     83                                for(int i = 1; i < attractorBasins.length+1; i++){ 
     84                                        returnData[(2*i)-1] = constructData(this.data[0],attractorBasins[i-1],"file:text/nwb",DataProperty.NETWORK_TYPE,"Attractor Basin " + (i) + " of " + attractorSizes[i-1] + " nodes"); 
     85                                        returnData[(2*i)] = constructData(this.data[0],attractorTables[i-1],"file:text/csv",DataProperty.TABLE_TYPE,"Table view of Attractor " + (i)); 
    8086                                } 
    81                         monitor.done(); 
    82                         return returnData; 
    83                          
    84                          
    85                 }catch(InterruptedException ie){ 
    86                         throw new AlgorithmExecutionException("There was an error in calculating the basins of attraction.",ie); 
    87                 }                        
    88  
    89         } 
    90         else{ 
    91                 throw new AlgorithmExecutionException("The correct input data was not provided."); 
    92         } 
     87 
     88                                monitor.done(); 
     89                                return returnData; 
     90 
     91 
     92                        }catch(InterruptedException ie){ 
     93                                throw new AlgorithmExecutionException("There was an error in calculating the basins of attraction.",ie); 
     94                        }                        
     95 
     96                } 
     97                else{ 
     98                        throw new AlgorithmExecutionException("The correct input data was not provided."); 
     99                } 
    93100        } 
    94101 
     
    112119        } 
    113120 
    114         private static boolean intStringToBoolean(String s){ 
    115                 int value = new Integer(s).intValue(); 
    116                 if(value >= 1){ 
    117                         return true; 
    118                 } 
    119                 return false; 
    120         } 
    121  
    122         private static String[] handleOptionalData(boolean b, String s, String errorMessage) throws AlgorithmExecutionException{ 
    123                 String[] returnValue = null; 
    124                 if(!b){ 
    125                         if(s.trim().equals("") || s == null){ 
    126                                 throw new AlgorithmExecutionException(errorMessage); 
    127                         } 
    128                         returnValue = s.split("\\s+"); 
    129                 } 
    130                 return returnValue; 
    131         } 
     121        private int getInputData(Object[] dataList){ 
     122                int graphIndex = -1; 
     123                if (data.length != 2) { 
     124                        logger.log (LogService.LOG_ERROR,  
     125                                        "Error: This algorithm requires two datasets as inputs: a graph/network "+ 
     126                        "and a node list with the instruction of merging nodes. \n"); 
     127                        return graphIndex; 
     128                }        
     129                for (int index =0; index<data.length; index++){ 
     130                        String dataFormat = data[index].getData().getClass().getName();                  
     131                        if (dataFormat.equalsIgnoreCase("java.io.File")){ 
     132                                dataList[0] = data[index].getData(); 
     133                                graphIndex = index; 
     134                        } 
     135                        else if (dataFormat.equalsIgnoreCase("prefuse.data.Table")) 
     136                                dataList[1] = data[index].getData(); 
     137                        else { 
     138                                logger.log (LogService.LOG_ERROR,  
     139                                                "Error: the data format of the input dataset is "+dataFormat+",\n"+ 
     140                                                "This algorithm requires the following data formats as inputs: \n"+ 
     141                                                "       file:text/nwb for a graph/network and \n"+ 
     142                                "       prefuse.data.Table for a node list. \n"); 
     143                                return -1; 
     144                        } 
     145                } 
     146                if (dataList[1]==null){ 
     147                        logger.log (LogService.LOG_ERROR,  
     148                                        "Error: This algorithm did not get prefuse.data.Table for a node list as one of the inputs. \n"); 
     149                        return -1; 
     150                }        
     151                if(dataList[0] == null){ 
     152                        logger.log (LogService.LOG_ERROR,  
     153                                        "Error: This algorithm did not get prefuse.data.Graph for a graph/network as one of the inputs. \n"); 
     154                        return -1; 
     155                } 
     156                return graphIndex; 
     157        } 
     158 
     159        private static SimpleStateSpaceGraph getStateSpaceGraph(File nwbFile) throws AlgorithmExecutionException{ 
     160                SimpleStateSpaceGraph sssg = new SimpleStateSpaceGraph(); 
     161                try{ 
     162                        FileReader nwbFileReader = new FileReader(nwbFile); 
     163                        BufferedReader bufferedReader = new BufferedReader(nwbFileReader); 
     164                        String line; 
     165                        line = bufferedReader.readLine(); 
     166                        line = bufferedReader.readLine(); 
     167                        int nodeCount = 1; 
     168                        line = bufferedReader.readLine(); 
     169                        String label =""; 
     170                        String[] attributes = line.split("\\s+"); 
     171                        int i = 1; 
     172                        while(!attributes[i].endsWith("\"")){ 
     173                                label+=attributes[i]+ " "; 
     174                                i++; 
     175                        } 
     176                        label+=attributes[i]; 
     177                        int labelLength = ((label.length())-1)/2; 
     178                        sssg.setLabelSize(labelLength); 
     179                        while(!(line = bufferedReader.readLine()).startsWith("*DirectedEdges")){ 
     180                                nodeCount++; 
     181                        } 
     182                        sssg.createEdgeLists(nodeCount); 
     183                        line = bufferedReader.readLine(); 
     184                        String[] sourceTarget; 
     185                        while((line=bufferedReader.readLine()) != null){ 
     186                                line.trim(); 
     187                                sourceTarget = line.split("\\s+"); 
     188                                int source = new Integer(sourceTarget[0]).intValue()-1; 
     189                                int target = new Integer(sourceTarget[1]).intValue()-1; 
     190 
     191                                sssg.addEdge(source, target); 
     192                        } 
     193                        bufferedReader.close(); 
     194                }catch (IOException ioe){ 
     195                        throw new AlgorithmExecutionException("Error reading " + nwbFile.getName().toString(), ioe); 
     196                } 
    132197         
    133          private int getInputData(Object[] dataList){ 
    134                  int graphIndex = -1; 
    135                 if (data.length != 2) { 
    136                   logger.log (LogService.LOG_ERROR,  
    137                                         "Error: This algorithm requires two datasets as inputs: a graph/network "+ 
    138                                         "and a node list with the instruction of merging nodes. \n"); 
    139                   return graphIndex; 
    140                 }        
    141                 for (int index =0; index<data.length; index++){ 
    142                         String dataFormat = data[index].getData().getClass().getName();                  
    143                         if (dataFormat.equalsIgnoreCase("prefuse.data.Graph")){ 
    144                                 dataList[0] = data[index].getData(); 
    145                                 graphIndex = index; 
    146                         } 
    147                         else if (dataFormat.equalsIgnoreCase("prefuse.data.Table")) 
    148                                 dataList[1] = data[index].getData(); 
    149                         else { 
    150                                 logger.log (LogService.LOG_ERROR,  
    151                                         "Error: the data format of the input dataset is "+dataFormat+",\n"+ 
    152                                         "This algorithm requires the following data formats as inputs: \n"+ 
    153                                         "       prefuse.data.Graph for a graph/network and \n"+ 
    154                                         "       prefuse.data.Table for a node list. \n"); 
    155                                 return -1; 
    156                         } 
    157                 } 
    158                 if (dataList[1]==null){ 
    159                         logger.log (LogService.LOG_ERROR,  
    160                         "Error: This algorithm did not get prefuse.data.Table for a node list as one of the inputs. \n"); 
    161                         return -1; 
    162                 }        
    163                 if(dataList[0] == null){ 
    164                         logger.log (LogService.LOG_ERROR,  
    165                         "Error: This algorithm did not get prefuse.data.Graph for a graph/network as one of the inputs. \n"); 
    166                         return -1; 
    167                 } 
    168                 return graphIndex; 
    169             } 
     198                return sssg; 
     199        } 
     200 
    170201 
    171202} 
  • trunk/plugins/analysis/edu.iu.nwb.analysis.extractattractors/src/edu/iu/nwb/analysis/extractattractors/components/BasinConstructorThread.java

    r2167 r2403  
    55import java.io.IOException; 
    66import java.io.PrintWriter; 
     7import java.math.BigInteger; 
    78import java.util.HashMap; 
    89import java.util.Iterator; 
    910import java.util.LinkedHashMap; 
    1011import java.util.LinkedHashSet; 
     12import java.util.Stack; 
    1113 
    1214import org.cishell.framework.algorithm.AlgorithmExecutionException; 
    1315 
    14 import prefuse.data.Edge; 
    15 import prefuse.data.Graph; 
    16 import prefuse.data.Node; 
    17 import prefuse.data.Schema; 
    1816import prefuse.data.Table; 
    1917import edu.iu.nwb.util.nwbfile.NWBFileWriter; 
     
    2422        int basinSize; 
    2523        LinkedHashSet nodes; 
    26         Graph stateGraph
     24        SimpleStateSpaceGraph simpleStateGraph = null
    2725        final Table originalTable; 
    2826        final String labelColumn; 
    29          
    30          
    31         public BasinConstructorThread(Graph stateGraph,final Table originalTable,final String labelColumn, LinkedHashSet nodes){ 
     27        final int systemSize; 
     28        final int nodeStates; 
     29        ExtractAttractorBasins eab; 
     30        double observedCoherency = 0; 
     31        private int strongComponents = 0; 
     32        private Stack componentMembers = new Stack(); 
     33        int[] preOrderColumn; 
     34        int[] strongComponentColumn; 
     35         
     36        public BasinConstructorThread(SimpleStateSpaceGraph sssg, final Table originalTable, final String labelColumn, LinkedHashSet nodes, int systemSize,int nodeStates,int[] preOrder, int[] strongComponent,ExtractAttractorBasins eab){ 
    3237                super(); 
    33                 this.stateGraph = stateGraph
     38                this.simpleStateGraph = sssg
    3439                this.nodes = nodes; 
    3540                this.originalTable = originalTable; 
    3641                this.labelColumn = labelColumn; 
    37                  
    38         } 
    39          
     42                this.systemSize = systemSize; 
     43                this.nodeStates = nodeStates; 
     44                this.eab = eab; 
     45                this.preOrderColumn = preOrder; 
     46                this.strongComponentColumn = strongComponent; 
     47        } 
     48         
     49         
     50        public double getObservedCoherency(){ 
     51                return this.observedCoherency; 
     52        } 
     53         
     54 
     55        public void queueComponent(LinkedHashSet componentNodeIDs){ 
     56                //this.components.add(componentNodeIDs); 
     57                this.nodes = componentNodeIDs; 
     58        } 
     59 
    4060        public void run() { 
    41                 LinkedHashMap sourceToTargetMap = new LinkedHashMap(); 
    42                 Graph basinGraph = initializeBasinGraph(); 
    43                 for(Iterator it = nodes.iterator(); it.hasNext();){ 
    44                         int sourceNodeRow = ((Integer)it.next()).intValue(); 
    45                         int targetNodeRow = basinGraph.addNodeRow(); 
    46                         sourceToTargetMap.put(new Integer(sourceNodeRow), new Integer(targetNodeRow)); 
    47                         copyNode(basinGraph,sourceNodeRow,targetNodeRow); 
    48                 } 
    49                  
    50                 for(Iterator it = nodes.iterator(); it.hasNext();){ 
    51                         int sourceNodeRow = ((Integer)it.next()).intValue(); 
    52                          
    53                         for(Iterator edges = this.stateGraph.outEdges(this.stateGraph.getNode(sourceNodeRow)); edges.hasNext();){ 
    54                                 Edge e = (Edge)edges.next(); 
    55                                 int source = e.getSourceNode().getRow(); 
    56                                 int target = e.getTargetNode().getRow(); 
    57                                  
    58                                 basinGraph.addEdge(((Integer)sourceToTargetMap.get(new Integer(source))).intValue(), ((Integer)sourceToTargetMap.get(new Integer(target))).intValue()); 
    59                         }        
    60                 } 
    61                  
    62                 NodeCleaningThread nct = new NodeCleaningThread(basinGraph,this.originalTable,this.labelColumn); 
    63                 nct.run(); 
    6461                try{ 
    65                         nct.join(); 
    66                          
    67                         this.basinGraph = generateStateSpaceFile(basinGraph); 
    68                         this.basinSize = basinGraph.getNodeCount(); 
    69                         this.attractorTable = generateAttractorCSV(nct.getAttractorTable()); 
    70                          
    71                 }catch(InterruptedException ie){ 
     62                System.gc(); 
     63                Table attractorTable = constructAttractorTable(originalTable, labelColumn,true); 
     64                File stateSpaceFile = File.createTempFile("NWB-Session-StateSpace-", ".nwb"); 
     65                NWBFileWriter stateSpaceFileWriter = createNWBFileWriter(stateSpaceFile); 
     66                 
     67                Stack firstStack = new Stack(); 
     68                firstStack.setSize(nodes.size()); 
     69                Stack secondStack = new Stack(); 
     70                secondStack.setSize(nodes.size()); 
     71                 
     72                Integer testCount = new Integer(0); 
     73 
     74                for(Iterator it = this.nodes.iterator(); it.hasNext();){ 
     75                        int nodeID = ((Integer)it.next()).intValue(); 
     76                        if(preOrderColumn[nodeID] == -1){ 
     77                                recursiveStrongComponentCalculation(nodeID,strongComponentColumn,preOrderColumn,firstStack,secondStack,testCount,attractorTable, stateSpaceFileWriter); 
     78                        } 
     79                } 
     80                stateSpaceFileWriter.setDirectedEdgeSchema(NWBFileWriter.getDefaultEdgeSchema()); 
     81                writeEdges(simpleStateGraph.outNeighbor,stateSpaceFileWriter); 
     82                stateSpaceFileWriter.haltParsingNow(); 
     83                 
     84                        this.basinGraph = stateSpaceFile; 
     85                        this.basinSize = nodes.size(); 
     86                        this.attractorTable = generateAttractorCSV(attractorTable); 
     87                        this.observedCoherency = this.observedCoherency/(double)nodes.size(); 
     88                        ExtractAttractorBasins.updateProgress(eab, nodes.size()); 
     89                 
     90                 
     91                        System.gc(); 
     92                }catch(IOException ioe){ 
    7293                         
    7394                }catch(AlgorithmExecutionException aee){ 
    7495                         
    7596                } 
    76          
    77         } 
    78          
     97        } 
     98         
     99        private void recursiveStrongComponentCalculation(int nodeNumber, int[] strongComponentColumn,int[] preOrderColumn, Stack firstStack, Stack secondStack, Integer count, Table attractorTable, NWBFileWriter nfw){ 
     100                int v; 
     101                count = new Integer(count.intValue()+1); 
     102                preOrderColumn[nodeNumber] = count.intValue(); 
     103                firstStack.push(new Integer(nodeNumber)); 
     104                secondStack.push(new Integer(nodeNumber)); 
     105 
     106                int outNode = this.simpleStateGraph.getOutNeighbor(nodeNumber).intValue(); 
     107                if(preOrderColumn[outNode] == -1)  
     108                        recursiveStrongComponentCalculation(outNode,strongComponentColumn,preOrderColumn,firstStack,secondStack, count,attractorTable,nfw); 
     109                else if (strongComponentColumn[outNode]  == -1){ 
     110                        while (preOrderColumn[((Integer)secondStack.peek()).intValue()] > preOrderColumn[outNode]){  
     111                                secondStack.pop(); 
     112 
     113                        } 
     114 
     115                } 
     116 
     117 
     118                if(((Integer)secondStack.peek()).intValue() == nodeNumber){  
     119                        secondStack.pop(); 
     120                } 
     121                else{ 
     122                        return; 
     123                } 
     124 
     125 
     126                do { 
     127                        v = ((Integer)firstStack.pop()).intValue(); 
     128                        componentMembers.push(new Integer(v)); 
     129                        strongComponentColumn[v] = this.strongComponents; 
     130                } while (nodeNumber != v); 
     131 
     132 
     133                if(componentMembers.size() > 1){ 
     134                        int componentSize = componentMembers.size(); 
     135                        while(!componentMembers.isEmpty()){ 
     136                                int n1 = ((Integer)componentMembers.pop()).intValue(); 
     137                                double nodeRobustness; 
     138                                String label = getLabel(n1); 
     139                                BigInteger stateSpace = convertStringToBigInt(label,nodeStates); 
     140                                nodeRobustness = calculateNodeRobustness(stateSpace,nodeStates,nodes); 
     141                                this.observedCoherency+=nodeRobustness; 
     142                                writeNode(n1,label,componentSize,nodeRobustness,nfw); 
     143                                //this.simpleStateGraph.setAttractor(n1, componentSize); 
     144                                annotateAttractorTable(attractorTable,label,true); 
     145                        } 
     146                } 
     147                if(componentMembers.size() == 1){ 
     148                        int n1 = ((Integer)componentMembers.pop()).intValue(); 
     149                        int target = this.simpleStateGraph.getOutNeighbor(n1).intValue(); 
     150                        double nodeRobustness; 
     151                        String label = getLabel(n1); 
     152                        BigInteger stateSpace = convertStringToBigInt(label,nodeStates); 
     153                        nodeRobustness = calculateNodeRobustness(stateSpace,nodeStates,nodes); 
     154                        this.observedCoherency += nodeRobustness; 
     155                        if(target == n1){ 
     156                                //this.simpleStateGraph.setAttractor(target, 1); 
     157                                annotateAttractorTable(attractorTable,label,true); 
     158                                writeNode(n1,label,1,nodeRobustness,nfw); 
     159                        } 
     160                        else{ 
     161                                writeNode(n1,label,0,nodeRobustness,nfw); 
     162                        } 
     163                } 
     164 
     165                componentMembers = new Stack(); 
     166                this.strongComponents++; 
     167        } 
     168         
     169        private String getLabel(int nodeID){ 
     170                BigInteger labelInteger = new BigInteger(new Integer(nodeID).toString()); 
     171                int[] labelArray = new int[this.simpleStateGraph.labelSize]; 
     172                labelArray = convertBigIntToIntArray(labelInteger,labelArray,nodeStates); 
     173                String label = convertIntArrayToString(labelArray); 
     174                 
     175                return label; 
     176        } 
     177         
     178        private Table constructAttractorTable(Table orgTable, String labelColumn, boolean isHorizontal){ 
     179                Table attractorTable = new Table(); 
     180                if(!isHorizontal){ 
     181                        attractorTable.addColumn("Label", String.class); 
     182                } 
     183                for(int i = 0; i < orgTable.getRowCount(); i++){ 
     184                        if(isHorizontal){ 
     185                                if(labelColumn == null || labelColumn.equals("")){ 
     186                                        attractorTable.addColumn("x" + (i+1), int.class); 
     187                                }else{ 
     188                                        attractorTable.addColumn(orgTable.getString(i, labelColumn), int.class); 
     189                                } 
     190                        }else{ 
     191                                int rowNumber = attractorTable.addRow(); 
     192                                if(labelColumn == null || labelColumn.equals("")){ 
     193                                        attractorTable.setString(rowNumber, "Label", "x"+(i+1)); 
     194                                } 
     195                                else{ 
     196                                        attractorTable.setString(rowNumber, "Label", orgTable.getString(i, labelColumn)); 
     197                                } 
     198                        } 
     199                } 
     200                return attractorTable; 
     201        } 
     202         
     203        private Table annotateAttractorTable(Table attractorTable, String value, boolean isHorizontal){ 
     204                String[] discreteValues = value.split("\\s+"); 
     205                if(isHorizontal){ 
     206                        int rowNumber = attractorTable.addRow(); 
     207                        for(int i = 0; i < discreteValues.length; i++){ 
     208                                attractorTable.setInt(rowNumber, i, new Integer(discreteValues[i]).intValue()); 
     209                        } 
     210                }else{ 
     211                        attractorTable.addColumn(new Integer(attractorTable.getColumnCount()).toString(), int.class); 
     212                        for(int i = 0; i < discreteValues.length; i++){ 
     213                                attractorTable.setInt(0, attractorTable.getColumnCount()-1, new Integer(discreteValues[i]).intValue()); 
     214                        } 
     215                } 
     216                return attractorTable; 
     217        } 
     218 
     219        public int getSystemSize(){ 
     220                return this.systemSize; 
     221        } 
     222 
     223        protected double calculateNodeRobustness(BigInteger nodeState, int nodeStates, LinkedHashSet nodes){ 
     224                int[] nodeStateArray = new int[this.systemSize]; 
     225                int[] checkStateArray = new int[this.systemSize]; 
     226                BigInteger checkState = BigInteger.ZERO; 
     227                double counter = 0; 
     228                Integer checkStateInteger; 
     229                nodeStateArray = convertBigIntToIntArray(nodeState,nodeStateArray,nodeStates); 
     230 
     231                for(int i = 0; i < nodeStateArray.length; i++){ 
     232                        checkStateArray = convertBigIntToIntArray(nodeState,checkStateArray,nodeStates); 
     233                        for(int j = 1; j < nodeStates; j++){ 
     234 
     235                                checkStateArray[i] = ((nodeStateArray[i]+j) % nodeStates); 
     236                                checkState = new BigInteger(convertIntArrayToString(checkStateArray,nodeStates),nodeStates); 
     237                                checkStateInteger = new Integer(checkState.intValue()); 
     238                                if(nodes.contains(checkStateInteger)){ 
     239                                        counter++; 
     240                                } 
     241                        } 
     242                } 
     243 
     244                counter = counter/((nodeStates-1)*nodeStateArray.length); 
     245                return counter; 
     246 
     247        } 
     248 
     249        protected static String convertIntArrayToString(final int[] stateSpace, int numberOfStates){ 
     250                StringBuffer s = new StringBuffer(); 
     251                for(int i = 0; i < stateSpace.length; i++){ 
     252                        s.append(Character.forDigit(stateSpace[i], numberOfStates)); 
     253                } 
     254                return s.toString(); 
     255        } 
     256 
     257        protected static int[] convertBigIntToIntArray(BigInteger bi, int[] stateSpace, int nodeStates){ 
     258                int pos = stateSpace.length; 
     259                String biAsString = bi.toString(nodeStates); 
     260                biAsString = biAsString.trim(); 
     261 
     262                String[] biToStateSpace; 
     263 
     264                biToStateSpace = biAsString.split(""); 
     265 
     266                pos = stateSpace.length - (biToStateSpace.length-1); 
     267                for(int i = 0; i < pos; i++){ 
     268                        stateSpace[i] = 0; 
     269                } 
     270 
     271                for(int i = 1; i < biToStateSpace.length; i++){ 
     272                        int digit = Character.digit(biToStateSpace[i].charAt(0),nodeStates); 
     273 
     274                        stateSpace[(pos-1)+i] = new Integer(digit).intValue(); 
     275                } 
     276 
     277                return stateSpace; 
     278        } 
     279 
     280        protected static int convertBigIntegerToCorrectInt(BigInteger bi, int[] stateSpace,int nodeStates){ 
     281                int[] convertedIntArray = convertBigIntToIntArray(bi,stateSpace,nodeStates); 
     282                int returnValue = new Integer(convertIntArrayToString(convertedIntArray,nodeStates)).intValue(); 
     283                return returnValue; 
     284        } 
     285 
    79286        public File getAttractorBasin(){ 
    80287                return this.basinGraph; 
    81288        } 
    82          
    83         public int getBasinSize(){ 
    84                 return this.basinSize; 
    85         } 
    86          
     289 
     290 
     291 
     292        public Integer getBasinSize(){ 
     293                return new Integer(this.basinSize); 
     294        } 
     295 
     296 
    87297        public File getAttractorTable(){ 
    88298                return this.attractorTable; 
    89299        } 
    90300         
    91         private Graph initializeBasinGraph(){ 
    92                 Graph basinGraph; 
    93                 Schema nodeSchema = getSchema("node"); 
    94                 Schema edgeSchema = getSchema("edge"); 
    95                  
    96                 nodeSchema.addColumn("attractor", int.class, new Integer(0)); 
    97                 basinGraph = new Graph(nodeSchema.instantiate(),edgeSchema.instantiate(),true); 
    98                 return basinGraph; 
    99         } 
    100          
    101         private Schema getSchema(String schemaType){ 
    102                 Schema newSchema = new Schema(); 
    103                 Schema oldSchema = null; 
    104                 if(schemaType.equals("node")){ 
    105                         oldSchema = this.stateGraph.getNodeTable().getSchema(); 
    106                          
    107                 } 
    108                 else if(schemaType.equals("edge")){ 
    109                         oldSchema = this.stateGraph.getEdgeTable().getSchema(); 
    110                 } 
    111                  
    112                 for(int i = 0; i < oldSchema.getColumnCount(); i++){ 
    113                         newSchema.addColumn(oldSchema.getColumnName(i), oldSchema.getColumnType(i)); 
    114                 } 
    115                  
    116                 return newSchema; 
    117         } 
    118          
    119         private void copyNode(Graph basinGraph, int sourceNodeRow, int targetNodeRow){ 
    120                 for(int i = 0; i < this.stateGraph.getNodeTable().getColumnCount(); i++){ 
    121                         basinGraph.getNodeTable().set(targetNodeRow, i, this.stateGraph.getNodeTable().get(sourceNodeRow, i)); 
    122                 } 
    123         } 
    124          
    125          
    126         private static File generateStateSpaceFile(final Graph g) throws AlgorithmExecutionException{ 
     301        private static NWBFileWriter createNWBFileWriter(File stateSpaceFile) throws AlgorithmExecutionException{ 
    127302                try{ 
    128                         File stateSpaceFile = File.createTempFile("NWB-Session-StateSpace-", ".nwb"); 
    129                         NWBFileWriter stateSpaceWriter = new NWBFileWriter(stateSpaceFile); 
    130                         LinkedHashMap nodeSchema = NWBFileWriter.getDefaultNodeSchema(); 
    131                         LinkedHashMap edgeSchema = NWBFileWriter.getDefaultEdgeSchema(); 
    132                          
    133                         nodeSchema = generateSchema(g.getNodeTable(),nodeSchema); 
    134                         edgeSchema = generateSchema(g.getEdgeTable(),edgeSchema); 
    135                          
    136                         stateSpaceWriter.setNodeSchema(nodeSchema); 
    137                         writeNodes(g,stateSpaceWriter); 
    138                         stateSpaceWriter.setDirectedEdgeSchema(edgeSchema); 
    139                         writeEdges(g,stateSpaceWriter); 
    140                          
    141                         stateSpaceWriter.haltParsingNow(); 
    142                         return stateSpaceFile; 
     303                NWBFileWriter stateSpaceWriter = new NWBFileWriter(stateSpaceFile); 
     304                LinkedHashMap nodeSchema = NWBFileWriter.getDefaultNodeSchema(); 
     305                nodeSchema.put("attractor", int.class); 
     306                nodeSchema.put("observedrobustness","real"); 
     307                stateSpaceWriter.setNodeSchema(nodeSchema); 
     308                 
     309                return stateSpaceWriter; 
    143310                }catch(IOException ioe){ 
    144311                        throw new AlgorithmExecutionException("Error creating and writing the state space graph file.\n",ioe); 
    145312                } 
    146313        } 
    147          
     314 
    148315        private static File generateAttractorCSV(final Table t) throws AlgorithmExecutionException{ 
    149316                try{ 
     
    154321                                pw.print("\""+t.getColumnName(i)+"\","); 
    155322                        } 
    156                                pw.println(); 
     323                        pw.println(); 
    157324                        for(int i = 0; i < t.getRowCount(); i++){ 
    158325                                for(int j = 0; j < t.getColumnCount();j++){ 
     
    163330                        pw.flush(); 
    164331                        pw.close(); 
    165                          
     332 
    166333                        return attractorTableFile; 
    167334                }catch(IOException ioe){ 
     
    169336                }        
    170337        } 
    171          
    172         private static LinkedHashMap generateSchema(final Table t, LinkedHashMap schema){ 
    173                 for(int i = 0; i < t.getColumnCount(); i++){ 
    174                         if(schema.get(t.getColumnName(i)) == null){ 
    175                                 schema.put(t.getColumnName(i), t.getColumnType(i).toString().toLowerCase()); 
    176                         } 
    177                 } 
    178                  
    179                 return schema; 
    180         } 
    181          
    182         private static void writeNodes(final Graph g, NWBFileWriter nfw){ 
    183                 int i = 0; 
    184                 for(Iterator it = g.nodes(); it.hasNext();){ 
    185                         i++; 
     338 
     339        private static void writeNode(int id, String label, int attractor, double observedRobustness, NWBFileWriter nfw){ 
     340 
     341                 
    186342                        HashMap columnValues = new HashMap(); 
    187                         Node n = (Node)it.next(); 
    188                          
    189                         columnValues.put("attractor", n.get("attractor")); 
    190                         nfw.addNode(n.getRow()+1, n.getString("label"), columnValues); 
    191                          
    192                 } 
    193         } 
    194          
    195         private static void writeEdges(final Graph g, NWBFileWriter nfw){ 
    196                 int i = 0; 
    197                 for(Iterator it = g.edges(); it.hasNext();){ 
    198                         i++; 
    199                         Edge e = (Edge)it.next(); 
    200                          
    201                  
    202                         nfw.addDirectedEdge(e.getSourceNode().getRow()+1, e.getTargetNode().getRow()+1,null); 
    203                          
    204                 } 
    205         } 
    206          
     343 
     344                        columnValues.put("attractor", new Integer(attractor)); 
     345                        columnValues.put("observedrobustness", new Double(observedRobustness)); 
     346 
     347                        nfw.addNode(id+1, label, columnValues); 
     348 
     349                 
     350        } 
     351 
     352        private static void writeEdges(int[] targets, NWBFileWriter nfw){ 
     353 
     354                for(int i = 0; i < targets.length; i++){ 
     355                        int target = targets[i]; 
     356 
     357 
     358                        nfw.addDirectedEdge(i+1, target+1,null); 
     359 
     360                } 
     361        } 
     362         
     363        protected static BigInteger convertStringToBigInt(String state,int nodeStates){ 
     364                String stateString = state.replaceAll("\\s+", ""); 
     365                BigInteger bi = new BigInteger(stateString,nodeStates); 
     366                return bi; 
     367        } 
     368         
     369 
     370        protected static String convertIntArrayToString(final int[] stateSpace){ 
     371                String s = ""; 
     372                for(int i = 0; i < stateSpace.length; i++){ 
     373                        s += stateSpace[i]+ " ";                 
     374                }        
     375                return s.trim(); 
     376        } 
     377 
    207378} 
  • trunk/plugins/analysis/edu.iu.nwb.analysis.extractattractors/src/edu/iu/nwb/analysis/extractattractors/components/ExtractAttractorBasins.java

    r2167