python/methylnet/predictions.py
def make_new_predictions(test_pkl, model_pickle, batch_size, n_workers, interest_cols, categorical, cuda, categorical_encoder, output_dir):
	"""Run prediction model again to further assess outcome. Only evaluate prediction model."""
	os.makedirs(output_dir,exist_ok=True)
	test_methyl_array = MethylationArray.from_pickle(test_pkl) # generate results pickle to run through classification/regression report
	if cuda:
		model = torch.load(model_pickle)
		model.vae.cuda_on=True
	else:
		model = torch.load(model_pickle,map_location='cpu')
		model.vae.cuda_on=False
	if not categorical:
		test_methyl_array.remove_na_samples(interest_cols if len(interest_cols)>1 else interest_cols[0])
	if os.path.exists(categorical_encoder):
		categorical_encoder=pickle.load(open(categorical_encoder,'rb'))
	else:
		categorical_encoder=None
	test_methyl_dataset = get_methylation_dataset(test_methyl_array,interest_cols,categorical=categorical, predict=True, categorical_encoder=categorical_encoder)
	test_methyl_dataloader = DataLoader(
		dataset=test_methyl_dataset,
		num_workers=n_workers,
		batch_size=min(batch_size,len(test_methyl_dataset)),
		shuffle=False)
	vae_mlp=MLPFinetuneVAE(mlp_model=model,categorical=categorical,cuda=cuda)

	Y_pred, Y_true, latent_projection, _ = vae_mlp.predict(test_methyl_dataloader)

	results = dict(test={})
	results['test']['y_pred'], results['test']['y_true'] = copy.deepcopy(Y_pred), copy.deepcopy(Y_true)

	if categorical:
		Y_true=Y_true.argmax(axis=1)[:,np.newaxis]
		Y_pred=Y_pred.argmax(axis=1)[:,np.newaxis]
	test_methyl_array = test_methyl_dataset.to_methyl_array()

	Y_pred=pd.DataFrame(Y_pred.flatten() if (np.array(Y_pred.shape)==1).any() else Y_pred,index=test_methyl_array.beta.index,columns=(['y_pred'] if categorical else interest_cols))
	Y_true=pd.DataFrame(Y_true.flatten() if (np.array(Y_true.shape)==1).any() else Y_true,index=test_methyl_array.beta.index,columns=(['y_true'] if categorical else interest_cols))
	results_df = pd.concat([Y_pred,Y_true],axis=1) if categorical else pd.concat([Y_pred.rename(columns={name:name+'_pred' for name in list(Y_pred)}),Y_true.rename(columns={name:name+'_true' for name in list(Y_pred)})],axis=1)  # FIXME
	latent_projection=pd.DataFrame(latent_projection,index=test_methyl_array.beta.index)
	test_methyl_array.beta=latent_projection

	output_file = join(output_dir,'results.csv')
	results_file = join(output_dir,'results.p')
	output_file_latent = join(output_dir,'latent.csv')
	output_pkl = join(output_dir, 'vae_mlp_methyl_arr.pkl')

	test_methyl_array.write_pickle(output_pkl)
	pickle.dump(results,open(results_file,'wb'))
	latent_projection.to_csv(output_file_latent)
	results_df.to_csv(output_file)
Run prediction model again to further assess outcome. Only evaluate prediction model.
Run prediction model again to further assess outcome. Only evaluate prediction model.
Run prediction model again to further assess outcome . Only evaluate prediction model .
def make_new_predictions(test_pkl, model_pickle, batch_size, n_workers, interest_cols, categorical, cuda, categorical_encoder, output_dir):
	os.makedirs(output_dir,exist_ok=True)
	test_methyl_array = MethylationArray.from_pickle(test_pkl) 
	if cuda:
		model = torch.load(model_pickle)
		model.vae.cuda_on=True
	else:
		model = torch.load(model_pickle,map_location='cpu')
		model.vae.cuda_on=False
	if not categorical:
		test_methyl_array.remove_na_samples(interest_cols if len(interest_cols)>1 else interest_cols[0])
	if os.path.exists(categorical_encoder):
		categorical_encoder=pickle.load(open(categorical_encoder,'rb'))
	else:
		categorical_encoder=None
	test_methyl_dataset = get_methylation_dataset(test_methyl_array,interest_cols,categorical=categorical, predict=True, categorical_encoder=categorical_encoder)
	test_methyl_dataloader = DataLoader(
		dataset=test_methyl_dataset,
		num_workers=n_workers,
		batch_size=min(batch_size,len(test_methyl_dataset)),
		shuffle=False)
	vae_mlp=MLPFinetuneVAE(mlp_model=model,categorical=categorical,cuda=cuda)
	Y_pred, Y_true, latent_projection, _ = vae_mlp.predict(test_methyl_dataloader)
	results = dict(test={})
	results['test']['y_pred'], results['test']['y_true'] = copy.deepcopy(Y_pred), copy.deepcopy(Y_true)
	if categorical:
		Y_true=Y_true.argmax(axis=1)[:,np.newaxis]
		Y_pred=Y_pred.argmax(axis=1)[:,np.newaxis]
	test_methyl_array = test_methyl_dataset.to_methyl_array()
	Y_pred=pd.DataFrame(Y_pred.flatten() if (np.array(Y_pred.shape)==1).any() else Y_pred,index=test_methyl_array.beta.index,columns=(['y_pred'] if categorical else interest_cols))
	Y_true=pd.DataFrame(Y_true.flatten() if (np.array(Y_true.shape)==1).any() else Y_true,index=test_methyl_array.beta.index,columns=(['y_true'] if categorical else interest_cols))
	results_df = pd.concat([Y_pred,Y_true],axis=1) if categorical else pd.concat([Y_pred.rename(columns={name:name+'_pred' for name in list(Y_pred)}),Y_true.rename(columns={name:name+'_true' for name in list(Y_pred)})],axis=1)  
	latent_projection=pd.DataFrame(latent_projection,index=test_methyl_array.beta.index)
	test_methyl_array.beta=latent_projection
	output_file = join(output_dir,'results.csv')
	results_file = join(output_dir,'results.p')
	output_file_latent = join(output_dir,'latent.csv')
	output_pkl = join(output_dir, 'vae_mlp_methyl_arr.pkl')
	test_methyl_array.write_pickle(output_pkl)
	pickle.dump(results,open(results_file,'wb'))
	latent_projection.to_csv(output_file_latent)
	results_df.to_csv(output_file)
def make_new_predictions ( test_pkl , model_pickle , batch_size , n_workers , interest_cols , categorical , cuda , categorical_encoder , output_dir ) : os . makedirs ( output_dir , exist_ok = True ) test_methyl_array = MethylationArray . from_pickle ( test_pkl ) if cuda : model = torch . load ( model_pickle ) model . vae . cuda_on = True else : model = torch . load ( model_pickle , map_location = 'cpu' ) model . vae . cuda_on = False if not categorical : test_methyl_array . remove_na_samples ( interest_cols if len ( interest_cols ) > 1 else interest_cols [ 0 ] ) if os . path . exists ( categorical_encoder ) : categorical_encoder = pickle . load ( open ( categorical_encoder , 'rb' ) ) else : categorical_encoder = None test_methyl_dataset = get_methylation_dataset ( test_methyl_array , interest_cols , categorical = categorical , predict = True , categorical_encoder = categorical_encoder ) test_methyl_dataloader = DataLoader ( dataset = test_methyl_dataset , num_workers = n_workers , batch_size = min ( batch_size , len ( test_methyl_dataset ) ) , shuffle = False ) vae_mlp = MLPFinetuneVAE ( mlp_model = model , categorical = categorical , cuda = cuda ) Y_pred , Y_true , latent_projection , _ = vae_mlp . predict ( test_methyl_dataloader ) results = dict ( test = { } ) results [ 'test' ] [ 'y_pred' ] , results [ 'test' ] [ 'y_true' ] = copy . deepcopy ( Y_pred ) , copy . deepcopy ( Y_true ) if categorical : Y_true = Y_true . argmax ( axis = 1 ) [ : , np . newaxis ] Y_pred = Y_pred . argmax ( axis = 1 ) [ : , np . newaxis ] test_methyl_array = test_methyl_dataset . to_methyl_array ( ) Y_pred = pd . DataFrame ( Y_pred . flatten ( ) if ( np . array ( Y_pred . shape ) == 1 ) . any ( ) else Y_pred , index = test_methyl_array . beta . index , columns = ( [ 'y_pred' ] if categorical else interest_cols ) ) Y_true = pd . DataFrame ( Y_true . flatten ( ) if ( np . array ( Y_true . shape ) == 1 ) . any ( ) else Y_true , index = test_methyl_array . beta . index , columns = ( [ 'y_true' ] if categorical else interest_cols ) ) results_df = pd . concat ( [ Y_pred , Y_true ] , axis = 1 ) if categorical else pd . concat ( [ Y_pred . rename ( columns = { name : name + '_pred' for name in list ( Y_pred ) } ) , Y_true . rename ( columns = { name : name + '_true' for name in list ( Y_pred ) } ) ] , axis = 1 ) latent_projection = pd . DataFrame ( latent_projection , index = test_methyl_array . beta . index ) test_methyl_array . beta = latent_projection output_file = join ( output_dir , 'results.csv' ) results_file = join ( output_dir , 'results.p' ) output_file_latent = join ( output_dir , 'latent.csv' ) output_pkl = join ( output_dir , 'vae_mlp_methyl_arr.pkl' ) test_methyl_array . write_pickle ( output_pkl ) pickle . dump ( results , open ( results_file , 'wb' ) ) latent_projection . to_csv ( output_file_latent ) results_df . to_csv ( output_file )
Run prediction model again to further assess outcome.
Run prediction model again to further assess outcome .
"""Run prediction model again to further assess outcome. Only evaluate prediction model.""" # generate results pickle to run through classification/regression report # FIXME
test_pkl
model_pickle
batch_size
n_workers
interest_cols
categorical
cuda
categorical_encoder
output_dir