List of usage examples for weka.core Instance setDataset
public void setDataset(Instances instances);
From source file:cish.CISH.java
/** * Returns the (only) centromer points (cep), (only) gene points and the points which are classified as * centromer and gene (whenever the gene is too close to the centromer). * @param ip The image as image processor on which points are classified. * @param circles The points which are classified into the three classes cep, gene and both. * @param r A radius r for feature extraction around each point. * @param classifier A trained classifier used for classification. * @param dataset A dataset used for classification (technically necessary). * @return Three lists are returned: ceps, a n x 5 list for n centromer points, each with x coord, y coord, R G and B color value; * genes, a m x 5 list for m gene points, each with x coord, y coord, R G and B color value; * both, a k x 5 list for k "both" points, each with x coord, y coord, R G and B color value; *//*from ww w .j a v a 2s . c om*/ static List<List> getPoints(ImageProcessor ip, Point[] circles, int r, Classifier classifier, Instances dataset) { List<int[]> ceps = new ArrayList<>(); List<int[]> genes = new ArrayList<>(); List<int[]> both = new ArrayList<>(); int x, y; Instance inst; double c; for (Point p : circles) { try { x = p.x; y = p.y; double[] attValues = new double[(2 * r + 1) * (2 * r + 1) * 3 + 1]; int k = 0; int[] rgb = new int[3]; for (int n = 0; n < 3; n++) { for (int i = x - r; i <= x + r; i++) { for (int j = y - r; j <= y + r; j++) { ip.getPixel(i, j, rgb); attValues[k] = rgb[n] / 255.0; k++; } } } inst = new Instance(1, attValues); inst.setDataset(dataset); c = classifier.classifyInstance(inst); int[] entry = Misc.concat(new int[] { x, y }, COLORS[(int) c]); switch ((int) c) { case 0: ceps.add(entry); break; case 1: genes.add(entry); break; case 4: both.add(entry); break; default: break; } } catch (Exception ex) { //Logger.getLogger(CISH.class.getName()).log(Level.SEVERE, null, ex); } } List<List> out = new ArrayList<>(); out.add(ceps); out.add(genes); out.add(both); return out; }
From source file:classifier.CustomStringToWordVector.java
License:Open Source License
/** * Converts the instance w/o normalization. * /* w w w . j a va 2 s. c o m*/ * @oaram instance the instance to convert * @param v * @return the conerted instance */ private int convertInstancewoDocNorm(Instance instance, FastVector v) { // Convert the instance into a sorted set of indexes TreeMap contained = new TreeMap(); // Copy all non-converted attributes from input to output int firstCopy = 0; for (int i = 0; i < getInputFormat().numAttributes(); i++) { if (!m_SelectedRange.isInRange(i)) { if (getInputFormat().attribute(i).type() != Attribute.STRING) { // Add simple nominal and numeric attributes directly if (instance.value(i) != 0.0) { contained.put(new Integer(firstCopy), new Double(instance.value(i))); } } else { if (instance.isMissing(i)) { contained.put(new Integer(firstCopy), new Double(Utils.missingValue())); } else { // If this is a string attribute, we have to first add // this value to the range of possible values, then add // its new internal index. if (outputFormatPeek().attribute(firstCopy).numValues() == 0) { // Note that the first string value in a // SparseInstance doesn't get printed. outputFormatPeek().attribute(firstCopy) .addStringValue("Hack to defeat SparseInstance bug"); } int newIndex = outputFormatPeek().attribute(firstCopy) .addStringValue(instance.stringValue(i)); contained.put(new Integer(firstCopy), new Double(newIndex)); } } firstCopy++; } } for (int j = 0; j < instance.numAttributes(); j++) { // if ((getInputFormat().attribute(j).type() == Attribute.STRING) if (m_SelectedRange.isInRange(j) && (instance.isMissing(j) == false)) { m_Tokenizer.tokenize(instance.stringValue(j)); while (m_Tokenizer.hasMoreElements()) { String word = (String) m_Tokenizer.nextElement(); if (this.m_lowerCaseTokens == true) word = word.toLowerCase(); word = m_Stemmer.stem(word); Integer index = (Integer) m_Dictionary.get(word); if (index != null) { if (m_OutputCounts) { // Separate if here rather than // two lines down to avoid // hashtable lookup Double count = (Double) contained.get(index); if (count != null) { contained.put(index, new Double(count.doubleValue() + 1.0)); } else { contained.put(index, new Double(1)); } } else { contained.put(index, new Double(1)); } } } } } // Doing TFTransform if (m_TFTransform == true) { Iterator it = contained.keySet().iterator(); for (int i = 0; it.hasNext(); i++) { Integer index = (Integer) it.next(); if (index.intValue() >= firstCopy) { double val = ((Double) contained.get(index)).doubleValue(); val = Math.log(val + 1); contained.put(index, new Double(val)); } } } // Doing IDFTransform if (m_IDFTransform == true) { Iterator it = contained.keySet().iterator(); for (int i = 0; it.hasNext(); i++) { Integer index = (Integer) it.next(); if (index.intValue() >= firstCopy) { double val = ((Double) contained.get(index)).doubleValue(); val = val * Math.log(m_NumInstances / (double) m_DocsCounts[index.intValue()]); contained.put(index, new Double(val)); } } } // Convert the set to structures needed to create a sparse instance. double[] values = new double[contained.size()]; int[] indices = new int[contained.size()]; Iterator it = contained.keySet().iterator(); for (int i = 0; it.hasNext(); i++) { Integer index = (Integer) it.next(); Double value = (Double) contained.get(index); values[i] = value.doubleValue(); indices[i] = index.intValue(); } Instance inst = new SparseInstance(instance.weight(), values, indices, outputFormatPeek().numAttributes()); inst.setDataset(outputFormatPeek()); v.addElement(inst); return firstCopy; }
From source file:classifier.page.PageClassifier.java
License:Open Source License
public double[] classify(String target) throws Exception { double[] result = null; try {//from w w w . j a va2 s. c o m double[] values = getValues(new Page(null, target)); weka.core.Instance instanceWeka = new weka.core.Instance(1, values); instanceWeka.setDataset(instances); result = classifier.distributionForInstance(instanceWeka); } catch (Exception ex) { ex.printStackTrace(); } return result; }
From source file:cluster.ABC.ClusterUtils.java
License:Open Source License
/** Finds the sum of instance sum with instance inst */// w ww . j a v a2s . co m public static Instance sumWithInstance(Instance sum, Instance inst, Instances m_Instances) throws Exception { Instance newSum; if (sum == null) { if (inst instanceof SparseInstance) { newSum = new SparseInstance(inst); newSum.setDataset(m_Instances); } else { newSum = new Instance(inst); newSum.setDataset(m_Instances); } } else { newSum = sumInstances(sum, inst, m_Instances); } return newSum; }
From source file:cn.edu.xjtu.dbmine.StringToWordVector.java
License:Open Source License
/** * Converts the instance w/o normalization. * /*from w w w . ja v a2s . c o m*/ * @oaram instance the instance to convert * @param v * @return the conerted instance */ private int convertInstancewoDocNorm(Instance instance, FastVector v) { // Convert the instance into a sorted set of indexes TreeMap contained = new TreeMap(); // Copy all non-converted attributes from input to output int firstCopy = 0; for (int i = 0; i < getInputFormat().numAttributes(); i++) { if (!m_SelectedRange.isInRange(i)) { if (getInputFormat().attribute(i).type() != Attribute.STRING) { // Add simple nominal and numeric attributes directly if (instance.value(i) != 0.0) { contained.put(new Integer(firstCopy), new Double(instance.value(i))); } } else { if (instance.isMissing(i)) { contained.put(new Integer(firstCopy), new Double(Instance.missingValue())); } else { // If this is a string attribute, we have to first add // this value to the range of possible values, then add // its new internal index. if (outputFormatPeek().attribute(firstCopy).numValues() == 0) { // Note that the first string value in a // SparseInstance doesn't get printed. outputFormatPeek().attribute(firstCopy) .addStringValue("Hack to defeat SparseInstance bug"); } int newIndex = outputFormatPeek().attribute(firstCopy) .addStringValue(instance.stringValue(i)); contained.put(new Integer(firstCopy), new Double(newIndex)); } } firstCopy++; } } for (int j = 0; j < instance.numAttributes(); j++) { // if ((getInputFormat().attribute(j).type() == Attribute.STRING) if (m_SelectedRange.isInRange(j) && (instance.isMissing(j) == false)) { m_Tokenizer.tokenize(instance.stringValue(j)); while (m_Tokenizer.hasMoreElements()) { String word = (String) m_Tokenizer.nextElement(); if (this.m_lowerCaseTokens == true) word = word.toLowerCase(); word = m_Stemmer.stem(word); Integer index = (Integer) m_Dictionary.get(word); if (index != null) { if (m_OutputCounts) { // Separate if here rather than // two lines down to avoid // hashtable lookup Double count = (Double) contained.get(index); if (count != null) { contained.put(index, new Double(count.doubleValue() + 1.0)); } else { contained.put(index, new Double(1)); } } else { contained.put(index, new Double(1)); } } } } } // Doing TFTransform if (m_TFTransform == true) { Iterator it = contained.keySet().iterator(); for (int i = 0; it.hasNext(); i++) { Integer index = (Integer) it.next(); if (index.intValue() >= firstCopy) { double val = ((Double) contained.get(index)).doubleValue(); val = Math.log(val + 1); contained.put(index, new Double(val)); Tfcontained.put(index, new Double(val)); ; } } } // Doing IDFTransform if (m_IDFTransform == true) { Iterator it = contained.keySet().iterator(); for (int i = 0; it.hasNext(); i++) { Integer index = (Integer) it.next(); if (index.intValue() >= firstCopy) { double val = ((Double) contained.get(index)).doubleValue(); val = val * Math.log(m_NumInstances / ((double) m_DocsCounts[index.intValue()] + 0.01)); contained.put(index, new Double(val)); } } } // Convert the set to structures needed to create a sparse instance. double[] values = new double[contained.size()]; int[] indices = new int[contained.size()]; Iterator it = contained.keySet().iterator(); for (int i = 0; it.hasNext(); i++) { Integer index = (Integer) it.next(); Double value = (Double) contained.get(index); values[i] = value.doubleValue(); indices[i] = index.intValue(); } Instance inst = new SparseInstance(instance.weight(), values, indices, outputFormatPeek().numAttributes()); inst.setDataset(outputFormatPeek()); v.addElement(inst); return firstCopy; }
From source file:cn.ict.zyq.bestConf.bestConf.BestConf.java
License:Open Source License
public Instances loadPropertiesAsInstancesPre(String Path) { HashMap<String, String> pmap = null; try {/*from www . j a v a2 s .c o m*/ pmap = Yaml.loadType(new FileInputStream(yamlPath), HashMap.class); } catch (FileNotFoundException e) { e.printStackTrace(); } atts = new ArrayList<Attribute>(); Instance dfIns = new DenseInstance(pmap.size()); int pos = 0; double[] vals = new double[pmap.size()]; for (Map.Entry<String, String> ent : pmap.entrySet()) { try { double val = Double.valueOf(String.valueOf(ent.getValue())); vals[pos] = val; Properties p1 = new Properties(); double upper, lower; if (val != 0) { upper = val * (1. + 0.5); lower = val * (1. - 0.5); } else { lower = val; upper = 1; } p1.setProperty("range", "[" + String.valueOf(lower) + "," + String.valueOf(upper) + "]"); ProtectedProperties prop1 = new ProtectedProperties(p1); atts.add(new Attribute(String.valueOf(ent.getKey()), prop1)); pos++; } catch (Exception e) { } } Instances dfProp = new Instances("DefaultConfig", atts, 1); dfProp.add(dfIns); dfIns.setDataset(dfProp); for (int i = 0; i < pos; i++) { dfIns.setValue(atts.get(i), vals[i]); //System.err.println(atts.get(i)+":"+vals[i]); } return dfProp; }
From source file:cn.ict.zyq.bestConf.bestConf.BestConf.java
License:Open Source License
public Instances loadPropertiesAsInstances(String Path) { HashMap<String, String> pmap = null; HashMap rangeMap = null;/*w w w . j av a2 s .c o m*/ try { pmap = Yaml.loadType(new FileInputStream(yamlPath), HashMap.class); rangeMap = Yaml.loadType(new FileInputStream(yamlPath + "_range"), HashMap.class); } catch (FileNotFoundException e) { e.printStackTrace(); } atts = new ArrayList<Attribute>(); int pos = 0; double[] vals = new double[pmap.size()]; Object range = null; for (Map.Entry<String, String> ent : pmap.entrySet()) { try { double val = Double.valueOf(String.valueOf(ent.getValue())); vals[pos] = val; Properties p1 = new Properties(); range = rangeMap.get(ent.getKey()); if (range != null) { String list = (String) range; if (list.indexOf('[') == -1 && list.indexOf('(') == -1) throw new Exception("No Range for You" + ent.getKey()); p1.setProperty("range", list.trim()); } else { double upper, lower; if (val != 0) { upper = val * (1. + 0.5); lower = val * (1. - 0.5); } else { lower = val; upper = 1; } p1.setProperty("range", "[" + String.valueOf(lower) + "," + String.valueOf(upper) + "]"); } ProtectedProperties prop1 = new ProtectedProperties(p1); atts.add(new Attribute(String.valueOf(ent.getKey()), prop1)); pos++; } catch (Exception e) { } } Instances dfProp = new Instances("DefaultConfig", atts, 1); Instance dfIns = new DenseInstance(atts.size()); for (int i = 0; i < pos; i++) { dfIns.setValue(atts.get(i), vals[i]); //System.err.println(atts.get(i)+":"+vals[i]); } dfProp.add(dfIns); dfIns.setDataset(dfProp); return dfProp; }
From source file:cn.ict.zyq.bestConf.COMT2.Branch2.java
License:Open Source License
public Instance maxPoint(Instances dataset) throws Exception { Instance max = new DenseInstance(dataset.numAttributes()); max.setDataset(dataset); double[] combinedCoefs = null; int len = 0;//from w w w .ja v a 2 s. c o m for (PreConstructedLinearModel model : linearModelList) { //initialization if (combinedCoefs == null) { len = model.coefficients().length; combinedCoefs = new double[len]; for (int i = 0; i < len; i++) combinedCoefs[i] = 0; } for (int i = 0; i < len; i++) combinedCoefs[i] += model.coefficients()[i]; } //the max value is obtained at ends of a range for (Map.Entry<Attribute, Range<Double>> ent : rangeMap.entrySet()) { int attIdx = ent.getKey().index(); if (combinedCoefs[attIdx] > 0) { //use the upper bound if (ent.getValue().hasUpperBound()) max.setValue(attIdx, ent.getValue().upperEndpoint()); } else if (combinedCoefs[attIdx] < 0) { //use the lower bound if (ent.getValue().hasLowerBound()) max.setValue(attIdx, ent.getValue().lowerEndpoint()); } } //now we set the predicted values double y = 0; for (PreConstructedLinearModel model : linearModelList) { y += model.classifyInstance(max); } y /= linearModelList.size(); max.setClassValue(y); return max; }
From source file:com.actelion.research.orbit.imageAnalysis.imaging.TMAPoints.java
License:Open Source License
/** * returns x/y pairs for each input point * * @param pList//from w ww. j a v a2 s . co m * @return */ private HashMap<Point, Point> clusterLines(List<Point> pList) { ArrayList<Attribute> attrListX = new ArrayList<Attribute>(2); attrListX.add(new Attribute("xvalue")); ArrayList<Attribute> attrListY = new ArrayList<Attribute>(2); attrListY.add(new Attribute("yvalue")); //attrList.add(new Attribute("class")); Instances xInst = new Instances("xlines", attrListX, pList.size()); Instances yInst = new Instances("ylines", attrListY, pList.size()); //instances.setClassIndex(1); for (Point p : pList) { //Instance inst = new DenseInstance(1d, new double[]{p.x,Double.NaN}); Instance instX = new DenseInstance(1d, new double[] { p.x }); instX.setDataset(xInst); //inst.setClassMissing(); xInst.add(instX); Instance instY = new DenseInstance(1d, new double[] { p.y }); instY.setDataset(yInst); yInst.add(instY); } try { EM colClusterer = new EM(); int numCols = guessNumClusters(colClusterer, xInst, 1, 20); colClusterer.setNumClusters(numCols); colClusterer.buildClusterer(xInst); logger.debug("NumCols: " + colClusterer.getNumClusters()); EM rowClusterer = new EM(); int numRows = guessNumClusters(rowClusterer, yInst, 1, 20); rowClusterer.setNumClusters(numRows); rowClusterer.buildClusterer(yInst); logger.debug("NumRows: " + rowClusterer.getNumClusters()); logger.trace("ColClusterer:"); HashMap<Integer, Integer> colHash = sortAndpPrintCluster(colClusterer); logger.trace("RowClusterer:"); HashMap<Integer, Integer> rowHash = sortAndpPrintCluster(rowClusterer); if (logger.isTraceEnabled()) { logger.trace("ColHash:"); for (Integer i : colHash.keySet()) { logger.trace("cluster " + i + ": " + colHash.get(i)); } logger.trace("RowHash:"); for (Integer i : rowHash.keySet()) { logger.trace("cluster " + i + ": " + rowHash.get(i)); } } // classify points HashMap<Point, Point> pMap = new HashMap<Point, Point>(); for (Point p : pList) { Instance instX = new DenseInstance(1d, new double[] { p.x }); instX.setDataset(xInst); Instance instY = new DenseInstance(1d, new double[] { p.y }); instY.setDataset(yInst); int x = colClusterer.clusterInstance(instX); int y = rowClusterer.clusterInstance(instY); x = colHash.get(x); y = rowHash.get(y); logger.trace(p + ": " + x + "/" + y); pMap.put(p, new Point(x, y)); } return pMap; } catch (Exception e) { e.printStackTrace(); logger.error("error while clustering points", e); return null; } }
From source file:com.actelion.research.orbit.imageAnalysis.tasks.ClassificationTaskTiled.java
License:Open Source License
public Long[] call() throws Exception { if (executed) throw new RuntimeException("only one execution allowed"); // only one execution! executed = true;/*from w w w . ja v a2 s .com*/ Long[] ratio = new Long[classShapes.size()]; for (int i = 0; i < ratio.length; i++) ratio[i] = new Long(0); Color c; Instance inst = null; TissueFeatures tissueFeatures = OrbitUtils.createTissueFeatures(featureDescription, bimg); double[] feats = new double[(windowSize * 2 + 1) * (windowSize * 2 + 1) * 3 + 1]; // +1 for contextclassification??? TiledImage classImg = null; if (writeClassificationImage) classImg = classImage.getImage(); PlanarImage image = bimg.getImage(); int numDone = 0; double oldProgress = 0d; if ((tileList == null) || (tileList.size() == 0)) { logger.trace("Warning: tileList.size()==0 (e.g. too many threads, but not problem)"); // happens if to many threads, but no problem firePropertyChangeEvent( new PropertyChangeEvent(this, CLASSIFICATION_PROGRESS, new Double(0), new Double(100))); return ratio; } if (tileList.size() == 1 && tileList.get(0).x == -1 && tileList.get(0).y == -1) { tileList = Arrays.asList(image.getTileIndices(null)); } HashSet<Integer> featureClassSet = null; if (featureDescription.getFeatureClasses() != null && featureDescription.getFeatureClasses().length > 0) { featureClassSet = new HashSet<Integer>(featureDescription.getFeatureClasses().length); for (int i = 0; i < featureDescription.getFeatureClasses().length; i++) { featureClassSet.add(featureDescription.getFeatureClasses()[i]); } } //System.out.println("tileList: "+tileList); for (Point tileNum : tileList) { //logger.trace("before getting raster "+tileNum.x+"/"+tileNum.y); Raster readRaster; if (OrbitUtils.TILEMODE) { readRaster = bimg.getModifiedImage(featureDescription).getTile(tileNum.x, tileNum.y); } else readRaster = bimg.getData(new Rectangle(image.tileXToX(tileNum.x) - windowSize, image.tileYToY(tileNum.y) - windowSize, image.getTileWidth() + (windowSize * 2 + 1), image.getTileHeight() + (windowSize * 2 + 1)), featureDescription); if (readRaster == null) { throw new OrbitImageServletException("error getting image raster"); } // apply raster modifications like color deconvolution readRaster = OrbitUtils.getModifiedRaster(readRaster, featureDescription, bimg.getImage().getColorModel(), true, tileNum.x, tileNum.y, "modifiedRaster"); WritableRaster writeRaster = null; if (writeClassificationImage) { try { writeRaster = classImg.getWritableTile(tileNum.x, tileNum.y); } catch (Throwable ex) { writeRaster = null; logger.error("error getting writable tile: " + ex.getMessage()); // batch mode error: unable to create new native thread } } int samples = featureDescription.getSampleSize(); int[] rgb = null; if (histograms != null) { if (histograms.length != samples) throw new IllegalArgumentException("Histogram size must match sample size. Histogram.length=" + histograms.length + " but samplesize=" + samples); rgb = new int[samples]; } final boolean doHistogram = (histograms != null && featureClassSet != null); int minX = image.tileXToX(tileNum.x); int maxX = image.tileXToX(tileNum.x) + image.getTileWidth(); int minY = image.tileYToY(tileNum.y); int maxY = image.tileYToY(tileNum.y) + image.getTileHeight(); if (ROI != null) { Rectangle bb = ROI.getBounds(); if (bb != null) { if (bb.getMinX() > minX) minX = (int) bb.getMinX(); if (bb.getMinY() > minY) minY = (int) bb.getMinY(); if (bb.getMaxX() < maxX) maxX = (int) bb.getMaxX(); if (bb.getMaxY() < maxY) maxY = (int) bb.getMaxY(); } } for (int x = minX; x < maxX; x++) for (int y = minY; y < maxY; y++) { if (canceled) continue; if (OrbitUtils.TILEMODE) { if (!((x > readRaster.getMinX() + windowSize) && (y > readRaster.getMinY() + windowSize) && (x < readRaster.getMinX() + readRaster.getWidth() - windowSize) && (y < readRaster.getMinY() + readRaster.getHeight() - windowSize))) continue; } if (OrbitUtils.isInROI(x, y, ROI, exclusionMapGen)) { if ((pixelFuzzyness > 0) && (rand.nextDouble() < pixelFuzzyness)) continue; feats = tissueFeatures.buildFeatures(readRaster, x, y, Double.NaN); inst = new DenseInstance(1.0d, feats); inst.setDataset(dataSet); try { double classVal; classVal = classifier.classifyInstance(inst); if (classVal > ratio.length - 1) logger.warn( "classVal > image ratios (class numbers and rations don't match anymore!) ratioLength: " + ratio.length + " classVal: " + classVal); ratio[(int) classVal]++; if (doHistogram) { if (featureClassSet.contains((int) classVal)) { rgb = readRaster.getPixel(x, y, rgb); for (int i = 0; i < rgb.length; i++) { histograms[i].inc(rgb[i]); } } } if (writeClassificationImage) { c = classShapes.get((byte) classVal).getColor(); writeRaster.setPixel(x, y, new int[] { c.getRed(), c.getGreen(), c.getBlue(), 255 }); } } catch (Exception e) { e.printStackTrace(); logger.error("error classifying image", e); } } // ROI check } // x,y if (writeClassificationImage) { classImg.releaseWritableTile(tileNum.x, tileNum.y); //DiskMemImage.getCommonTileCache().flush(); // really needed? -> NO! } if (canceled) break; numDone++; progress = (numDone / (double) tileList.size()) * 100d; firePropertyChangeEvent(new PropertyChangeEvent(this, CLASSIFICATION_PROGRESS, oldProgress, progress)); oldProgress = progress; // pause while (paused) { try { //this.wait(); Thread.sleep(200); } catch (Exception ex) { } } } // tileNum return ratio; }