Skip to content

Commit

Permalink
Added creation of custom fit scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
UsAndRufus committed Feb 27, 2017
1 parent 0237990 commit 21b46b0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ hs_err_pid*
# Project files
*.data

gnuplot/created

# Output files
*.png
*.ps
Expand Down
8 changes: 3 additions & 5 deletions gnuplot/fit.plt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
# R^2 calculation from: http://computingnote.blogspot.co.uk/2013/04/calculating-r2-with-gnuplot.html

mean(x)= m
fit mean(x) "test~2017-02-27-21-02.data" using 1:2 via m # FIXME
fit mean(x) "REPLACE" using 1:2 via m
SST = FIT_WSSR/(FIT_NDF+1)

f(x) = (k*x**-b)*exp(-x/c)
k=1
b=1
c=1
fit f(x) "test~2017-02-27-21-02.data" via b,k,c # FIXME
fit f(x) "REPLACE" via b,k,c
SSE=FIT_WSSR/(FIT_NDF)

SSR=SST-SSE
R2=SSR/SST

#set label sprintf("f(x)=%fx+%f\nR²=%f", a, b, R2) # print r^2.

set label 1 sprintf("β=%3.5g; R^2=%f",b,R2) at 50,1.1

plot "test~2017-02-27-21-02.data" title "data", f(x) title "fit" # FIXME
plot "REPLACE" title "data", f(x) title "fit"
2 changes: 1 addition & 1 deletion gnuplot/setup.plt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
set logscale x
set logscale y
set xrange [1:]
set yrange [:1]
set yrange [:2]
set xlabel "a"
set ylabel "P(A≥a)"
51 changes: 51 additions & 0 deletions src/main/java/output/FitScriptCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package output;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

public class FitScriptCreator {
private static final String REPLACE_STRING = "REPLACE";
private static final String FILE_TYPE = ".data";
private static final Path FIT_SCRIPT_PATH = Paths.get("ThirdYearProject/gnuplot/fit.plt");
private static final String CREATED_DIRECTORY_PATH_STRING = "ThirdYearProject/gnuplot/created/";

public static void main(String[] args) throws IOException {
System.out.println("Run from outside ThirdYearProject");
if (!(args[0].startsWith("ThirdYearProject/"))) {
System.out.println("Filepath argument must start with ThirdYearProject/");
throw new IllegalArgumentException();
}
String dataFilePathString = args[0];

createFor(Paths.get(dataFilePathString));

}

public static void createFor(Path dataFilePath) throws IOException {
String filename = dataFilePath.getFileName().toString();
filename = "fit_" + filename.substring(0, filename.length() - FILE_TYPE.length());
Path createdFitScript = Paths.get(CREATED_DIRECTORY_PATH_STRING + filename);
Files.copy(FIT_SCRIPT_PATH, createdFitScript, REPLACE_EXISTING, COPY_ATTRIBUTES);

replaceFilename(createdFitScript, dataFilePath);
}

private static void replaceFilename(Path fitScript, Path dataFilePath) throws IOException {
Charset charset = StandardCharsets.UTF_8;

String content = new String(Files.readAllBytes(fitScript), charset);
String relativeFilePath = fitScript.getParent().relativize(dataFilePath).toString();
relativeFilePath = relativeFilePath.replace("\\", "/");
System.out.println(relativeFilePath);
content = content.replaceAll(REPLACE_STRING, relativeFilePath);
System.out.println(content);
Files.write(fitScript, content.getBytes(charset));
}
}

0 comments on commit 21b46b0

Please sign in to comment.