Example usage for weka.core Instances relationName

List of usage examples for weka.core Instances relationName

Introduction

In this page you can find the example usage for weka.core Instances relationName.

Prototype


publicString relationName() 

Source Link

Document

Returns the relation's name.

Usage

From source file:adams.gui.menu.ROC.java

License:Open Source License

/**
 * Launches the functionality of the menu item.
 *///from   w  w w.j  av a2  s.  c  om
@Override
public void launch() {
    File file;
    if (m_Parameters.length == 0) {
        // choose file
        int retVal = m_FileChooser.showOpenDialog(null);
        if (retVal != JFileChooser.APPROVE_OPTION)
            return;
        file = m_FileChooser.getSelectedFile();
    } else {
        file = new PlaceholderFile(m_Parameters[0]).getAbsoluteFile();
        m_FileChooser.setSelectedFile(file);
    }

    // create plot
    Instances result;
    try {
        result = m_FileChooser.getLoader().getDataSet();
    } catch (Exception e) {
        GUIHelper.showErrorMessage(getOwner(),
                "Error loading file '" + file + "':\n" + adams.core.Utils.throwableToString(e));
        return;
    }
    result.setClassIndex(result.numAttributes() - 1);
    ThresholdVisualizePanel vmc = new ThresholdVisualizePanel();
    vmc.setROCString("(Area under ROC = " + Utils.doubleToString(ThresholdCurve.getROCArea(result), 4) + ")");
    vmc.setName(result.relationName());
    PlotData2D tempd = new PlotData2D(result);
    tempd.setPlotName(result.relationName());
    tempd.addInstanceNumberAttribute();
    // specify which points are connected
    boolean[] cp = new boolean[result.numInstances()];
    for (int n = 1; n < cp.length; n++)
        cp[n] = true;
    try {
        tempd.setConnectPoints(cp);
        vmc.addPlot(tempd);
    } catch (Exception e) {
        GUIHelper.showErrorMessage(getOwner(), "Error adding plot:\n" + adams.core.Utils.throwableToString(e));
        return;
    }

    ChildFrame frame = createChildFrame(vmc, GUIHelper.getDefaultDialogDimension());
    frame.setTitle(frame.getTitle() + " - " + file);
}

From source file:adams.gui.tools.previewbrowser.InstanceExplorerHandler.java

License:Open Source License

/**
 * Creates the actual view./*from w w  w  .  j  ava  2 s. com*/
 *
 * @param file   the file to create the view for
 * @return      the view
 */
@Override
protected PreviewPanel createPreview(File file) {
    InstancePanel result;
    Instances dataset;
    int i;
    Instance inst;
    List<InstanceContainer> data;

    result = new InstancePanel();
    try {
        data = new ArrayList<InstanceContainer>();
        dataset = DataSource.read(file.getAbsolutePath());
        for (i = 0; i < dataset.numInstances(); i++) {
            inst = new Instance();
            inst.set(dataset.instance(i));
            inst.setID((i + 1) + "." + dataset.relationName());
            data.add(result.getContainerManager().newContainer(inst));
        }
        result.getContainerManager().addAll(data);
    } catch (Exception e) {
        getLogger().log(Level.SEVERE, "Failed to load dataset '" + file + "':", e);
    }

    return new PreviewPanel(result);
}

From source file:adams.gui.visualization.debug.inspectionhandler.WekaInstances.java

License:Open Source License

/**
 * Returns further inspection values./*  w w  w  .ja va 2 s.c  o  m*/
 *
 * @param obj      the object to further inspect
 * @return      the named inspected values
 */
@Override
public Hashtable<String, Object> inspect(Object obj) {
    Hashtable<String, Object> result;
    Instances data;
    Instance inst;

    result = new Hashtable<String, Object>();

    if (obj instanceof Instances) {
        data = (Instances) obj;
        inst = null;
    } else {
        inst = (Instance) obj;
        data = inst.dataset();
    }

    result.put("relation", data.relationName());
    result.put("num attributes", data.numAttributes());
    result.put("class attribute", (data.classIndex() == -1) ? "-none-"
            : ((data.classIndex() + 1) + " (" + data.classAttribute().name() + ")"));
    if (inst == null) {
        result.put("num instances", data.numInstances());
        result.put("instances", data.toArray());
    }

    return result;
}

From source file:adams.gui.visualization.instance.InstanceExplorer.java

License:Open Source License

/**
 * pops up SQL Viewer for SQL statement.
 *//*from  ww  w.j a  va  2 s  . c  o  m*/
public void loadDataFromDatabase() {
    InstanceQuery query;
    List<InstanceContainer> data;
    Instances dataset;
    Instance inst;
    int i;

    if (m_LoadFromDatabaseDialog == null) {
        if ((getParentFrame() != null) && (getParentFrame() instanceof JFrame))
            m_LoadFromDatabaseDialog = new SqlViewerDialog((JFrame) getParentFrame());
        else
            m_LoadFromDatabaseDialog = new SqlViewerDialog(null);
    }

    m_LoadFromDatabaseDialog.setVisible(true);
    if (m_LoadFromDatabaseDialog.getReturnValue() != JOptionPane.OK_OPTION)
        return;

    try {
        showStatus("Executing query: " + m_LoadFromDatabaseDialog.getQuery());
        query = new InstanceQuery();
        query.setDatabaseURL(m_LoadFromDatabaseDialog.getURL());
        query.setUsername(m_LoadFromDatabaseDialog.getUser());
        query.setPassword(m_LoadFromDatabaseDialog.getPassword());
        query.setQuery(m_LoadFromDatabaseDialog.getQuery());
        query.setSparseData(m_LoadFromDatabaseDialog.getGenerateSparseData());
        if (query.isConnected())
            query.disconnectFromDatabase();
        query.connectToDatabase();

        showStatus("Loading data...");
        data = new ArrayList<>();
        dataset = query.retrieveInstances();
        for (i = 0; i < dataset.numInstances(); i++) {
            inst = new Instance();
            inst.set(dataset.instance(i));
            inst.setID(dataset.relationName() + "." + i);
            data.add(getContainerManager().newContainer(inst));
            showStatus("Loading data " + (i + 1) + "/" + dataset.numInstances());
        }
        loadData(dataset, data);
    } catch (Exception e) {
        GUIHelper.showErrorMessage(this, "Failed to load data from database:\n" + Utils.throwableToString(e),
                "Database error");
    }

    showStatus("");
}

From source file:adams.gui.visualization.instance.InstanceExplorer.java

License:Open Source License

/**
 * pops up file dialog for loading dataset form disk.
 *
 * @param file   an optional file, use null to ignore
 *///  w ww  .ja  v  a  2  s . com
public void loadDataFromDisk(File file) {
    if (m_LoadFromDiskDialog == null) {
        if (getParentDialog() != null)
            m_LoadFromDiskDialog = new LoadDatasetDialog(getParentDialog());
        else
            m_LoadFromDiskDialog = new LoadDatasetDialog(getParentFrame());
        m_LoadFromDiskDialog.setCurrent(new File(getProperties().getPath("InitialDir", "%h")));
        m_LoadFromDiskDialog.setDefaultAttributeRange(getProperties().getPath("AttributeRange", "first-last"));
        m_LoadFromDiskDialog.setDefaultClassIndex(getProperties().getPath("ClassIndex", ""));
        m_LoadFromDiskDialog.setDefaultSortIndex(getProperties().getPath("SortIndex", ""));
        m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.NUMERIC,
                getProperties().getBoolean("IncludeNumericAttributes", true));
        m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.DATE,
                getProperties().getBoolean("IncludeDateAttributes", false));
        m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.NOMINAL,
                getProperties().getBoolean("IncludeNominalAttributes", false));
        m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.STRING,
                getProperties().getBoolean("IncludeStringAttributes", false));
        m_LoadFromDiskDialog.setDefaultIncludeAttributes(Attribute.RELATIONAL,
                getProperties().getBoolean("IncludeRelationalAttributes", false));
        m_LoadFromDiskDialog.setAcceptListener((ChangeEvent e) -> {
            int[] indices = m_LoadFromDiskDialog.getIndices();
            if (indices == null)
                return;
            if (m_RecentFilesHandler != null)
                m_RecentFilesHandler.addRecentItem(m_LoadFromDiskDialog.getCurrent());

            HashSet<Integer> attTypes = new HashSet<>();
            if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.NUMERIC))
                attTypes.add(Attribute.NUMERIC);
            if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.DATE))
                attTypes.add(Attribute.DATE);
            if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.NOMINAL))
                attTypes.add(Attribute.NOMINAL);
            if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.STRING))
                attTypes.add(Attribute.STRING);
            if (m_LoadFromDiskDialog.getIncludeAttributes(Attribute.RELATIONAL))
                attTypes.add(Attribute.RELATIONAL);

            showStatus("Loading data...");
            List<InstanceContainer> data = new ArrayList<>();
            Instances dataset = m_LoadFromDiskDialog.getDataset();
            int[] additional = m_LoadFromDiskDialog.getAdditionalAttributes();
            Range range = m_LoadFromDiskDialog.getCurrentAttributeRange();
            int id = m_LoadFromDiskDialog.getCurrentIDIndex();
            for (int i = 0; i < indices.length; i++) {
                weka.core.Instance winst = dataset.instance(indices[i]);
                Instance inst = new Instance();
                inst.set(winst, i, additional, range, attTypes);
                if (id == -1) {
                    inst.setID((indices[i] + 1) + "." + dataset.relationName());
                } else {
                    if (winst.attribute(id).isNumeric())
                        inst.setID("" + winst.value(id));
                    else
                        inst.setID(winst.stringValue(id));
                }
                data.add(getContainerManager().newContainer(inst));
                showStatus("Loading data " + (i + 1) + "/" + dataset.numInstances());
            }
            loadData(dataset, data);

            showStatus("");
        });
    }

    if (file != null)
        m_LoadFromDiskDialog.setCurrent(file);
    m_LoadFromDiskDialog.setVisible(true);
}

From source file:adams.opt.genetic.AbstractClassifierBasedGeneticAlgorithm.java

License:Open Source License

/**
 * Generates a Properties file that stores information on the setup of
 * the genetic algorithm. E.g., it backs up the original relation name.
 * The generated properties file will be used as new relation name for
 * the data. Derived classes can add additional parameters to this
 * properties file./*from  w w  w.jav a2s .  c  o  m*/
 *
 * @param data   the data to create the setup for
 * @param job      the associated job
 * @see      #PROPS_RELATION
 * @return      the generated setup
 */
protected Properties storeSetup(Instances data, GeneticAlgorithmJob job) {
    Properties result;

    result = new Properties();

    // relation name
    result.setProperty(PROPS_RELATION, data.relationName());

    // filter (default is empty)
    result.setProperty(PROPS_FILTER, "");

    return result;
}

From source file:adams.opt.optimise.genetic.AbstractGeneticAlgorithm.java

License:Open Source License

/**
 * Generates a Properties file that stores information on the setup of
 * the genetic algorithm. E.g., it backs up the original relation name.
 * The generated properties file will be used as new relation name for
 * the data. Derived classes can add additional parameters to this
 * properties file.//from w  ww . j  a  v  a2  s. c  om
 *
 * @param data   the data to create the setup for
 * @param job      the associated job
 * @see      #PROPS_RELATION
 * @return      the generated setup
 */
protected Properties storeSetup(Instances data) {
    Properties result;

    result = new Properties();

    // relation name
    result.setProperty(PROPS_RELATION, data.relationName());

    // filter (default is empty)
    result.setProperty(PROPS_FILTER, "");

    return result;
}

From source file:asap.CrossValidation.java

/**
 *
 * @param dataInput//  www .j ava2  s . c o m
 * @param classIndex
 * @param removeIndices
 * @param cls
 * @param seed
 * @param folds
 * @param modelOutputFile
 * @return
 * @throws Exception
 */
public static String performCrossValidation(String dataInput, String classIndex, String removeIndices,
        AbstractClassifier cls, int seed, int folds, String modelOutputFile) throws Exception {

    PerformanceCounters.startTimer("cross-validation ST");

    PerformanceCounters.startTimer("cross-validation init ST");

    // loads data and set class index
    Instances data = DataSource.read(dataInput);
    String clsIndex = classIndex;

    switch (clsIndex) {
    case "first":
        data.setClassIndex(0);
        break;
    case "last":
        data.setClassIndex(data.numAttributes() - 1);
        break;
    default:
        try {
            data.setClassIndex(Integer.parseInt(clsIndex) - 1);
        } catch (NumberFormatException e) {
            data.setClassIndex(data.attribute(clsIndex).index());
        }
        break;
    }

    Remove removeFilter = new Remove();
    removeFilter.setAttributeIndices(removeIndices);
    removeFilter.setInputFormat(data);
    data = Filter.useFilter(data, removeFilter);

    // randomize data
    Random rand = new Random(seed);
    Instances randData = new Instances(data);
    randData.randomize(rand);
    if (randData.classAttribute().isNominal()) {
        randData.stratify(folds);
    }

    // perform cross-validation and add predictions
    Evaluation eval = new Evaluation(randData);
    Instances trainSets[] = new Instances[folds];
    Instances testSets[] = new Instances[folds];
    Classifier foldCls[] = new Classifier[folds];

    for (int n = 0; n < folds; n++) {
        trainSets[n] = randData.trainCV(folds, n);
        testSets[n] = randData.testCV(folds, n);
        foldCls[n] = AbstractClassifier.makeCopy(cls);
    }

    PerformanceCounters.stopTimer("cross-validation init ST");
    PerformanceCounters.startTimer("cross-validation folds+train ST");
    //paralelize!!:--------------------------------------------------------------
    for (int n = 0; n < folds; n++) {
        Instances train = trainSets[n];
        Instances test = testSets[n];

        // the above code is used by the StratifiedRemoveFolds filter, the
        // code below by the Explorer/Experimenter:
        // Instances train = randData.trainCV(folds, n, rand);
        // build and evaluate classifier
        Classifier clsCopy = foldCls[n];
        clsCopy.buildClassifier(train);
        eval.evaluateModel(clsCopy, test);
    }

    cls.buildClassifier(data);
    //until here!-----------------------------------------------------------------

    PerformanceCounters.stopTimer("cross-validation folds+train ST");
    PerformanceCounters.startTimer("cross-validation post ST");
    // output evaluation
    String out = "\n" + "=== Setup ===\n" + "Classifier: " + cls.getClass().getName() + " "
            + Utils.joinOptions(cls.getOptions()) + "\n" + "Dataset: " + data.relationName() + "\n" + "Folds: "
            + folds + "\n" + "Seed: " + seed + "\n" + "\n"
            + eval.toSummaryString("=== " + folds + "-fold Cross-validation ===", false) + "\n";

    if (!modelOutputFile.isEmpty()) {
        SerializationHelper.write(modelOutputFile, cls);
    }

    PerformanceCounters.stopTimer("cross-validation post ST");
    PerformanceCounters.stopTimer("cross-validation ST");

    return out;
}

From source file:asap.CrossValidation.java

/**
 *
 * @param dataInput//from w w  w.ja  v a  2 s. c o  m
 * @param classIndex
 * @param removeIndices
 * @param cls
 * @param seed
 * @param folds
 * @param modelOutputFile
 * @return
 * @throws Exception
 */
public static String performCrossValidationMT(String dataInput, String classIndex, String removeIndices,
        AbstractClassifier cls, int seed, int folds, String modelOutputFile) throws Exception {

    PerformanceCounters.startTimer("cross-validation MT");

    PerformanceCounters.startTimer("cross-validation init MT");

    // loads data and set class index
    Instances data = DataSource.read(dataInput);
    String clsIndex = classIndex;

    switch (clsIndex) {
    case "first":
        data.setClassIndex(0);
        break;
    case "last":
        data.setClassIndex(data.numAttributes() - 1);
        break;
    default:
        try {
            data.setClassIndex(Integer.parseInt(clsIndex) - 1);
        } catch (NumberFormatException e) {
            data.setClassIndex(data.attribute(clsIndex).index());
        }
        break;
    }

    Remove removeFilter = new Remove();
    removeFilter.setAttributeIndices(removeIndices);
    removeFilter.setInputFormat(data);
    data = Filter.useFilter(data, removeFilter);

    // randomize data
    Random rand = new Random(seed);
    Instances randData = new Instances(data);
    randData.randomize(rand);
    if (randData.classAttribute().isNominal()) {
        randData.stratify(folds);
    }

    // perform cross-validation and add predictions
    Evaluation eval = new Evaluation(randData);
    List<Thread> foldThreads = (List<Thread>) Collections.synchronizedList(new LinkedList<Thread>());

    List<FoldSet> foldSets = (List<FoldSet>) Collections.synchronizedList(new LinkedList<FoldSet>());

    for (int n = 0; n < folds; n++) {
        foldSets.add(new FoldSet(randData.trainCV(folds, n), randData.testCV(folds, n),
                AbstractClassifier.makeCopy(cls)));

        if (n < Config.getNumThreads() - 1) {
            Thread foldThread = new Thread(new CrossValidationFoldThread(n, foldSets, eval));
            foldThreads.add(foldThread);
        }
    }

    PerformanceCounters.stopTimer("cross-validation init MT");
    PerformanceCounters.startTimer("cross-validation folds+train MT");
    //paralelize!!:--------------------------------------------------------------
    if (Config.getNumThreads() > 1) {
        for (Thread foldThread : foldThreads) {
            foldThread.start();
        }
    } else {
        //use the current thread to run the cross-validation instead of using the Thread instance created here:
        new CrossValidationFoldThread(0, foldSets, eval).run();
    }

    cls.buildClassifier(data);

    for (Thread foldThread : foldThreads) {
        foldThread.join();
    }

    //until here!-----------------------------------------------------------------
    PerformanceCounters.stopTimer("cross-validation folds+train MT");
    PerformanceCounters.startTimer("cross-validation post MT");
    // evaluation for output:
    String out = "\n" + "=== Setup ===\n" + "Classifier: " + cls.getClass().getName() + " "
            + Utils.joinOptions(cls.getOptions()) + "\n" + "Dataset: " + data.relationName() + "\n" + "Folds: "
            + folds + "\n" + "Seed: " + seed + "\n" + "\n"
            + eval.toSummaryString("=== " + folds + "-fold Cross-validation ===", false) + "\n";

    if (!modelOutputFile.isEmpty()) {
        SerializationHelper.write(modelOutputFile, cls);
    }

    PerformanceCounters.stopTimer("cross-validation post MT");
    PerformanceCounters.stopTimer("cross-validation MT");
    return out;
}

From source file:asap.CrossValidation.java

static String performCrossValidationMT(Instances data, AbstractClassifier cls, int seed, int folds,
        String modelOutputFile) {

    PerformanceCounters.startTimer("cross-validation MT");

    PerformanceCounters.startTimer("cross-validation init MT");

    // randomize data
    Random rand = new Random(seed);
    Instances randData = new Instances(data);
    randData.randomize(rand);/*  w ww  .j a v a  2  s.c  o  m*/
    if (randData.classAttribute().isNominal()) {
        randData.stratify(folds);
    }

    // perform cross-validation and add predictions
    Evaluation eval;
    try {
        eval = new Evaluation(randData);
    } catch (Exception ex) {
        Logger.getLogger(CrossValidation.class.getName()).log(Level.SEVERE, null, ex);
        return "Error creating evaluation instance for given data!";
    }
    List<Thread> foldThreads = (List<Thread>) Collections.synchronizedList(new LinkedList<Thread>());

    List<FoldSet> foldSets = (List<FoldSet>) Collections.synchronizedList(new LinkedList<FoldSet>());

    for (int n = 0; n < folds; n++) {
        try {
            foldSets.add(new FoldSet(randData.trainCV(folds, n), randData.testCV(folds, n),
                    AbstractClassifier.makeCopy(cls)));
        } catch (Exception ex) {
            Logger.getLogger(CrossValidation.class.getName()).log(Level.SEVERE, null, ex);
        }

        //TODO: use Config.getNumThreads() for limiting these::
        if (n < Config.getNumThreads() - 1) {
            Thread foldThread = new Thread(new CrossValidationFoldThread(n, foldSets, eval));
            foldThreads.add(foldThread);
        }
    }

    PerformanceCounters.stopTimer("cross-validation init MT");
    PerformanceCounters.startTimer("cross-validation folds+train MT");
    //paralelize!!:--------------------------------------------------------------
    if (Config.getNumThreads() > 1) {
        for (Thread foldThread : foldThreads) {
            foldThread.start();
        }
    } else {
        new CrossValidationFoldThread(0, foldSets, eval).run();
    }

    try {
        cls.buildClassifier(data);
    } catch (Exception ex) {
        Logger.getLogger(CrossValidation.class.getName()).log(Level.SEVERE, null, ex);
    }

    for (Thread foldThread : foldThreads) {
        try {
            foldThread.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(CrossValidation.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //until here!-----------------------------------------------------------------
    PerformanceCounters.stopTimer("cross-validation folds+train MT");
    PerformanceCounters.startTimer("cross-validation post MT");
    // evaluation for output:
    String out = "\n" + "=== Setup ===\n" + "Classifier: " + cls.getClass().getName() + " "
            + Utils.joinOptions(cls.getOptions()) + "\n" + "Dataset: " + data.relationName() + "\n" + "Folds: "
            + folds + "\n" + "Seed: " + seed + "\n" + "\n"
            + eval.toSummaryString("=== " + folds + "-fold Cross-validation ===", false) + "\n";

    if (modelOutputFile != null) {
        if (!modelOutputFile.isEmpty()) {
            try {
                SerializationHelper.write(modelOutputFile, cls);
            } catch (Exception ex) {
                Logger.getLogger(CrossValidation.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    PerformanceCounters.stopTimer("cross-validation post MT");
    PerformanceCounters.stopTimer("cross-validation MT");
    return out;
}