Skip to content

Commit

Permalink
Language migration aids
Browse files Browse the repository at this point in the history
  • Loading branch information
rzo1 committed Oct 23, 2023
1 parent cf0ff35 commit b79bd41
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 67 deletions.
22 changes: 8 additions & 14 deletions zlibsvm-api/src/main/java/de/hhn/mi/configuration/KernelType.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,14 @@ public int getNumericType() {
* @throws ClassificationCoreException if there is no kernel for the given argument.
*/
public static KernelType getByValue(int kernelType) {
switch (kernelType) {
case 0:
return LINEAR;
case 1:
return POLYNOMIAL;
case 2:
return RBF;
case 3:
return SIGMOID;
case 4:
return PRECOMPUTED;
default:
throw new ClassificationCoreException("unknown kernel type");
}
return switch (kernelType) {
case 0 -> LINEAR;
case 1 -> POLYNOMIAL;
case 2 -> RBF;
case 3 -> SIGMOID;
case 4 -> PRECOMPUTED;
default -> throw new ClassificationCoreException("unknown kernel type");
};

}

Expand Down
22 changes: 8 additions & 14 deletions zlibsvm-api/src/main/java/de/hhn/mi/configuration/SvmType.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,14 @@ public int getNumericType() {
* @throws ClassificationCoreException if there is no kernel for the given argument.
*/
public static SvmType getByValue(int svmType) {
switch (svmType) {
case 0:
return C_SVC;
case 1:
return NU_SVC;
case 2:
return ONE_CLASS;
case 3:
return EPSILON_SVR;
case 4:
return NU_SVR;
default:
throw new ClassificationCoreException("unknown svm type");
}
return switch (svmType) {
case 0 -> C_SVC;
case 1 -> NU_SVC;
case 2 -> ONE_CLASS;
case 3 -> EPSILON_SVR;
case 4 -> NU_SVR;
default -> throw new ClassificationCoreException("unknown svm type");
};

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Licensed 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.
Expand All @@ -31,14 +31,7 @@
/**
*
*/
public class NativeSvmModelWrapper {

protected final svm_model svmModel;

public NativeSvmModelWrapper(svm_model svmModel) {
this.svmModel = svmModel;
}

public record NativeSvmModelWrapper(svm_model svmModel) {

public NativeSvmModelWrapper(SvmConfigurationBuilder svmConfigurationBuilder, int probabilityEstimates,
List<Double> rhoConstants, List<Integer>
Expand All @@ -56,32 +49,30 @@ public NativeSvmModelWrapper(SvmConfigurationBuilder svmConfigurationBuilder, in
Map<Integer, List<Double>> svCoefficients, Map<Integer, List<SvmFeature>>
supportVectors) {

final svm_model nativeSvmModel = new svm_model();
this(new svm_model());

nativeSvmModel.l = (amountOfSupportVectors);
nativeSvmModel.nr_class = (numberOfClasses);
svmModel.l = (amountOfSupportVectors);
svmModel.nr_class = (numberOfClasses);

nativeSvmModel.rho = (ArrayUtils.toPrimitive(rhoConstants.toArray(new Double[0])));
nativeSvmModel.probA = (ArrayUtils.toPrimitive(probabilityA.toArray(new Double[0])));
nativeSvmModel.probB = (ArrayUtils.toPrimitive(probabilityB.toArray(new Double[0])));
nativeSvmModel.label = ((ArrayUtils.toPrimitive(labelForEachClass.toArray(new Integer[0]))));
nativeSvmModel.nSV = (ArrayUtils.toPrimitive(numberOfSVforEachClass.toArray(new Integer[0])));
svmModel.rho = (ArrayUtils.toPrimitive(rhoConstants.toArray(new Double[0])));
svmModel.probA = (ArrayUtils.toPrimitive(probabilityA.toArray(new Double[0])));
svmModel.probB = (ArrayUtils.toPrimitive(probabilityB.toArray(new Double[0])));
svmModel.label = ((ArrayUtils.toPrimitive(labelForEachClass.toArray(new Integer[0]))));
svmModel.nSV = (ArrayUtils.toPrimitive(numberOfSVforEachClass.toArray(new Integer[0])));

if (svCoefficients != null) {
nativeSvmModel.sv_coef = (PrimitiveHelper.doubleMapTo2dArray(svCoefficients));
svmModel.sv_coef = (PrimitiveHelper.doubleMapTo2dArray(svCoefficients));
}

if (supportVectors != null) {
nativeSvmModel.SV = (PrimitiveHelper.svmFeatureMapTo2dArray(supportVectors));
svmModel.SV = (PrimitiveHelper.svmFeatureMapTo2dArray(supportVectors));
}

nativeSvmModel.param = AbstractSvmTool.unwrap((svmConfigurationBuilder.setProbability(probabilityEstimates == 1).build()));

this.svmModel = nativeSvmModel;
svmModel.param = AbstractSvmTool.unwrap((svmConfigurationBuilder.setProbability(probabilityEstimates == 1).build()));
}

public svm_model getSvmModel() {
return svmModel;
return svmModel();
}

}
24 changes: 12 additions & 12 deletions zlibsvm-core/src/main/java/de/hhn/mi/util/PrimitiveHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
public class PrimitiveHelper {

public static svm_node[][] svmFeatureMapTo2dArray(Map<Integer, List<SvmFeature>> svmFeatureMap) {
Set<Integer> set = svmFeatureMap.keySet();
svm_node[][] feature2dArray = new svm_node[set.size()][];
final Set<Integer> set = svmFeatureMap.keySet();
final svm_node[][] feature2dArray = new svm_node[set.size()][];
for (Integer i : set) {
List<SvmFeature> features = svmFeatureMap.get(i);
final List<SvmFeature> features = svmFeatureMap.get(i);
feature2dArray[i] = new svm_node[svmFeatureMap.get(i).size()];
for (int j = 0; j < features.size(); j++) {
svm_node f = new svm_node();
final svm_node f = new svm_node();
f.index = features.get(j).getIndex();
f.value = features.get(j).getValue();
feature2dArray[i][j] = f;
Expand All @@ -47,13 +47,13 @@ public static svm_node[][] svmFeatureMapTo2dArray(Map<Integer, List<SvmFeature>>
}

public static Map<Integer, List<SvmFeature>> svmFeature2dArrayToMap(svm_node[][] svmFeatures) {
Map<Integer, List<SvmFeature>> map = new HashMap<>();
final Map<Integer, List<SvmFeature>> map = new HashMap<>();
for (int i = 0; i < svmFeatures.length; i++) {

List<SvmFeature> feature = new ArrayList<>();
final List<SvmFeature> feature = new ArrayList<>();

for (int j = 0; j < svmFeatures[i].length; j++) {
SvmFeature f = new SvmFeatureImpl(svmFeatures[i][j].index, svmFeatures[i][j].value);
final SvmFeature f = new SvmFeatureImpl(svmFeatures[i][j].index, svmFeatures[i][j].value);
feature.add(f);
}
map.put(i, feature);
Expand All @@ -62,11 +62,11 @@ public static Map<Integer, List<SvmFeature>> svmFeature2dArrayToMap(svm_node[][]
}

public static double[][] doubleMapTo2dArray(Map<Integer, List<Double>> doubleMap) {
Set<Integer> set = doubleMap.keySet();
double[][] double2dArray = new double[set.size()][];
final Set<Integer> set = doubleMap.keySet();
final double[][] double2dArray = new double[set.size()][];
for (Integer i : set) {
double2dArray[i] = new double[doubleMap.get(i).size()];
List<Double> features = doubleMap.get(i);
final List<Double> features = doubleMap.get(i);
for (int j = 0; j < features.size(); j++) {
double2dArray[i][j] = features.get(j);
}
Expand All @@ -75,9 +75,9 @@ public static double[][] doubleMapTo2dArray(Map<Integer, List<Double>> doubleMap
}

public static Map<Integer, List<Double>> double2dArrayToMap(double[][] doubleArray) {
Map<Integer, List<Double>> map = new HashMap<>();
final Map<Integer, List<Double>> map = new HashMap<>();
for (int i = 0; i < doubleArray.length; i++) {
List<Double> doubleList = new ArrayList<>();
final List<Double> doubleList = new ArrayList<>();
for (int j = 0; j < doubleArray[i].length; j++) {
doubleList.add(doubleArray[i][j]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public static void main(String[] argv) throws IOException {
}

private static double atof(String s) {
double d = Double.valueOf(s).doubleValue();
double d = Double.parseDouble(s);
if (Double.isNaN(d) || Double.isInfinite(d)) {
System.err.print("NaN or Infinity in input\n");
System.exit(1);
Expand Down Expand Up @@ -297,8 +297,8 @@ private void parse_command_line(String[] argv) {
private void read_problem() throws IOException {
BufferedReader fp = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream
(input_file_name), StandardCharsets.UTF_8));
Vector<Double> vy = new Vector<Double>();
Vector<svm_node[]> vx = new Vector<svm_node[]>();
Vector<Double> vy = new Vector<>();
Vector<svm_node[]> vx = new Vector<>();
int max_index = 0;
//skip first line because of meta information
fp.readLine();
Expand Down

0 comments on commit b79bd41

Please sign in to comment.