Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* $#
* FOS R implementation
*
* Copyright (C) 2013 Feedzai SA
*
* This software is licensed under the Apache License, Version 2.0 (the "Apache License") or the GNU
* Lesser General Public License version 3 (the "GPL License"). You may choose either license to govern
* your use of this software only upon the condition that you accept all of the terms of either the Apache
* License or the LGPL License.
*
* You may obtain a copy of the Apache License and the LGPL License at:
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Unless required by applicable law or agreed to in writing, software distributed under the Apache License
* or the LGPL License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the Apache License and the LGPL License for the specific language governing
* permissions and limitations under the Apache License and the LGPL License.
* #$
*/
package com.feedzai.fos.impl.r;
import com.feedzai.fos.api.*;
import com.feedzai.fos.common.validation.NotNull;
import com.feedzai.fos.impl.r.config.RModelConfig;
import com.feedzai.fos.impl.r.rserve.FosRserve;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Implementation of the Scorer API using R as the backend
*
* @author miguel.duarte
* @since 1.0.2
*/
public class RScorer implements Scorer {
/**
* Scorer logger
*/
private static final Logger logger = LoggerFactory.getLogger(RScorer.class);
/**
* Reference to the backing RServe daemon
*/
private FosRserve rserve;
/**
* Set with all the configured models
*/
private Set uuids = new HashSet<>();
/**
* Return the scorer for a given model ID
* @param modelId UUID of the model to score
* @return the scorer
* @throws FOSException
*/
private Scorer getScorer(UUID modelId) throws FOSException {
return this;
}
/**
* Creates a RScorer instance with a backing RServe process
* @param rserve Backing rserve process
*/
public RScorer(FosRserve rserve) throws FOSException {
checkNotNull(rserve, "Manager config cannot be null");
this.rserve = rserve;
}
/**
* Create a RScorer instance loading custom libraries
*
* @param rserve Backing rserve process
* @param rlibraries Libraries that will be loaded prior to generating the scoring function
*
* @throws FOSException If unable to add the relevant libraries
*/
public RScorer(FosRserve rserve, String[] rlibraries) throws FOSException {
this(rserve);
for (String library : rlibraries) {
rserve.eval("library(" + library + ")");
}
}
@Override
public List score(List modelIds, Object[] scorables) throws FOSException {
List scores = new ArrayList<>();
for (UUID uuid : modelIds) {
StringBuilder sb = new StringBuilder();
sb.append(uuid2environment(uuid))
.append("$score(c(");
for (int i = 0; i != scorables.length - 1; ++i) {
appendValue(scorables[i], sb);
sb.append(',');
}
appendValue(scorables[scorables.length - 1], sb);
sb.append("))");
double[] result = rserve.eval(sb.toString());
scores.add(result);
}
return scores;
}
/**
* Generate the scoring vector in the correct format by quoting strings.
* All other values will be printed as is
* @param scorable scorable to be appended
* @param sb String buffer that contain the genreated string
*/
private void appendValue(Object scorable, StringBuilder sb) {
if (scorable instanceof String) {
sb.append('"')
.append(scorable)
.append('"');
} else {
sb.append(scorable);
}
}
@Override
public Map score(Map modelIdsToScorables) throws FOSException {
Map scoreMap = new HashMap<>();
for(UUID uuid : modelIdsToScorables.keySet()) {
scoreMap.put(uuid, score(ImmutableList.of(uuid), modelIdsToScorables.get(uuid)).get(0));
}
return scoreMap;
}
@Override
public void close() throws FOSException {
for (UUID uuid : uuids) {
removeModel(uuid);
}
}
@Override
@NotNull
public List score(UUID modelId, List