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