Package medite :: Package MediteAppli :: Module complexite
[hide private]
[frames] | no frames]

Source Code for Module medite.MediteAppli.complexite

  1  # -*- coding: iso-8859-1 -*- 
  2  # Copyright 20003 - 2008: Julien Bourdaillet (julien.bourdaillet@lip6.fr), Jean-Gabriel Ganascia (jean-gabriel.ganascia@lip6.fr) 
  3  # This file is part of MEDITE. 
  4  # 
  5  #    MEDITE is free software; you can redistribute it and/or modify 
  6  #    it under the terms of the GNU General Public License as published by 
  7  #    the Free Software Foundation; either version 2 of the License, or 
  8  #    (at your option) any later version. 
  9  # 
 10  #    MEDITE is distributed in the hope that it will be useful, 
 11  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  #    GNU General Public License for more details. 
 14  # 
 15  #    You should have received a copy of the GNU General Public License 
 16  #    along with Foobar; if not, write to the Free Software 
 17  #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 18   
 19  import logging, os, os.path, string, sys 
 20  import numpy.oldnumeric as Numeric 
 21  import numpy.numarray as numarray 
 22  import numpy.numarray.ma as ma 
 23   
 24   
25 -def _readFile(name):
26 """ Lit un fichier dans le dossier courant et renvoie une chaine """ 27 path=os.path.join(os.getcwd(),"MediteAppli","test",name) 28 f = open(path) 29 res = f.read() 30 f.close() 31 return res
32 33
34 -def test():
35 comite = _readFile('comite.txt') 36 papers = _readFile('papers.txt') 37 lcom = comite.splitlines() 38 logging.debug('# comite='+str(len(lcom))) 39 sepTable = string.maketrans("""\r\n\t"""," ") 40 i = 0 41 for c in lcom: 42 liste_nom = c.split() 43 nom = liste_nom[-1] 44 pos = papers.find(nom) 45 if pos != -1: 46 p = papers[pos-30:pos+70] 47 p = p.translate(sepTable) 48 logging.debug(c+' / '+ p +'\n') 49 i += 1 50 logging.debug('# comite member with paper='+str(i))
51
52 -def test2():
53 nb =1.0/(11) 54 i=0 55 while i < 10000 and nb > 0: 56 nb1 = 60.0*nb 57 ar = int(nb1) 58 nb = nb1-ar 59 assert 0 <= ar <60 60 logging.debug('nb1=%f / ar=%f / nb=%f',nb1,ar,nb) 61 i+=1 62 logging.debug('i='+str(i))
63
64 -def test3():
65 import numarray 66 a = numarray.zeros(5,numarray.Int8) 67 for i in range(len(a)): 68 a[i] = i 69 b = numarray.ones(5,numarray.Int8) 70 c = numarray.zeros(5,numarray.Int8) 71 print a 72 print b 73 print a-b 74 print b-a 75 print numarray.minimum(a,b) 76 taille = len(a) 77 numarray.logical_and(b-a,c, a) 78 print a 79 #for pos in xrange(taille): 80 81 a = numarray.array([1,2,3,4]) 82 print numarray.sum(a)
83
84 -def test4():
85 import numarray 86 pos = numarray.array([1,5,9,1]) 87 x = numarray.array([0,0,0,0,0,0,0,0,0,0]) 88 numarray.put(x, pos, [1,1,1,1]) 89 print x 90 def d(test,i): 91 return where(test==i,1,0) 92 def d2(test): 93 d(test) 94 #a=numarray.fromfunction(d2,(10,)) 95 #pos2 = numarray.resize(pos,(4,10)) 96 b = numarray.zeros(10) 97 a = numarray.zeros((4,10)) 98 print a 99 for i in xrange(4-1,-1,-1): 100 pos2 = pos[i] 101 c = numarray.where(numarray.arange(10)==pos2,1,0) 102 numarray.add(b,c,b) 103 a[i] = b 104 print a 105
106 -def test5():
107 ma = Numeric.array([[1,2,3],[4,5,6]]) 108 for x in ma: print x
109
110 -def _readFile(name):
111 """ Lit un fichier dans le dossier courant et renvoie une chaine """ 112 path=os.path.join(os.getcwd(),"MediteAppli","test", "textes",name) 113 f = open(path) 114 res = f.read() 115 f.close() 116 return res
117
118 -def test6():
119 a1 = [-1,1,2,3,10,5] 120 b1 = [-1,10,3,2,1,6] # ; b1.extend([-1]*2) 121 #b1.extend([-1] * 3) 122 a = numarray.array(a1, type = 'Float32') 123 b = numarray.array(b1)#,6])#, type = 'Float32') 124 125 #c = numarray.where(((a / b) > 0.5) or ((b / a) < 0.4),a,0) 126 c = numarray.where((a / b > 0.5) , a, 0) 127 c2 = numarray.where((a / b < 2) , b, 0) 128 c3 = numarray.logical_and(c,c2) #c>0 c2 >0,c,0) 129 print c,c2, c3 130 #c4 = numarray.ma.array(c,mask=c3) 131 c4 = numarray.where(c3,a,0) 132 c5 = numarray.where(c3,b,0) 133 c6 = numarray.logical_and(c4,c5) 134 print c4, c5, c6 135 #d = numarray.zeros(5)#, type = 'Float32') 136 #for i in xrange(len(a)): 137 ## print i,a[i] / b[i], b[i] / a[i] 138 # if a[i] / b[i] > 0.5: d[i] = a[i] 139 #print d 140 import alignement 141 #from test.test_data import TestDataFactory 142 #f = TestDataFactory() 143 #td = f.getTestData(test_data.ClaudeBernard,"2") 144 align = alignement.AlignAstarRecur2(_readFile('CB_experience.txt')+_readFile("CB_Texte_de_1857.txt")) 145 c7,c8 = align.extractRemplacements(a1,b1) 146 #print numarray.nonzero(c7),numarray.nonzero(c7)[0][1] 147 c9 = numarray.array([0,0,0]) 148 #print numarray.nonzero(c9),numarray.nonzero(c9)[0][1] 149 print c7,c8 150 print numarray.argmax(a1) 151 print numarray.array() 152 print len([])
153
154 -def test7():
155 a = ['aa','bb'] 156 b = ['aa','bb'] 157 print a == b
158
159 -def test8():
160 a = [ 100,80,75,74,70,60,50,30,22,20,15,10,2] 161 b = [2,5,10,12,13,19,20,30,50] 162 import bisect 163 print bisect.bisect_left(a,11) 164 print bisect.bisect_right(b,2)
165 166
167 -def test9():
168 import math 169 print math.log(0L)
170
171 -def fact(x):
172 if x == 0: 173 return 1 174 else: 175 return x * fact(x-1)
176
177 -def fact2(n):
178 return reduce(lambda x,y:x*y,range(1,n+1))
179 180 F ={}
181 -def fact3(n):
182 n1 = n 183 try: 184 return F[n] 185 except KeyError: 186 pass 187 f = 1 188 while (n > 0): 189 f = f * n 190 n = n - 1 191 F[n1] = f 192 return f
193
194 -def test10():
195 import math 196 #f = lambda(n: n* f(n-1)) 197 n = 7 ; m = 7 198 t1 = math.pow(2,n) * math.pow(2,m) 199 t2 = 0 200 for i in xrange(1,m+1): 201 #print 'i',i 202 for j in xrange(1,i+1): 203 #print 'j',j 204 new_t2 = (fact3(i)/fact3(i-j)) * math.pow(3,j) 205 t2 += new_t2 206 print 'i',i,new_t2 207 print t1,t2,t1+t2
208
209 -def test11():
210 import math 211 n = 7 ; m = 7 212 f_n = fact3(n) ; f_m = fact3(m) 213 t = 0 214 for i in xrange(1,m): 215 t2 = 0 216 for j in xrange(1,n): 217 c2 = f_n / (fact3(j) * fact3(n-j)) 218 #c2 = fact3(i) / (fact3(j) * fact3(i-j)) 219 new_t2 = 1#(fact3(i)/fact3(i-j)) #* math.pow(3,j) 220 t2 += c2 * new_t2 221 print 'i:',i,' / c2:',c2,' / new_t2:',new_t2,' / c2*new_t2:',c2*new_t2,' / t2:',t2 222 c1 = f_m / (fact3(i) * fact3(m-i)) 223 t += c1 * t2 224 print 'i:',i,' / c1:',c1,' / t2:',t2,' / c1*t2:',c1*t2 225 print 't:',t
226
227 -def comb(n,k):
228 assert n >= k 229 if k < 0: k = 0 230 return fact3(n) / (fact3(n-k) * fact3(k))
231
232 -def arang(n,k):
233 assert n >= k 234 return fact3(n) / fact3(n-k)
235
236 -def test12():
237 import math 238 n = 7 ; m = 7 239 f_n = fact3(n-1) ; f_m = fact3(m-1) 240 t = 0 241 for i in xrange(1,m): 242 t2 = 0 243 for j in xrange(1,n): 244 c2 = comb(m-1,j-1) #f_n / (fact3(j-1) * fact3(n-1-(j-1))) 245 #c2 = fact3(i) / (fact3(j) * fact3(i-j)) 246 #if i >= j: 247 new_t2 = (fact3(i)/fact3(i-j)) #* math.pow(3,j) 248 #else: new_t2 = (fact3(j)/fact3(j-i)) 249 c1 = comb(n-1,i-1) #f_m / (fact3(i-1) * fact3(m-1-(i-1))) 250 t2 += c2 * new_t2 *c1 251 print 'i:',i,' / c2:',c2,' / new_t2:',new_t2,' / c2*new_t2:',c2*new_t2,' / t2:',t2 252 253 t += t2 254 print 'i:',i,' / c1:',c1,' / t2:',t2,' / c1*t2:',c1*t2 255 print 't:',t
256
257 -def test13():
258 import math 259 n = 3 ; m = 3 260 f_n = fact3(n-1) ; f_m = fact3(m-1) 261 t = 0 262 for i in xrange(1,m): 263 #t2 = 0 264 for j in xrange(1,n): 265 c2 = comb(m-1,j-1) #f_n / (fact3(j-1) * fact3(n-1-(j-1))) 266 #c2 = fact3(i) / (fact3(j) * fact3(i-j)) 267 #if i >= j: 268 #new_t2 = (fact3(i)/fact3(i-j)) #* math.pow(3,j) 269 #else: new_t2 = (fact3(j)/fact3(j-i)) 270 c1 = comb(n-1,i-1) #f_m / (fact3(i-1) * fact3(m-1-(i-1))) 271 new_t2 = 0 ; x = min(i,j) 272 for k in xrange(x+1): 273 new_t2 += math.pow(comb(x,k),2) #* math.pow(3,k) 274 t += c1 * c2 * new_t2 275 print 'i:',i,' / c2:',c2,' / new_t2:',new_t2#,' / c2*new_t2:',c2*new_t2,' / t2:',t2 276 277 #t += t2 278 print 'i:',i,' / c1:',c1#,' / t2:',t2,' / c1*t2:',c1*t2 279 print 't:',t
280 281 282 l = [2, [2, 5, [3, 3, 1], [0, 0], 0], [5, 5, [28, 28,1], [0, 0], 1]] 283
284 -def aplatir(s,x):
285 #print 's=',s,' / x=',x 286 try: 287 reduce(aplatir,x,s) 288 except: 289 #print 'exception' 290 s.append(x) 291 #print s 292 return s
293 import math 294 #print aplatir([], l) 295 296 import cPickle 297 fichier_cp = os.path.join(os.getcwd(),'cp.pkl') 298 if False and os.path.isfile(fichier_cp): 299 pkl_file = open(fichier_cp, 'rb') 300 CP = cPickle.load(pkl_file) 301 pkl_file.close() 302 else: 303 CP = {} 304
305 -def choose_pos(nb_choix,taille_seq):
306 assert nb_choix <= taille_seq 307 try: 308 return CP[(nb_choix,taille_seq)] 309 except KeyError: 310 pass 311 liste_nuplet = [] 312 for i in xrange(taille_seq,0,-1): 313 if nb_choix == 1: 314 liste_nuplet.append([i]) 315 else: 316 l2 = choose_pos(nb_choix-1,taille_seq-1) 317 #print l2 318 #if len(l2) == 0: continue 319 l3 = map(lambda elt: aplatir([],[i,elt]), l2) 320 #print l3 321 #j = 1 ; maxi = sum(l3[0]) ; l4 = [l3[0]] 322 #while j < len(l3): 323 # if sum(l3[j]) < maxi and len(set(l3[j]))==len(l3[j]): 324 # l4.append(l3[j]) 325 # maxi = sum(l3[j]) 326 # j += 1 327 328 for x in l3: 329 assert len(x) == nb_choix, (x,nb_choix) 330 liste_nuplet.extend(l3) 331 332 l4 = filtre2(liste_nuplet, taille_seq, nb_choix) 333 334 if not CP.has_key((nb_choix,taille_seq)): 335 logging.debug( 'BU CP[' +str((nb_choix,taille_seq))+ ']') 336 CP[(nb_choix,taille_seq)] = l4 337 output = open(fichier_cp, 'wb') 338 cPickle.dump(CP, output) 339 output.close() 340 return l4
341
342 -def filtre2(liste_nuplet, taille_seq, nb_choix):
343 l4=[] 344 for x in liste_nuplet: 345 previous = taille_seq+1 ; OK = True ; totalEcart = maxEcart = 0 346 for pos in x: 347 #if x == [6, 5, 3, 1]: print previous,pos,abs(previous-pos)> 3,previous <= pos 348 ecart = abs(previous-pos) 349 totalEcart += ecart-1 ; maxEcart = max(maxEcart,ecart-1) 350 if (ecart > 2) or (previous <= pos): 351 OK = False 352 #break 353 previous = pos 354 #if x == [6, 5, 3, 1]: print previous,0,abs(previous-0)> 3,previous <= 0 355 ecart = abs(previous-0) 356 totalEcart += ecart-1 ; maxEcart = max(maxEcart,ecart-1) 357 if (ecart > 2) or (previous <= 0): 358 OK = False 359 if OK: 360 l4.append(x) 361 assert totalEcart <= 2*nb_choix+1,(totalEcart, 2*nb_choix+1, taille_seq, nb_choix,x) 362 #else: 363 #assert totalEcart > 2*nb_choix+1 or maxEcart > 1,(totalEcart, 2*nb_choix+1, maxEcart, taille_seq, nb_choix,x) 364 #sys.stdout.flush() 365 return l4
366
367 -def filtre1(liste_nuplet, taille_seq, nb_choix):
368 l4 = [] 369 for x in liste_nuplet: 370 #print x 371 i = 0 ; totalEcarts = 0 372 sorted = True ; following = True ; ecartOK = True ; ecartExtremes = True 373 for i in xrange(0,len(x)-1): 374 if x[i] <= x[i+1]: 375 sorted = False 376 break 377 ecart = abs(x[i]-x[i+1]) 378 totalEcarts += ecart 379 if ecart >= 3: 380 following = False 381 break 382 if sorted and following: 383 totalEcarts += abs(taille_seq-x[0]) + abs(1-x[-1]) 384 if totalEcarts > nb_choix+1: 385 ecartOK = False 386 #print x,taille_seq,x[0],abs(taille_seq-x[0]),'/',1,x[-1],abs(1-x[-1]) 387 if (abs(taille_seq-x[0])>=2) or (abs(1-x[-1])>=2): 388 ecartExtremes = False 389 # print 'ecartExtremes = False' 390 #else: print 'ecartExtremes = True' 391 if ecartOK and ecartExtremes: 392 l4.append(x) 393 #else: print 'not S F' 394 #print 'l4',l4 395 return l4
396
397 -def test14(m = 2, n = 3):
398 t = 0 399 for k in xrange(1,m): 400 t += comb(n,k) * math.pow(2,k) 401 print 't140:',math.pow(2,n-1)*math.pow(2,m-1)*t 402 print 't141:',math.pow(2,n-1)*math.pow(2,m-1)*comb(n-1,m-1) * math.pow(2,min(n-1,m-1)) 403 print 't142:',math.pow(2,n-1)*math.pow(2,m-1)*comb(n-1,m-1) * math.pow(2,min(n-1,m-1)) 404 print 't143:',math.pow(2,n)*math.pow(2,m)*comb(n,m) * math.pow(2,min(n,m)) 405 t2 = t3 = 0 406 for i in xrange(1,m+1): 407 #print 'i:',i 408 #for j in xrange(1,min(n+1,2*i+1)): 409 for j in xrange(1,n+1): 410 #if j > 2*i+1: 411 #print 'i=',i,'/ j=',j 412 # continue 413 c1 = comb(m-1,i-1) 414 c2 = comb(n-1,j-1) 415 new_t2 = 0 416 x = min(i,j) ; y = max(i,j) 417 418 new_t2 = 0 ; new_t3 = 0 419 for k in xrange(1,x+1): 420 new_t2 += comb(y,k) * arang(x,k) * math.pow(2,k) 421 new_t3 += arang(y,k) * comb(x,k) * math.pow(3,k) 422 #print x,y,k,new_t2 423 new_tt2 = c1 * c2 * new_t2 424 new_tt3 = c1 * c2 * new_t3 425 t2 += new_tt2 426 t3 += new_tt3 427 #print ' j:',j,' / c1:',c1,' / c2:',c2,' / new_t2:',new_t2,' / new_t',new_t 428 #print 'i:',i,' / c1:',c1#,' / t2:',t2,' / c1*t2:',c1*t2 429 logging.debug('t14 t2:'+str(t2)) 430 logging.debug('t14 t3:'+str(t3)) 431 print 't14 t2:',t2 432 print 't14 t3:',t3 433 sys.stdout.flush()
434
435 -def test15(m = 2, n = 3):
436 t2 = t3 = t4 = 0 437 for i in xrange(1,m+1): 438 z = min(n+1,2*i+2) ; z2 = z-1 439 #z = n+1 ; z2 = z-1 440 for j in xrange(1,z): 441 assert j <= 2*i+1 442 #if j > 2*i+1: 443 #print 'i=',i,'/ j=',j 444 # continue 445 c1 = comb(m-1,i-1) 446 c2 = comb(z2-1,j-1) 447 new_t2 = 0 448 x = min(i,j) ; y = max(i,j) 449 450 new_t2 = 0 ; new_t3 = 0 ; new_t4 = 0 451 b_k = int(math.floor(y / 2.0)) 452 for k in xrange(b_k,x+1): 453 new_t2 += comb(y,k) * arang(x,k) * math.pow(2,k) 454 new_t3 += arang(y,k) * comb(x,k) * math.pow(2,k) 455 #new_t2 = len(cp) #* math.pow(2,x) 456 #new_t2 = comb(y,x) 457 new_tt2 = c1 * c2 * new_t2 458 new_tt3 = c1 * c2 * new_t3 459 new_tt4 = c1 * c2 * comb(y,x) * arang(x,x) * math.pow(2,k) 460 t2 += new_tt2 461 t3 += new_tt3 462 t4 += new_tt4 463 #print ' j:',j,' / c1:',c1,' / c2:',c2,' / new_t2:',new_t2,' / new_t',new_t 464 #print 'i:',i,' / c1:',c1#,' / t2:',t2,' / c1*t2:',c1*t2 465 logging.debug('t15 t2:'+str(t2)) 466 logging.debug('t15 t3:'+str(t3)) 467 print 't15 t2:',t2 468 print 't15 t3:',t3 469 print 't15 t4:',t4
470 471 import bisect
472 -def test16():
473 #A = "ce.matin.le.chat.observa.de.petits.oiseaux.dans.les.arbres." 474 #B = "le.chat.etait.en.train.d.observer.des.oiseaux.dans.les.petits.arbres.ce.matin.il.observa.les.oiseaux.pendant.deux.heures." 475 A = "Ce matin le chat observa de petits oiseaux dans les arbres." 476 B = "Le chat était en train d'observer des oiseaux dans les petits arbres ce matin. Il observa les oiseaux pendant deux heures." 477 #print A 478 #print B 479 dicoRepet = {} #; dicoRepet2 = {} 480 dicoRepet = _extractRepet(A, B, dicoRepet) 481 dicoRepet = _extractRepet(B, A, dicoRepet) 482 483 dicoRepet = _filterDico(dicoRepet, A, B)#, dicoRepet2) 484 485 #print dicoRepet 486 i = 1 ; cumul = 0 487 while i <= len(dicoRepet): 488 cumul += _afficheDic(dicoRepet,i) 489 print '---------------------------------------' 490 i += 1 491 print 'total = ',cumul
492
493 -def _filterDico(dicoRepet, A, B):#1, dicoRepet2):
494 nouveauDicoRet = {} 495 for longueur, dico_chaine in dicoRepet.iteritems(): 496 nouveauDicoRet[longueur] = {} 497 for chaine,(liste_pos1,liste_pos2) in dico_chaine.iteritems(): 498 l1 = [] ; l2 = [] 499 for i in liste_pos1: 500 if chaine == A[i:i+longueur]: 501 l1.append(i) 502 for j in liste_pos2: 503 if chaine == B[j:j+longueur]: 504 l2.append(j) 505 assert len(l1) > 0 and len(l2) > 0 506 nouveauDicoRet[longueur][chaine] = l1, l2 507 return nouveauDicoRet 508
509 -def _afficheDic(dicoRepet, longueur):
510 cumul = 0 511 liste_alpha = [] 512 for cle,item in dicoRepet[longueur].iteritems(): 513 #print cle ; sys.stdout.flush() 514 bisect.insort_right(liste_alpha, cle) 515 a = liste_alpha 516 liste_alpha.sort() 517 assert a == liste_alpha 518 for chaine in liste_alpha: 519 liste_pos1, liste_pos2 = dicoRepet[longueur][chaine] 520 print longueur,' / ',chaine,' / ',liste_pos1,len(liste_pos1),' / ',liste_pos2,len(liste_pos2) 521 cumul += len(liste_pos1) + len(liste_pos2) 522 print longueur,' / ',cumul 523 return cumul
524
525 -def _extractRepet(sequence1, sequence2, dicoRepet):
526 527 for longueur in xrange(1,len(sequence1)): 528 i = 0 529 while i < len(sequence1) - longueur + 1: 530 chaine1 = sequence1[i:i+longueur] 531 #print chaine1 532 if longueur <= len(sequence2): 533 j = 0 534 while j < len(sequence2) - longueur +1: 535 chaine2 = sequence2[j:j+longueur] 536 if chaine1 == chaine2: 537 if not dicoRepet.has_key(longueur): 538 dicoRepet[longueur] = {} 539 if not dicoRepet[longueur].has_key(chaine1): 540 dicoRepet[longueur][chaine1] = [],[] 541 liste_pos1, liste_pos2 = dicoRepet[longueur][chaine1] 542 if i not in liste_pos1: 543 bisect.insort_right(liste_pos1, i) 544 if j not in liste_pos2: 545 bisect.insort_right(liste_pos2, j) 546 dicoRepet[longueur][chaine1] = liste_pos1, liste_pos2 547 j += 1 548 i += 1 549 return dicoRepet
550 551 552 if __name__ == '__main__': 553 logging.basicConfig(level=logging.DEBUG,#INFO, 554 format='%(asctime)s %(levelname)s %(message)s', 555 #datefmt='%H:%M:%S', 556 filename=os.path.join(os.getcwd(),'log_temp.txt'), 557 filemode='w') 558 console = logging.StreamHandler() 559 console.setLevel(logging.INFO) 560 #test4() 561 #print ord('a'),ord('A'),ord('z'),ord('Z') 562 #print [4]*3 563 #test10() 564 #test11() 565 #test12() 566 #test13() 567 m = 10; n = 100 568 #test14(m,n) 569 #test15(m,n) 570 #print (math.floor(5 / 2.0)) 571 test16() 572