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