def test_run(): ###### Test updating F, G, S in that order # Test whether a single iteration gives the correct behaviour, updating F, G, S in that order R = numpy.array([[1,2],[3,4]],dtype='f') M = numpy.array([[1,1],[0,1]]) K = 3 L = 1 F = numpy.array([[1,2,3],[4,5,6]],dtype='f') S = numpy.array([[7],[8],[9]],dtype='f') G = numpy.array([[10],[11]],dtype='f') #FSG = numpy.array([[500,550],[1220,1342]],dtype='f') #FS = numpy.array([[50],[122]],dtype='f') #SG = numpy.array([[70,77],[80,88],[90,99]],dtype='f') nmtf = NMTF(R,M,K,L) # Check we get an Exception if W, H are undefined with pytest.raises(AssertionError) as error: nmtf.run(0) assert str(error.value) == "F, S and G have not been initialised - please run NMTF.initialise() first." nmtf.F = numpy.copy(F) nmtf.S = numpy.copy(S) nmtf.G = numpy.copy(G) nmtf.run(1)
def test_run(): ###### Test updating F, G, S in that order # Test whether a single iteration gives the correct behaviour, updating F, G, S in that order R = numpy.array([[1, 2], [3, 4]], dtype='f') M = numpy.array([[1, 1], [0, 1]]) K = 3 L = 1 F = numpy.array([[1, 2, 3], [4, 5, 6]], dtype='f') S = numpy.array([[7], [8], [9]], dtype='f') G = numpy.array([[10], [11]], dtype='f') #FSG = numpy.array([[500,550],[1220,1342]],dtype='f') #FS = numpy.array([[50],[122]],dtype='f') #SG = numpy.array([[70,77],[80,88],[90,99]],dtype='f') nmtf = NMTF(R, M, K, L) # Check we get an Exception if W, H are undefined with pytest.raises(AssertionError) as error: nmtf.run(0) assert str( error.value ) == "F, S and G have not been initialised - please run NMTF.initialise() first." nmtf.F = numpy.copy(F) nmtf.S = numpy.copy(S) nmtf.G = numpy.copy(G) nmtf.run(1)
sys.path.append(project_location) from BNMTF.code.nmtf_np import NMTF from BNMTF.drug_sensitivity.experiments_gdsc.load_data import load_gdsc import matplotlib.pyplot as plt ########## standardised = False #standardised Sanger or unstandardised iterations = 1000 I, J, K, L = 622,138,5, 5 init_S = 'exponential' init_FG = 'kmeans' expo_prior = 1/10. # Load in data (_,X_min,M,_,_,_,_) = load_gdsc(standardised=standardised) # Run the algorithm nmtf = NMTF(X_min,M,K,L) nmtf.initialise(init_S,init_FG,expo_prior) nmtf.run(iterations) # Print the performances across iterations (MSE) print "all_performances = %s" % nmtf.all_performances['MSE'] # Plot the performances (MSE) plt.plot(nmtf.all_performances['MSE'])
# We now run the VB algorithm on each of the M's for each fraction. all_performances = {metric:[] for metric in metrics} average_performances = {metric:[] for metric in metrics} # averaged over repeats for (fraction,Ms,Ms_test) in zip(fractions_unknown,all_Ms,all_Ms_test): print "Trying fraction %s." % fraction # Run the algorithm <repeats> times and store all the performances for metric in metrics: all_performances[metric].append([]) for (repeat,M,M_test) in zip(range(0,repeats),Ms,Ms_test): print "Repeat %s of fraction %s." % (repeat+1, fraction) # Run the VB algorithm nmtf = NMTF(R,M,K,L) nmtf.initialise(init_S,init_FG) nmtf.run(iterations) # Measure the performances performances = nmtf.predict(M_test) for metric in metrics: # Add this metric's performance to the list of <repeat> performances for this fraction all_performances[metric][-1].append(performances[metric]) # Compute the average across attempts for metric in metrics: average_performances[metric].append(sum(all_performances[metric][-1])/repeats) print "repeats=%s \nfractions_unknown = %s \nall_performances = %s \naverage_performances = %s" % \ (repeats,fractions_unknown,all_performances,average_performances)