X-Git-Url: http://iramuteq.org/git?p=iramuteq;a=blobdiff_plain;f=layout.py;h=310b3ff212a07163a5294582c708f21b0bf09775;hp=8cfcd962ed5ce26ee61cc3f3d3595411bf3e07c7;hb=b19770356272772c8c8ba75f351520eca186bd19;hpb=ad8fe486b34f1cee918ea8564bf909e30cd25328 diff --git a/layout.py b/layout.py index 8cfcd96..310b3ff 100644 --- a/layout.py +++ b/layout.py @@ -23,11 +23,12 @@ from listlex import * from Liste import * from search_tools import SearchFrame from dialog import PrefGraph, PrefExport, PrefSimpleFile, PrefDendro -from guifunct import SelectColumn, PrepSimi +from guifunct import SelectColumn, PrepSimi, PrefSimi from corpus import Corpus import datetime import sys import tempfile +from time import sleep import shutil import codecs import logging @@ -44,7 +45,7 @@ class GraphPanelAfc(wx.Panel): self.coding = coding self.itempath = itempath self.parent = self.GetParent()#parent - self.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "courier")) + self.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "Arial")) self.labels = [] self.listimg = [] self.buts = [] @@ -250,7 +251,7 @@ class GraphPanel(wx.ScrolledWindow): self.Dict = dico self.txt = txt self.parent = parent - self.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.FONTWEIGHT_BOLD, 0, "courier")) + self.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "Arial")) self.labels = [] self.listimg = [] self.dirout = os.path.dirname(self.Dict['ira']) @@ -340,6 +341,7 @@ class OpenCHDS(): panel.corpus = self.corpus else : panel.tableau = self.corpus + #self.parent.tableau = panel.tableau panel.dictpathout = self.DictPathOut panel.pathout = self.DictPathOut panel.parent = self.parent @@ -534,7 +536,6 @@ class OpenCHDS(): if res == wx.ID_OK : fileout = dial.fbb.GetValue() dial.Destroy() - print fileout self.corpus.get_stat_by_cluster(fileout) msg = u"Fini !" dlg = wx.MessageDialog(self.parent, msg, u"Stat par classe", wx.OK | wx.NO_DEFAULT | wx.ICON_INFORMATION) @@ -654,20 +655,21 @@ class StatLayout: self.read_result() self.TabStat = aui.AuiNotebook(ira.nb, -1, wx.DefaultPosition) self.TabStat.parametres = parametres + self.TabStat.corpus = corpus + self.TabStat.pathout = self.pathout # CHD = GraphPanel(panel.TabChdSim, DictPathOut, list_graph, txt = self.debtext) # panel.TabChdSim.AddPage(CHD,'CHD') #self.TabStatTot = wx.TextCtrl(self.TabStat, -1, style=wx.NO_BORDER | wx.TE_MULTILINE | wx.TE_RICH2) list_graph = [['zipf.png', 'zipf']] self.TabStatTot = GraphPanel(ira.nb, self.pathout, list_graph, self.result['glob']) - #self.TabStatTot.write(self.result['glob']) self.TabStat.AddPage(self.TabStatTot, 'global') for item in self.result: if item != 'glob': datam = [['forme', 'nb']] self.ListPan = ListPanel(ira, self, self.result[item]) - self.TabStat.AddPage(self.ListPan, item) - ira.nb.AddPage(self.TabStat, 'Stat') + self.TabStat.AddPage(self.ListPan, ' '.join(item.split('_'))) + ira.nb.AddPage(self.TabStat, '%s' % parametres['name']) ira.nb.SetSelection(ira.nb.GetPageCount() - 1) ira.ShowAPane("Tab_content") @@ -877,51 +879,6 @@ class CopusPanel(wx.Panel) : text.Wrap( -1 ) self.fgSizer5.Add( text, 0, wx.ALL, 5 ) -class GraphPanelSimi(wx.Panel): - def __init__(self, parent, dico, list_graph): - wx.Panel.__init__(self,parent) - self.afcnb = 1 - self.Dict = dico - self.dirout = os.path.dirname(self.Dict['ira']) - self.parent = self.GetParent()#parent - self.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "courier")) - self.labels = [] - self.listimg = [] - self.tabsimi = self.parent.GetParent() - self.ira = self.tabsimi.GetParent() - self.panel_1 = wx.ScrolledWindow(self, -1, style=wx.TAB_TRAVERSAL) - afc_img = wx.Image(os.path.join(self.ira.images_path,'button_simi.jpg'), wx.BITMAP_TYPE_ANY).ConvertToBitmap() - self.butafc = wx.BitmapButton(self, -1, afc_img) - export_img = wx.Image(os.path.join(self.ira.images_path,'button_export.jpg'), wx.BITMAP_TYPE_ANY).ConvertToBitmap() - self.butexport = wx.BitmapButton(self, -1, export_img) - - for i in range(0,len(list_graph)): - if os.path.exists(os.path.join(self.dirout,list_graph[i][0])) and list_graph[i][0] != '' : - self.listimg.append(wx.StaticBitmap(self.panel_1, -1, wx.Bitmap(os.path.join(self.dirout,list_graph[i][0]), wx.BITMAP_TYPE_ANY))) - self.labels.append(wx.StaticText(self.panel_1, -1, list_graph[i][1])) - - self.__set_properties() - self.__do_layout() - - def __set_properties(self): - self.panel_1.EnableScrolling(True,True) - #self.panel_1.SetSize((1000,1000)) - self.panel_1.SetScrollRate(20, 20) - - def __do_layout(self): - self.sizer_1 = wx.BoxSizer(wx.HORIZONTAL) - self.sizer_2 = wx.BoxSizer(wx.VERTICAL) - self.sizer_3 = wx.BoxSizer(wx.VERTICAL) - self.sizer_2.Add(self.butafc, 0, 0, 0) - self.sizer_2.Add(self.butexport, 0, 0, 0) - for i in range(0, len(self.listimg)): - self.sizer_3.Add(self.listimg[i], 0, wx.ALIGN_CENTER_HORIZONTAL, 0) - self.sizer_3.Add(self.labels[i], 0, wx.ALIGN_CENTER_HORIZONTAL, 0) - self.panel_1.SetSizer(self.sizer_3) - self.sizer_1.Add(self.sizer_2, 0, wx.EXPAND, 0) - self.sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0) - self.SetSizer(self.sizer_1) - class DefaultTextLayout : def __init__(self, ira, corpus, parametres) : self.pathout = PathOut(dirout = parametres['pathout']) @@ -952,7 +909,7 @@ class WordCloudLayout(DefaultTextLayout): self.Tab.AddPage(self.TabStatTot, 'Nuage') self.Tab.corpus = self.corpus self.Tab.parametres = self.parametres - self.ira.nb.AddPage(self.Tab, 'WordCloud %s' % self.parametres.get('corpus_name','corpus_name')) + self.ira.nb.AddPage(self.Tab, '%s' % self.parametres['name']) self.ira.nb.SetSelection(self.ira.nb.GetPageCount() - 1) self.ira.ShowAPane("Tab_content") @@ -987,7 +944,13 @@ class SimiLayout(DefaultTextLayout) : with codecs.open(self.pathout['actives.csv'], 'r', self.parametres['encoding']) as f : self.actives = f.read() self.actives = self.actives.splitlines()#[act for act in self.actives.splitlines()] - dictcol = dict([[i, [act, self.corpus.getlemeff(act)]] for i, act in enumerate(self.actives)]) + if os.path.exists(self.pathout['actives_nb.csv']) : + with open(self.pathout['actives_nb.csv'], 'r') as f : + act_nb = f.read() + act_nb = act_nb.splitlines() + dictcol = dict([[i, [self.actives[i], int(act_nb[i])]] for i, val in enumerate(self.actives)]) + else : + dictcol = dict([[i, [act, self.corpus.getlemeff(act)]] for i, act in enumerate(self.actives)]) #res = SelectColumn(self.ira, dictcol, self.actives, self.pathout['selected.csv'], selected = selected, dlg = True) #if res.ok : prep = PrepSimi(self.ira, self, self.parametres,self.pathout['selected.csv'], self.actives, indices_simi, wordlist = dictcol, selected = selected) @@ -999,26 +962,288 @@ class SimiLayout(DefaultTextLayout) : pid = exec_rcode(self.ira.RPath, script.scriptout, wait = True) check_Rresult(self.ira, pid) if self.parametres['type_graph'] == 1: + if self.parametres['svg'] : + filename, ext = os.path.splitext(script.filename) + fileout = filename + '.svg' + else : + fileout = script.filename if os.path.exists(self.pathout['liste_graph']): graph_simi = read_list_file(self.pathout['liste_graph']) - graph_simi.append([os.path.basename(script.filename), script.txtgraph]) + graph_simi.append([os.path.basename(fileout), script.txtgraph]) else : - graph_simi = [[os.path.basename(script.filename), script.txtgraph]] + graph_simi = [[os.path.basename(fileout), script.txtgraph]] print_liste(self.pathout['liste_graph'], graph_simi) DoConf().makeoptions([self.parametres['type']], [self.parametres], self.pathout['Analyse.ira']) if self.parametres['type_graph'] == 1: - self.graphpan.sizer_3.Add(wx.StaticBitmap(self.graphpan.panel_1, -1, wx.Bitmap(script.filename, wx.BITMAP_TYPE_ANY)), 0, wx.ALIGN_CENTER_HORIZONTAL, 0) + if self.parametres['svg'] : + self.graphpan.sizer_3.Add(hl.HyperLinkCtrl(self.graphpan.panel_1, -1, fileout, URL = fileout), 0, wx.ALIGN_CENTER_HORIZONTAL, 0) + else : + self.graphpan.sizer_3.Add(wx.StaticBitmap(self.graphpan.panel_1, -1, wx.Bitmap(fileout, wx.BITMAP_TYPE_ANY)), 0, wx.ALIGN_CENTER_HORIZONTAL, 0) self.graphpan.sizer_3.Add(wx.StaticText(self.graphpan.panel_1,-1, script.txtgraph), 0, wx.ALIGN_CENTER_HORIZONTAL, 0) self.graphpan.sizer_3.Fit(self.graphpan.panel_1) self.graphpan.Layout() self.graphpan.panel_1.Scroll(0,self.graphpan.panel_1.GetScrollRange(wx.VERTICAL)) def export(self, evt) : + nb = 1 + while os.path.exists(os.path.join(self.pathout.dirout,'graph_'+str(nb)+'.graphml')): + nb +=1 + fileout = ffr(os.path.join(self.pathout.dirout,'graph_'+str(nb)+'.graphml')) + txt = """ + library(igraph) + load("%s") + fileout <- "%s" + graph <- graph.simi$graph + V(graph)$x <- graph.simi$layout[,1] + V(graph)$y <- graph.simi$layout[,2] + V(graph)$weight <- graph.simi$label.cex + V(graph)$colors <- vertex.label.color + E(graph)$weigth <- graph.simi$we.width + write.graph(graph, fileout, format = 'graphml') + """ % (self.pathout['RData.RData'], fileout) + filetmp = tempfile.mktemp() + with open(filetmp, 'w') as f : + f.write(txt) + exec_rcode(self.ira.RPath, filetmp) + mss = wx.MessageDialog(self.ira, fileout, u'Fichier exporté', wx.OK) + mss.CenterOnParent() + mss.ShowModal() + mss.Destroy() + +class DefaultMatLayout : + def __init__(self, parent, tableau, parametres) : + self.pathout = PathOut(dirout = parametres['pathout']) + self.ira = parent + self.parent = parent + self.tableau = tableau + self.parametres = parametres + self.dolayout() + + def dolayout(self) : pass - # def read_result(self) : - # #self.corpus.read_corpus_from_shelves(self.corpus.dictpathout['db']) - # #self.corpus.make_et_table() - # self.result = {} - # with open(os.path.join(self.pathout,'glob.txt'), 'r') as f : - # self.result['glob'] = f.read() +class SimiMatLayout(DefaultMatLayout) : + def dolayout(self): + self.pathout.basefiles(simipath) + self.indices = indices_simi + if os.path.exists(self.pathout['liste_graph']) : + list_graph = read_list_file(self.pathout['liste_graph']) + else : + list_graph = [['','']] + notebook_flags = aui.AUI_NB_DEFAULT_STYLE | aui.AUI_NB_TAB_EXTERNAL_MOVE | aui.AUI_NB_TAB_MOVE | aui.AUI_NB_TAB_FLOAT + self.tabsimi = aui.AuiNotebook(self.parent.nb, -1, wx.DefaultPosition) + self.tabsimi.SetAGWWindowStyleFlag(notebook_flags) + self.tabsimi.SetArtProvider(aui.ChromeTabArt()) + self.graphpan = GraphPanelSimi(self.tabsimi, self.pathout, list_graph) + self.graphpan.Bind(wx.EVT_BUTTON, self.redosimi, self.graphpan.butafc) + self.graphpan.Bind(wx.EVT_BUTTON, self.export, self.graphpan.butexport) + self.tabsimi.AddPage(self.graphpan, 'Graph') + self.tabsimi.parametres = self.parametres + self.parent.nb.AddPage(self.tabsimi, 'Analyse de graph') + self.parent.ShowTab(True) + self.parent.nb.SetSelection(self.parent.nb.GetPageCount() - 1) + + def redosimi(self,evt) : + with open(self.pathout['selected.csv'],'r') as f : + selected = f.read() + selected = [int(val) for val in selected.splitlines()] + #if self.actives is None : + # with codecs.open(self.pathout['actives.csv'], 'r', self.parametres['encoding']) as f : + # self.actives = f.read() + # self.actives = self.actives.splitlines()#[act for act in self.actives.splitlines()] + try : + actives = [[val, self.tableau.actives[val][0]] for val in self.tableau.actives] + except : + actives = [[val, self.tableau.actives[val]] for val in self.tableau.actives] + + #self.tableau.make_listactives() + actives = dict([[i, val] for i, val in enumerate(actives)]) + #dictcol = dict([[i, [act, self.corpus.getlemeff(act)]] for i, act in enumerate(self.actives)]) + self.dial = PrefSimi(self.parent, -1, self.parametres, self.indices, wordlist = actives, selected = selected, actives = self.tableau.listactives) + self.dial.CenterOnParent() + self.val = self.dial.ShowModal() + if self.val == wx.ID_OK : + last = self.dial.listcol.GetFirstSelected() + lastl = [self.dial.listcol.GetFirstSelected()] + indexes = [self.dial.listcol.getColumnText(self.dial.listcol.GetFirstSelected(),0)] + while self.dial.listcol.GetNextSelected(last) != -1: + last = self.dial.listcol.GetNextSelected(last) + lastl.append(last) + indexes.append(self.dial.listcol.getColumnText(last,0)) + self.column = [self.tableau.listactives.index(val) for val in indexes] + self.column.sort() + with open(self.pathout['selected.csv'], 'w') as f : + f.write('\n'.join([`val` for val in self.column])) + self.make_param() + self.dial.Destroy() + self.script = PrintSimiScript(self) + self.script.make_script() + self.tmpfile = self.script.scriptout + dlg = progressbar(self, maxi = 2) + self.DoR(dlg) + dlg.Destroy() + if self.parametres['type_graph'] == 1: + if self.parametres['svg'] : + filename, ext = os.path.splitext(self.script.filename) + fileout = filename + '.svg' + else : + fileout = self.script.filename + if os.path.exists(self.pathout['liste_graph']): + graph_simi = read_list_file(self.pathout['liste_graph']) + graph_simi.append([os.path.basename(fileout), self.script.txtgraph]) + else : + graph_simi = [[os.path.basename(fileout), self.script.txtgraph]] + print_liste(self.pathout['liste_graph'], graph_simi) + DoConf().makeoptions([self.parametres['type']], [self.parametres], self.pathout['Analyse.ira']) + if self.parametres['type_graph'] == 1: + if self.parametres['svg'] : + self.graphpan.sizer_3.Add(hl.HyperLinkCtrl(self.graphpan.panel_1, -1, fileout, URL = fileout), 0, wx.ALIGN_CENTER_HORIZONTAL, 0) + else : + self.graphpan.sizer_3.Add(wx.StaticBitmap(self.graphpan.panel_1, -1, wx.Bitmap(self.script.filename, wx.BITMAP_TYPE_ANY)), 0, wx.ALIGN_CENTER_HORIZONTAL, 0) + self.graphpan.sizer_3.Add(wx.StaticText(self.graphpan.panel_1,-1, self.script.txtgraph), 0, wx.ALIGN_CENTER_HORIZONTAL, 0) + self.graphpan.sizer_3.Fit(self.graphpan.panel_1) + self.graphpan.Layout() + self.graphpan.panel_1.Scroll(0,self.graphpan.panel_1.GetScrollRange(wx.VERTICAL)) + else : + self.dial.Destroy() + + + def make_param(self) : + if self.parametres['first'] : + keep_coord = False + else : + keep_coord = self.dial.check_coord.GetValue() + #self.select = self.dial.check_colch.GetValue() + + paramsimi = {'coeff' : self.dial.choice1.GetSelection(), + 'layout' : self.dial.choice2.GetSelection(), + 'type_graph' : self.dial.choice3.GetSelection(), + 'arbremax' : self.dial.check1.GetValue(), + 'coeff_tv' : self.dial.check_s_size.GetValue(), + 'coeff_tv_nb' : self.dial.spin_tv.GetValue(), + 'tvprop' : self.dial.check2.GetValue(), + 'tvmin' : self.dial.spin_tvmin.GetValue(), + 'tvmax' : self.dial.spin_tvmax.GetValue(), + 'coeff_te' : self.dial.check3.GetValue(), + 'coeff_temin' : self.dial.spin_temin.GetValue(), + 'coeff_temax' : self.dial.spin_temax.GetValue(), + 'label_e' : self.dial.check_elab.GetValue(), + 'label_v' : self.dial.check_vlab.GetValue(), + 'vcex' : self.dial.check_vcex.GetValue(), + 'vcexmin' : self.dial.spin_vcexmin.GetValue(), + 'vcexmax' : self.dial.spin_vcexmax.GetValue(), + 'cex' : self.dial.spin_cex.GetValue(), + 'seuil_ok' : self.dial.check_seuil.GetValue(), + 'seuil' : self.dial.spin_seuil.GetValue(), + 'cols' : self.dial.cols.GetColour(), + 'cola' : self.dial.cola.GetColour(), + 'width' : self.dial.spin_width.GetValue(), + 'height' : self.dial.spin_height.GetValue(), + 'first' : False, + 'keep_coord' : keep_coord, + 'alpha' : self.dial.slider_sphere.GetValue(), + 'film' : self.dial.film.GetValue(), + 'svg' : self.dial.choix_format.GetSelection(), + 'halo' : self.dial.halo.GetValue(), + 'com' : self.dial.comcheck.GetValue(), + 'communities' : self.dial.choix_com.GetSelection(), + } + if 'cexfromchi' in self.parametres : + paramsimi['cexfromchi'] = self.dial.checkit.GetValue() + if 'sfromchi' in self.parametres : + paramsimi['sfromchi'] = self.dial.checki.GetValue() + if 'vlabcolor' in self.parametres : + paramsimi['vlabcolor'] = self.parametres['vlabcolor'] + if 'check_bystar' in dir(self.dial) : + paramsimi['bystar'] = self.dial.check_bystar.GetValue() + paramsimi['stars'] = self.parametres['stars'] + self.parametres.update(paramsimi) + + def DoR(self, dlg): + if self.parametres['type_graph'] == 1 : + graph = False + wait = False + else : + graph = True + wait = True + pid = exec_rcode(self.parent.RPath, self.tmpfile, wait = wait, graph = graph) + if self.parametres['type_graph'] == 1 : + while pid.poll() == None : + dlg.Pulse(u'R ...') + sleep(0.2) + check_Rresult(self.parent, pid) + + def export(self, evt) : + nb = 1 + while os.path.exists(os.path.join(self.pathout.dirout,'graph_'+str(nb)+'.graphml')): + nb +=1 + fileout = ffr(os.path.join(self.pathout.dirout,'graph_'+str(nb)+'.graphml')) + txt = """ + library(igraph) + load("%s") + fileout <- "%s" + graph <- graph.simi$graph + V(graph)$x <- graph.simi$layout[,1] + V(graph)$y <- graph.simi$layout[,2] + V(graph)$weight <- graph.simi$label.cex + V(graph)$colors <- vertex.label.color + E(graph)$weigth <- graph.simi$we.width + write.graph(graph, fileout, format = 'graphml') + """ % (self.pathout['RData.RData'], fileout) + filetmp = tempfile.mktemp() + with open(filetmp, 'w') as f : + f.write(txt) + exec_rcode(self.ira.RPath, filetmp) + mss = wx.MessageDialog(self.ira, fileout, u'Fichier exporté', wx.OK) + mss.CenterOnParent() + mss.ShowModal() + mss.Destroy() + + +class GraphPanelSimi(wx.Panel): + def __init__(self,parent, dico, list_graph): + wx.Panel.__init__(self,parent) + self.afcnb = 1 + self.Dict = dico + self.dirout = os.path.dirname(self.Dict['ira']) + self.parent = self.GetParent()#parent + self.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "courier")) + self.labels = [] + self.listimg = [] + self.tabsimi = self.parent.GetParent() + self.ira = self.tabsimi.GetParent() + self.panel_1 = wx.ScrolledWindow(self, -1, style=wx.TAB_TRAVERSAL) + afc_img = wx.Image(os.path.join(self.ira.images_path,'button_simi.jpg'), wx.BITMAP_TYPE_ANY).ConvertToBitmap() + self.butafc = wx.BitmapButton(self, -1, afc_img) + export_img = wx.Image(os.path.join(self.ira.images_path,'button_export.jpg'), wx.BITMAP_TYPE_ANY).ConvertToBitmap() + self.butexport = wx.BitmapButton(self, -1, export_img) + for i in range(0,len(list_graph)): + if os.path.exists(os.path.join(self.dirout,list_graph[i][0])) : + filename, ext = os.path.splitext(list_graph[i][0]) + if ext == '.svg' : + self.listimg.append(hl.HyperLinkCtrl(self.panel_1, -1, os.path.join(self.dirout,list_graph[i][0]), URL=os.path.join(self.dirout,list_graph[i][0]))) + else : + self.listimg.append(wx.StaticBitmap(self.panel_1, -1, wx.Bitmap(os.path.join(self.dirout,list_graph[i][0]), wx.BITMAP_TYPE_ANY))) + self.labels.append(wx.StaticText(self.panel_1, -1, list_graph[i][1])) + + self.__set_properties() + self.__do_layout() + + def __set_properties(self): + self.panel_1.EnableScrolling(True,True) + #self.panel_1.SetSize((1000,1000)) + self.panel_1.SetScrollRate(20, 20) + + def __do_layout(self): + self.sizer_1 = wx.BoxSizer(wx.HORIZONTAL) + self.sizer_2 = wx.BoxSizer(wx.VERTICAL) + self.sizer_3 = wx.BoxSizer(wx.VERTICAL) + self.sizer_2.Add(self.butafc, 0, 0, 0) + self.sizer_2.Add(self.butexport, 0, 0, 0) + for i in range(0, len(self.listimg)): + self.sizer_3.Add(self.listimg[i], 0, wx.ALIGN_CENTER_HORIZONTAL, 0) + self.sizer_3.Add(self.labels[i], 0, wx.ALIGN_CENTER_HORIZONTAL, 0) + self.panel_1.SetSizer(self.sizer_3) + self.sizer_1.Add(self.sizer_2, 0, wx.EXPAND, 0) + self.sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0) + self.SetSizer(self.sizer_1)