afc 3d
[iramuteq] / textsimi.py
1 # -*- coding: utf-8 -*-
2 #Author: Pierre Ratinaud
3 #Copyright (c) 2008-2013 Pierre Ratinaud
4 #Lisense: GNU/GPL
5
6 from chemins import ffr, simipath
7 #from corpus import Corpus
8 import os
9 from analysetxt import AnalyseText
10 #from ConfigParser import RawConfigParser
11 #from guifunct import getPage, getCorpus
12 from dialog import StatDialog
13 from guifunct import SelectColumn, PrepSimi
14 from functions import indices_simi, progressbar, treat_var_mod, read_list_file, print_liste
15 #from tableau import Tableau
16 #from tabsimi import DoSimi
17 from PrintRScript import PrintSimiScript
18 import wx
19 from copy import copy
20
21 import logging
22
23 log = logging.getLogger('iramuteq.textsimi')
24
25 class SimiTxt(AnalyseText): 
26     def doanalyse(self) :
27         self.parametres['type'] = 'simitxt'
28         self.pathout.basefiles(simipath)
29         self.indices = indices_simi
30         self.makesimiparam()
31         #FIXME
32         self.actives = self.corpus.make_actives_limit(3)
33         dictcol = dict([[i, [act, self.corpus.getlemeff(act)]] for i, act in enumerate(self.actives)]) 
34         continu = False
35         if self.dlg :
36             #cont = SelectColumn(self.ira, dictcol, self.actives, self.pathout['selected.csv'], dlg = self.dlg)
37             #if cont.ok :
38             self.listet = self.corpus.make_etoiles()
39             self.listet.sort()
40             self.stars = copy(self.listet)
41             self.parametres['stars'] = copy(self.listet)
42             self.parametres['sfromchi'] = False
43             prep = PrepSimi(self.ira, self, self.parametres, self.pathout['selected.csv'], self.actives, indices_simi, wordlist=dictcol)
44             if prep.val == wx.ID_OK :
45                 continu = True
46                 self.parametres = prep.parametres
47         if continu :
48             self.makefiles()
49             script = PrintSimiScript(self)
50             script.make_script()
51             if not self.doR(script.scriptout, dlg = self.dlg, message = 'R...') :
52                 log.info('Problem')
53                 return False
54             if self.parametres['type_graph'] == 1:
55                 if self.parametres['svg'] :
56                     filename, ext = os.path.splitext(script.filename)
57                     fileout = filename + '.svg'                    
58                 else :
59                     fileout = script.filename
60                 if os.path.exists(self.pathout['liste_graph']):
61                     graph_simi = read_list_file(self.pathout['liste_graph'])
62                     graph_simi.append([os.path.basename(fileout), script.txtgraph])
63                 else :
64                     graph_simi = [[os.path.basename(fileout), script.txtgraph]]
65                 print_liste(self.pathout['liste_graph'], graph_simi)
66         else : 
67             return False
68
69 #    def preferences(self) :
70 #        dial = StatDialog(self, self.parent)
71 #        dial.CenterOnParent()
72 #        val = dial.ShowModal()
73 #        if val == 5100 :
74 #            if dial.radio_lem.GetSelection() == 0 :
75 #                lem = 1
76 #            else :
77 #                lem = 0            
78 #            self.parametres['lem'] = lem
79 #            dial.Destroy()
80 #            return self.parametres
81 #        else :
82 #            dial.Destroy()
83 #            return None
84
85     def makesimiparam(self) :
86         self.paramsimi = {'coeff' : 0,
87                           'layout' : 2,
88                           'type_graph' : 1,
89                           'arbremax' : 1,
90                           'coeff_tv' : 1,
91                           'coeff_tv_nb' : 0,
92                           'tvprop' : 0,
93                           'tvmin' : 5,
94                           'tvmax' : 30,
95                           'coeff_te' : 1,
96                           'coeff_temin' : 1,
97                           'coeff_temax' : 10,
98                           'label_v': 1,
99                           'label_e': 0,
100                           'vcex' : 1,
101                           'cexfromchi' : False,
102                           'vcexmin' : 10,
103                           'vcexmax' : 25,
104                           'cex' : 10,
105                           'seuil_ok' : 0,
106                           'seuil' : 1,
107                           'cols' : (255,0,0),
108                           'cola' : (200,200,200),
109                           'width' : 1000,
110                           'height' : 1000,
111                           'bystar' : False,
112                           'first' : True,
113                           'keep_coord' : True,
114                           'alpha' : 20,
115                           'film': False,
116                           'svg' : 0,
117                           'com' : 0,
118                           'communities' : 0,
119                           'halo' : 0,
120                           #'ira' : self.pathout['Analyse.ira']
121                           }
122         self.parametres.update(self.paramsimi)
123
124     def makefiles(self, lim=3) :
125         #self.actives, lim = self.corpus.make_actives_nb(self.parametres.get('max_actives',1500), 1)
126         self.parametres['eff_min_forme'] = lim
127         self.parametres['nbactives'] = len(self.actives)
128         self.parametres['fromprof'] = False
129         self.corpus.make_and_write_sparse_matrix_from_uces(self.actives, self.pathout['mat01.csv'], self.pathout['listeuce1.csv'])
130         with open(self.pathout['actives.csv'], 'w') as f :
131             f.write('\n'.join(self.actives).encode(self.ira.syscoding))
132
133 class SimiFromCluster(SimiTxt) :
134     def __init__(self, ira, corpus, actives, numcluster, parametres = None, dlg = False) :
135         self.actives = actives
136         self.numcluster = numcluster
137         parametres['name'] = 'simi_classe_%i' % (numcluster + 1)
138         SimiTxt.__init__(self, ira, corpus, parametres, dlg, lemdial = False)
139     
140     def preferences(self) :
141         return self.parametres
142
143     def doanalyse(self) :
144         self.parametres['type'] = 'clustersimitxt'
145         self.pathout.basefiles(simipath)
146         self.indices = indices_simi
147         self.makesimiparam()
148         if 'bystar' in self.parametres :
149             del self.parametres['bystar']
150         dictcol = dict([[i, [act, self.corpus.getlemclustereff(act, self.numcluster)]] for i, act in enumerate(self.actives)]) 
151         continu = True
152         if self.dlg :
153             #self.listet = self.corpus.make_etoiles()
154             #self.listet.sort()
155             self.stars = []#copy(self.listet)
156             self.parametres['stars'] = False#copy(self.listet)
157             self.parametres['sfromchi'] = True
158             prep = PrepSimi(self.ira, self, self.parametres, self.pathout['selected.csv'], self.actives, indices_simi, wordlist=dictcol)
159             if prep.val == wx.ID_OK :
160                 continu = True
161                 self.parametres = prep.parametres
162             else :
163                 continu = False
164         if continu :
165             self.makefiles()
166             script = PrintSimiScript(self)
167             script.make_script()
168             if not self.doR(script.scriptout, dlg = self.dlg, message = 'R ...') :
169                 return False
170             if self.parametres['type_graph'] == 1:
171                 if self.parametres['svg'] :
172                     filename, ext = os.path.splitext(script.filename)
173                     fileout = filename + '.svg'                    
174                 else :
175                     fileout = script.filename
176                 if os.path.exists(self.pathout['liste_graph']):
177                     graph_simi = read_list_file(self.pathout['liste_graph'])
178                     graph_simi.append([os.path.basename(fileout), script.txtgraph])
179                 else :
180                     graph_simi = [[os.path.basename(fileout), script.txtgraph]]
181                 print_liste(self.pathout['liste_graph'], graph_simi)
182         else : 
183             return False
184
185     def makefiles(self) :
186         self.parametres['eff_min_forme'] = 3
187         self.parametres['nbactives'] = len(self.actives)
188         self.parametres['fromprof'] = True
189         self.corpus.make_and_write_sparse_matrix_from_classe(self.actives, self.corpus.lc[self.numcluster], self.pathout['mat01.csv'])
190         with open(self.pathout['actives.csv'], 'w') as f :
191             f.write('\n'.join(self.actives).encode(self.ira.syscoding))