def test_calculate_self_confusion_vector(self):
     n_items = 6
     report, image_sets = rate_fp.get_docs(n_items=n_items)
     report = rate_fp.calculate_self_confusion_vector(
         image_sets,
         fingerprint_function=fingerprint_core.fp,
         weights=np.ones(constants.fingerprint_length),
         distance_function=NNSearch.distance_1_k,
         distance_power=0.5,
         report=report,
         parallelize=False)
     avg = report['average_weighted']
     print('self item avg without parallelization:' + str(avg))
     self.assertTrue(avg > 0 and avg < 100)  # this 100 is arbirary...
     report = rate_fp.calculate_self_confusion_vector(
         image_sets,
         fingerprint_function=fingerprint_core.fp,
         weights=np.ones(constants.fingerprint_length),
         distance_function=NNSearch.distance_1_k,
         distance_power=0.5,
         report=report,
         parallelize=True)
     avg2 = report['average_weighted']
     print('self item avg with parallelization:' + str(avg2))
     self.assertTrue(avg == avg2)  # this 100 is arbirary...
 def test_get_docs(self):
     n_items = 5
     report, docs = rate_fp.get_docs(n_items=n_items)
     l = len(docs)
     print('n:' + str(l) + ' docs:' + str(docs))
     print('report:' + str(report))
     self.assertTrue(l == n_items)
Esempio n. 3
0
def optimize_weights(weights=np.ones(fingerprint_length),k=0.5):
    '''
    optimizes weights given everything else constant (fingerprint function, distance function, distance power k)
    :param k: distance power - k=0.5 is for euclidean distance
    :return:optimal weights vector (or at least best found so far)
    '''
    print('fp length:'+str(fingerprint_length))
    print('weights:'+str(weights))

#    x_min = scipy.optimize.minimize(f,initial_weights,args=(k),tol=0.1)
    f = rate_wrapper
    init=weights
# TO DO CONSTRAINED MINIMIZATION USE COBYLA  or SQLSQP - see docs - currentyly constraining 'by hand'
    # x_min = scipy.optimize.minimize(f,init,args=(k,),tol=0.1)   #in case u need only one optional argument this is how to do it

    pr = cProfile.Profile()
    pr.enable()



    self_report, image_sets = rate_fp.get_docs(n_docs)
    comparisons_to_make = rate_fp.make_cross_comparison_sets(image_sets)
    print('k:' + str(k) + 'len imsets:' + str(len(image_sets)) + 'selfrep:' + str(self_report) )

    name = 'report.' + datetime.datetime.now().strftime("%Y-%m-%d.%H%M")
    outfilename = os.path.join('fp_ratings', name)
    print(name)

    x_min = scipy.optimize.minimize(f, init, args=(k, image_sets, self_report, comparisons_to_make, outfilename),
                                    tol=0.05,
                                    options={'maxiter': 50, 'disp': True})

    pr.disable()
    s = StringIO.StringIO()
    sortby = 'cumulative'
    ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
    ps.print_stats()
    print(s.getvalue())


#    x_min = scipy.optimize.fmin(f,[2,3])
    print('output of optimize:'+str(x_min))
    print('xvals:'+str(x_min.x))
    print('f('+str(x_min.x)+')='+str(f(x_min.x)))
 def test_make_cross_comparison_sets(self):
     n_items = 4
     report, image_sets = rate_fp.get_docs(n_items=n_items)
     sets = rate_fp.make_cross_comparison_sets(image_sets)
     print(sets)
     self.assertTrue(len(sets) == n_items)