Skip to content

Commit

Permalink
Added PolarisationResult class; moved export package under dna
Browse files Browse the repository at this point in the history
  • Loading branch information
leifeld committed Jan 7, 2025
1 parent 6e5d3e7 commit 4c6db70
Showing 12 changed files with 167 additions and 15 deletions.
2 changes: 1 addition & 1 deletion dna/src/main/java/dna/HeadlessDna.java
Original file line number Diff line number Diff line change
@@ -10,13 +10,13 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import export.*;
import me.tongfei.progressbar.ProgressBar;
import model.Value;
import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
import org.rosuda.JRI.RConsoleOutputStream;
import org.rosuda.JRI.Rengine;

import dna.export.*;
import logger.LogEvent;
import logger.Logger;
import model.Coder;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package export;
package dna.export;

/**
* Data for representing a barplot, including the variable name, the distinct values for the variable in alphabetical
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package export;
package dna.export;

import java.util.ArrayList;

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package export;
package dna.export;


import model.Statement;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package export;
package dna.export;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -4260,7 +4260,7 @@ private int[] balanceClusterDistribution(int[] memberships, int k) {
*
* @param clusterSolutions The list of parent generation cluster solutions.
* @param subtractNetwork The network matrix to subtract from the original network.
* @param qualityFunction The quality function to evaluate cluster solutions.
* @param qualityFunction The quality function to evaluate cluster solutions. Supported values are "modularity" and "eiIndex".
* @param n The number of nodes in the network.
* @param elitePercentage The percentage of elite solutions to retain.
* @param mutationPercentage The percentage of solutions to mutate.
@@ -4269,8 +4269,6 @@ private int[] balanceClusterDistribution(int[] memberships, int k) {
* @return A list of children cluster solutions.
*/
private ArrayList<ClusterSolution> geneticIteration(ArrayList<ClusterSolution> clusterSolutions, double[][] subtractNetwork, String qualityFunction, int n, double elitePercentage, double mutationPercentage, int k, Random rng) {
int numClusterSolutions = clusterSolutions.size();

// Validate elitePercentage is within the valid range [0, 1]
if (elitePercentage < 0.0 || elitePercentage > 1.0) {
throw new IllegalArgumentException("Elite percentage must be between 0 and 1 (inclusive).");
@@ -4282,6 +4280,7 @@ private ArrayList<ClusterSolution> geneticIteration(ArrayList<ClusterSolution> c
}

// Calculate the number of elites based on the percentage
int numClusterSolutions = clusterSolutions.size();
int numElites = Math.max(1, (int) Math.round(elitePercentage * numClusterSolutions)); // At least one elite
LogEvent log = new LogEvent(Logger.MESSAGE, "Number of elites: " + numElites, "Number of elite solutions based on the elite percentage.");
Dna.logger.log(log);
@@ -4487,5 +4486,4 @@ private ArrayList<ClusterSolution> mutationStep(ArrayList<ClusterSolution> child
}
return children;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package export;
package dna.export;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package export;
package dna.export;

import java.util.ArrayList;

154 changes: 154 additions & 0 deletions dna/src/main/java/dna/export/PolarisationResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package dna.export;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;

/**
* Stores the results of a single run of the genetic algorithm, i.e., for a
* single time step of the time window algorithm or the whole network if no
* time window was set.
*/
public final class PolarisationResult {

private final double[] maxQ;
private final double[] avgQ;
private final double[] sdQ;
private final double finalMaxQ;
private final int[] memberships;
private final String[] names;
private final boolean earlyConvergence;
private final LocalDateTime start;
private final LocalDateTime stop;
private final LocalDateTime middle;

/**
* Creates a PolarizationResult for a single time step.
*
* @param maxQ The maximum quality score for each iteration of the genetic algorithm.
* @param avgQ The mean quality score for each iteration of the genetic algorithm.
* @param sdQ The standard deviation of the quality scores for each iteration of the genetic algorithm.
* @param finalMaxQ The maximum quality score of the final iteration of the genetic algorithm.
* @param memberships A membership array containing the cluster levels for each node, starting with 0 and going up to K - 1.
* @param names The node labels of the network.
* @param earlyConvergence A boolean indicating whether the genetic algorithm converged before the last iteration.
* @param start The start date and time of the time window network. Cannot be null. If no time window was set, this is the date of the network.
* @param stop The end date and time of the time window network. Cannot be null. If no time window was set, this is the date of the network.
* @param middle The mid-point date of the time window network. Cannot be null. If no time window was set, this is the date of the network.
* @throws IllegalArgumentException if any array is null, has invalid sizes, or if start, stop, or middle is null.
*/
public PolarisationResult(double[] maxQ, double[] avgQ, double[] sdQ, double finalMaxQ,
int[] memberships, String[] names, boolean earlyConvergence,
LocalDateTime start, LocalDateTime stop, LocalDateTime middle) {

// Validate input
if (maxQ == null || avgQ == null || sdQ == null || memberships == null || names == null) {
throw new IllegalArgumentException("Input arrays cannot be null.");
}
if (start == null || stop == null || middle == null) {
throw new IllegalArgumentException("Start, stop, and middle dates cannot be null.");
}
if (maxQ.length != avgQ.length || maxQ.length != sdQ.length) {
throw new IllegalArgumentException("maxQ, avgQ, and sdQ must have the same length.");
}
if (memberships.length != names.length) {
throw new IllegalArgumentException("Memberships and names must have the same length.");
}

// Assign fields
this.maxQ = Arrays.copyOf(maxQ, maxQ.length);
this.avgQ = Arrays.copyOf(avgQ, avgQ.length);
this.sdQ = Arrays.copyOf(sdQ, sdQ.length);
this.finalMaxQ = finalMaxQ;
this.memberships = Arrays.copyOf(memberships, memberships.length);
this.names = Arrays.copyOf(names, names.length);
this.earlyConvergence = earlyConvergence;
this.start = start;
this.stop = stop;
this.middle = middle;
}

public LocalDateTime getStart() {
return start;
}

public LocalDateTime getStop() {
return stop;
}

public LocalDateTime getMiddle() {
return middle;
}

public int[] getMemberships() {
return Arrays.copyOf(memberships, memberships.length);
}

public String[] getNames() {
return Arrays.copyOf(names, names.length);
}

public double[] getMaxQ() {
return Arrays.copyOf(maxQ, maxQ.length);
}

public double[] getAvgQ() {
return Arrays.copyOf(avgQ, avgQ.length);
}

public double[] getSdQ() {
return Arrays.copyOf(sdQ, sdQ.length);
}

public double getFinalMaxQ() {
return finalMaxQ;
}

public boolean isEarlyConvergence() {
return earlyConvergence;
}

@Override
public String toString() {
return "PolarizationResult{" +
"maxQ=" + Arrays.toString(maxQ) +
", avgQ=" + Arrays.toString(avgQ) +
", sdQ=" + Arrays.toString(sdQ) +
", finalMaxQ=" + finalMaxQ +
", memberships=" + Arrays.toString(memberships) +
", names=" + Arrays.toString(names) +
", earlyConvergence=" + earlyConvergence +
", start=" + start +
", stop=" + stop +
", middle=" + middle +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PolarisationResult that = (PolarisationResult) o;
return Double.compare(that.finalMaxQ, finalMaxQ) == 0 &&
earlyConvergence == that.earlyConvergence &&
Arrays.equals(maxQ, that.maxQ) &&
Arrays.equals(avgQ, that.avgQ) &&
Arrays.equals(sdQ, that.sdQ) &&
Arrays.equals(memberships, that.memberships) &&
Arrays.equals(names, that.names) &&
Objects.equals(start, that.start) &&
Objects.equals(stop, that.stop) &&
Objects.equals(middle, that.middle);
}

@Override
public int hashCode() {
int result = Objects.hash(finalMaxQ, earlyConvergence, start, stop, middle);
result = 31 * result + Arrays.hashCode(maxQ);
result = 31 * result + Arrays.hashCode(avgQ);
result = 31 * result + Arrays.hashCode(sdQ);
result = 31 * result + Arrays.hashCode(memberships);
result = 31 * result + Arrays.hashCode(names);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package export;
package dna.export;

import java.io.Serializable;

2 changes: 1 addition & 1 deletion dna/src/main/java/gui/BackboneExporter.java
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import com.github.lgooddatepicker.components.DateTimePicker;
import com.github.lgooddatepicker.components.TimePickerSettings;
import dna.Dna;
import export.Exporter;
import dna.export.Exporter;
import logger.LogEvent;
import logger.Logger;
import me.tongfei.progressbar.ProgressBar;
2 changes: 1 addition & 1 deletion dna/src/main/java/gui/NetworkExporter.java
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
import com.github.lgooddatepicker.components.DateTimePicker;
import com.github.lgooddatepicker.components.TimePickerSettings;
import dna.Dna;
import export.Exporter;
import dna.export.Exporter;
import logger.LogEvent;
import logger.Logger;
import model.StatementType;
2 changes: 1 addition & 1 deletion dna/src/main/java/sql/DataExchange.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sql;

import dna.Dna;
import export.DataFrame;
import dna.export.DataFrame;
import logger.LogEvent;
import logger.Logger;
import me.tongfei.progressbar.ProgressBar;

0 comments on commit 4c6db70

Please sign in to comment.