/*
 *   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 3 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, see .
 */

/*
 *    ChangeDateFormat.java
 *    Copyright (C) 2004-2012 University of Waikato, Hamilton, New Zealand
 *
 */

package weka.filters.unsupervised.attribute;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;

import weka.core.*;
import weka.core.Capabilities.Capability;
import weka.filters.Filter;
import weka.filters.StreamableFilter;
import weka.filters.UnsupervisedFilter;

/**
 *  Changes the date format used by a date attribute.
 * This is most useful for converting to a format with less precision, for
 * example, from an absolute date to day of year, etc. This changes the format
 * string, and changes the date values to those that would be parsed by the new
 * format.
 * 

* * * Valid options are: *

* *

 * -C <col>
 *  Sets the attribute index (default last).
 * 
* *
 * -F <value index>
 *  Sets the output date format string (default corresponds to ISO-8601).
 * 
* * * * @author Len Trigg * @version $Revision: 14508 $ */ public class ChangeDateFormat extends Filter implements UnsupervisedFilter, StreamableFilter, OptionHandler, WeightedInstancesHandler, WeightedAttributesHandler { /** for serialization */ static final long serialVersionUID = -1609344074013448737L; /** The default output date format. Corresponds to ISO-8601 format. */ private static final SimpleDateFormat DEFAULT_FORMAT = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss"); /** The attribute's index setting. */ private final SingleIndex m_AttIndex = new SingleIndex("last"); /** The output date format. */ private SimpleDateFormat m_DateFormat = DEFAULT_FORMAT; /** The output attribute. */ private Attribute m_OutputAttribute; /** * Returns a string describing this filter * * @return a description of the filter suitable for displaying in the * explorer/experimenter gui */ public String globalInfo() { return "Changes the date format used by a date attribute. This is most " + "useful for converting to a format with less precision, for example, " + "from an absolute date to day of year, etc. This changes the format " + "string, and changes the date values to those that would be parsed " + "by the new format."; } /** * Returns the Capabilities of this filter. * * @return the capabilities of this object * @see Capabilities */ @Override public Capabilities getCapabilities() { Capabilities result = super.getCapabilities(); result.disableAll(); // attributes result.enableAllAttributes(); result.enable(Capability.MISSING_VALUES); // class result.enableAllClasses(); result.enable(Capability.MISSING_CLASS_VALUES); result.enable(Capability.NO_CLASS); return result; } /** * Sets the format of the input instances. * * @param instanceInfo an Instances object containing the input instance * structure (any instances contained in the object are ignored - * only the structure is required). * @return true if the outputFormat may be collected immediately * @throws Exception if the input format can't be set successfully */ @Override public boolean setInputFormat(Instances instanceInfo) throws Exception { super.setInputFormat(instanceInfo); m_AttIndex.setUpper(instanceInfo.numAttributes() - 1); if (!instanceInfo.attribute(m_AttIndex.getIndex()).isDate()) { throw new UnsupportedAttributeTypeException("Chosen attribute not date."); } setOutputFormat(); return true; } /** * Input an instance for filtering. * * @param instance the input instance * @return true if the filtered instance may now be collected with output(). * @throws Exception if the input format was not set or the date format cannot * be parsed */ @Override public boolean input(Instance instance) throws Exception { if (getInputFormat() == null) { throw new IllegalStateException("No input instance format defined"); } if (m_NewBatch) { resetQueue(); m_NewBatch = false; } Instance newInstance = (Instance) instance.copy(); int index = m_AttIndex.getIndex(); if (!newInstance.isMissing(index)) { double value = instance.value(index); try { // Format and parse under the new format to force any required // loss in precision. value = m_OutputAttribute .parseDate(m_OutputAttribute.formatDate(value)); } catch (ParseException pe) { throw new RuntimeException( "Output date format couldn't parse its own output!!"); } newInstance.setValue(index, value); } push(newInstance, false); // No need to copy instance return true; } /** * Returns an enumeration describing the available options * * @return an enumeration of all the available options */ @Override public Enumeration

Ads help maintain this website.