Merge branch 'master' of http://www.netdig.org/git/iramuteq
[iramuteq] / profile_segment.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2010, Pierre Ratinaud
5 #Lisense: GNU/GPL
6
7 import tempfile
8 from ProfList import *
9 import agw.aui as aui
10 from functions import exec_rcode, check_Rresult, ReadProfileAsDico, ReadList
11 from listlex import *
12 from dialog import PrefSegProf, PrefProfTypes
13 from time import sleep
14
15 class ProfileSegment() :
16     def __init__(self, parent, pathout, parametres, corpus) :
17         self.parent = parent
18         self.corpus = corpus
19         self.dictpathout = pathout
20         self.parametres = parametres
21         dial = PrefSegProf(self.parent)
22         dial.CenterOnParent()
23         if dial.ShowModal() == wx.ID_OK :
24            if dial.box_lem.GetSelection() == 0 :
25                self.lem = True
26            else :
27                self.lem = False
28            self.mini = dial.spin_min.GetValue()
29            self.maxi = dial.spin_max.GetValue()
30            self.eff = dial.spin_eff.GetValue()
31            dial.Destroy()
32            self.dlg = progressbar(self, maxi = 4)
33            self.dlg.Update(1, u'Recherche des segments')
34            self.make_table()
35            self.make_prof()
36            self.dlg.Update(3, u'ouverture des profils')
37            self.do_layout()
38            self.dlg.Update(4, 'fini')
39            self.dlg.Destroy()
40     
41     def make_table(self) :
42         self.corpus.make_segments_profile(self.dictpathout['segments_classes'], lenmin = self.mini, lenmax = self.maxi, effmin = self.eff, lem = self.lem)
43
44     def make_prof(self) :
45         txt = """
46         load("%s")
47         source("%s")
48         """ % (self.dictpathout['RData'], self.parent.RscriptsPath['chdfunct'])
49
50         txt += """
51         dt <- read.csv2("%s", row.names = 1)
52         to <- build.pond.prof(dt)
53         PrintProfile(n1,to[4],NULL,to[5],NULL,clnb,"%s","%s")
54         """ % (self.corpus.dictpathout['segments_classes'], self.dictpathout['prof_seg'], self.dictpathout['antiprof_seg'])
55         fo = tempfile.mktemp(dir=self.parent.TEMPDIR)
56         with open(fo, 'w') as f :
57             f.write(txt)
58         pid = exec_rcode(self.parent.RPath, fo, wait = False)
59         while pid.poll() == None :
60             self.dlg.Pulse(u'Construction des profils...')
61             sleep(0.2)
62         check_Rresult(self.parent, pid)
63
64     def do_layout(self) :
65         SelectTab = self.parent.nb.GetSelection()
66         page = self.parent.nb.GetPage(SelectTab).TabChdSim
67         prof_seg = ReadProfileAsDico(self.dictpathout['prof_seg'], True, self.parent.syscoding)
68         prof_seg_nb = aui.AuiNotebook(self.parent, -1, wx.DefaultPosition)
69         for i in range(0,len(self.corpus.lc)) :
70             ntab = ProfListctrlPanel(self.parent, self, prof_seg[str(i + 1)], False, i + 1)
71             prof_seg_nb.AddPage(ntab, 'classe %i' % (i + 1))
72         page.AddPage(prof_seg_nb, u'Profils des segements répétés')
73         page.SetSelection(page.GetPageCount() - 1)
74
75 class ProfilType() :
76     def __init__(self, parent, corpus, parametres) :
77         self.parent = parent
78         self.corpus = corpus
79         self.parametres = parametres
80         self.outprof = self.corpus.dictpathout['prof_type']
81         dial = PrefProfTypes(self.parent)
82         dial.fbb.SetValue(self.outprof)
83         dial.CenterOnParent()
84         res = dial.ShowModal()
85         if res == wx.ID_OK :
86             if dial.radio_type.GetSelection() == 0 :
87                 alceste = True
88             else :
89                 alceste = False
90             #if 'outprof' in self.corpus.parametre :
91             #    self.corpus.parametre['outprof'][self.outprof] = alceste
92             #else :
93             #    self.corpus.parametre['outprof'] = {self.outprof: alceste}
94             self.dlg = progressbar(self, maxi = 4)
95             self.dlg.Update(1, u'Recherche des types')
96             self.make_table()
97             self.dlg.Update(1, u'Construction des profils')
98             self.make_prof(alceste = alceste)
99             self.dlg.Update(3, u'Ouverture des profils')
100             self.do_layout(alceste = alceste)
101             self.dlg.Update(4, 'fini')
102             self.dlg.Destroy()
103     
104     def make_table(self) :
105         self.corpus.make_proftype(self.corpus.dictpathout['type_cl'])
106
107     def make_prof(self, alceste = True) :
108         txt = """
109         load("%s")
110         source("%s")
111         """ % (self.corpus.dictpathout['RData'], self.parent.RscriptsPath['chdfunct'])
112
113         txt += """
114         dt <- read.csv2("%s", row.names = 1)
115         """ % self.corpus.dictpathout['type_cl']
116         if alceste :
117             txt += """
118             to <- build.pond.prof(dt)
119             PrintProfile(n1,to[4],NULL,to[5],NULL,clnb,"%s","%s")
120             """ % (self.outprof, self.corpus.dictpathout['antiprof_type'])
121         else :
122             txt += """
123             to <- AsLexico2(dt)
124             write.csv2(to[[1]], file = "%s")
125             """ % (self.outprof)
126             #write.csv2(to[[3]], file = "%s")
127             # % (self.outprof)
128         fo = tempfile.mktemp(dir=self.parent.TEMPDIR)
129         with open(fo, 'w') as f :
130             f.write(txt)
131         pid = exec_rcode(self.parent.RPath, fo, wait = False)
132         while pid.poll() == None :
133             self.dlg.Pulse(u'Construction des profils...')
134             sleep(0.2)
135         check_Rresult(self.parent, pid)
136
137     def do_layout(self, alceste = True) :
138         SelectTab = self.parent.nb.GetSelection()
139         page = self.parent.nb.GetPage(SelectTab).TabChdSim
140         prof_seg_nb = aui.AuiNotebook(self.parent, -1, wx.DefaultPosition)
141         if alceste :
142             prof_seg = ReadProfileAsDico(self.outprof, True)
143             for i in range(0,len(self.corpus.lc)) :
144                 ntab = ProfListctrlPanel(self.parent, self, prof_seg[str(i + 1)], False, i + 1)
145                 prof_seg_nb.AddPage(ntab, 'classe %i' % (i + 1))
146         else :
147             self.DictSpec, first = ReadList(self.outprof)
148             self.ListPan = ListForSpec(self.parent, self, self.DictSpec, first)
149             prof_seg_nb.AddPage(self.ListPan, u'Spécificités')
150
151         page.AddPage(prof_seg_nb, u'Profils des types')
152         page.SetSelection(page.GetPageCount() - 1)
153