edu.cmu.tetradapp.editor.datamanip.DiscreteDiscretizationEditor Maven / Gradle / Ivy
///////////////////////////////////////////////////////////////////////////////
// For information as to what this class does, see the Javadoc, below. //
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, //
// 2007, 2008, 2009, 2010, 2014, 2015, 2022 by Peter Spirtes, Richard //
// Scheines, Joseph Ramsey, and Clark Glymour. //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
///////////////////////////////////////////////////////////////////////////////
package edu.cmu.tetradapp.editor.datamanip;
import edu.cmu.tetrad.data.DiscreteDiscretizationSpec;
import edu.cmu.tetrad.data.DiscreteVariable;
import edu.cmu.tetrad.data.DiscretizationSpec;
import edu.cmu.tetradapp.util.StringTextField;
import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Edits discretization parameters for a discrete variable.
*
* @author josephramsey
*/
class DiscreteDiscretizationEditor extends JPanel implements DiscretizationEditor {
private final DiscreteVariable variable;
private RemapEditor remapEditor;
public DiscreteDiscretizationEditor(DiscreteVariable variable) {
if (variable == null) {
throw new NullPointerException();
}
this.variable = variable;
//String name = variable.getNode();
Box b1 = Box.createVerticalBox();
b1.add(Box.createVerticalStrut(10));
Box b6 = Box.createHorizontalBox();
b6.add(Box.createRigidArea(new Dimension(10, 0)));
b6.add(new JLabel(
"Edit new categories that old categories should map to:"));
b6.add(Box.createHorizontalGlue());
b1.add(b6);
b1.add(Box.createVerticalStrut(10));
b1.add(createRemapEditor());
b1.add(Box.createVerticalGlue());
setLayout(new BorderLayout());
add(b1, BorderLayout.CENTER);
}
//===========================PUBLIC METHODS==========================//
private static int[] defaultRemap(List categories) {
int[] remap = new int[categories.size()];
for (int i = 0; i < remap.length; i++) {
remap[i] = i;
}
return remap;
}
private static List defaultCategories(DiscreteVariable variable) {
List categories = new LinkedList<>();
for (int i = 0; i < variable.getNumCategories(); i++) {
categories.add(variable.getCategory(i));
}
return categories;
}
//===========================PRIVATE METHODS=========================//
public DiscretizationSpec getDiscretizationSpec() {
return this.remapEditor.getDiscretizationSpec();
}
public void setDiscretizationSpec(DiscretizationSpec spec) {
this.remapEditor.setDiscretizationSpec((DiscreteDiscretizationSpec) spec);
}
private RemapEditor createRemapEditor() {
List categories = DiscreteDiscretizationEditor.defaultCategories(this.variable);
int[] remap = DiscreteDiscretizationEditor.defaultRemap(categories);
DiscreteDiscretizationSpec discretizationSpec =
new DiscreteDiscretizationSpec(remap, categories);
this.remapEditor = new RemapEditor(this.variable, discretizationSpec);
return this.remapEditor;
}
//================================CLASSES============================//
static final class BigLabel extends JLabel {
private static final Font FONT = new Font("Dialog", Font.BOLD, 20);
public BigLabel() {
super(" --> ");
setFont(BigLabel.FONT);
}
}
static final class RemapEditor extends JComponent {
private final DiscreteVariable variable;
private final List newCategories = new LinkedList<>();
private final Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy