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