Skip to content

Commit

Permalink
[dbs-leipzig#1571] Code cleanup and changed all result values to float
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherLausch committed Jan 16, 2024
1 parent 6c2d234 commit ea07695
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,58 @@
package org.gradoop.temporal.model.impl.operators.metric;

import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.gradoop.flink.model.api.operators.UnaryBaseGraphToValueOperator;
import org.gradoop.flink.model.impl.operators.sampling.functions.VertexDegree;
import org.gradoop.temporal.model.api.TimeDimension;
import org.gradoop.temporal.model.impl.TemporalGraph;
import org.gradoop.temporal.model.impl.operators.metric.functions.*;
import org.gradoop.temporal.model.impl.operators.metric.functions.BuildTemporalDegreeTree;
import org.gradoop.temporal.model.impl.operators.metric.functions.FlatMapVertexIdEdgeInterval;
import org.gradoop.temporal.model.impl.operators.metric.functions.GroupDegreeTreesToVariance;
import org.gradoop.temporal.model.impl.operators.metric.functions.TransformDeltaToAbsoluteDegreeTree;
import org.gradoop.temporal.model.impl.operators.metric.functions.MapDegreesToInterval;

import java.util.Objects;

/**
* Operator that calculates the degree variance evolution of a temporal graph for the
* whole lifetime of the graph.
*/
public class DegreeVarianceEvolution implements UnaryBaseGraphToValueOperator<TemporalGraph, DataSet<Tuple3<Long, Long, Double>>> {
/**
* The time dimension that will be considered.
*/
private final TimeDimension dimension;
public class DegreeVarianceEvolution implements UnaryBaseGraphToValueOperator<TemporalGraph, DataSet<Tuple3<Long, Long, Float>>> {
/**
* The time dimension that will be considered.
*/
private final TimeDimension dimension;

/**
* The degree type (IN, OUT, BOTH);
*/
private final VertexDegree degreeType;
/**
* The degree type (IN, OUT, BOTH);
*/
private final VertexDegree degreeType;

/**
* Creates an instance of this average degree evolution operator.
*
* @param degreeType the degree type to use (IN, OUT, BOTH).
* @param dimension the time dimension to use (VALID_TIME, TRANSACTION_TIME).
*/
public DegreeVarianceEvolution(VertexDegree degreeType, TimeDimension dimension) {
this.degreeType = Objects.requireNonNull(degreeType);
this.dimension = Objects.requireNonNull(dimension);
}
/**
* Creates an instance of this average degree evolution operator.
*
* @param degreeType the degree type to use (IN, OUT, BOTH).
* @param dimension the time dimension to use (VALID_TIME, TRANSACTION_TIME).
*/
public DegreeVarianceEvolution(VertexDegree degreeType, TimeDimension dimension) {
this.degreeType = Objects.requireNonNull(degreeType);
this.dimension = Objects.requireNonNull(dimension);
}

@Override
public DataSet<Tuple3<Long, Long, Double>> execute(TemporalGraph graph) {
return graph.getEdges()
// 1) Extract vertex id(s) and corresponding time intervals
.flatMap(new FlatMapVertexIdEdgeInterval(dimension, degreeType))
// 2) Group them by the vertex id
.groupBy(0)
// 3) For each vertex id, build a degree tree data structure
.reduceGroup(new BuildTemporalDegreeTree())
// 4) Transform each tree to aggregated evolution
.map(new TransformDeltaToAbsoluteDegreeTree())
// 6) Merge trees together and calculate aggregation
.reduceGroup(new GroupDegreeTreesToVariance())
.mapPartition(new MapDegreesToInterval());
}
}
@Override
public DataSet<Tuple3<Long, Long, Float>> execute(TemporalGraph graph) {
return graph.getEdges()
// 1) Extract vertex id(s) and corresponding time intervals
.flatMap(new FlatMapVertexIdEdgeInterval(dimension, degreeType))
// 2) Group them by the vertex id
.groupBy(0)
// 3) For each vertex id, build a degree tree data structure
.reduceGroup(new BuildTemporalDegreeTree())
// 4) Transform each tree to aggregated evolution
.map(new TransformDeltaToAbsoluteDegreeTree())
// 6) Merge trees together and calculate aggregation
.reduceGroup(new GroupDegreeTreesToVariance())
.mapPartition(new MapDegreesToInterval());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,75 +32,73 @@
* that represents the aggregated degree value for the whole graph at the given time.
*/
public class GroupDegreeTreesToVariance
implements GroupReduceFunction<Tuple2<GradoopId, TreeMap<Long, Integer>>, Tuple2<Long, Double>> {

/**
* Creates an instance of this group reduce function.
*
*/
public GroupDegreeTreesToVariance() {

implements GroupReduceFunction<Tuple2<GradoopId, TreeMap<Long, Integer>>, Tuple2<Long, Float>> {

/**
* Creates an instance of this group reduce function.
*
*/
public GroupDegreeTreesToVariance() {
}

@Override
public void reduce(Iterable<Tuple2<GradoopId, TreeMap<Long, Integer>>> iterable,
Collector<Tuple2<Long, Float>> collector) throws Exception {

// init necessary maps and set
HashMap<GradoopId, TreeMap<Long, Integer>> degreeTrees = new HashMap<>();
HashMap<GradoopId, Integer> vertexDegrees = new HashMap<>();
SortedSet<Long> timePoints = new TreeSet<>();

// convert the iterables to a hashmap and remember all possible timestamps
for (Tuple2<GradoopId, TreeMap<Long, Integer>> tuple : iterable) {
degreeTrees.put(tuple.f0, tuple.f1);
timePoints.addAll(tuple.f1.keySet());
}

@Override
public void reduce(Iterable<Tuple2<GradoopId, TreeMap<Long, Integer>>> iterable,
Collector<Tuple2<Long, Double>> collector) throws Exception {

// init necessary maps and set
HashMap<GradoopId, TreeMap<Long, Integer>> degreeTrees = new HashMap<>();
HashMap<GradoopId, Integer> vertexDegrees = new HashMap<>();
SortedSet<Long> timePoints = new TreeSet<>();

// convert the iterables to a hashmap and remember all possible timestamps
for (Tuple2<GradoopId, TreeMap<Long, Integer>> tuple : iterable) {
degreeTrees.put(tuple.f0, tuple.f1);
timePoints.addAll(tuple.f1.keySet());
int numberOfVertices = degreeTrees.size();

// Add default times
timePoints.add(Long.MIN_VALUE);

for (Long timePoint : timePoints) {
// skip last default time
/* if (Long.MAX_VALUE == timePoint) {
continue;
}*/
// Iterate over all vertices
for (Map.Entry<GradoopId, TreeMap<Long, Integer>> entry : degreeTrees.entrySet()) {
// Make sure the vertex is registered in the current vertexDegrees capture
if (!vertexDegrees.containsKey(entry.getKey())) {
vertexDegrees.put(entry.getKey(), 0);
}
// Check if timestamp is in tree, if not, take the lower key
if (entry.getValue().containsKey(timePoint)) {
vertexDegrees.put(entry.getKey(), entry.getValue().get(timePoint));
} else {
Long lowerKey = entry.getValue().lowerKey(timePoint);
if (lowerKey != null) {
vertexDegrees.put(entry.getKey(), entry.getValue().get(lowerKey));
}
}
}

int numberOfVertices = degreeTrees.size();

// Add default times
timePoints.add(Long.MIN_VALUE);

for (Long timePoint : timePoints) {
// skip last default time
/* if (Long.MAX_VALUE == timePoint) {
continue;
}*/
// Iterate over all vertices
for (Map.Entry<GradoopId, TreeMap<Long, Integer>> entry : degreeTrees.entrySet()) {
// Make sure the vertex is registered in the current vertexDegrees capture
if (!vertexDegrees.containsKey(entry.getKey())) {
vertexDegrees.put(entry.getKey(), 0);
}

// Check if timestamp is in tree, if not, take the lower key
if (entry.getValue().containsKey(timePoint)) {
vertexDegrees.put(entry.getKey(), entry.getValue().get(timePoint));
} else {
Long lowerKey = entry.getValue().lowerKey(timePoint);
if (lowerKey != null) {
vertexDegrees.put(entry.getKey(), entry.getValue().get(lowerKey));
}
}
}

//sum of the degrees of all vertices
Optional<Integer> opt = vertexDegrees.values().stream().reduce(Math::addExact);
Optional<Double> opt2 = Optional.empty();
//sum of the degrees of all vertices
Optional<Integer> opt = vertexDegrees.values().stream().reduce(Math::addExact);
Optional<Double> opt2 = Optional.empty();

double mean;
double mean;

//if we have a sum at the current timestamp
if (opt.isPresent()) {
//calculate the avg degree of the graph at the current timestamp
mean = (double) opt.get() / (double) numberOfVertices;
opt2 = Optional.of(vertexDegrees.values().stream()
.mapToDouble(val -> (val - mean) * (val - mean)).sum());
}
//if we have a sum at the current timestamp
if (opt.isPresent()) {
//calculate the avg degree of the graph at the current timestamp
mean = (double) opt.get() / (double) numberOfVertices;
opt2 = Optional.of(vertexDegrees.values().stream()
.mapToDouble(val -> (val - mean) * (val - mean)).sum());
}

opt2.ifPresent(val -> collector.collect(
new Tuple2<>(timePoint, val / (double) numberOfVertices)));
}
opt2.ifPresent(val -> collector.collect(
new Tuple2<>(timePoint, (float) (val / numberOfVertices))));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,42 @@
*
*/

public class MapDegreesToInterval implements MapPartitionFunction<Tuple2<Long, Double>, Tuple3<Long, Long, Double>> {
@Override
public void mapPartition(Iterable<Tuple2<Long, Double>> values, Collector<Tuple3<Long, Long, Double>> out) {
public class MapDegreesToInterval implements MapPartitionFunction<Tuple2<Long, Float>, Tuple3<Long, Long, Float>> {
@Override
public void mapPartition(Iterable<Tuple2<Long, Float>> values, Collector<Tuple3<Long, Long, Float>> out) {

//set starting values to null
Long startTimestamp = null;
Long endTimestamp = null;
Double value = null;
Boolean collected = false;
//set starting values to null
Long startTimestamp = null;
Long endTimestamp = null;
Float value = null;
Boolean collected = false;

//loop through each tuple
for (Tuple2<Long, Double> tuple : values) {
if (startTimestamp == null) {
// First element in the group
startTimestamp = tuple.f0;
endTimestamp = tuple.f0;
value = tuple.f1;
} else {
if (!tuple.f1.equals(value)) {
// Value changed, emit the current interval and start a new one
out.collect(new Tuple3<>(startTimestamp, tuple.f0, value));
startTimestamp = tuple.f0;
endTimestamp = tuple.f0;
value = tuple.f1;
collected = true;
} else {
// Extend the current interval
endTimestamp = tuple.f0;
collected = false;
}
}
}
//check if the latest interval was collected, if not, collect it
//this happens when the last interval has the value 0
if (!collected) {
out.collect(new Tuple3<>(startTimestamp, endTimestamp, value));
//loop through each tuple
for (Tuple2<Long, Float> tuple : values) {
if (startTimestamp == null) {
// First element in the group
startTimestamp = tuple.f0;
endTimestamp = tuple.f0;
value = tuple.f1;
} else {
if (!tuple.f1.equals(value)) {
// Value changed, emit the current interval and start a new one
out.collect(new Tuple3<>(startTimestamp, tuple.f0, value));
startTimestamp = tuple.f0;
endTimestamp = tuple.f0;
value = tuple.f1;
collected = true;
} else {
// Extend the current interval
endTimestamp = tuple.f0;
collected = false;
}
}
}
//check if the latest interval was collected, if not, collect it
//this happens when the last interval has the value 0
if (!collected) {
out.collect(new Tuple3<>(startTimestamp, endTimestamp, value));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,23 @@ public class TransformDeltaToAbsoluteDegreeTree
implements MapFunction<Tuple2<GradoopId, TreeMap<Long, Integer>>,
Tuple2<GradoopId, TreeMap<Long, Integer>>> {

/**
* To reduce object instantiations.
*/
private TreeMap<Long, Integer> absoluteDegreeTree;
/**
* To reduce object instantiations.
*/
private TreeMap<Long, Integer> absoluteDegreeTree;
@Override
public Tuple2<GradoopId, TreeMap<Long, Integer>> map(
Tuple2<GradoopId, TreeMap<Long, Integer>> vIdTreeMapTuple) throws Exception {
// init the degree and the temporal tree
int degree = 0;
absoluteDegreeTree = new TreeMap<>();

@Override
public Tuple2<GradoopId, TreeMap<Long, Integer>> map(
Tuple2<GradoopId, TreeMap<Long, Integer>> vIdTreeMapTuple) throws Exception {
// init the degree and the temporal tree
int degree = 0;
absoluteDegreeTree = new TreeMap<>();

// aggregate the degrees
for (Map.Entry<Long, Integer> entry : vIdTreeMapTuple.f1.entrySet()) {
degree += entry.getValue();
absoluteDegreeTree.put(entry.getKey(), degree);
}
vIdTreeMapTuple.f1 = absoluteDegreeTree;
return vIdTreeMapTuple;
// aggregate the degrees
for (Map.Entry<Long, Integer> entry : vIdTreeMapTuple.f1.entrySet()) {
degree += entry.getValue();
absoluteDegreeTree.put(entry.getKey(), degree);
}
}
vIdTreeMapTuple.f1 = absoluteDegreeTree;
return vIdTreeMapTuple;
}
}
Loading

0 comments on commit ea07695

Please sign in to comment.