2018-07-01 22:22:12 -07:00
|
|
|
package ca.joeltherrien.randomforest;
|
|
|
|
|
2018-07-05 12:59:29 -07:00
|
|
|
import ca.joeltherrien.randomforest.covariates.Covariate;
|
2018-07-01 22:22:12 -07:00
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
2018-09-14 14:53:01 -07:00
|
|
|
import java.io.Serializable;
|
2018-07-16 16:58:11 -07:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
2018-07-01 22:22:12 -07:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
@RequiredArgsConstructor
|
2018-09-14 14:53:01 -07:00
|
|
|
public class CovariateRow implements Serializable {
|
2018-07-01 22:22:12 -07:00
|
|
|
|
2018-09-18 11:17:15 -07:00
|
|
|
private final Covariate.Value[] valueArray;
|
2018-07-01 22:22:12 -07:00
|
|
|
|
|
|
|
@Getter
|
|
|
|
private final int id;
|
|
|
|
|
2018-09-18 11:17:15 -07:00
|
|
|
public Covariate.Value<?> getCovariateValue(Covariate covariate){
|
|
|
|
return valueArray[covariate.getIndex()];
|
2018-07-01 22:22:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString(){
|
|
|
|
return "CovariateRow " + this.id;
|
|
|
|
}
|
|
|
|
|
2018-07-16 16:58:11 -07:00
|
|
|
public static CovariateRow createSimple(Map<String, String> simpleMap, List<Covariate> covariateList, int id){
|
2018-09-18 11:17:15 -07:00
|
|
|
final Covariate.Value[] valueArray = new Covariate.Value[covariateList.size()];
|
2018-07-16 16:58:11 -07:00
|
|
|
final Map<String, Covariate> covariateMap = new HashMap<>();
|
|
|
|
|
|
|
|
covariateList.forEach(covariate -> covariateMap.put(covariate.getName(), covariate));
|
|
|
|
|
|
|
|
simpleMap.forEach((name, valueStr) -> {
|
2018-09-18 11:17:15 -07:00
|
|
|
final Covariate covariate = covariateMap.get(name);
|
|
|
|
|
|
|
|
if(covariate != null){ // happens often in tests where we experiment with adding / removing covariates
|
|
|
|
valueArray[covariate.getIndex()] = covariate.createValue(valueStr);
|
2018-07-16 16:58:11 -07:00
|
|
|
}
|
2018-09-18 11:17:15 -07:00
|
|
|
|
2018-07-16 16:58:11 -07:00
|
|
|
});
|
|
|
|
|
2018-09-18 11:17:15 -07:00
|
|
|
return new CovariateRow(valueArray, id);
|
2018-07-16 16:58:11 -07:00
|
|
|
}
|
|
|
|
|
2018-07-01 22:22:12 -07:00
|
|
|
}
|