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