|
From: Brendan M. <mc...@us...> - 2006-08-29 19:30:06
|
Update of /cvsroot/jigs/jigs/src/edu/whitman/halfway/util In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv3714/src/edu/whitman/halfway/util Modified Files: Timing.java MiscUtil.java StringUtil.java FileUtil.java CernUtil.java Added Files: TimingTree.java Log Message: --- NEW FILE: TimingTree.java --- package edu.whitman.halfway.util; import java.util.*; import org.apache.log4j.Logger; import cern.colt.matrix.ObjectFactory2D; import cern.colt.matrix.ObjectMatrix2D; public class TimingTree { protected static Logger log = Logger.getLogger(TimingTree.class); /** * As with the static instance in the Timing class, use with care. */ public static TimingTree tt = new TimingTree(); protected Node root = null; protected Node activeNode = null; /** Creates and starts the root timer */ public TimingTree(){ this.root = new Node("root", null); this.activeNode = root; } public static void setStaticInstance(TimingTree t){ TimingTree.tt = t; } public void start(String name){ activeNode = activeNode.start(name); } public ExecutionTimer getTimer(String dottedName){ String[] name = dottedName.split("."); Node node = root; for(int i=0; i<name.length; i++){ node = (Node) node.children.get(name); if(node == null){ log.warn("Not timer of name " + dottedName); return null; } } return node.timer; } protected void incTime(long ms){ activeNode.timer.incTime(ms); Node parent = activeNode.parent; while(parent != null){ parent.timer.incTime(ms); parent = parent.parent; } } protected void incTimeRand(){ long time = MiscUtil.randLongInRange(0, 999);//ms if(MiscUtil.randDoubleInRange(0, 1) < 0.9){//sec time += MiscUtil.randLongInRange(0, 59) * 1000; } if(MiscUtil.randDoubleInRange(0, 1) < 0.8){//min time += MiscUtil.randLongInRange(0, 59) * 1000*60; } if(MiscUtil.randDoubleInRange(0, 1) < 0.10){//h time += MiscUtil.randLongInRange(0, 5) * 1000*60*60; } incTime(time); } public boolean areTimersRunning(){ return (activeNode != root); } public String stopAndPop(){ if(activeNode == root){ throw new IllegalArgumentException("Can't stop the root!"); } String stoppedName = activeNode.name; activeNode.timer.stop(); activeNode = activeNode.parent; return stoppedName; } public String stopAndPop(String name){ if(!name.equals(activeNode.name)){ log.warn(String.format("You're stopping %s, not %s like you thought.", activeNode.name, name)); } return stopAndPop(); } ArrayList nameCol, timeCol; public String toString(){ return toString(false); } public String toString(boolean printZeros){ nameCol = new ArrayList(); timeCol = new ArrayList(); StringBuffer sb = new StringBuffer(); ArrayList childList = new ArrayList(root.children.keySet()); Collections.sort(childList); for(Object childName : childList){ Node child = (Node)root.children.get(childName); buildString(child, 0, printZeros); } ObjectMatrix2D stringMatrix = ObjectFactory2D.dense.make(nameCol.size(),2); for(int i=0; i<nameCol.size(); i++){ stringMatrix.set(i, 0, nameCol.get(i)); stringMatrix.set(i, 1, timeCol.get(i)); } cern.colt.matrix.objectalgo.Formatter objFormatter = new cern.colt.matrix.objectalgo.Formatter(); //objFormatter.setAlignment(Formatter.RIGHT); objFormatter.setPrintShape(false); return objFormatter.toString(stringMatrix); } protected long buildString(Node node, int depth, boolean printZeros){ //depth first pre-order traversal nameCol.add(pad(depth) + node.name); timeCol.add(pad(depth) + "|" + StringUtil.formatMillis( node.timer.getElapsedTime(), printZeros)); long totalChildrenTime = 0; ArrayList childList = new ArrayList(node.children.keySet()); Collections.sort(childList); for(Object childName : childList){ Node child = (Node)node.children.get(childName); totalChildrenTime += buildString(child, depth+1, printZeros); } if(childList.size() > 0){ nameCol.add(pad(depth+1) + "[overhead]"); long overhead = node.timer.getElapsedTime() - totalChildrenTime; timeCol.add(pad(depth+1) + "|" + StringUtil.formatMillis(overhead, printZeros)); } return node.timer.getElapsedTime(); } protected String pad(int depth){ StringBuffer sb = new StringBuffer(); for(int i=0; i<depth; i++){ sb.append(" "); } return sb.toString(); } /** Takes a list of TimingTrees and returns a tree that is the "sum" of * timings across same-name timiers. * * Note: Sums times over the union of the trees: eg, sum has a leaf if any of * the timers have such a leaf. * * All timers should be stopped. * * */ public static TimingTree sum(List trees){ TimingTree sumTT = new TimingTree(); List roots = new ArrayList(); for(Object tree : trees){ roots.add(((TimingTree)tree).root); } sumNodeAndRecurse(sumTT.root, roots); return sumTT; } protected static void sumNodeAndRecurse(Node sumNode, List nodesToSum){ long time = 0; Map childListMap = new HashMap(); //map from String name -> Nodes of that name // calculate time for sumNode in the sumTree, and // create a list of all children of each name for(Object nodeObj : nodesToSum){ Node node = (Node) nodeObj; time += node.timer.getElapsedTime(); //if(node.timer.isRunning()){ //log.warn("Error, running timer " + node.name); //} for(Object childKey : node.children.keySet()){ if(!childListMap.containsKey(childKey)){ childListMap.put(childKey, new ArrayList()); } ArrayList listForName = (ArrayList) childListMap.get(childKey); listForName.add(node.children.get(childKey)); } } sumNode.timer.incTime(time); for(Object entryObj : childListMap.entrySet()){ Map.Entry entry = (Map.Entry) entryObj; String childKey = (String) entry.getKey(); List childNodeList = (List)entry.getValue(); Node sumChild = sumNode.getOrCreateChild(childKey); sumNodeAndRecurse(sumChild, childNodeList); } } class Node{ String name; ExecutionTimer timer; Node parent; Map children; // maps String name -> Node child protected Node(String name, Node parent){ this.parent = parent; this.name = name; this.timer = new ExecutionTimer(name); this.children = new HashMap(); } protected Node start(String name){ Node child = getOrCreateChild(name); assert !child.timer.isRunning(); child.timer.start(); return child; } protected Node getOrCreateChild(String name){ if(children.containsKey(name)){ return (Node) children.get(name); }else{ Node child = new Node(name, this); children.put(name, child); return child; } } } public static TimingTree getSampleTree(){ TimingTree tt = new TimingTree(); tt.start("root task 1"); tt.start("subroutine"); tt.start("super-sub"); tt.incTimeRand(); tt.stopAndPop();tt.incTimeRand(); tt.start("equally super sub");tt.incTimeRand(); tt.stopAndPop();tt.incTimeRand(); tt.stopAndPop();tt.incTimeRand(); tt.start("subroutine 2");tt.incTimeRand();tt.incTimeRand(); tt.stopAndPop();tt.incTimeRand(); tt.stopAndPop();tt.incTimeRand(); tt.start("root task 2");tt.incTimeRand(); tt.stopAndPop(); return tt; } public static void test(){ System.out.println("Timer One:"); TimingTree tt1 = getSampleTree(); assert(!tt1.areTimersRunning()); System.out.println(tt1); System.out.println(); System.out.println("Timer Two:"); TimingTree tt2 = getSampleTree(); assert(!tt2.areTimersRunning()); System.out.println(tt2); System.out.println(); System.out.println(); ArrayList trees = new ArrayList(2); trees.add(tt1); trees.add(tt2); TimingTree ttSum = TimingTree.sum(trees); assert(!ttSum.areTimersRunning()); System.out.println(ttSum); } } Index: Timing.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/Timing.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** Timing.java 10 Aug 2006 22:53:35 -0000 1.1 --- Timing.java 29 Aug 2006 19:29:56 -0000 1.2 *************** *** 28,32 **** protected static Logger log = Logger.getLogger(Timing.class); ! public static Timing t = new Timing(); protected Map map = new HashMap(); --- 28,32 ---- protected static Logger log = Logger.getLogger(Timing.class); ! private static Timing t = new Timing(); protected Map map = new HashMap(); Index: MiscUtil.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/MiscUtil.java,v retrieving revision 1.22 retrieving revision 1.23 diff -C2 -d -r1.22 -r1.23 *** MiscUtil.java 6 Aug 2006 19:02:46 -0000 1.22 --- MiscUtil.java 29 Aug 2006 19:29:56 -0000 1.23 *************** *** 822,825 **** --- 822,831 ---- + public final static boolean isNonNegative(double[] a){ + for(double v : a){ + if( v < 0) return false; + } + return true; + } /** Checks that the values in dist sum to 1 (within 10e-6) */ *************** *** 827,830 **** --- 833,837 ---- double sum = 0; for(int i=0; i< dist.length; i++){ + if(dist[i] < 0) return false; sum += dist[i]; } Index: StringUtil.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/StringUtil.java,v retrieving revision 1.12 retrieving revision 1.13 diff -C2 -d -r1.12 -r1.13 *** StringUtil.java 10 Aug 2006 22:53:35 -0000 1.12 --- StringUtil.java 29 Aug 2006 19:29:56 -0000 1.13 *************** *** 7,317 **** import org.apache.oro.text.regex.*; ! public final class StringUtil{ ! public static final String newline = System.getProperty("line.separator"); ! ! static PatternMatcher matcher = new Perl5Matcher(); ! static PatternCompiler compiler = new Perl5Compiler(); ! static Perl5Util regxp = new Perl5Util(); ! static Logger log = Logger.getLogger(StringUtil.class.getName()); ! ! public static final String formatMillis(long ms){ ! String w = " "; ! long totalMillis = ms; ! long sec = ms / 1000; ! ms = ms % 1000; ! if(sec == 0){ ! return String.format("%s%s%s %03dms",w,w,w, ms); ! } ! long min = sec / 60; ! sec = sec % 60; ! if (min ==0){ ! return String.format("%s%s%02ds %03dms",w,w, sec, ms); ! } ! long hr = min / 60; ! min = min % 60; ! if (hr == 0){ ! return String.format("%s%02dm:%02ds %03dms", w, min, sec, ms); ! } ! ! assert 60*60*1000*hr + 60*1000*min + 1000*sec + ms == totalMillis; ! ! ! return String.format("%dh:%02dm:%02ds %03dms", hr, min, sec, ms); ! } ! public static final String stripLeadingNumsUnderscore(String s){ ! return regxp.substitute("s/^[0-9]+_//", s); } ! /** Returns true if the string is non-null and non-whitespace */ ! public static final boolean hasContents(String s){ ! if(s==null) return false; ! return !s.trim().equals(""); ! } ! /** Returns a representation of the current time without any ! * spaces, usefull for use in temprorary file names, etc. ! The format is month-day-year_[hr]h-[min]m ! */ ! public static final String timeForFile(){ ! Calendar c = Calendar.getInstance(); ! ! return c.get(c.MONTH) + "-" + ! c.get(c.DAY_OF_MONTH) + "-" + ! c.get(c.YEAR) + "_" + ! c.get(c.HOUR_OF_DAY) +"h-" + ! c.get(c.MINUTE) + "m"; ! } ! public static String readToString(InputStream in){ ! StringBuffer sb = new StringBuffer(); ! try{ ! BufferedReader inbr = new BufferedReader(new InputStreamReader(in)); ! String line; ! while((line = inbr.readLine() )!=null){ ! sb.append(line); ! } ! }catch(IOException e){ ! log.error("IOException while reading from stream " + in+ " to String.", e); ! return null; ! } ! ! return sb.toString(); } ! /** replaces all occurence of the "key" strings with the "value" ! * string in target String*/ ! public static final String simpleReplace(Map oldNew, String target){ ! Set entries = oldNew.entrySet(); ! Iterator iter = entries.iterator(); ! while(iter.hasNext()){ ! Map.Entry en = (Map.Entry)iter.next(); ! String key = (String)en.getKey(); ! String val = (String)en.getValue(); ! target = simpleReplace(target, key, val); ! } ! return target; } ! //replaces all occurences of old with subsValue in string ! public static final String simpleReplace(String string, String old, String subsValue){ ! try{ ! return Util.substitute ! (matcher, ! compiler.compile(Perl5Compiler.quotemeta(old)), ! new StringSubstitution(subsValue), ! string, ! Util.SUBSTITUTE_ALL ); ! }catch(MalformedPatternException e){ ! log.error("Error substituting for " + old + StringUtil.newline + ! " with subs value: " + StringUtil.newline + subsValue, e); ! return null; ! } } ! ! public static AbstractList tokenize(String string){ ! StringTokenizer st = new StringTokenizer(string); ! ArrayList tokens = new ArrayList(); ! while(st.hasMoreTokens()){ ! tokens.add(st.nextToken()); ! } ! return tokens; } ! ! /** Tokenizes the string, treating strings quoted with " as a ! * single entry. Also parses numbers. The resulting List thus ! * has Strings and Doubles. Returns null if parsing fails.*/ ! public static AbstractList tokenizeWithQuotes(String string){ ! // StreamTokenizer seems to be pretty badly broken ... it ! // won't parse strings like "../CMU/Climbing" if number ! // parsing is disabled. so we reset everything, make ! // everything except whitespace word characters. then, for ! // each word we try to parse it as a double, and if that ! // succeeds we keep it as a double, if not we put the string ! // in the list. ! ! ! try{ ! StringReader reader = new StringReader(string); ! StreamTokenizer st = new StreamTokenizer(reader); ! st.resetSyntax(); ! st.wordChars(0,255); ! st.whitespaceChars(0, ' '); ! st.quoteChar('"'); ! st.slashSlashComments(false); ! st.slashStarComments(false); ! LinkedList tokens = new LinkedList(); ! while(st.nextToken() != StreamTokenizer.TT_EOF){ ! if(st.ttype == StreamTokenizer.TT_WORD || st.ttype == '"'){ ! log.debug("Read Word or Quote" + st.sval); ! String s = st.sval; ! Double d = makeDouble(s); ! tokens.add( (d == null) ? (Object)s : (Object)d ); ! }else{//single character, convert to string and add ! char c = (char)st.ttype; ! log.warn("Got unexpected ttype " + st.ttype + " " + c+ ", sval = " + st.sval +", nval = " + st.nval); ! } ! } ! return tokens; ! }catch(IOException e){ ! log.error("tokenizing failed", e); ! return null; ! } ! } ! ! /** Prefixes the string input with enough copies of pad that it ! has length equal to length. Returns null and logs an error if ! padding with pad cannot produce a string of exactly length. ! Usually, pad should be a single character.*/ ! public static String pad(String input, int length, String pad){ ! StringBuffer sb = new StringBuffer(); ! int padLength = length - input.length(); ! while(sb.length() < padLength){ ! sb.append(pad); ! } ! sb.append(input); ! if(sb.length() != length){ ! log.error("Could not pad \"" + input +"\" with \"" + pad +"\" to get length " + length); ! return null; } ! return sb.toString(); } ! // --------------- Convert Objects to Strings, etc -------------------- // ! ! /** ! Takes an object input and makes an appropriate string, which can then ! be placed in the properties class. If the class type is unknown, ! returns the null string. ! @deprecated Use ObjectSerializer; ! */ ! public static String getObjectsString(Object o){ ! return ObjectSerializer.getStringForObject(o); ! } ! /** ! Takes the string input and tries to make it an instance of ! type. If type is primitive, tries to create the appropriate ! wrapped object. Returns null if input is null, if input is ! "null", or if some error in conversion occurs. ! @deprecated Use ObjectSerializer; ! */ ! ! public static Object makeStringObject(String input){ ! return ObjectSerializer.getObjectForString(input); ! } ! // --------------- String Conversion Methods --------------- // ! public static Integer makeInteger(String s){ ! if(s==null) return null; ! Integer rval = null; ! try{ ! rval = new Integer(s); ! }catch(NumberFormatException e){ ! return null; ! } ! return rval; ! } ! public static Short makeShort(String s){ ! if(s==null) return null; ! Short rval = null; ! try{ ! rval = new Short(s); ! }catch(NumberFormatException e){ ! return null; ! } ! return rval; } ! public static Long makeLong(String s){ ! if(s==null) return null; ! Long rval = null; ! try{ ! rval = new Long(s); ! }catch(NumberFormatException e){ ! return null; ! } ! return rval; } ! public static Float makeFloat(String s){ ! if(s==null) return null; ! Float rval = null; ! try{ ! rval = new Float(s); ! }catch(NumberFormatException e){ ! return null; ! } ! return rval; } ! public static Double makeDouble(String s){ ! if(s==null) return null; ! Double rval = null; ! try{ ! rval = new Double(s); ! }catch(NumberFormatException e){ ! return null; ! } ! return rval; } ! ! public static Boolean makeBoolean(String s){ ! if(s==null) return null; ! s=s.trim(); ! if(s.equalsIgnoreCase("true") ! || s.equalsIgnoreCase("yes") ! || s.equalsIgnoreCase("1") ) ! { ! return new Boolean(true); ! }else{ ! return new Boolean(false); ! } } ! public static Byte makeByte(String s){ ! if(s==null) return null; ! Byte rval; ! try{ ! rval = new Byte(s); ! }catch(NumberFormatException e){ ! return null; ! } ! return rval; } ! public static Character makeCharacter(String s){ ! if(s==null) return null; ! if(s.length() != 1) return null; ! return new Character(s.charAt(0)); } ! public static void main(String[] args){ ! long[] times = { 100, 13011, ! 60*60*1000*59 + 60*1000*32 + 1000*49 + 333, ! 60*60*1000*0 + 60*1000*32 + 1000*49 + 333, ! 60*60*1000*0 + 60*1000*0 + 1000*49 + 3, ! 60*60*1000*90 + 60*1000*0 + 1000*49 + 3, ! }; ! for (long t : times){ ! System.out.println(StringUtil.formatMillis(t)); ! } } ! } --- 7,333 ---- import org.apache.oro.text.regex.*; ! public final class StringUtil { ! public static final String newline = System.getProperty("line.separator"); ! static PatternMatcher matcher = new Perl5Matcher(); ! static PatternCompiler compiler = new Perl5Compiler(); ! static Perl5Util regxp = new Perl5Util(); ! static Logger log = Logger.getLogger(StringUtil.class.getName()); ! ! public static final String formatMemory(long bytes){ ! String format = "%.2f"; ! double bytesD = bytes; ! if(bytesD < 1024){ ! return String.format(format + " B", bytesD); ! }else if (bytesD < 1024*1024){ ! return String.format(format + " KB", bytesD/1024); ! }else if(bytesD < 1024*1024*1024){ ! return String.format(format + " MB", bytesD/(1024*1024)); ! }else{ ! return String.format(format + " GB", bytesD/(1024*1024*1024)); ! } ! } ! ! public static final String formatMillis(long ms){ ! return formatMillis(ms, false); ! } ! ! public static final String formatMillis(long ms, boolean printZeros) { ! String w = " "; ! long totalMillis = ms; ! long sec = ms / 1000; ! ms = ms % 1000; ! if (sec == 0 && !printZeros) { ! return String.format("%s%s%s %03dms", w, w, w, ms); ! } ! long min = sec / 60; ! sec = sec % 60; ! if (min == 0 && !printZeros) { ! return String.format("%s%s%02ds %03dms", w, w, sec, ms); ! } ! long hr = min / 60; ! min = min % 60; ! if (hr == 0 && !printZeros) { ! return String.format("%s%02dm:%02ds %03dms", w, min, sec, ms); } ! assert 60 * 60 * 1000 * hr + 60 * 1000 * min + 1000 * sec + ms == totalMillis; ! ! if(!printZeros){ ! return String.format("%2dh:%02dm:%02ds %03dms", hr, min, sec, ms); ! }else{ ! return String.format("%02dh:%02dm:%02ds %03dms", hr, min, sec, ms); } ! } + public static final String stripLeadingNumsUnderscore(String s) { + return regxp.substitute("s/^[0-9]+_//", s); + } ! /** Returns true if the string is non-null and non-whitespace */ ! public static final boolean hasContents(String s) { ! if (s == null) ! return false; ! return !s.trim().equals(""); ! } + /** Returns a representation of the current time without any + * spaces, usefull for use in temprorary file names, etc. + The format is month-day-year_[hr]h-[min]m + */ + public static final String timeForFile() { + Calendar c = Calendar.getInstance(); ! return c.get(c.MONTH) + "-" + c.get(c.DAY_OF_MONTH) + "-" + c.get(c.YEAR) ! + "_" + c.get(c.HOUR_OF_DAY) + "h-" + c.get(c.MINUTE) + "m"; ! } ! ! public static String readToString(InputStream in) { ! StringBuffer sb = new StringBuffer(); ! try { ! BufferedReader inbr = new BufferedReader(new InputStreamReader(in)); ! String line; ! while ((line = inbr.readLine()) != null) { ! sb.append(line); ! } ! } catch (IOException e) { ! log.error("IOException while reading from stream " + in + " to String.", ! e); ! return null; } + return sb.toString(); + } ! /** replaces all occurence of the "key" strings with the "value" ! * string in target String*/ ! public static final String simpleReplace(Map oldNew, String target) { ! Set entries = oldNew.entrySet(); ! Iterator iter = entries.iterator(); ! while (iter.hasNext()) { ! Map.Entry en = (Map.Entry) iter.next(); ! String key = (String) en.getKey(); ! String val = (String) en.getValue(); ! target = simpleReplace(target, key, val); } + return target; + } ! //replaces all occurences of old with subsValue in string ! public static final String simpleReplace(String string, String old, ! String subsValue) { ! try { ! return Util.substitute(matcher, compiler.compile(Perl5Compiler ! .quotemeta(old)), new StringSubstitution(subsValue), string, ! Util.SUBSTITUTE_ALL); ! } catch (MalformedPatternException e) { ! log.error("Error substituting for " + old + StringUtil.newline ! + " with subs value: " + StringUtil.newline + subsValue, e); ! return null; } + } ! public static AbstractList tokenize(String string) { ! StringTokenizer st = new StringTokenizer(string); ! ArrayList tokens = new ArrayList(); ! while (st.hasMoreTokens()) { ! tokens.add(st.nextToken()); } + return tokens; + } ! /** Tokenizes the string, treating strings quoted with " as a ! * single entry. Also parses numbers. The resulting List thus ! * has Strings and Doubles. Returns null if parsing fails.*/ ! public static AbstractList tokenizeWithQuotes(String string) { ! // StreamTokenizer seems to be pretty badly broken ... it ! // won't parse strings like "../CMU/Climbing" if number ! // parsing is disabled. so we reset everything, make ! // everything except whitespace word characters. then, for ! // each word we try to parse it as a double, and if that ! // succeeds we keep it as a double, if not we put the string ! // in the list. ! try { ! StringReader reader = new StringReader(string); ! StreamTokenizer st = new StreamTokenizer(reader); ! st.resetSyntax(); ! st.wordChars(0, 255); ! st.whitespaceChars(0, ' '); ! st.quoteChar('"'); ! st.slashSlashComments(false); ! st.slashStarComments(false); ! LinkedList tokens = new LinkedList(); ! while (st.nextToken() != StreamTokenizer.TT_EOF) { ! if (st.ttype == StreamTokenizer.TT_WORD || st.ttype == '"') { ! log.debug("Read Word or Quote" + st.sval); ! String s = st.sval; ! Double d = makeDouble(s); ! tokens.add((d == null) ? (Object) s : (Object) d); ! } else {//single character, convert to string and add ! char c = (char) st.ttype; ! log.warn("Got unexpected ttype " + st.ttype + " " + c + ", sval = " ! + st.sval + ", nval = " + st.nval); } ! } ! return tokens; ! } catch (IOException e) { ! log.error("tokenizing failed", e); ! return null; ! } ! } + /** Prefixes the string input with enough copies of pad that it + has length equal to length. Returns null and logs an error if + padding with pad cannot produce a string of exactly length. + Usually, pad should be a single character.*/ + public static String pad(String input, int length, String pad) { + StringBuffer sb = new StringBuffer(); + int padLength = length - input.length(); + while (sb.length() < padLength) { + sb.append(pad); + } + sb.append(input); + if (sb.length() != length) { + log.error("Could not pad \"" + input + "\" with \"" + pad + + "\" to get length " + length); + return null; } + return sb.toString(); ! } ! // --------------- Convert Objects to Strings, etc -------------------- // ! /** ! Takes an object input and makes an appropriate string, which can then ! be placed in the properties class. If the class type is unknown, ! returns the null string. ! @deprecated Use ObjectSerializer; ! */ ! public static String getObjectsString(Object o) { ! return ObjectSerializer.getStringForObject(o); ! } + /** + Takes the string input and tries to make it an instance of + type. If type is primitive, tries to create the appropriate + wrapped object. Returns null if input is null, if input is + "null", or if some error in conversion occurs. ! @deprecated Use ObjectSerializer; ! */ ! public static Object makeStringObject(String input) { ! return ObjectSerializer.getObjectForString(input); ! } ! // --------------- String Conversion Methods --------------- // ! ! public static Integer makeInteger(String s) { ! if (s == null) ! return null; ! Integer rval = null; ! try { ! rval = new Integer(s); ! } catch (NumberFormatException e) { ! return null; } + return rval; + } ! public static Short makeShort(String s) { ! if (s == null) ! return null; ! Short rval = null; ! try { ! rval = new Short(s); ! } catch (NumberFormatException e) { ! return null; } + return rval; + } ! public static Long makeLong(String s) { ! if (s == null) ! return null; ! Long rval = null; ! try { ! rval = new Long(s); ! } catch (NumberFormatException e) { ! return null; } + return rval; + } ! public static Float makeFloat(String s) { ! if (s == null) ! return null; ! Float rval = null; ! try { ! rval = new Float(s); ! } catch (NumberFormatException e) { ! return null; } ! return rval; ! } ! ! public static Double makeDouble(String s) { ! if (s == null) ! return null; ! Double rval = null; ! try { ! rval = new Double(s); ! } catch (NumberFormatException e) { ! return null; } + return rval; + } ! public static Boolean makeBoolean(String s) { ! if (s == null) ! return null; ! s = s.trim(); ! if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("yes") ! || s.equalsIgnoreCase("1")) { ! return new Boolean(true); ! } else { ! return new Boolean(false); } + } ! public static Byte makeByte(String s) { ! if (s == null) ! return null; ! Byte rval; ! try { ! rval = new Byte(s); ! } catch (NumberFormatException e) { ! return null; } + return rval; + } ! public static Character makeCharacter(String s) { ! if (s == null) ! return null; ! if (s.length() != 1) ! return null; ! return new Character(s.charAt(0)); ! } ! ! public static void main(String[] args) { ! long[] times = { 100, 13011, ! 60 * 60 * 1000 * 59 + 60 * 1000 * 32 + 1000 * 49 + 333, ! 60 * 60 * 1000 * 0 + 60 * 1000 * 32 + 1000 * 49 + 333, ! 60 * 60 * 1000 * 0 + 60 * 1000 * 0 + 1000 * 49 + 3, ! 60 * 60 * 1000 * 90 + 60 * 1000 * 0 + 1000 * 49 + 3, }; ! for (long t : times) { ! System.out.println(StringUtil.formatMillis(t)); } ! } } Index: FileUtil.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/FileUtil.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** FileUtil.java 2 Aug 2006 21:36:16 -0000 1.13 --- FileUtil.java 29 Aug 2006 19:29:56 -0000 1.14 *************** *** 129,136 **** } public static boolean writeObjectToFile(File f, Serializable object){ try{ FileOutputStream ostream = new FileOutputStream(f); ! ObjectOutputStream p = new ObjectOutputStream(ostream); p.writeObject(object); p.flush(); --- 129,151 ---- } + public static boolean writeToFile(File f, double[] data){ + try { + Writer out = new FileWriter(f); + for(int i=0; i<data.length; i++){ + out.write(String.format("%.15g%n", data[i])); + } + out.close(); + } catch (IOException e) { + log.warn("Error writing array to file " + f, e); + return false; + } + return true; + } + + public static boolean writeObjectToFile(File f, Serializable object){ try{ FileOutputStream ostream = new FileOutputStream(f); ! ObjectOutputStream p = new ObjectOutputStream(new BufferedOutputStream(ostream)); p.writeObject(object); p.flush(); *************** *** 389,392 **** --- 404,409 ---- } + + public static DoubleArrayList readToDoubleArray(File f){ try{ Index: CernUtil.java =================================================================== RCS file: /cvsroot/jigs/jigs/src/edu/whitman/halfway/util/CernUtil.java,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** CernUtil.java 29 Mar 2005 20:53:59 -0000 1.11 --- CernUtil.java 29 Aug 2006 19:29:56 -0000 1.12 *************** *** 3,6 **** --- 3,7 ---- import cern.colt.matrix.*; //import cern.colt.matrix.impl.*; + import cern.colt.matrix.impl.RCDoubleMatrix2D; import cern.colt.matrix.linalg.Algebra; import cern.colt.list.DoubleArrayList; *************** *** 11,14 **** --- 12,20 ---- import java.io.*; + import no.uib.cipr.matrix.Matrix; + import no.uib.cipr.matrix.MatrixEntry; + import no.uib.cipr.matrix.Vector; + import no.uib.cipr.matrix.VectorEntry; + import org.apache.log4j.Logger; *************** *** 18,25 **** public final class CernUtil{ - private static Logger log = Logger.getLogger(CernUtil.class); - - private static cern.jet.math.Functions F = cern.jet.math.Functions.functions; private static Algebra linalg = new Algebra(); --- 24,28 ---- *************** *** 32,35 **** --- 35,73 ---- + public static final String densityStatString(String prefix, + DoubleMatrix1D vector, double approxThresh){ + StringBuffer sb = new StringBuffer(prefix); + double fracNonzero = fractionNonzero(vector); + sb.append(String.format("%.3f%% nonzero", fracNonzero*100.0)); + if(approxThresh != 0){ + double approxNonzero = fractionNonzero(vector, approxThresh); + sb.append(String.format(", %.3f%% approx nonzero", + approxNonzero*100)); + } + return sb.toString(); + } + + + /** + * Note sure how efficient this actually is. + * @param m + * @return + */ + public static final DoubleMatrix2D makeRC(Matrix m){ + DoubleMatrix2D m2 = DoubleFactory2D.rowCompressed.make(m.numRows(), m.numColumns()); + for(MatrixEntry me : m){ + m2.set(me.row(), me.column(), me.get()); + } + return m2; + } + + public static final DoubleMatrix1D makeSparse(Vector v){ + DoubleMatrix1D nv = DoubleFactory1D.sparse.make(v.size()); + for(VectorEntry ve : v){ + nv.set(ve.index(), ve.get()); + } + return nv; + } + /* the bins are 1, ..., numBins (not 0, ... , numBins-1) so we can * use sparsity as a mask.*/ *************** *** 90,93 **** --- 128,138 ---- } + public static final int minNonzeroIndex(DoubleMatrix1D a){ + IntArrayList indexList = new IntArrayList(); + a.getNonZeros(indexList, null); + indexList.sort(); + return indexList.get(0); + } + protected static final boolean sanityCheckBins(DoubleMatrix2D in, DoubleMatrix2D out, int numBins){ int nr = in.rows(); *************** *** 166,169 **** --- 211,236 ---- } + public static final double fractionNonzero(DoubleMatrix1D a){ + return fractionNonzero(a, 0.0); + } + + public static final double fractionNonzero(DoubleMatrix1D a, double epsilon){ + if(epsilon == 0.0){ + return (double)a.cardinality()/a.size(); + }else{ + DoubleArrayList valueList = new DoubleArrayList(0); + a.getNonZeros(null, valueList); + int nnz = valueList.size(); + double count = 0; + for(int i = 0; i<nnz; i++){ + double v = valueList.get(i); + if(v > epsilon || v < -epsilon){ + count ++; + } + } + return count/(double)a.size(); + } + } + public static final double maxIgnoreZero(DoubleMatrix1D a){ int n = a.size(); |