def   analyze():
  patternDistance = PatternDistanceAnalyzer()
  START_AT_N_CONC = 5 
  slider = ConcursoExt()
  last_concurso = slider.get_last_concurso()
  repeats_dict = Dict2()
  for nDoConc in range(START_AT_N_CONC, last_concurso.nDoConc+1):
    concurso = slider.get_concurso_by_nDoConc(nDoConc)
    contrajogos_as_dezenas_list = last_concurso.get_contrajogos_as_dezenas_list_down_to_depth(depth=4, inclusive=True)
    if contrajogos_as_dezenas_list == None:
      continue
    #print concurso.nDoConc, concurso.date, concurso.get_dezenas(), contrajogos_as_dezenas_list 
    repeats_array = jogos_functions.get_array_n_repeats_with_m_previous_games(concurso.get_dezenas(), contrajogos_as_dezenas_list)
    token = ''.join(map(str, repeats_array))
    patternDistance.add_pattern(token)
    repeats_dict.add1_or_set1_to_key(token)
  tokens_and_quants = repeats_dict.items()
  tokens_and_quants.sort(key = lambda x : x[1])
  tokens_and_quants.reverse()
  for token_and_quant in tokens_and_quants:
    token = token_and_quant[0]
    quant = token_and_quant[1]
    pattern = rightFillToNSpaces(token, 3)
    zquant  = leftFillToNSpaces(str(quant), 3)
    print pattern, ':', zquant 
  patternDistance.mount_distances_histogram()
  patternDistance.summarize()
  analyze()
Exemplo n.º 2
0
class HistFreqUpdater(object):
  '''
  '''
  def __init__(self): # concurso_range
    self.slider = ConcursoExt()
    self.histfreqdbslider = HistFreqDBSlider()
    self.update_db_if_needed()
    self.verify_histfreqs_consistency()

  def update_db_if_needed(self):
    self.total_concursos = self.slider.get_total_concursos()
    self.last_n_histfreq_updated = self.histfreqdbslider.get_total_histfreqs()
    n_missing_histfreqs = self.total_concursos - self.last_n_histfreq_updated 
    if n_missing_histfreqs < 0:
      error_msg = 'Inconsistent histfreqs size. It is greater than concursos.  Program execution cannot continue.'
      raise IndexError, error_msg
    elif n_missing_histfreqs == 0:
      # nothing to do! Sizes match.
      return
    print 'Need to update %d hist-freq concursos (from %d to %d)' %(n_missing_histfreqs, self.last_n_histfreq_updated, self.total_concursos)
    self.update_histfreqs_from_last_updated()
       
  def update_histfreqs_from_last_updated(self):
    histfreqdbs_to_update = []
    if self.last_n_histfreq_updated == 0:
      numpy_histfreq = numpy.zeros(60,int)
    else:
      numpy_histfreq = self.histfreqdbslider.get_histfreq_at(self.last_n_histfreq_updated)
    for nDoConc in range(self.last_n_histfreq_updated + 1, self.total_concursos + 1):
      jogo = self.slider.get_concurso_by_nDoConc(nDoConc)
      for dezena in jogo.get_dezenas():
        index = dezena - 1 
        numpy_histfreq[index] += 1 
      histfreqdb = HistFreqDB()
      histfreqdb.set_charfreqs(numpy_histfreq, nDoConc)
      histfreqdbs_to_update.append(histfreqdb)
    self.histfreqdbslider.update_histfreqs(histfreqdbs_to_update)

  def verify_histfreqs_consistency(self):
    print 'About to verify histfreqs consistency of %d concursos : Please, wait. ' %(self.total_concursos)  
    numpy_histfreq = numpy.array([0]*60)
    for nDoConc in range(1, self.total_concursos + 1):
      jogo = self.slider.get_concurso_by_nDoConc(nDoConc)
      for dezena in jogo.get_dezenas():
        index = dezena - 1 
        numpy_histfreq[index] += 1
      self.compare_calc_histfreq_with_db(nDoConc, numpy_histfreq)
    print 'Verified histfreqs consistency of %d concursos : ok! ' %(self.total_concursos)  
    
  def compare_calc_histfreq_with_db(self, nDoConc, numpy_histfreq):
    histfreqdb = self.histfreqdbslider.get_histfreq_at(nDoConc)
    bool_array = histfreqdb == numpy_histfreq
    # unfortunately, comparison of numpy arrays should compare element by element
    # old implementation 
    # one_to_one = zip(histfreqdb, numpy_histfreq)
    # for pair in one_to_one:
      # if pair[0] != pair[1]:
    if False in bool_array:
      error_msg = ' at nDoConc=%d histfreqdb != numpy_histfreq \n db = %s \n calculated = %s' %(nDoConc, histfreqdb, numpy_histfreq)
      raise ValueError, error_msg
def generator_with_a_bet():
  '''

  The 4 most occurring repeats_array (processed on 2013-01-06)
  
  repeats_array : occurrence (from nDoConc=5 to 1556) 
            2   : 363
            1   : 300
            3   : 218
            21  : 132
  '''
  compare_repeat_lists = [[4], [3,1]]
  gerador = Gerador()
  slider = ConcursoExt() 
  last_concurso = slider.get_last_concurso()
  contrajogos = last_concurso.get_contrajogos_as_dezenas_list_down_to_depth(depth=4, inclusive=True)
  n_passed = 0; counter = 0; n_passed_for_each_compare_repeat_list = {0:0, 1:0} 
  for jogo_as_dezenas in gerador:
    counter += 1
#    if counter > 100:
#      break
    for compare_repeat_list in compare_repeat_lists:
      passed = filter_functions.filter_in_those_within_n_repeats_with_m_previous_games(jogo_as_dezenas, contrajogos, compare_repeat_list)
      if passed:
        n_passed += 1 
        n_passed_for_each_compare_repeat_list[compare_repeat_lists.index(compare_repeat_list)]+=1
        print n_passed, counter, 'Passed repeats_array', compare_repeat_list, str(jogo_as_dezenas) #, contrajogos, compare_repeat_list
        break
  print n_passed, counter, n_passed_for_each_compare_repeat_list 
Exemplo n.º 4
0
 def set_history_ini_fin_range(self, history_ini_fin_range=None):
   '''
   This method is private!!!  From the "outside", use set_concurso_and_range() 
   '''
   slider = ConcursoExt()
   HIST_RANGE_DEFAULT = (1, slider.get_n_last_concurso()) 
   self.history_ini_fin_range = volante_functions.return_int_range_or_default_or_raise_ValueError(history_ini_fin_range, HIST_RANGE_DEFAULT)
   self.redo_place_items_in_the_slots()
Exemplo n.º 5
0
 def set_nDoConc(self, nDoConc):
   slider = ConcursoExt()
   n_last_concurso = slider.get_n_last_concurso() # len(sl.getListAllConcursosObjs())      
   if nDoConc == None:
     nDoConc = n_last_concurso
   elif nDoConc < 1 or nDoConc > n_last_concurso:
     indexErrorMsg = 'passed in nDoConcurso=%d and range acceptable is 1 to %d' %(nDoConc, n_last_concurso)
     raise IndexError, indexErrorMsg 
   self.nDoConc = nDoConc
def adhoc_test1():
  slider = ConcursoExt()
  for nDoConc in range(1401, slider.get_n_last_concurso() + 1):
    concurso = slider.get_concurso_by_nDoConc(nDoConc)
    line = '%d ' %nDoConc
    concursotil = ConcursoTil(concurso)
    for n_slots in [2, 3, 4, 5, 6, 10, 12]: 
      concursotil.reset_n_slots(n_slots)
      line += 'tslot%d%s ' %(n_slots, concursotil.get_tilpattern_interlaced_with_n_dozens_per_til()) #wpattern
    print line  
Exemplo n.º 7
0
def adhoc_test():
  
  slider = ConcursoExt()
  # last_nDoConc = find_last_nDoConc()
  # backtrack_amount = 400
  # down_to_nDoConc = last_nDoConc - backtrack_amount
  # for nDoConc in range(last_nDoConc, down_to_nDoConc - 1, -1):
  concurso = slider.get_last_concurso()
  analyzer = Analyzer(concurso)
  analyzer.run()
  analyzer.report()
Exemplo n.º 8
0
def process():
  slider = ConcursoExt()
  concursos = slider.get_all_concursos(); soma_anterior = None
  for concurso in concursos:
    soma = sum(concurso.get_dezenas())
    if soma_anterior == None:
      soma_anterior = soma
      continue
    difference = soma - soma_anterior
    word = '(%d, %d, %d)' %(soma_anterior, soma, difference)
    if difference > 0: # ie soma > soma_anterior:
      prefix = 'up' 
    elif difference == 0: # ie soma == soma_anterior:
      prefix = '~' 
    else: # difference < 0: # ie  soma < soma_anterior:
      prefix = 'down'
    print '%s%s' %(prefix, word) ,
    soma_anterior = soma
def adhoc_test3():
  slider = ConcursoExt()
  last_concurso = slider.get_last_concurso()
  concursotil = ConcursoTil(last_concurso)
  print 'concursotil', concursotil, 'wpatt', concursotil.wpattern 
  # tilproducer = TilProducer(5, 6)
  tilfreqslotter = TilFreqSlotSeparator(5)
  # print tilproducer.alltilwpatterns 
  # patterns_as_intlist = tilproducer.get_alltilpatterns_as_intlist()
  # tilsets = tilfreqslotter.getTilSets()
  n_dozens_per_tilslot = tilfreqslotter.get_quantities_of_dozens_per_tilslot()
  print 'dezenas per tilslot:', n_dozens_per_tilslot 
  # tilhistogram = tilfreqslotter.get_tilhistogram()
  #print 'tilhistogram:', tilhistogram 
  # filtered_patterns_as_intlist, patterns_filtered_out = filter_out(patterns_as_intlist, tilhistogram)
  # print 'filtered_patterns_as_intlist', filtered_patterns_as_intlist, len(filtered_patterns_as_intlist)
  # print 'filtered out patterns', patterns_filtered_out, len(patterns_filtered_out)
  tilfreqslotter.show_slot_elements()
  print tilfreqslotter.show_tilhistogram_table()      
def adhoc_test2():
  slider = ConcursoExt()
  for nDoConc in range(1401, slider.get_n_last_concurso() + 1):
    concurso = slider.get_concurso_by_nDoConc(nDoConc)
    concursotil = ConcursoTil(concurso)
    concursotilr = TilR(n_slots = 5, concurso=concurso)

    nDoConc_anterior = nDoConc - 1
    print concurso.nDoConc, concurso.date, concurso.get_dezenas(), concurso.get_dezenas_in_orig_order() 
    print 'histfreq for conc', nDoConc_anterior, concursotil.get_histfreq_obj().get_histfreq_tuplelike_at(nDoConc_anterior)
    #print concursotil.get_dezenas_and_their_frequencies_for_concurso()
    for n_slots in [5, 6, 10]: 
      concursotil.reset_n_slots(n_slots)
      print 'dezenas, frequencies, tils:', concursotil.get_dezenas_their_frequencies_and_til_for_concurso()
      print 'concursotil', concursotil, 'wpatt', concursotil.wpattern 
      print 'BorderTupleOfTilSets', concursotil.getBorderTupleOfTilSets()
      tilfreqslotter = TilFreqSlotSeparator(n_slots)
      print tilfreqslotter.show_tilhistogram_table()
      print '-'*50      
    print '=*'*27
def report():
  slider = ConcursoExt()
  concurso = slider.get_last_concurso()
  aggregated_histogram = {}
  N_SORTEADAS = len(concurso.get_dezenas())
  N_SORTEADAS_MAIS_NENHUMA_COINCIDENCIA = N_SORTEADAS + 1
  for i in range(N_SORTEADAS_MAIS_NENHUMA_COINCIDENCIA): 
    aggregated_histogram[i] = []
  for i in range(10):
    print i, concurso.nDoConc, concurso.get_dezenas()
    coincides_histogram = jogos_fd.get_coincides_histogram_against_a_lookup_depth(concurso.get_dezenas(), up_to_nDoConc=concurso.nDoConc-1, LOOKUP_DEPTH=1000) 
    if coincides_histogram == None:
      continue 
    print coincides_histogram
    the_n_coincides = coincides_histogram.values()
    print the_n_coincides, sum(the_n_coincides)
    for i, coincide_quantity in enumerate(the_n_coincides):
      aggregated_histogram[i].append(coincide_quantity)
    concurso = concurso.get_previous()
  analyzer = AnalyzerOfCoincides(aggregated_histogram)
  analyzer.analyze()
Exemplo n.º 12
0
def run_history():
  slider = ConcursoExt()
  volante_caract = VolanteCharacteristics(n_dezenas_no_volante=60, n_dezenas_no_sorteio=6)
  tilfstats = TilStats(n_slots=5, soma=volante_caract.n_dezenas_no_sorteio)
  tilrstats = TilStats(n_slots=5, soma=volante_caract.n_dezenas_no_sorteio)
  tilfobj   = TilF(n_slots=5, history_ini_fin_range = None, volante_caract=volante_caract)
  tilrobj   = TilR(n_slots=5, history_ini_fin_range = None, volante_caract=volante_caract)
  for nDoConc in xrange(101, slider.get_n_last_concurso()+1):
    concurso = slider.get_concurso_by_nDoConc(nDoConc)
    # reuse tilrobj!
    tilfobj.set_history_ini_fin_range(history_ini_fin_range = (1, nDoConc-1))
    tilrobj.set_history_ini_fin_range(history_ini_fin_range = (1, nDoConc-1))
    f_pattern_as_list, f_wpattern = tilfobj.get_items_tilpattern_as_list_and_str(concurso.get_dezenas())
    r_pattern_as_list, r_wpattern = tilrobj.get_items_tilpattern_as_list_and_str(concurso.get_dezenas())
    tilfstats.add_pattern_as_list(f_pattern_as_list)
    tilrstats.add_pattern_as_list(r_pattern_as_list)
    print 'f', f_wpattern, 'r', r_wpattern, concurso.get_dezenas_str(), nDoConc 
  tilfstats.print_summary()
  print 'len(tilfstats)', len(tilfstats) 
  tilrstats.print_summary()
  print 'len(tilrstats)', len(tilrstats) 
Exemplo n.º 13
0
 def __init__(self): # concurso_range
   self.histfreqdbslider = HistFreqDBSlider()
   self.total_histfreqs  = self.histfreqdbslider.get_total_histfreqs()
   self.slider           = ConcursoExt()
   self.total_concursos  = self.slider.get_total_concursos()
   self.update_histfreq_if_needed()
Exemplo n.º 14
0
class HistFreq(object):
  '''
  A histfreq composes (ie, its attributes):
  --------------------
  + bottomconc = the lower concurso number in the range composing the frequency histogram
  + topconc = the upper concurso number in the range composing the frequency histogram
  + (private) dezenasfreqs = sorted list of each dezena's total occurrence (ie, its frequency) in the related range (bottomconc, topconc)  
  --------------------
  A histfreq does (ie, its methods):
  + mount_histfreq  
  '''
  def __init__(self): # concurso_range
    self.histfreqdbslider = HistFreqDBSlider()
    self.total_histfreqs  = self.histfreqdbslider.get_total_histfreqs()
    self.slider           = ConcursoExt()
    self.total_concursos  = self.slider.get_total_concursos()
    self.update_histfreq_if_needed()
    
  def reissueHistFreqUpdater(self):
    HistFreqUpdater()
    self.total_histfreqs  = self.histfreqdbslider.get_total_histfreqs()
        
  def update_histfreq_if_needed(self, secondTry=False):
    if self.total_histfreqs < self.total_concursos:
      self.reissueHistFreqUpdater()
      if not secondTry:
        return self.update_histfreq_if_needed(secondTry=True)
      else:
        error_msg = 'Failed to update histfreq array. It should have the same size (=%d) as "concursos (=%d)".' %(self.total_histfreqs, self.total_concursos)
        raise ValueError, error_msg

  def get_concurso_by_nDoConc(self, nDoConc):
    '''
    Just a "convenience" public method (not used internally by the class)
    '''
    return self.concursoSlider.get_concurso_by_nDoConc(nDoConc)

  def get_histfreq_at(self, nDoConc=None, secondTry=False):
    '''
    histfreq is a numpy array that contains frequencies of all dozens (ex., in Megasena it's a 60-number array)
    dozen 1 has its frequency at index 0, dozen 2, index 1, so on until dozen 60 at index 59
    '''
    if nDoConc == None:
      nDoConc = self.total_concursos # JogoSlider().get_total_jogos() # ie, number of last one
    elif nDoConc < 1 or nDoConc > self.total_concursos:
      error_msg = 'nº (=%d) do concurso fora da faixa válida (1, %d) ' %(nDoConc, self.total_concursos)
      raise ValueError, error_msg
    elif nDoConc > self.total_histfreqs:
      if secondTry:
        error_msg = 'There is a size different between total_jogo (=%d) and total_histfreqs (=%d) and nDoConc = %d. Program cannot continue until this is corrected.' %(self.total_jogos, self.total_histfreqs, nDoConc)
        raise ValueError, error_msg
      else:
        # this should be a very rare use case, ie, database has changed while this class is still instantiated and running :: try to update histfreqs so that total_histfreqs equals total_concursos
        self.reissueHistFreqUpdater()
        return self.get_concurso_by_nDoConc(nDoConc, secondTry=True)
    # it return as a numpy array object
    histfreq = self.histfreqdbslider.get_histfreq_at(nDoConc)
    if histfreq == None:
      error_msg = "histfreq for concurso %d was not found. It's missing in database." %nDoConc
      raise IndexError, error_msg
    return histfreq
    # index = nDoConc - 1
    # return self.allhistfreqs[index]

  def get_histfreq_tuplelike_at(self, nDoConc=None, secondTry=False):
    '''
    Same as get_histfreq_at(), the plus is that it zips range(1, 61) with histfreq
    '''
    total_dezenas = len(self.get_histfreq_at(nDoConc))
    dezenas = range(1, total_dezenas + 1)
    return zip(dezenas, self.get_histfreq_at(nDoConc))

  def get_freqstair_at(self, nDoConc=None):
    '''
    freqstair is an ordered array from minimum frequency (least occurred) to maximum frequency (most occurred) at nDoConc
    '''
    histstair = set ( self.get_histfreq_at(nDoConc) )
    histstair = list( histstair )
    histstair.sort()
    return histstair

  def get_freqstair_with_dozens_at(self, nDoConc=None):
    '''
    freqstair is an ordered array from minimum frequency (least occurred) to maximum frequency (most occurred) at nDoConc
    '''
    freqstair_with_dozens = []
    freqstair = self.get_freqstair_at(nDoConc)
    for freq in freqstair:
      subset_dezenas = []; offset = 0; histfreq = list( self.get_histfreq_at(nDoConc) ) 
      while 1:
        try:
          index = histfreq.index(freq)
          del histfreq[ : index + 1]
          dezena = offset + index + 1
          offset += index + 1
          subset_dezenas.append(dezena)
          # print 'dezena', dezena, 'freq', freq, freqstair
          # print histfreq, 'tam', len(histfreq) 
          if len(histfreq) == 0:
            break
        except ValueError:
          break
      freqstair_with_dozens.append( (freq, subset_dezenas) )
    return freqstair_with_dozens

  def get_histfreq_within_range(self, concurso_range=None):
    if concurso_range == None:
      return self.get_histfreq_at()
    bottomconc = concurso_range[0] 
    topconc    = concurso_range[1]
    if bottomconc == 1:
      return self.get_histfreq_at(topconc)
    return self.get_deltahistfreq(bottomconc, topconc)

  def get_deltahistfreq(self, bottomconc, topconc):
    if bottomconc == 1:
      return self.get_histfreq_at(topconc)
    bottomfreqhist = self.get_histfreq_at(bottomconc-1)
    topfreqhist    = self.get_histfreq_at(topconc)
    deltafreqhist  = topfreqhist - bottomfreqhist  # + self.numpy_ones
    # it return as a numpy array object 
    return deltafreqhist # no need to hardcopy this one, because it's computed here

  def get_histfreq_for_dezena_witin_range(self, dezena, concurso_range=None):
    histfreq = self.get_histfreq_within_range(concurso_range)
    if histfreq == None:
      return None
    if dezena < 1 or dezena > len(histfreq) + 1:
      error_msg = 'Dezena (=%d) fora da faixa válida (%d, %d)' %(dezena, 1, len(histfreq) + 1)
      raise ValueError, error_msg
    index = dezena - 1
    return histfreq[index]

  def __str__(self):
    return '<HistFreq self.total_histfreqs=%d>' %self.total_histfreqs
Exemplo n.º 15
0
 def __init__(self): # concurso_range
   self.slider = ConcursoExt()
   self.histfreqdbslider = HistFreqDBSlider()
   self.update_db_if_needed()
   self.verify_histfreqs_consistency()
Exemplo n.º 16
0
def get_tilrwpattern_of_game(dezenas, n_slots=5, up_to_concurso=None):
  slider = ConcursoExt()
  if up_to_concurso == None:
    up_to_concurso = slider.get_last_concurso()
  tilr_obj = TilR(n_slots, up_to_concurso) #, inclusive=False, concurso_range = None)
  return tilr_obj.get_tilrwpattern_of_game(dezenas)