All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.mahout.common.distance.WeightedDistanceMeasure Maven / Gradle / Ivy

There is a newer version: 0.13.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.mahout.common.distance;

import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.mahout.common.ClassUtils;
import org.apache.mahout.common.parameters.ClassParameter;
import org.apache.mahout.common.parameters.Parameter;
import org.apache.mahout.common.parameters.PathParameter;
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;

/** Abstract implementation of DistanceMeasure with support for weights. */
public abstract class WeightedDistanceMeasure implements DistanceMeasure {
  
  private List> parameters;
  private Parameter weightsFile;
  private ClassParameter vectorClass;
  private Vector weights;
  
  @Override
  public void createParameters(String prefix, Configuration jobConf) {
    parameters = new ArrayList<>();
    weightsFile = new PathParameter(prefix, "weightsFile", jobConf, null,
        "Path on DFS to a file containing the weights.");
    parameters.add(weightsFile);
    vectorClass = new ClassParameter(prefix, "vectorClass", jobConf, DenseVector.class,
        "Class file specified in parameter weightsFile has been serialized with.");
    parameters.add(vectorClass);
  }
  
  @Override
  public Collection> getParameters() {
    return parameters;
  }
  
  @Override
  public void configure(Configuration jobConf) {
    if (parameters == null) {
      ParameteredGeneralizations.configureParameters(this, jobConf);
    }
    try {
      if (weightsFile.get() != null) {
        FileSystem fs = FileSystem.get(weightsFile.get().toUri(), jobConf);
        VectorWritable weights =
            ClassUtils.instantiateAs((Class) vectorClass.get(), VectorWritable.class);
        if (!fs.exists(weightsFile.get())) {
          throw new FileNotFoundException(weightsFile.get().toString());
        }
        try (DataInputStream in = fs.open(weightsFile.get())){
          weights.readFields(in);
        }
        this.weights = weights.get();
      }
    } catch (IOException e) {
      throw new IllegalStateException(e);
    }
  }
  
  public Vector getWeights() {
    return weights;
  }
  
  public void setWeights(Vector weights) {
    this.weights = weights;
  }
  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy