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