<commit_before>package etomica.graphics;
import java.io.Serializable;

import ptolemy.plot.Plot;
import etomica.EtomicaElement;
import etomica.EtomicaInfo;
import etomica.data.Data;
import etomica.data.DataInfo;
import etomica.data.DataProcessor;
import etomica.data.DataSet;
import etomica.data.DataSetListener;
import etomica.data.DataSet.DataCasterJudge;
import etomica.data.types.CastArrayToGroup;
import etomica.data.types.DataArray;
import etomica.data.types.DataFunction;
import etomica.data.types.DataGroup;
import etomica.units.Dimension;
import etomica.units.Null;
import etomica.units.Unit;
import etomica.units.systems.UnitSystem;
import etomica.util.Arrays;

/**
 * Class for creating a plot of simulation data.  Data for plot is taken
 * from a DataSinkTable instance, which can be obtained via the getDataTable
 * method.  Data sets are added to the plot by piping the data to a new DataSink
 * obtained from the makeColumn method of the data table.  
 *
 * @author <NAME>
 */

public class DisplayPlot extends Display implements DataSetListener, EtomicaElement {
    
    /**
     * Creates a plot with a new, empty, DataSinkTable.
     */
    public DisplayPlot() {
        this(new DataSet(new DataCasterJudgeFunction()));
    }
    
    /**
     * Creates a plot using data from the given table.
     */
    public DisplayPlot(DataSet dataSet) {
        super();
        this.dataSet = dataSet;
        if (!(dataSet.getDataCasterJudge() instanceof DataCasterJudgeFunction)) {
            // consider just calling the default constructor if you're hitting this exception
            throw new IllegalArgumentException("DisplayPlot a DataSet with a DataCasterJudgeFunction");
        }
        dataSet.addDataListener(this);
        plot = new Plot();
        panel.add(plot);
        setName("Data Plot");
        units = new Unit[0];
    }
    
    public static EtomicaInfo getEtomicaInfo() {
        EtomicaInfo info = new EtomicaInfo("X-Y graphical plot of data");
        return info;
    }
    
    /**
     * Returns the DataSinkTable instance that specifies the plot data.
     * Data sets are added to the plot by piping them to a new data sink
     * given by the makeColumn method of the table.
     * @return
     */
    public DataSet getDataSet() {
        return dataSet;
    }
    
    /**
     * Causes the display of the plot to be updated.
     */
    public void dataChanged(DataSet dataSet) {
        doUpdate();
    }
    
    /**
     * Updates the units array for the new column, using the default units.
     */
    public void dataCountChanged(DataSet dataSet) {
        int oldColumnCount = units.length;
        int newDataCount = dataSet.getDataCount();
        if(newDataCount > oldColumnCount) {
            units = (Unit[])Arrays.resizeArray(units, newDataCount);
            Dimension xDimension = null;
            if (units.length > 0) {
                xDimension = ((DataFunction)dataSet.getData(0)).getXData(0).getDataInfo().getDimension();
                if (xUnit == null) {
                    xUnit = xDimension.getUnit(UnitSystem.SIM);
                }
            }
            for(int i=oldColumnCount; i<units.length; i++) {
                DataFunction iData = (DataFunction)dataSet.getData(i);
                if (iData.getXDimension() != 1 || !iData.getXData(0).getDataInfo().getDimension().equals(xDimension)) {
                    throw new IllegalArgumentException("All data functions must have the same X dimension");
                }
                units[i] = (defaultUnit != null) ? defaultUnit : iData.getYData().getDataInfo().getDimension().getUnit(UnitSystem.SIM);
            }
        } else {
            //TODO have DisplayTable adjust appropriately to removal of columns; this works only for newColumnCount = 0
            //may need to remember columns and reassign units to match
            units = (Unit[])Arrays.resizeArray(units, newDataCount);<commit_msg>take X axis label from the X data<commit_after>package etomica.graphics;
import java.io.Serializable;

import ptolemy.plot.Plot;
import etomica.EtomicaElement;
import etomica.EtomicaInfo;
import etomica.data.Data;
import etomica.data.DataInfo;
import etomica.data.DataProcessor;
import etomica.data.DataSet;
import etomica.data.DataSetListener;
import etomica.data.DataSet.DataCasterJudge;
import etomica.data.types.CastArrayToGroup;
import etomica.data.types.DataArray;
import etomica.data.types.DataFunction;
import etomica.data.types.DataGroup;
import etomica.units.Dimension;
import etomica.units.Null;
import etomica.units.Unit;
import etomica.units.systems.UnitSystem;
import etomica.util.Arrays;

/**
 * Class for creating a plot of simulation data.  Data for plot is taken
 * from a DataSinkTable instance, which can be obtained via the getDataTable
 * method.  Data sets are added to the plot by piping the data to a new DataSink
 * obtained from the makeColumn method of the data table.  
 *
 * @author <NAME>
 */

public class DisplayPlot extends Display implements DataSetListener, EtomicaElement {
    
    /**
     * Creates a plot with a new, empty, DataSinkTable.
     */
    public DisplayPlot() {
        this(new DataSet(new DataCasterJudgeFunction()));
    }
    
    /**
     * Creates a plot using data from the given table.
     */
    public DisplayPlot(DataSet dataSet) {
        super();
        this.dataSet = dataSet;
        if (!(dataSet.getDataCasterJudge() instanceof DataCasterJudgeFunction)) {
            // consider just calling the default constructor if you're hitting this exception
            throw new IllegalArgumentException("DisplayPlot a DataSet with a DataCasterJudgeFunction");
        }
        dataSet.addDataListener(this);
        plot = new Plot();
        panel.add(plot);
        setName("Data Plot");
        units = new Unit[0];
    }
    
    public static EtomicaInfo getEtomicaInfo() {
        EtomicaInfo info = new EtomicaInfo("X-Y graphical plot of data");
        return info;
    }
    
    /**
     * Returns the DataSinkTable instance that specifies the plot data.
     * Data sets are added to the plot by piping them to a new data sink
     * given by the makeColumn method of the table.
     * @return
     */
    public DataSet getDataSet() {
        return dataSet;
    }
    
    /**
     * Causes the display of the plot to be updated.
     */
    public void dataChanged(DataSet dataSet) {
        doUpdate();
    }
    
    /**
     * Updates the units array for the new column, using the default units.
     */
    public void dataCountChanged(DataSet dataSet) {
        int oldColumnCount = units.length;
        int newDataCount = dataSet.getDataCount();
        if(newDataCount > oldColumnCount) {
            String xLabel = "";
            units = (Unit[])Arrays.resizeArray(units, newDataCount);
            Dimension xDimension = null;
            if (units.length > 0) {
                DataInfo xDataInfo = ((DataFunction)dataSet.getData(0)).getXData(0).getDataInfo();
                xDimension = xDataInfo.getDimension();
                if (xUnit == null) {
                    xUnit = xDimension.getUnit(UnitSystem.SIM);
                }
                xLabel = xDataInfo.getLabel();
                plot.setXLabel(xLabel);
            }
            for(int i=oldColumnCount; i<units.length; i++) {
                DataFunction iData = (DataFunction)dataSet.getData(i);
                if (iData.getXDimension() != 1 || !iData.getXData(0).getDataInfo().getDimension().equals(xDimension)) {
                    throw new IllegalArgumentException("All data functions must have the same X dimension");
                }
                units[i] = (defaultUnit != null) ? defaultUnit : iData.getYData().getDataInfo().getDimension().getUnit(UnitSystem.SIM);
            }
        } else {
            //TODO have DisplayTable adjust appropriately to removal of columns; this works only for newColumnCount = 0
            //may need to remember columns and reassign units to match
            units = (Unit[])Arrays.resizeArray(units, newDataCount);
{'fraction_non_alphanumeric': 0.09192131634294198, 'fraction_numerical': 0.0017320301868118274, 'mean_word_length': 2.5612334801762113, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3202014', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2253, 'n_words': 658}