Skip to content

Commit

Permalink
Beginnings of better Parameter handling
Browse files Browse the repository at this point in the history
Added ParamHandlerSingleton class
Added ParameterGroup class
Added Parameter class interface
Added DoubleParameter class
Added IntegerParameter class
Added PTMShepherd.initParameters
  • Loading branch information
danielgeiszler committed Apr 7, 2024
1 parent e6fdf10 commit c59a658
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/edu/umich/andykong/ptmshepherd/PTMShepherd.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static java.lang.System.out;

import edu.umich.andykong.ptmshepherd.paramhandling.*;
import edu.umich.andykong.ptmshepherd.cleaner.CombinedExperimentsSummary;
import edu.umich.andykong.ptmshepherd.cleaner.CombinedTable;
import edu.umich.andykong.ptmshepherd.core.MXMLReader;
Expand Down Expand Up @@ -69,8 +70,9 @@
public class PTMShepherd {

public static final String name = "PTM-Shepherd";
public static final String version = "3.0.0-rc3";
public static final String version = "3.0.0-rc3";

static ParamHandlerSingleton paramHandler;
static HashMap<String,String> params;
static TreeMap<String,ArrayList<String []>> datasets;
static HashMap<String,HashMap<String,File>> mzMap;
Expand Down Expand Up @@ -540,7 +542,7 @@ public static void main(String [] args) throws Exception {
print("Created modification summary\n");
}

//PTMiner-style iterative localization
//Iterative localization
Boolean iterLocMode = Boolean.parseBoolean(params.get("iterloc_mode"));
if (iterLocMode) {
out.println("Beginning iterative localization");
Expand Down Expand Up @@ -1167,4 +1169,15 @@ private static GlycoParams parseGlycoParams() {
return glycoParams;
}

private static void initParameters() {
// Add system-level parameters
paramHandler.addParamGroup("system");
params.put("threads", String.valueOf(Runtime.getRuntime().availableProcessors()));
paramHandler.addParam("system", "threads", new IntegerParameter(
"threads", 1, 0, 0, 1000, Runtime.getRuntime().availableProcessors(),
"Number of threads to be used, 0 = automatic.")
);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package edu.umich.andykong.ptmshepherd.paramhandling;


public class DoubleParameter implements Parameter<Double> {
private String key;
private double value;
private double min;
private double max;

public DoubleParameter(String key, double min, double max) {
this.key = key;
this.min = min;
this.max = max;
}

@Override
public Double getValue() {
return this.value;
}

@Override
public void setValue(Double value) {
if (!isValid(value)) {
throw new IllegalArgumentException("Value out of bounds");
}
this.value = value;
}

@Override
public boolean isValid(Double value) {
return ((value >= this.min) && (value <= this.max));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package edu.umich.andykong.ptmshepherd.paramhandling;


public class IntegerParameter implements Parameter<Integer> {
private String key;
private int level;
private int order;
private int value;
private int min;
private int max;
private int defaultValue;
private String description;

public IntegerParameter(String key, int level, int order, int min, int max, int value, String description) {
this.key = key;
this.level = level;
this.order = order;
this.min = min;
this.max = max;
this.value = value;
this.defaultValue = value;
this.description = description;
}

@Override
public Integer getValue() {
return value;
}

@Override
public void setValue(Integer value) throws IllegalArgumentException {
if (!isValid(value)) {
throw new IllegalArgumentException(String.format(this.key + " received an invalid argument."));
}
this.value = value;
}

@Override
public boolean isValid(Integer value) {
return ((value >= min) && (value <= max));
}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package edu.umich.andykong.ptmshepherd.paramhandling;

import java.util.HashMap;
import java.util.Map;

public class ParamHandlerSingleton {
private static ParamHandlerSingleton instance;
private Map<String, ParameterGroup> groups;
//todo make a map here that links to parameter keys to group parameters


public static synchronized ParamHandlerSingleton getInstance() {
if (instance == null) {
instance = new ParamHandlerSingleton();
}
return instance;
}


public void addParamGroup(String key) {
try {
if (groups.containsKey(key)) {
throw new IllegalArgumentException("Parameter group already exists: " + key);
}
groups.put(key, new ParameterGroup(key));
} catch (IllegalArgumentException e) {
return;
}
}


@SuppressWarnings("unchecked")
public ParameterGroup getParamGroup(String key) {
ParameterGroup parameterGroup = groups.get(key);
if (parameterGroup == null) {
throw new IllegalArgumentException("Parameter group not found: " + key);
}
return parameterGroup;
}

public <T> void addParam(String group, String key, Parameter<T> param) {
ParameterGroup parameterGroup = groups.get(group);
parameterGroup.setParamValue(key, param);
}

public <T> void setParamValue(String group, String key, T value) {
ParameterGroup parameterGroup = groups.get(group);
parameterGroup.setParamValue(key, value);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package edu.umich.andykong.ptmshepherd.paramhandling;

public interface Parameter<T> {
T getValue();
void setValue(T value) throws IllegalArgumentException;
boolean isValid(T value);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package edu.umich.andykong.ptmshepherd.paramhandling;

import java.util.HashMap;
import java.util.Map;

public class ParameterGroup {
private String name;
private Map<String, Parameter<?>> parameters;


public ParameterGroup(String name) {
this.name = name;
this.parameters = new HashMap<>();
}

public void addParam(String key, Parameter<?> parameter) {
parameters.put(key, parameter);
}


public Parameter<?> getParam(String key) {
return parameters.get(key);
}


public <T> void setParamValue(String key, T value) {
Parameter<T> parameter = (Parameter<T>) parameters.get(key);
if (parameter == null)
throw new IllegalArgumentException("Parameter not found: " + key);
parameter.setValue(value);
}



public void printParameters() {
System.out.println("Group: " + name);
parameters.forEach((key, parameter) -> System.out.println(key + ": " + parameter.getValue()));
}
}

0 comments on commit c59a658

Please sign in to comment.