899a0bb91565e6c6b1a395b7939f1895e9524916
[iramuteq] / openanalyse.py
1 #!/bin/env python
2 # -*- coding: utf-8 -*-
3 #Author: Pierre Ratinaud
4 #Copyright (c) 2008-2012, Pierre Ratinaud
5 #License: GNU/GPL
6
7 from chemins import ChdTxtPathOut, StatTxtPathOut, PathOut
8 from layout import OpenCHDS, dolexlayout, StatLayout, WordCloudLayout, OpenCorpus, SimiLayout, SimiMatLayout, ProtoLayout, MatLayout, FreqLayout, Chi2Layout
9 from corpus import Corpus, copycorpus
10 from tableau import Tableau
11 import os
12 #import shelve
13 #from tabsimi import DoSimi
14 from functions import DoConf, ReadDicoAsDico
15 from tableau import Tableau
16 import logging
17
18 log = logging.getLogger('iramuteq.openanalyse')
19
20 class OpenAnalyse():
21     def __init__(self, parent, parametres, Alceste=True, simifromprof = False):
22         log.info('OpenAnalyse')
23         self.parent = parent
24         if isinstance(parametres, dict) :
25             self.conf = DoConf(parametres['ira']).getoptions()
26             self.path = parametres['ira']
27         else :
28             self.conf = DoConf(parametres).getoptions()
29             self.path = parametres
30             self.conf = self.redopath(self.conf, parametres)
31         
32         if self.conf['type'] == 'corpus' :
33             corpus = self.opencorpus()
34         elif self.conf['type'] == 'matrix' :
35             matrix = self.openmatrix()
36         elif self.conf.get('corpus', False) in self.parent.history.corpus :
37             if self.conf['uuid'] in self.parent.history.analyses :
38                 intree  = True
39             else :
40                 intree = False
41             corpus = self.openanalyse()
42             
43             if self.conf.get('lem',1) :
44                 dolem = True
45             else :
46                 dolem = False
47             if self.conf.get('dictionary', False) :
48                 dico = ReadDicoAsDico(self.conf['dictionary'])
49                 corpus.make_lems_from_dict(dico, dolem = dolem)
50             else :
51                 corpus.make_lems(lem = dolem)
52             if not intree :
53                 self.parent.tree.AddAnalyse(self.conf, bold = True)
54             else :
55                 self.parent.tree.GiveFocus(uuid = self.conf['uuid'], bold = True)
56             self.doopen(corpus)
57         elif self.conf.get('matrix', False) in self.parent.history.ordermatrix :
58             corpus = None
59             matrix = Tableau(self.parent, parametres = self.parent.history.matrix[self.parent.history.ordermatrix[self.conf['matrix']]])
60             matrix.open()
61             #if isinstance(parametres, dict) :
62             #    tableau = Tableau(parent, parametres['ira'])
63             #else :
64             #    tableau = Tableau(parent, parametres)
65             #tableau.parametres = self.conf 
66             #tableau.dictpathout = PathOut(filename = tableau.parametres['filename'], dirout = self.conf['pathout'], analyse_type = self.conf['type'])
67             #tableau.dictpathout.basefiles(ChdTxtPathOut)
68             #tableau.read_tableau(tableau.dictpathout['db'])
69             #if self.parent.tree.IsInTree(uuid = self.conf['uuid']) :
70             self.parent.tree.GiveFocus(uuid = self.conf['uuid'], bold = True)
71             self.doopen(matrix)
72         else :
73             self.parent.tree.AddAnalyse(self.conf, bold = True)
74         self.parent.history.addtab(self.conf)
75     
76     def redopath(self, conf, path) :
77         conf['ira'] = os.path.realpath(path)
78         conf['pathout'] = os.path.dirname(os.path.realpath(path))
79         DoConf(conf['ira']).makeoptions([conf['type']], [conf])
80         return conf
81     
82     def opencorpus(self) :
83         log.info('open corpus')
84         if self.conf['uuid'] not in self.parent.history.corpus :
85             self.parent.history.add(self.conf) 
86             log.info('add corpus to history')
87             self.parent.tree.OnItemAppend(self.conf)
88         if self.conf['uuid'] in self.parent.history.openedcorpus :
89             log.info('corpus is already opened')
90             self.doopen(self.parent.history.openedcorpus[self.conf['uuid']])
91         else :
92             #dial = progressbar(2)
93             #dial.Update(1, 'Ouverture du corpus')
94             corpus = Corpus(self, parametres = self.conf, read = self.parent.history.history[self.parent.history.ordercorpus[self.conf['uuid']]]['ira'])
95             #dial.Update(2, 'Fini')
96             #dial.Destroy()
97             self.parent.history.openedcorpus[self.conf['uuid']] = corpus
98             self.opencorpus_analyses()
99             self.doopen(corpus)
100     
101     def openmatrix(self):
102         log.info('open matrix')
103         if self.conf['uuid'] not in self.parent.history.ordermatrix :
104             self.parent.history.addMatrix(self.conf)
105             log.info('add matrix to history')
106             self.parent.tree.OnItemAppend(self.conf)
107         if self.conf['uuid'] in self.parent.history.openedmatrix :
108             log.info('matrix is already opened')
109             self.doopen(self.parent.history.openedmatrix[self.conf['uuid']])
110         else :
111             #dial = progressbar(2)
112             #dial.Update(1, 'Ouverture du corpus')
113             matrix = Tableau(self, parametres = self.conf)
114             matrix.open()
115             self.parent.history.openedmatrix[self.conf['uuid']] = matrix
116             self.openmatrix_analyses()
117             self.doopen(matrix)        
118             self.parent.history.addtab(self.conf)
119
120     def opencorpus_analyses(self) :
121         log.info('open analysis')
122         basepath = self.conf['pathout']
123         analyses = []
124         for root, subfolders, files in os.walk(basepath) :
125             for folder in subfolders :
126                 if os.path.exists(os.path.join(folder, 'Analyse.ira')) :
127                     analyse_conf = DoConf(os.path.join(folder, 'Analyse.ira')).getoptions()
128                     analyse_conf = self.redopath(analyse_conf, os.path.join(folder, 'Analyse.ira'))
129                     if analyse_conf['corpus'] == self.conf['uuid'] :
130                         analyses.append(analyse_conf)
131         if len(analyses) :
132             self.parent.history.addmultiple(analyses)
133         for analyse in analyses :
134             self.parent.tree.AddAnalyse(analyse, bold = False)
135     
136     def openmatrix_analyses(self):
137         pass
138
139     def openanalyse(self) :
140         if self.conf['corpus'] in self.parent.history.openedcorpus :
141             log.info('corpus is already opened')
142             corpus = copycorpus(self.parent.history.openedcorpus[self.conf['corpus']])
143         else :
144             if os.path.exists(self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira']) :
145                 corpus = Corpus(self, parametres = DoConf(self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira']).getoptions('corpus'), read = self.parent.history.history[self.parent.history.ordercorpus[self.conf['corpus']]]['ira'])
146                 self.parent.history.openedcorpus[self.conf['corpus']] = corpus
147         self.parent.history.add(self.conf)
148         return corpus
149
150     def doopen(self, corpus) :
151         if self.conf['type'] == 'corpus' :
152             OpenCorpus(self.parent, self.conf) 
153         elif self.conf['type'] == 'stat' :
154             StatLayout(self.parent, corpus, self.conf)
155         elif self.conf['type'] == 'spec' :
156             dolexlayout(self.parent, corpus, self.conf)
157         elif self.conf['type'] == 'alceste' :
158             OpenCHDS(self.parent,  corpus, self.conf, Alceste = True)
159         elif self.conf['type'] == 'simitxt' or self.conf['type'] == 'clustersimitxt' :
160             SimiLayout(self.parent, corpus, self.conf)
161         elif self.conf['type'] == 'wordcloud' or self.conf['type'] == 'clustercloud':
162             WordCloudLayout(self.parent, corpus, self.conf)
163         elif self.conf['type'] == 'reinertmatrix' :
164             OpenCHDS(self.parent,  corpus, self.conf, Alceste = False)
165         elif self.conf['type'] == 'simimatrix' :
166             SimiMatLayout(self.parent, corpus, self.conf)
167         elif self.conf['type'] == 'proto' :
168             ProtoLayout(self.parent, corpus, self.conf)
169         elif self.conf['type'] == 'matrix' :
170             MatLayout(self.parent, corpus)
171         elif self.conf['type'] == 'freq' or self.conf['type'] == 'freqmulti':
172             FreqLayout(self.parent, corpus, self.conf)
173         elif self.conf['type'] == 'chi2' :
174             Chi2Layout(self.parent, corpus, self.conf)
175