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