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