Skip to content

Commit

Permalink
added non RNG parameters for TOML parameters files
Browse files Browse the repository at this point in the history
  • Loading branch information
B0SKAMP committed Feb 29, 2024
1 parent f573334 commit b3a4c07
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import java.nio.file.Path;
import java.util.List;
import org.fairdatapipeline.distribution.Distribution;
import org.fairdatapipeline.distribution.ImmutableDistribution;
import org.fairdatapipeline.estimate.ImmutableEstimate;
import org.fairdatapipeline.file.CleanableFileChannel;
import org.fairdatapipeline.parameters.ReadComponent;
import org.fairdatapipeline.samples.ImmutableSamples;

/**
* This represents an object_component to read from (or raise issues with) An object_component
Expand Down Expand Up @@ -77,7 +80,11 @@ public Number readEstimate() {
} catch (IOException e) {
throw (new RuntimeException("readEstimate() -- IOException trying to read from file", e));
}
return data.getEstimate();
if (!(data instanceof ImmutableEstimate)) {
throw (new RuntimeException(

Check warning on line 84 in api/src/main/java/org/fairdatapipeline/api/Object_component_read.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/fairdatapipeline/api/Object_component_read.java#L84

Added line #L84 was not covered by tests
"readEstimate() -- this objComponent (" + this.component_name + ") is not an estimate"));
}
return ((ImmutableEstimate) data).getEstimate();
}

/**
Expand All @@ -93,7 +100,13 @@ public Distribution readDistribution() {
throw (new RuntimeException(
"readDistribution() -- IOException trying to read from file.", e));
}
return data.getDistribution();
if (!(data instanceof ImmutableDistribution)) {
throw (new RuntimeException(

Check warning on line 104 in api/src/main/java/org/fairdatapipeline/api/Object_component_read.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/fairdatapipeline/api/Object_component_read.java#L104

Added line #L104 was not covered by tests
"readDistribution() -- this objComponent ("
+ this.component_name
+ ") is not a distribution"));
}
return ((ImmutableDistribution) data).getDistribution();
}

/**
Expand All @@ -108,7 +121,11 @@ public List<Number> readSamples() {
} catch (IOException e) {
throw (new RuntimeException("readSamples() -- IOException trying to read from file.", e));
}
return data.getSamples();
if (!(data instanceof ImmutableSamples)) {
throw (new RuntimeException(

Check warning on line 125 in api/src/main/java/org/fairdatapipeline/api/Object_component_read.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/fairdatapipeline/api/Object_component_read.java#L125

Added line #L125 was not covered by tests
"readSamples() -- this objComponent (" + this.component_name + ") is not a samples"));
}
return ((ImmutableSamples) data).getSamples();
}

void register_me_in_registry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.apache.commons.math3.distribution.RealDistribution;
import org.apache.commons.math3.distribution.UniformRealDistribution;
import org.apache.commons.math3.random.EmpiricalDistribution;
import org.fairdatapipeline.parameters.Component;
import org.fairdatapipeline.parameters.RngComponent;
import org.immutables.value.Value.Auxiliary;
import org.immutables.value.Value.Check;
import org.immutables.value.Value.Immutable;
Expand All @@ -33,7 +33,7 @@
TODO support other distributions:
https://github.com/ScottishCovidResponse/SCRCIssueTracking/issues/671
*/
public interface Distribution extends Component {
public interface Distribution extends RngComponent {
enum DistributionType {
gamma(),
exponential(),
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/org/fairdatapipeline/estimate/Estimate.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.util.List;
import org.fairdatapipeline.distribution.Distribution;
import org.fairdatapipeline.parameters.Component;
import org.fairdatapipeline.parameters.RngComponent;
import org.immutables.value.Value.Immutable;

/** a component to store a plain simple single Number */
@Immutable
@JsonDeserialize
@JsonSerialize
public interface Estimate extends Component {
public interface Estimate extends RngComponent {
/** @return Number - the value that is stored in this component */
@JsonProperty("value")
Number internalValue();
Expand Down
19 changes: 19 additions & 0 deletions api/src/main/java/org/fairdatapipeline/parameters/BoolList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.fairdatapipeline.parameters;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.util.List;
import org.immutables.value.Value.Immutable;

@Immutable
@JsonSerialize
@JsonDeserialize
public interface BoolList extends Component {
List<Boolean> bools();

@JsonIgnore
default List<Boolean> getBools() {
return bools();

Check warning on line 17 in api/src/main/java/org/fairdatapipeline/parameters/BoolList.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/fairdatapipeline/parameters/BoolList.java#L17

Added line #L17 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
package org.fairdatapipeline.parameters;

import org.apache.commons.math3.random.RandomGenerator;
import org.immutables.value.Value.Auxiliary;

public interface Component extends ReadComponent, WriteComponent {
@Auxiliary
RandomGenerator rng();
}
public interface Component extends ReadComponent, WriteComponent {}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ public ComponentsDeserializer(RandomGenerator rng) {

private static final Map<String, Class<?>> typeMapping =
Map.of(
"point-estimate", ImmutableEstimate.class,
"distribution", ImmutableDistribution.class,
"samples", ImmutableSamples.class);
"point-estimate",
ImmutableEstimate.class,
"distribution",
ImmutableDistribution.class,
"samples",
ImmutableSamples.class,
"bools",
ImmutableBoolList.class,
"strings",
ImmutableStringList.class);

@Override
public Components deserialize(JsonParser jsonParser, DeserializationContext ctxt)
Expand All @@ -42,23 +49,24 @@ public Components deserialize(JsonParser jsonParser, DeserializationContext ctxt
Streams.stream(rootNode.fields())
.map(this::deserializeSingleComponent)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));

return ImmutableComponents.builder().components(components).build();
}

private Entry<String, Component> deserializeSingleComponent(Entry<String, JsonNode> entry) {
String key = entry.getKey();
ObjectNode componentNode = entry.getValue().deepCopy();
componentNode.putPOJO("rng", new Object()); // this is a hack to force rng to populate via
// RandomGeneratorDeserializer.class
String type = componentNode.get("type").asText();
componentNode.remove("type");
Class<?> deserializeClass = typeMapping.get(type);

if (deserializeClass == null) {
throw new IllegalArgumentException(String.format("Unsupported component type %s", type));
}

if (RngComponent.class.isAssignableFrom(deserializeClass.getInterfaces()[0])) {
componentNode.putPOJO("rng", new Object());
// this is a hack to force rng to populate via
// RandomGeneratorDeserializer.class
}
ObjectMapper objectMapper = new DataPipelineMapper(rng);
Component component;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ public ComponentsSerializer(RandomGenerator rng) {

private static final Map<Class<?>, String> typeMapping =
Map.of(
ImmutableEstimate.class, "point-estimate",
ImmutableDistribution.class, "distribution",
ImmutableSamples.class, "samples");
ImmutableEstimate.class,
"point-estimate",
ImmutableDistribution.class,
"distribution",
ImmutableSamples.class,
"samples",
ImmutableStringList.class,
"strings",
ImmutableBoolList.class,
"bools");

@Override
public void serialize(Components components, JsonGenerator gen, SerializerProvider serializers)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
package org.fairdatapipeline.parameters;

import java.util.List;
import org.fairdatapipeline.distribution.Distribution;

public interface ReadComponent {
Number getEstimate();

List<Number> getSamples();

Distribution getDistribution();
}
public interface ReadComponent {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.fairdatapipeline.parameters;

import java.util.List;
import org.apache.commons.math3.random.RandomGenerator;
import org.fairdatapipeline.distribution.Distribution;
import org.immutables.value.Value;

public interface RngComponent extends Component {
@Value.Auxiliary
RandomGenerator rng();

Number getEstimate();

List<Number> getSamples();

Distribution getDistribution();
}
19 changes: 19 additions & 0 deletions api/src/main/java/org/fairdatapipeline/parameters/StringList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.fairdatapipeline.parameters;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.util.List;
import org.immutables.value.Value.Immutable;

@Immutable
@JsonSerialize
@JsonDeserialize
public interface StringList extends Component {
List<String> strings();

@JsonIgnore
default List<String> getStrings() {
return strings();

Check warning on line 17 in api/src/main/java/org/fairdatapipeline/parameters/StringList.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/fairdatapipeline/parameters/StringList.java#L17

Added line #L17 was not covered by tests
}
}
4 changes: 2 additions & 2 deletions api/src/main/java/org/fairdatapipeline/samples/Samples.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import java.util.List;
import org.fairdatapipeline.distribution.Distribution;
import org.fairdatapipeline.distribution.ImmutableDistribution;
import org.fairdatapipeline.parameters.Component;
import org.fairdatapipeline.parameters.RngComponent;
import org.immutables.value.Value.Immutable;

@Immutable
@JsonSerialize
@JsonDeserialize
public interface Samples extends Component {
public interface Samples extends RngComponent {
List<Number> samples();

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.fairdatapipeline.parameters;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class BoolListTest {
@Test
void makeBoolList() {
var boollist = ImmutableBoolList.builder().addBools(true, false, true).build();
assertThat(boollist.bools()).contains(true, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import org.apache.commons.math3.random.RandomGenerator;
import org.fairdatapipeline.distribution.Distribution.DistributionType;
import org.fairdatapipeline.distribution.ImmutableDistribution;
import org.fairdatapipeline.estimate.ImmutableEstimate;
import org.fairdatapipeline.mapper.DataPipelineMapper;
import org.fairdatapipeline.samples.ImmutableSamples;
import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand All @@ -25,17 +22,9 @@ class ComponentsDeserializerTest {
+ " \"shape\": 1,\n"
+ " \"type\": \"distribution\"\n"
+ " },\n"
+ " \"example-estimate\": {\n"
+ " \"type\": \"point-estimate\",\n"
+ " \"value\": 1.0\n"
+ " },\n"
+ " \"example-samples\": {\n"
+ " \"samples\": [\n"
+ " 1,\n"
+ " 2,\n"
+ " 3\n"
+ " ],\n"
+ " \"type\": \"samples\"\n"
+ " \"example-strings\": {\n"
+ " \"strings\": [\"bram\",\"rosalie\"],\n"
+ " \"type\": \"strings\"\n"
+ " }\n"
+ "}";

Expand All @@ -53,27 +42,28 @@ public void setUp() {
void deserialize() throws JsonProcessingException {
Components actualComponents = objectMapper.readValue(json, Components.class);

var estimate = ImmutableEstimate.builder().internalValue(1.0).rng(rng).build();
var distribution =
ImmutableDistribution.builder()
.internalType(DistributionType.gamma)
.internalShape(1)
.internalScale(2)
.rng(rng)
.build();
var samples = ImmutableSamples.builder().addSamples(1, 2, 3).rng(rng).build();
var expectedComponents =
var strings = ImmutableStringList.builder().addStrings("bram", "rosalie").build();
Components components =
ImmutableComponents.builder()
.components(
Map.of(
"example-estimate",
estimate,
"example-distribution",
distribution,
"example-samples",
samples))
.putComponents("example-distribution", distribution)
.putComponents("example-strings", strings)
.build();
/* var expectedComponents =
ImmutableComponents.builder()
.components(
Map.of(
"example-distribution",
distribution
))
.build();*/

assertThat(actualComponents).isEqualTo(expectedComponents);
assertThat(actualComponents).isEqualTo(components);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import org.apache.commons.math3.random.RandomGenerator;
import org.fairdatapipeline.distribution.Distribution.DistributionType;
import org.fairdatapipeline.distribution.ImmutableDistribution;
import org.fairdatapipeline.estimate.ImmutableEstimate;
import org.fairdatapipeline.mapper.DataPipelineMapper;
import org.fairdatapipeline.samples.ImmutableSamples;
import org.json.JSONException;
import org.junit.jupiter.api.*;

Expand All @@ -27,17 +25,9 @@ class ComponentsSerializerTest {
+ " \"shape\": 1,\n"
+ " \"type\": \"distribution\"\n"
+ " },\n"
+ " \"example-estimate\": {\n"
+ " \"type\": \"point-estimate\",\n"
+ " \"value\": 1.0\n"
+ " },\n"
+ " \"example-samples\": {\n"
+ " \"samples\": [\n"
+ " 1,\n"
+ " 2,\n"
+ " 3\n"
+ " ],\n"
+ " \"type\": \"samples\"\n"
+ " \"example-strings\": {\n"
+ " \"strings\": [\"bram\",\"rosalie\"],\n"
+ " \"type\": \"strings\"\n"
+ " }\n"
+ "}";

Expand All @@ -54,20 +44,18 @@ public void setUp() {
@Test
void serialize() throws IOException, JSONException {
var writer = new StringWriter();
var estimate = ImmutableEstimate.builder().internalValue(1.0).rng(rng).build();
var distribution =
ImmutableDistribution.builder()
.internalType(DistributionType.gamma)
.internalShape(1)
.internalScale(2)
.rng(rng)
.build();
var samples = ImmutableSamples.builder().addSamples(1, 2, 3).rng(rng).build();
var strings = ImmutableStringList.builder().addStrings("bram", "rosalie").build();
Components components =
ImmutableComponents.builder()
.putComponents("example-estimate", estimate)
.putComponents("example-distribution", distribution)
.putComponents("example-samples", samples)
.putComponents("example-strings", strings)
.build();

objectMapper.writeValue(writer, components);
Expand Down
Loading

0 comments on commit b3a4c07

Please sign in to comment.