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