multisplit
[iramuteq] / tabafcm.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 os
11 import tempfile
12 from time import sleep
13
14 #------------------------------------
15 # import des modules wx
16 #------------------------------------
17 import wx
18
19 #------------------------------------
20 # import des fichiers du projet
21 #------------------------------------
22 from chemins import ffr, ConstructAfcmPath, ConstructPathOut
23 from functions import exec_rcode, check_Rresult
24 from ProfList import *
25 from dialog import PrefQuestAlc
26
27
28 class AFCMQ():
29
30     def __init__(self, parent, LISTNUMACTIVE, LISTVARSUP):
31         #FIXME
32         self.parent = parent
33         txt = ''
34         self.tempgraph = tempfile.mktemp(dir=parent.TEMPDIR)
35         #if parent.g_id: rownames = '1'
36         #else : rownames = 'NULL'
37         #if parent.g_header : header = 'TRUE'
38         #else : header = 'FALSE'
39         txt += """
40         datadm <- read.table("%s", header = TRUE, sep = ';', quote='"', encoding="%s",row.names=1)
41         """ % (ffr(parent.tableau.parametre['csvfile']), parent.tableau.parametre['encodage'])
42         if len(LISTVARSUP) == 1 :
43             strlistsup = str(tuple(LISTVARSUP)).replace(',', '')
44         else: 
45             strlistsup = str(tuple(LISTVARSUP))
46         if len(LISTNUMACTIVE) == 1:
47             strlistact = str(tuple(LISTNUMACTIVE)).replace(',', '')
48         else:
49             strlistact = str(tuple(LISTNUMACTIVE))
50         txt += """
51         source("%s")
52         """ % self.parent.RscriptsPath['Rgraph']
53         txt += """
54         lact<-c%s+1
55         """ % strlistact
56         txt += """
57         lsup=c%s+1
58         """ % strlistsup
59         txt += """
60         filename<-"%s"
61         """ % ffr(self.tempgraph)
62         #FIXME : faire une fonction pour le graph
63         txt += """
64         library(MASS)
65         dataact<-datadm[,lact]
66         act <- mca(dataact, abbrev = TRUE)
67         datasup<-datadm[,lsup]
68         sup <- predict(act, datasup, type="factor")
69         ftab<-cbind(dataact,datasup)
70         #ftab<-
71         #library(ca)
72         #debs<-ncol(dataact)+1
73         #fins<-ncol(dataact)+ncol(datasup)
74         #ftab.mjca<-mjca(ftab,supcol=c(debs:fins),nd=3)
75         open_file_graph(filename, width = 800, height = 800)
76         plot(act)
77         dev.off()
78         #plot(ftab.mjca)
79         #print(ftab.mjca)
80         """
81         tmpfile = tempfile.mktemp(dir=parent.TEMPDIR)
82         tmpscript = open(tmpfile, 'w')
83         tmpscript.write(txt)
84         tmpscript.close()
85         pid = exec_rcode(self.parent.RPath, tmpfile, wait = False)
86         while pid.poll() == None :
87             sleep(0.2)
88         check_Rresult(self.parent, pid)
89
90     def DoLayout(self):
91         #FIXME
92         txt = '<img src="%s" />' % self.tempgraph
93         return txt
94
95 #    def OnRGL(self, event):
96 #        self.parent.text_ctrl_1.write('runrgl\n')
97 #        RAFC3DRGL = os.path.join(self.PathFile, self.RAFC3DRGL)
98 #        RunRgl(RAFC3DRGL)
99
100
101 class DoAFCM():
102
103     def __init__(self, parent):
104         dlg = PrefQuestAlc(parent, sim = True)
105         #dlg = CHDDialog(parent, -1, u"AFCM", size=(350, 400), style=wx.DEFAULT_DIALOG_STYLE)
106         dlg.CenterOnParent()
107         self.val = dlg.ShowModal()
108         if self.val == wx.ID_OK:
109             LISTNUMACTIVE = dlg.nactives
110             LISTVARSUP = dlg.varsup
111             print(LISTNUMACTIVE)
112             print(LISTVARSUP)
113             afcm = AFCMQ(parent, LISTNUMACTIVE, LISTVARSUP)
114             txtgraph = afcm.DoLayout()
115             parent.newtab = wx.html.HtmlWindow(parent.nb, -1)
116             if "gtk2" in wx.PlatformInfo:
117                 parent.newtab.SetStandardFonts()
118             parent.newtab.SetPage(txtgraph)
119             parent.nb.AddPage(parent.newtab, "AFCM")
120             parent.nb.SetSelection(parent.nb.GetPageCount() - 1)
121             parent.ShowTab(wx.EVT_BUTTON)