📄 mininginputstream.java
字号:
public abstract MiningVector read() throws MiningException;
/**
* Reads MiningVector at specified row number.
*
* @param rowNumber the row number
* @return MiningVector at specified row
* @exception MiningException if an error occurs
*/
public MiningVector read( int rowNumber ) throws MiningException
{
move( rowNumber );
return read();
}
/**
* Reads the MiningAttribute value on the current cursor position
* from the input stream.
*
* @param attributeIndex the index of MiningAttribute to read
* @return the attribute value
* @exception MiningException if an error occurs
*/
public double readAttributeValue( int attributeIndex ) throws MiningException
{
return read().getValue( attributeIndex );
}
/**
* Reads the MiningAttribute value on the current cursor position
* from the input stream.
*
* @param miningAttribute the MiningAttribute to read
* @return the attribute value
* @exception MiningException if an error occurs
*/
public double readAttributeValue( MiningAttribute miningAttribute ) throws MiningException
{
return readAttributeValue( metaData.getAttributeIndex( miningAttribute ) );
}
/**
* Reads value of <code>attributeIndex</code> MiningAttribute at
* <code>rowNumber</code> input stream row.
*
* @param rowNumber the row number
* @param attributeIndex the index of MiningAttribute to read value
*
* @return value of MiningAttribute with index <code>attributeIndex</code> at
* <code>rowNumber</code> row
* @exception MiningException if an error occurs
*/
public double readAttributeValue(int rowNumber, int attributeIndex) throws MiningException
{
move(rowNumber);
return readAttributeValue(attributeIndex);
}
/**
* Reads value of <code>miningAttribute</code> at <code>rowNumber</code>
* input stream row.
*
* @param rowNumber the row number
* @param miningAttribute MiningAttribute to read
*
* @return value of <code>miningAttribute</code> at
* <code>rowNumber</code> row
* @exception MiningException if an error occurs
*/
public double readAttributeValue(int rowNumber, MiningAttribute miningAttribute) throws MiningException
{
int attributeIndex = metaData.getAttributeIndex(miningAttribute);
return readAttributeValue(rowNumber, attributeIndex);
}
/**
* Reads some number of MiningVectors from the input stream and stores them into
* the buffer array <code>b</code>. The number of MiningVectors actually read is
* returned as an integer.
*
* @param b the buffer into which the data is read
* @return the total number of MiningVectors read into the buffer, or
* <code>-1</code> is there is no more data because the end of
* the stream has been reached
* @exception MiningException if an error occurs
* @see #read( MiningVector[], int, int )
*/
public int read( MiningVector b[] ) throws MiningException
{
return read(b, 0, b.length);
}
/**
* Reads some number of MiningAttribute values from the input stream and stores them into
* the buffer array <code>b</code>. The number of values actually read is
* returned as an integer.
*
* @param attributeIndex the index of MiningAttribute to read
* @param b the buffer into which the data is read
* @return the total number of values read into the buffer, or
* <code>-1</code> is there is no more data because the end of
* the stream has been reached
* @exception MiningException if an error occurs
* @see #read( MiningVector[], int, int )
* @see java.io.InputStream#read( byte[] )
*/
public int readAttributeValue( int attributeIndex, double[] b ) throws MiningException
{
return readAttributeValue( attributeIndex, b, 0, b.length );
}
/**
* Reads some number of MiningAttribute values from the input stream and
* stores them into the buffer array <code>b</code>. The number of values
* actually read is returned as an integer.
*
* @param miningAttribute the MiningAttribute to read
* @param b the buffer into which the data is read
* @return the total number of values read into the buffer, or
* <code>-1</code> is there is no more data because the end of
* the stream has been reached
* @exception MiningException if an error occurs
* @see #read( MiningVector[], int, int )
* @see java.io.InputStream#read( byte[] )
*/
public int readAttributeValue( MiningAttribute miningAttribute, double[] b ) throws MiningException
{
return readAttributeValue( miningAttribute, b, 0, b.length );
}
/**
* Reads up to <code>len</code> MiningVectors from the input stream into
* an array of MiningVectors. An attempt is made to read as many as
* <code>len</code> MiningVectors, but a smaller number may be read, possibly
* zero. The number of MiningVectors actually read is returned as an integer.
*
* @param b the buffer into which the data is read
* @param off the start offset in array <code>b</code>
* at which the data is written
* @param len the maximum number of MiningVectors to read
* @return the total number of MiningVectors read into the buffer, or
* <code>-1</code> if there is no more data because the end of
* the stream has been reached
* @exception MiningException if an error occurs
*/
public int read( MiningVector[] b, int off, int len ) throws MiningException
{
int i = 0;
if(b == null) throw new MiningException( "Array b can't be null." );
if( ( off < 0 ) || ( off > b.length ) || ( len < 0 ) || ( ( off + len ) > b.length ) || ( ( off + len ) < 0 ) ) throw new MiningException( "Index out of bounds. Check offset and length." );
if (len == 0) return 0;
for( ; i < len ; i++ )
{
if( !next() ) break;
b[off + i] = read();
}
return i;
}
/**
* Reads up to <code>len</code> MiningAttribute values from the input stream into
* an array of MiningVectors. An attempt is made to read as many as
* <code>len</code> values, but a smaller number may be read, possibly
* zero. The number of MiningVectors actually read is returned as an integer.
*
* @param attributeIndex the index of MiningAttribute to read
* @param b the buffer into which the data is read
* @param off the start offset in array <code>b</code>
* at which the data is written
* @param len the maximum number of MiningVectors to read
* @return the total number of MiningVectors read into the buffer, or
* <code>-1</code> if there is no more data because the end of
* the stream has been reached
* @exception MiningException if an error occurs
*/
public int readAttributeValue( int attributeIndex, double[] b, int off, int len ) throws MiningException
{
int i = 0;
if(b == null) throw new MiningException( "Array b can't be null." );
if( ( off < 0 ) || ( off > b.length ) || ( len < 0 ) || ( ( off + len ) > b.length ) || ( ( off + len ) < 0 ) ) throw new MiningException( "Index out of bounds. Check offset and length." );
if (len == 0) return 0;
for( ; i < len ; i++ )
{
if( !next() ) break;
b[off + i] = readAttributeValue( attributeIndex );
}
return i;
}
/**
* Reads up to <code>len</code> MiningAttribute values from the input stream
* into an array of MiningVectors. An attempt is made to read as many as
* <code>len</code> values, but a smaller number may be read, possibly
* zero. The number of MiningVectors actually read is returned as an integer.
*
* @param miningAttribute the MiningAttribute to read
* @param b the buffer into which the data is read
* @param off the start offset in array <code>b</code>
* at which the data is written
* @param len the maximum number of MiningVectors to read
* @return the total number of MiningVectors read into the buffer, or
* <code>-1</code> if there is no more data because the end of
* the stream has been reached
* @exception MiningException if an error occurs
*/
public int readAttributeValue( MiningAttribute miningAttribute, double[] b, int off, int len ) throws MiningException
{
return readAttributeValue( metaData.getAttributeIndex( miningAttribute ), b, off, len );
}
// -----------------------------------------------------------------------
// Methods of writing into the stream
// -----------------------------------------------------------------------
/**
* Sets new meta data to this stream.
*
* @param metaData new meta data of stream
* @exception MiningException if an error occurs
*/
public void updateSetMetaData(MiningDataSpecification metaData) throws MiningException {
throw new MiningException("not supported");
}
/**
* Removes all mining vectors from this stream. Note that metadata is not
* affected by this operation since it is fixed for any stream.
*
* @exception MiningException if an error occurs
*/
public void updateRemoveAllVectors() throws MiningException {
throw new MiningException("not supported");
}
/**
* Appends new mining vector to this stream.
*
* @param vector new mining vector to append
* @exception MiningException if an error occurs
*/
public void updateAppendVector(MiningVector vector) throws MiningException {
throw new MiningException("not supported");
}
// -----------------------------------------------------------------------
// Other methods
// -----------------------------------------------------------------------
/**
* Creates mining input stream from data file of text or ARFF format.
*
* @param dataFileName name of data file to read
* @param metaData meta data of this file
* @return mining input stream created from ths data file
* @exception MiningException could not create mining input stream
*/
public static MiningInputStream getMiningInputStream( String dataFileName, MiningDataSpecification metaData ) throws MiningException
{
MiningInputStream input = null;
if( metaData == null )
{
throw new MiningException( "Init metaData before file reading" );
}
int i = dataFileName.lastIndexOf( '.' );
if( i != -1 )
{
String extension = dataFileName.substring( i + 1 );
if( extension.equalsIgnoreCase( "arff" ) )
{
input = new MiningArffStream( dataFileName, metaData );
}
else if( extension.equalsIgnoreCase("txt") )
{
input = new TransactionStream( dataFileName );
}
else
{
input = new MiningCsvStream( dataFileName, metaData );
}
}
return input;
}
/**
* Representation of mining input stream as string.
* Attention: changes cursor position.
*
* @return representation of mining input stream as string
*/
public String toString() {
// Meta data:
String description = "";
try {
description = description + getMetaData() + "\n";
}
catch (MiningException ex) {
description = description + "no metadata" + "\n";
};
// Data:
description = description + "data" + "\n";
// Try to reset stream:
try {
reset();
}
catch (MiningException ex) {
description = description + "Warning: can't reset cursor. " +
"Start reading at current position" + "\n";
};
int i = 0;
// Read data:
try {
while( next() )
description = description + String.valueOf(i++) + ": " + read() + "\n";
}
catch (Exception ex) {
description = description + "Error: can't read vector " + i;
};
return description;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -