multisplit
[iramuteq] / tabverges.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2020 Pierre Ratinaud
4 #modification pour python 3 : Laurent Mérat, 6x7 - mai 2020
5 #License: GNU/GPL
6
7 #------------------------------------
8 # import des modules python
9 #------------------------------------
10 import os
11 import string
12 import sys
13 import tempfile
14 from time import sleep
15
16 #------------------------------------
17 # import des modules wx
18 #------------------------------------
19 import wx
20 import wx.lib.sized_controls as sc
21
22 #------------------------------------
23 # import des fichiers du projet
24 #------------------------------------
25 from chemins import ffr,FFF, ConstructPathOut
26 from functions import exec_rcode, check_Rresult, progressbar
27 from PrintRScript import ProtoScript
28 from analysematrix import AnalyseMatrix
29 from dialog import ProtoDial
30
31
32 class Prototypical(AnalyseMatrix) :
33
34 #     def __init__(self, parent, parametres):
35 #         self.parent = parent
36 #         self.tableau = self.parent.tableau
37 #         self.parametres = parametres
38 #         self.parametres['filename'] = parent.tableau.parametre['filename']
39 #         self.parametres['pathout'] = ConstructPathOut(parent.tableau.parametre['filename'], 'proto')
40 #         self.parametres['type'] = 'proto'
41 #         dlg = progressbar(self.parent, 2)
42 #         self.colnames = self.tableau.get_colnames()
43 #         AnalyseMatrix.__init__(self, parent, parent.tableau, self.parametres, dlg = dlg)
44
45     def doparametres(self, dlg = None):
46         self.dial = ProtoDial(self.ira, self.tableau.colnames)
47         self.dial.CenterOnParent()
48         self.val = self.dial.ShowModal()
49         if self.val==wx.ID_OK :
50                 self.ColSel1 = self.dial.variables.GetSelections()
51                 self.ColSel2 = self.dial.rangs.GetSelections()
52                 if len(self.ColSel1) != len(self.ColSel2) :
53                     print('pas meme taille')
54                     self.check_val()
55                 else :
56                     if self.dial.choix_freq.GetSelection() == 0 :
57                         self.parametres['limfreq'] = 'NULL'
58                     else :
59                         self.parametres['limfreq'] = self.dial.freqlim.GetValue()
60                     if self.dial.choix_rang.GetSelection() == 0 :
61                         self.parametres['limrang'] = 'NULL'
62                     else :
63                         self.parametres['limrang'] = self.dial.ranglim.GetValue()
64                     self.parametres['freqmin'] = int(self.dial.m_textCtrl4.GetValue())
65                     if self.dial.typegraph.GetSelection() == 0 :
66                         self.parametres['typegraph'] = 'classical'
67                         self.parametres['cloud'] = False
68                     elif self.dial.typegraph.GetSelection() == 1 :
69                         self.parametres['typegraph'] = 'classical'
70                         self.parametres['cloud'] = True
71                     else :
72                         self.parametres['typegraph'] = 'plan'
73                 self.dial.Destroy()
74         else :
75             self.dial.Destroy()
76             self.parametres = None
77
78     def doanalyse(self) :
79         table_assoc, table_rank = self.dotable()
80         self.makedatas(table_assoc, table_rank)
81         self.DoR()
82
83     def dotable(self) :
84         table_assoc = self.tableau.select_col(self.ColSel1)
85         table_rank = self.tableau.select_col(self.ColSel2)
86         return table_assoc, table_rank
87
88     def makedatas(self, table_assoc, table_rank) :
89         words = {}
90         for i in range(0, len(table_assoc)) :
91             for j, word in enumerate(table_assoc[i]) :
92                 if word.strip() != "" :
93                     if word in words :
94                         words[word][0] += 1
95                         if table_rank[i][j] != '' :
96                             words[word][1].append(float(table_rank[i][j]))
97                     else :
98                         if table_rank[i][j] != '' :
99                             words[word] = [1, [float(table_rank[i][j])]]
100                         else :
101                             words[word] = [1, []]
102         res = [[word, words[word][0], float(sum(words[word][1])) / len(words[word][1])] for word in words if len(words[word][1]) != 0 and words[word][0] >= self.parametres['freqmin']]
103         with open(self.pathout['table.csv'], 'w') as f :
104             f.write('\n'.join(['\t'.join(['"' + val[0] +'"', repr(val[1]), repr(val[2])]) for val in res]))
105         #self.parent.tableau.parametres = self.parent.tableau.parametre
106         #self.parent.tableau.save_tableau(self.pathout['analyse.db'])
107
108     def DoR(self) :
109         script = ProtoScript(self)
110         script.make_script()
111         self.doR(script.scriptout)