Changeset 2520
- Timestamp:
- 10/13/08 16:40:34 (3 months ago)
- Files:
-
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/.settings/org.eclipse.jdt.core.prefs (added)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/EdgeListParser.java (added)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/EdgeListToNWB.java (modified) (3 diffs)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/EdgeListToNWBFactory.java (modified) (1 diff)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/EdgeListValidator.java (added)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/EdgeListValidatorFactory.java (modified) (2 diffs)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/InvalidEdgeListFormatException.java (added)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/nwbwritable (added)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/nwbwritable/Edge.java (added)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/nwbwritable/NWBLineWritable.java (added)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/nwbwritable/Node.java (added)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/nwbwritable/UnweightedEdge.java (added)
- trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/nwbwritable/WeightedEdge.java (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/EdgeListToNWB.java
r1982 r2520 8 8 import java.io.FileWriter; 9 9 import java.io.IOException; 10 import java.util.ArrayList;11 10 import java.util.Dictionary; 12 import java.util.HashMap;13 11 import java.util.Iterator; 14 import java.util. Map;12 import java.util.List; 15 13 16 14 import org.cishell.framework.CIShellContext; … … 21 19 import org.osgi.service.log.LogService; 22 20 23 import edu.iu.nwb.converter.edgelist. EdgeListValidatorFactory.ValidateEdgeFile;24 21 import edu.iu.nwb.converter.edgelist.nwbwritable.Edge; 22 import edu.iu.nwb.converter.edgelist.nwbwritable.Node; 25 23 26 24 /** 27 * Converts from edgelist (raw data) to NWB file format 28 * @author Felix Terkhorn 25 * Converts from edgelist format to NWB file format 29 26 */ 30 27 public class EdgeListToNWB implements Algorithm { 31 Data[] data; 32 Dictionary parameters; 33 CIShellContext ciContext; 34 LogService logger; 35 36 Map vertexToIdMap; 37 38 /** 39 * Intializes the algorithm 40 * @param data List of Data objects to convert 41 * @param parameters Parameters passed to the converter 42 * @param context Provides access to CIShell services 43 * @param transformer 44 */ 45 public EdgeListToNWB(Data[] data, Dictionary parameters, CIShellContext context) { 46 this.data = data; 47 this.parameters = parameters; 48 this.ciContext = context; 49 this.logger = (LogService)ciContext.getService(LogService.class.getName()); 50 } 28 private Data[] data; 29 private CIShellContext ciContext; 30 private LogService logger; 51 31 32 public EdgeListToNWB(Data[] data, Dictionary parameters, CIShellContext context) { 33 this.data = data; 34 this.ciContext = context; 35 this.logger = (LogService) ciContext.getService(LogService.class.getName()); 36 } 52 37 53 /*54 * Reads edgelist data in from reader and outputs it as NWB data to writer 55 * @author Felix Terkhorn 56 */ 57 public void transform(BufferedReader reader, BufferedWriter writer, EdgeListValidatorFactory.ValidateEdgeFile validator) 58 throws IOException {59 int i=0,edgesCount;60 boolean badFormat = false;61 ArrayList validatorEdges = new ArrayList();62 38 /** 39 * Executes the conversion from edgelist format to NWB format 40 * 41 * @return an NWB file derived from the provided edgelist 42 */ 43 public Data[] execute() throws AlgorithmExecutionException { 44 File edgeFile = (File) data[0].getData(); 45 BufferedReader edgeReader = null; 46 BufferedWriter nwbWriter = null; 47 63 48 try { 64 if (badFormat) { 65 logger.log(LogService.LOG_ERROR, 66 "Sorry, your file does not comply with edge-list format specifications.\n"+ 67 "Please review the latest edge-list format specification at "+ 68 "https://nwb.slis.indiana.edu/community/?n=LoadData.Edgelist, and update your file. \n" 69 ); 70 throw (new IOException("Improperly formatted edgelist file")); 71 } 72 // currentLine is null 73 HashMap validatorMap = validator.getLabelIDMap(); 74 Iterator it = validatorMap.entrySet().iterator(); 49 // (may throw FileNotFoundException) 50 edgeReader = new BufferedReader(new FileReader(edgeFile)); 75 51 76 validatorEdges = validator.getEdges(); 77 writer.write("*Nodes "+validatorMap.size()+"\n"); 78 writer.write("id*int label*string\n"); 52 File nwbFile = getTempFile(); // TODO: make getTempFile() service 79 53 80 while (it.hasNext()) { 81 Map.Entry pairs = (Map.Entry)it.next(); 82 // print node IDs followed by node stringlabels 83 writer.write(((Integer)(pairs.getValue())).intValue() + " " +pairs.getKey() +"\n"); 84 } 85 edgesCount = validator.getTotalNumOfEdges(); 86 if (validator.isUndirectedGraph()) { 87 writer.write("*UndirectedEdges "+edgesCount+"\n"); 88 } else { 89 writer.write("*DirectedEdges "+edgesCount+"\n"); 90 } 91 if (validator.getWeightCount() == 0) { 92 // if there are no weights in the edgelist, there will be no weights in the NWB output 93 writer.write("source*int target*int\n"); 94 } else {// weightCount > 1 95 96 if (validator.usesFloatWeight()) { 97 writer.write("source*int target*int weight*float\n"); 98 } else { 99 writer.write("source*int target*int weight*int\n"); 100 } 101 102 } 103 for (i=0;i<validatorEdges.size();i++){ 104 // step through edges and output corresponding NWB edge definition 105 if (((String[])(validatorEdges.get(i))).length > 2) { // this tuple has a weight value 106 try { 107 108 writer.write(((Integer)validatorMap.get(((String[])(validatorEdges.get(i)))[0])).intValue() + " " + ((Integer)validatorMap.get(((String[])(validatorEdges.get(i)))[1])).intValue() + " " + ((String[])(validatorEdges.get(i)))[2] +"\n"); 109 } catch (NullPointerException e) { 110 e.printStackTrace(); 111 } 112 } else { // only source, target were found in this tuple 113 try { 114 writer.write(((Integer)validatorMap.get(((String[])(validatorEdges.get(i)))[0])).intValue() + " " + ((Integer)validatorMap.get(((String[])(validatorEdges.get(i)))[1])).intValue() +"\n"); 115 } catch (NullPointerException e) { 116 e.printStackTrace(); 117 } 118 119 } 54 // may throw FileIOException 55 nwbWriter = new BufferedWriter(new FileWriter(nwbFile)); 120 56 121 } 122 writer.flush(); 57 // edge list information is given by various 58 // methods of the parser. 59 EdgeListParser parser = new EdgeListParser(edgeFile); 60 // if it's a valid edgelist, go ahead with conversion 61 transform(edgeReader, nwbWriter, parser); 123 62 63 // should there be an else clause here? 64 return new Data[] { new BasicData(nwbFile, "file:text/nwb") }; 65 } catch (FileNotFoundException e) { 66 throw new AlgorithmExecutionException("Specified Edge list file not found! " 67 + ((File) edgeFile).getAbsolutePath() + "\n", e); 124 68 } catch (IOException e) { 125 e.printStackTrace();126 throw e;127 }128 129 // we need to take the edgelist and convert it into nwb format, then dump that to the writer..130 131 }132 133 /**134 * Executes the conversion from edgelist format to NWB format135 *136 * @return A single java file object137 */138 public Data[] execute() throws AlgorithmExecutionException {139 File inFile = (File)data[0].getData();140 BufferedReader edgelistreader;141 BufferedWriter nwb = null;142 EdgeListValidatorFactory eLVFact;143 ValidateEdgeFile validator;144 145 146 // BufferedReader wrapping a FileReader -- should give you readline147 try {148 edgelistreader = new BufferedReader(new FileReader(inFile));149 150 151 } catch (FileNotFoundException e) {152 69 throw new AlgorithmExecutionException( 153 "Specified Edge list file not found! "+((File)inFile).getAbsolutePath()+"\n", e); 154 } 155 File nwbFile = getTempFile(); 156 try { 157 try { 158 nwb = new BufferedWriter(new FileWriter(nwbFile)); 159 } catch (IOException e) { 160 throw new AlgorithmExecutionException ( 161 "Error writing from the specified edge list to the specified .nwb file.\n", e); 162 } 163 try { 164 //why validate the format again??? 165 eLVFact = new EdgeListValidatorFactory(); 166 // validate the file. file information is given by various methods 167 // of the validator. 168 validator = eLVFact.new ValidateEdgeFile(); 169 validator.validateEdgeFormat(inFile); 170 if (validator.getValidationResult()) { 171 // if it's a valid edgelist, go ahead with conversion 172 transform(edgelistreader, nwb, validator); 173 } 174 // should there be an else clause here? 175 return new Data[] {new BasicData(nwbFile, "file:text/nwb")}; 176 } catch (Exception e ) { 177 throw new AlgorithmExecutionException( 178 "Encountered an error while converting from edge list to .nwb", e); 179 } 70 "Error writing from the specified edge list to the specified .nwb file.\n", e); 71 } catch (InvalidEdgeListFormatException e) { 72 throw new AlgorithmExecutionException(e.getMessage(), e); 180 73 } finally { 181 if (nwb != null) {74 if (nwbWriter != null) { 182 75 try { 183 nwb.close();76 nwbWriter.close(); 184 77 } catch (IOException e) { 185 78 throw new AlgorithmExecutionException("Unable to close file stream.", e); … … 187 80 } 188 81 } 82 } 189 83 190 } 191 192 193 /** 194 * Creates a temporary file for the NWB file 195 * @return The temporary file 196 */ 197 private File getTempFile(){ 84 /* 85 * Reads edgelist data in from reader and outputs it as NWB data to writer 86 * @author Felix Terkhorn 87 */ 88 private void transform(BufferedReader reader, BufferedWriter writer, EdgeListParser parser) 89 throws IOException { 90 boolean badFormat = false; 91 92 try { 93 if (badFormat) { 94 logger 95 .log( 96 LogService.LOG_ERROR, 97 "Sorry, your file does not comply with edge-list format specifications.\n" 98 + "Please review the latest edge-list format specification at " 99 + "https://nwb.slis.indiana.edu/community/?n=LoadData.Edgelist, and update your file. \n"); 100 throw (new IOException("Improperly formatted edgelist file")); 101 } 102 // currentLine is null 103 104 105 106 107 List nodes = parser.getNodes(); 108 109 //write node header 110 writer.write("*Nodes " + nodes.size() + "\n"); 111 writer.write("id*int label*string\n"); 112 113 //write node lines 114 115 Iterator nodesIterator = nodes.iterator(); 116 while (nodesIterator.hasNext()) { 117 Node node = (Node) nodesIterator.next(); 118 String nodeLine = node.getNWBLine(); 119 writer.write(nodeLine); 120 } 121 122 //write edge header 123 124 if (parser.isUndirectedGraph()) { 125 writer.write("*UndirectedEdges\n"); 126 } else { 127 writer.write("*DirectedEdges\n"); 128 } 129 130 if (parser.isWeighted()) { 131 writer.write("source*int target*int weight*float\n"); 132 } else { 133 writer.write("source*int target*int\n"); 134 } 135 136 List edges = parser.getEdges(); 137 138 //write edge lines 139 Iterator edgesIterator = edges.iterator(); 140 while (edgesIterator.hasNext()) { 141 Edge edge = (Edge) edgesIterator.next(); 142 String edgeLine = edge.getNWBLine(); 143 writer.write(edgeLine); 144 } 145 146 writer.flush(); 147 148 } catch (IOException e) { 149 e.printStackTrace(); 150 throw e; 151 } 152 153 // we need to take the edgelist and convert it into nwb format, then 154 // dump that to the writer.. 155 156 } 157 158 /** 159 * Creates a temporary file for the NWB file 160 * 161 * @return The temporary file 162 */ 163 private File getTempFile() { 198 164 File tempFile; 199 165 200 166 String tempPath = System.getProperty("java.io.tmpdir"); 201 File tempDir = new File(tempPath +File.separator+"temp");202 if (!tempDir.exists())167 File tempDir = new File(tempPath + File.separator + "temp"); 168 if (!tempDir.exists()) 203 169 tempDir.mkdir(); 204 try {170 try { 205 171 tempFile = File.createTempFile("NWB-Session-", ".nwb", tempDir); 206 207 } catch (IOException e){172 173 } catch (IOException e) { 208 174 logger.log(LogService.LOG_ERROR, e.toString()); 209 tempFile = new File (tempPath+File.separator+"nwbTemp"+File.separator+"temp.nwb");175 tempFile = new File(tempPath + File.separator + "nwbTemp" + File.separator + "temp.nwb"); 210 176 211 177 } trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/EdgeListToNWBFactory.java
r1978 r2520 7 7 import org.cishell.framework.algorithm.AlgorithmFactory; 8 8 import org.cishell.framework.data.Data; 9 import org.osgi.service.component.ComponentContext;10 import org.osgi.service.metatype.MetaTypeProvider;11 9 12 10 public class EdgeListToNWBFactory implements AlgorithmFactory { 13 11 14 /** 15 * Create an converter 16 * 17 * @param data The data to convert 18 * @param parameters Parameters passed to the algorithm 19 * @param context Access to the CIShell environment 20 */ 21 public Algorithm createAlgorithm(Data[] data, Dictionary parameters, CIShellContext context) { 22 return new EdgeListToNWB(data, parameters, context); 23 24 25 } 26 12 public Algorithm createAlgorithm(Data[] data, Dictionary parameters, CIShellContext context) { 13 return new EdgeListToNWB(data, parameters, context); 14 } 27 15 } trunk/plugins/converter/edu.iu.nwb.converter.edgelist/src/edu/iu/nwb/converter/edgelist/EdgeListValidatorFactory.java
r2405 r2520 22 22 import org.osgi.service.metatype.MetaTypeProvider; 23 23 24 25 26 27 /**28 * @author Weixia(Bonnie) Huang29 */30 24 public class EdgeListValidatorFactory implements AlgorithmFactory { 31 25 … … 37 31 return new EdgeListValidator(dm, parameters, context); 38 32 } 39 40 public class EdgeListValidator implements Algorithm {41 42 Data[] data;43 Dictionary parameters;44 CIShellContext ciContext;45 LogService logger;46 47 public EdgeListValidator(Data[] dm, Dictionary parameters, CIShellContext context) {48 this.data = dm;49 this.parameters = parameters;50 this.ciContext = context;51 logger = (LogService)context.getService(LogService.class.getName());52 }53 54 public Data[] execute() throws AlgorithmExecutionException {55 56 String fileHandler = (String) data[0].getData();57 58 File inData = new File(fileHandler);59 ValidateEdgeFile validator = new ValidateEdgeFile();60 try{61 validator.validateEdgeFormat(inData);62 63 if(validator.getValidationResult()){64 65 Data[] dm = new Data[] {new BasicData(inData, "file:text/edge")};66 dm[0].getMetadata().put(DataProperty.LABEL, "edge file: " + fileHandler);67 dm[0].getMetadata().put(DataProperty.TYPE, DataProperty.NETWORK_TYPE);68 return dm;69 70 } else {71 throw new AlgorithmExecutionException(validator.getErrorMessages());72 }73 74 } catch (FileNotFoundException e){75 throw new AlgorithmExecutionException(76 "Could not find the specified edge list file.",e);77 } catch (IOException ioe){78 throw new AlgorithmExecutionException(79 "IO Errors will reading the specified edge list file.",ioe);80 81 } catch (edu.iu.nwb.converter.edgelist.EdgeListValidatorFactory.ValidateEdgeFile.EdgeFormat ef) {82 //System.out.println(">>>wrong format: "+validator.getErrorMessages());83 throw new AlgorithmExecutionException(84 "Sorry, your file does not comply with the edgelist file format specification.\n"+85 "Please review the latest edgelist file format Specification at "+86 "https://nwb.slis.indiana.edu/community/?n=LoadData.Edgelist, and update your file. \n"+87 validator.getErrorMessages());88 }89 90 91 92 }93 94 95 }96 97 98 99 public class ValidateEdgeFile {100 101 private int currentLine;102 private StringBuffer errorMessages = new StringBuffer();103 private boolean isFileGood = true;104 private int totalNumOfNodes = 0;105 106 private ArrayList edgelist = new ArrayList();107 private String[] currentTokens;108 private int edgesCount = 0;109 private HashMap labelIDMap = new HashMap();110 private int mapCount = 1;111 private int weightCount = 0;112 private boolean weightUseFloat=false;113 private boolean isUndirected = true;114 115 /* handle this merging of tokens with tail recursion */116 public String[] tokenmanage (String[] tokens, int startFrom) {117 118 int i;119 int j;120 String newtokens[];121 boolean breakflag = false;122 for (i = startFrom;i < tokens.length;i++) {123 if (breakflag) break;124 // find "\"\w" tokens (position i) and match them with end tokens (position j)125 // once matched and merged, set i = j, rinse and repeat126 if (tokens[i].matches("\".*\"")) { // it's a single word, quoted127 // continue128 129 }130 else if (tokens[i].matches("\".*\".*")) { // end-quote happens in the middle of the token131 // probably we should just continue...132 133 }134 else if (tokens[i].startsWith("\"")) {135 // starts with ", but since previous ifs didn't match, we know it doesn't end136 // with " or have a " in the middle137 138 j = i+1;139 if (j < tokens.length) {140 for (;j < tokens.length;j++) {141 if (tokens[j].matches(".*\"")) {142 newtokens = tokenmerge(tokens,i,j);143 return tokenmanage(newtokens, i+1);144 /* breakflag = true;145 break;*/146 147 }148 }149 }150 }151 152 }153 this.currentTokens = tokens;154 return tokens;155 }156 157 /*158 * Takes a String[] of tokens and merges all tokens from index i to index j159 * Returns the number of160 */161 public String[] tokenmerge(String[] tokens, int i, int j) {162 int x,y;163 String [] out = new String[tokens.length - (j - i)];164 String merged = "";165 for (y = i;y < j+1;y++){166 if (y == j) {167 merged = merged.concat(tokens[y]);168 } else {169 merged = merged.concat(tokens[y] + " ");170 }171 172 }173 for (x=0;x < tokens.length - (j - i);x++) {174 if (x < i) {175 out[x] = tokens[x];176 }177 else if (x == i) {178 out[x] = merged;179 } else { // x > i180 out[x] = tokens[x + (j - i)];181 }182 }183 return out;184 }185 186 187 188 public String getErrorMessages() {189 return this.errorMessages.toString();190 }191 192 public boolean getValidationResult() {193 return this.isFileGood;194 }195 196 public int getTotalNumOfNodes() {197 return this.totalNumOfNodes;198 }199 200 public int getWeightCount() {201 return this.weightCount;202 }203 204 public HashMap getLabelIDMap() {205 return this.labelIDMap;206 }207 208 public ArrayList getEdges() {209 return this.edgelist;210 }211 212 public int getTotalNumOfEdges () {213 return this.edgesCount;214 }215 216 public boolean isUndirectedGraph () {217 return this.isUndirected;218 }219 220 public boolean isDirectedGraph() {221 return !this.isUndirected;222 }223 224 public boolean usesFloatWeight() {225 return this.weightUseFloat;226 }227 228 /*229 * validateEdgeFormat invokes processFile with a BufferedReader corresponding to the file230 * that we will validate.231 * -- Felix Terkhorn232 * -- May 31 2007233 */234 public void validateEdgeFormat(File fileHandler)235 throws FileNotFoundException, IOException, EdgeFormat {236 237 currentLine = 0;238 BufferedReader reader = new BufferedReader(new FileReader(fileHandler));239 try {240 this.processFile(reader);241 } catch (EdgeFormat ef) {242 throw ef;243 }244 }245 246 public class EdgeFormat extends Exception {247 248 }249 250 public void processFile(BufferedReader reader) throws EdgeFormat, IOException {251 String line = reader.readLine();252 253 while (line != null && isFileGood) {254 currentLine++;255 256 //Remove leading and trailing whitespace;257 if (currentLine == 1)258 {259 line = line.trim();260 }261 262 // When first line matches "directed", graph is directed263 if (currentLine == 1 && line.matches("^\\s*directed\\s*$")) {264 this.isUndirected = false;265 line = reader.readLine();266 continue;267 }268 // Can also specify "undirected" for undirected graph269 if (currentLine == 1 && line.matches("^\\s*undirected\\s*$")) {270 this.isUndirected = true;271 line = reader.readLine();272 continue;273 }274 // process section header that looks like275 // *nodes or *nodes 1000276 if (line.matches("^\\s*$")) { // skip blank lines277 line = reader.readLine();278 continue;279 }280 if (this.validateEdge(line) && isFileGood) {281 282 processEdge(line);283 if (isFileGood) {284 this.checkFile();285 }286 line = reader.readLine();287 continue;288 } else {289 this.isFileGood = false;290 throw new EdgeFormat();291 }292 293 294 }295 }296 297 /*298 * This method saves information about the edge in this object299 */300 public void processEdge(String s) {301 // this could stand to be reworked, most of the state-changing302 // operations occur in validateEdge303 if (this.currentTokens != null) {304 this.edgelist.add(this.currentTokens);305 }306 307 }308 309 /*310 * This method checks to see if the given line validates edgelist rules.311 * Invalid edge formats:312 * case1-> node <single node line>313 * case2-> "node1 node2 <no matching end-quote in this line>314 * case3-> node1 node2" <no matching begin-quote in this line>315 * case4-> "" node2 <empty node name>316 * case5-> node1 node2 three <weight must be an int OR float value>317 */318 public boolean validateEdge(String s) {319 int i;320 321 String[] tokens = tokenmanage(s.trim().split("\\s+"),0);322 if (tokens.length < 2) { // case 1323 return false;324 } else if (s.matches("[^\"]*\"[^\"]*")) { // cases 2 & 3325 return false;326 } else if (s.matches(".*\"\".*")) { // case 4327 return false;328 &n
