Révision 3068

tmp/org.txm.groovy.core/src/groovy/org/txm/macro/r/PlotSpecifFileMacro.groovy (revision 3068)
1
// Copyright © 2021 ENS de Lyon, CNRS, University of Franche-Comté
2
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses)
3
// @author sheiden
4

  
5
package org.txm.macro.r
6

  
7
import org.kohsuke.args4j.*
8
import groovy.transform.Field
9
import org.txm.rcp.swt.widget.parameters.*
10
import org.txm.rcp.commands.*
11
import org.txm.Toolbox
12
import org.txm.statsengine.r.core.RWorkspace
13
import java.text.DecimalFormat
14
import java.util.Locale
15

  
16
// BEGIN PARAMETERS
17

  
18
@Field @Option(name="inputFile", usage="(f, F, t, T) values TSV table, one line per example", widget="FileOpen", required=true, def="")
19
def inputFile
20

  
21
@Field @Option(name="outputLanguage", usage="output langage (FR, EN...) for decimal point format", widget="String", required=false, def="FR")
22
def outputLanguage
23

  
24

  
25
// Open the parameters input dialog box
26
if (!ParametersDialog.open(this)) return
27

  
28
// END PARAMETERS
29

  
30
// Prepare R output environment
31
def r = RWorkspace.getRWorkspaceInstance()
32
def resultsDir = new File(Toolbox.getTxmHomePath(), "results")
33
resultsDir.mkdirs()
34
def file = File.createTempFile("txm", ".svg", resultsDir)
35

  
36
// Output header
37
println """f	F	t	T	mode	comp	sign	P(f')	P(f'<>mode)	specificity	specif"""
38

  
39
// For each line of input
40
inputFile.eachLine { line ->
41

  
42
  if (line == "") return
43

  
44
  def (sf, sF, st, sT) = line.split("\t")
45
  def f = sf.toInteger()
46
  def F = sF.toInteger()
47
  def t = st.toInteger()
48
  def T = sT.toInteger()
49

  
50
  /// BEGIN R SCRIPTS
51
  def script ="""
52
  library(textometry)
53
  res <- specificities.distribution.plot($f, $F, $t, $T)
54
  """
55
  /// END R SCRIPTS
56

  
57
  // Call R script
58
  r.plot(file, script)
59

  
60
  // Compute auxiliary results
61
  def px = r.eval('res$px').asDouble()
62
  def mode = r.eval('res$mode').asInteger()
63
  def pfsum = r.eval('res$pfsum'+"[$f+1]").asDouble()
64
  def isPositiveSpecificity = (f > mode.abs())  // True if S+
65
  def proba = pfsum.abs()  // Compute actual probability (should be a positive number, even for negative specificities, where pfsum = -probability)
66
  def specifSign = (isPositiveSpecificity ? "+1" : "-1")
67
  def specifSign2 = (isPositiveSpecificity ? "" : "-")
68
  def operatorForDisplay = isPositiveSpecificity ? '>=' : '<='
69
  def specif = Math.log10(proba).abs()  // Compute actual specificity
70
  def specifTruncPlusOne = specif.trunc().toInteger()+1
71

  
72
  def locale = new Locale(outputLanguage)
73
  def pxStr = String.format(locale, "%.16e", px)
74
  def probaStr = String.format(locale, "%.16e", proba)
75
  def specifStr = String.format(locale, "%.16f", specif)
76

  
77
  // Output results
78
  println """$f	$F	$t	$T	$mode	$operatorForDisplay	$specifSign	$pxStr	$probaStr	$specifSign2$specifStr	$specifSign2$specifTruncPlusOne"""
79

  
80
}
81

  
82
// Delete unused graphic
83
if (!file.delete()) {
84
  println "** problem deleting temporary file "+file+"."
85
}
86

  
87
/* TEST: generate content of PlotSpecifFile-test-file.tsv
88

  
89
51.times {
90
  println "$it	50	4588	41027"
91
}
92

  
93
*/
tmp/org.txm.groovy.core/src/groovy/org/txm/macro/r/SpecificitiesFileMacro.groovy (revision 3068)
1
// Copyright © 2021 ENS de Lyon, CNRS, University of Franche-Comté
2
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses)
3
// @author sheiden
4

  
5
package org.txm.macro.r
6

  
7
import org.kohsuke.args4j.*
8
import groovy.transform.Field
9
import org.txm.rcp.swt.widget.parameters.*
10
import org.txm.rcp.commands.*
11
import org.txm.Toolbox
12
import org.txm.statsengine.r.core.RWorkspace
13
import java.text.DecimalFormat
14
import java.util.Locale
15

  
16
// BEGIN PARAMETERS
17

  
18
@Field @Option(name="inputFile", usage="(f, F, t, T) values TSV table, one line per example", widget="FileOpen", required=true, def="")
19
def inputFile
20

  
21
@Field @Option(name="outputLanguage", usage="output langage (FR, EN...) for decimal point format", widget="String", required=false, def="FR")
22
def outputLanguage
23

  
24

  
25
// Open the parameters input dialog box
26
if (!ParametersDialog.open(this)) return
27

  
28
// END PARAMETERS
29

  
30
// Prepare R output environment
31
def r = RWorkspace.getRWorkspaceInstance()
32
def resultsDir = new File(Toolbox.getTxmHomePath(), "results")
33
resultsDir.mkdirs()
34
def file = File.createTempFile("txm", ".svg", resultsDir)
35

  
36
// Output header
37
println """f	F	t	T	mode	comp	sign	P(f')	P(f'<>mode)	specificity	specif"""
38

  
39
// For each line of input
40
inputFile.eachLine { line ->
41

  
42
  if (line == "") return
43

  
44
  def (sf, sF, st, sT) = line.split("\t")
45
  def f = sf.toInteger()
46
  def F = sF.toInteger()
47
  def t = st.toInteger()
48
  def T = sT.toInteger()
49

  
50
  /// BEGIN R SCRIPTS
51
  def script ="""
52
  library(textometry)
53
  res <- specificities.distribution.plot($f, $F, $t, $T)
54
  """
55
  /// END R SCRIPTS
56

  
57
  // Call R script
58
  r.plot(file, script)
59

  
60
  // Compute auxiliary results
61
  def px = r.eval('res$px').asDouble()
62
  def mode = r.eval('res$mode').asInteger()
63
  def pfsum = r.eval('res$pfsum'+"[$f+1]").asDouble()
64
  def isPositiveSpecificity = (f > mode.abs())  // True if S+
65
  def proba = pfsum.abs()  // Compute actual probability (should be a positive number, even for negative specificities, where pfsum = -probability)
66
  def specifSign = (isPositiveSpecificity ? "+1" : "-1")
67
  def specifSign2 = (isPositiveSpecificity ? "" : "-")
68
  def operatorForDisplay = isPositiveSpecificity ? '>=' : '<='
69
  def specif = Math.log10(proba).abs()  // Compute actual specificity
70
  def specifTruncPlusOne = specif.trunc().toInteger()+1
71

  
72
  def locale = new Locale(outputLanguage)
73
  def pxStr = String.format(locale, "%.16e", px)
74
  def probaStr = String.format(locale, "%.16e", proba)
75
  def specifStr = String.format(locale, "%.16f", specif)
76

  
77
  // Output results
78
  println """$f	$F	$t	$T	$mode	$operatorForDisplay	$specifSign	$pxStr	$probaStr	$specifSign2$specifStr	$specifSign2$specifTruncPlusOne"""
79

  
80
}
81

  
82
// Delete unused graphic
83
if (!file.delete()) {
84
  println "** problem deleting temporary file "+file+"."
85
}
86

  
87
/* TEST: generate content of PlotSpecifFile-test-file.tsv
88

  
89
51.times {
90
  println "$it	50	4588	41027"
91
}
92

  
93
*/

Formats disponibles : Unified diff