def test_initialisation(): I, J = 2, 3 R = numpy.ones((I, J)) M = numpy.ones((I, J)) K = 4 L = 5 # Init FG ones, S random init_FG = 'ones' init_S = 'random' nmtf = NMTF(R, M, K, L) nmtf.initialise(init_S, init_FG) assert numpy.array_equal(numpy.ones((I, K)), nmtf.F) assert numpy.array_equal(numpy.ones((J, L)), nmtf.G) for (k, l) in itertools.product(range(0, K), range(0, L)): assert nmtf.S[k, l] > 0 and nmtf.S[k, l] < 1 # Init FG random, S ones init_FG = 'random' init_S = 'ones' nmtf = NMTF(R, M, K, L) nmtf.initialise(init_S, init_FG) assert numpy.array_equal(numpy.ones((K, L)), nmtf.S) for (i, k) in itertools.product(range(0, I), range(0, K)): assert nmtf.F[i, k] > 0 and nmtf.F[i, k] < 1 for (j, l) in itertools.product(range(0, J), range(0, L)): assert nmtf.G[j, k] > 0 and nmtf.G[j, k] < 1 # Init FG kmeans, S exponential init_FG = 'kmeans' init_S = 'exponential' nmtf = NMTF(R, M, K, L) nmtf.initialise(init_S, init_FG) for (i, k) in itertools.product(range(0, I), range(0, K)): assert nmtf.F[i, k] == 0.2 or nmtf.F[i, k] == 1.2 for (j, l) in itertools.product(range(0, J), range(0, L)): assert nmtf.G[j, k] == 0.2 or nmtf.G[j, k] == 1.2 for (k, l) in itertools.product(range(0, K), range(0, L)): assert nmtf.S[k, l] > 0
def test_initialisation(): I,J = 2,3 R = numpy.ones((I,J)) M = numpy.ones((I,J)) K = 4 L = 5 # Init FG ones, S random init_FG = 'ones' init_S = 'random' nmtf = NMTF(R,M,K,L) nmtf.initialise(init_S,init_FG) assert numpy.array_equal(numpy.ones((I,K)),nmtf.F) assert numpy.array_equal(numpy.ones((J,L)),nmtf.G) for (k,l) in itertools.product(range(0,K),range(0,L)): assert nmtf.S[k,l] > 0 and nmtf.S[k,l] < 1 # Init FG random, S ones init_FG = 'random' init_S = 'ones' nmtf = NMTF(R,M,K,L) nmtf.initialise(init_S,init_FG) assert numpy.array_equal(numpy.ones((K,L)),nmtf.S) for (i,k) in itertools.product(range(0,I),range(0,K)): assert nmtf.F[i,k] > 0 and nmtf.F[i,k] < 1 for (j,l) in itertools.product(range(0,J),range(0,L)): assert nmtf.G[j,k] > 0 and nmtf.G[j,k] < 1 # Init FG kmeans, S exponential init_FG = 'kmeans' init_S = 'exponential' nmtf = NMTF(R,M,K,L) nmtf.initialise(init_S,init_FG) for (i,k) in itertools.product(range(0,I),range(0,K)): assert nmtf.F[i,k] == 0.2 or nmtf.F[i,k] == 1.2 for (j,l) in itertools.product(range(0,J),range(0,L)): assert nmtf.G[j,k] == 0.2 or nmtf.G[j,k] == 1.2 for (k,l) in itertools.product(range(0,K),range(0,L)): assert nmtf.S[k,l] > 0
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)
# Load in data (_,R,M,_,_,_,_) = load_Sanger(standardised=standardised) # Run the VB algorithm, <repeats> times times_repeats = [] performances_repeats = [] for i in range(0,repeats): # Set all the seeds numpy.random.seed(3) # Run the classifier nmtf = NMTF(R,M,K,L) nmtf.initialise(init_S,init_FG,expo_prior) nmtf.run(iterations) # Extract the performances and timestamps across all iterations times_repeats.append(nmtf.all_times) performances_repeats.append(nmtf.all_performances) # Check whether seed worked: all performances should be the same assert all(numpy.array_equal(performances, performances_repeats[0]) for performances in performances_repeats), \ "Seed went wrong - performances not the same across repeats!" # Print out the performances, and the average times all_times_average = list(numpy.average(times_repeats, axis=0)) all_performances = performances_repeats[0] print "np_all_times_average = %s" % all_times_average print "np_all_performances = %s" % all_performances