def get_image(noise_level, method, data_dict, eval_data): data_list = data_dict[noise_level][method] adv_path = random.choice(data_list) if method == 'div': # print('adv_path', adv_path) # raise Exception star_file = load_star(adv_path) with mrcfile.open( cleanStarPath( adv_path, star_file['external_reconstruct_general'] ['rlnExtReconsDataReal'])) as mrc: data_real = mrc.data with mrcfile.open( cleanStarPath( adv_path, star_file['external_reconstruct_general'] ['rlnExtReconsDataImag'])) as mrc: data_im = mrc.data with mrcfile.open( cleanStarPath( adv_path, star_file['external_reconstruct_general'] ['rlnExtReconsWeight'])) as mrc: kernel = mrc.data adv = np.divide(data_real + 1j * data_im, kernel + 1e-3) adv = irfft(adv) else: with mrcfile.open(adv_path) as mrc: adv = mrc.data with mrcfile.open(locate_gt(adv_path, noise_level, eval_data=eval_data)) as mrc: gt = mrc.data # print(locate_gt(adv_path, eval_data=eval_data)) # print(star_file) # raise Exception return gt, adv
print('TIK_REG' + TIK_REG) #print('-------') #print('Regularization'+str(ADVERSARIAL_REGULARIZATION)) #print('-------') REGULARIZATION_TY = TIK_REG # ADVERSARIAL_REGULARIZATION = 0.0075 #SAVES_PATH = '/local/scratch/public/sl767/SPA/Saves/SimDataPaper/' #SAVES_PATH += 'Adversarial_Regulariser/Cutoff_20/Roto-Translation_Augmentation' path = sys.argv assert len(path) == 2 file = load_star(path[1]) iteration='' l = path[1].split('_') for det in l: if det[0:2]=='it': iteration = det[2:5] print('Iteration: {}'.format(iteration)) #print('Regularization: '+str(ADVERSARIAL_REGULARIZATION)) with mrcfile.open(file['external_reconstruct_general']['rlnExtReconsDataReal']) as mrc: data_real = mrc.data with mrcfile.open(file['external_reconstruct_general']['rlnExtReconsDataImag']) as mrc: data_im = mrc.data
import os import subprocess as sp default_ext_reco_prog = '/home/sl767/bin/relion-devel-lmb/src/apps/external_reconstruct.cpp' def runCommand(cmd_string): sp.call(cmd_string.split(' ')) SAVES_PATH = '/local/scratch/public/sl767/SPA/Saves/Denoiser/All_EM' assert len(sys.argv) == 2 star_path = sys.argv[1] star_file = load_star(star_path) iteration = '' l = star_path.split('_') for det in l: if det[0:2] == 'it': iteration = det[2:5] print('Iteration: {}'.format(iteration)) target_path = star_file['external_reconstruct_general']['rlnExtReconsResult'] print('Classical Relion M-step...') os.environ["RELION_EXTERNAL_RECONSTRUCT_EXECUTABLE"] = "" print('Should be empty: ' + os.environ["RELION_EXTERNAL_RECONSTRUCT_EXECUTABLE"])
def continueEM(x, PDB_ID, em_iter_start, em_iter_finish=None, GPU_ids=''): print('This function needs an update') raise Exception if em_iter_finish == None: em_iter_finish = em_iter_start + 1 x = x.squeeze() base_path = '/local/scratch/public/sl767/MRC_Data/Data_002_10k' runCommand('rm -r {BP}/OneStepEM/Input/{PDB_ID}'.format(BP=base_path, PDB_ID=PDB_ID)) runCommand('rm -r {BP}/OneStepEM/Output/{PDB_ID}'.format(BP=base_path, PDB_ID=PDB_ID)) runCommand('rm -r {BP}/OneStepEM/FSC/{PDB_ID}'.format(BP=base_path, PDB_ID=PDB_ID)) runCommand('mkdir -p {BP}/OneStepEM/Input/{PDB_ID}'.format(BP=base_path, PDB_ID=PDB_ID)) runCommand('mkdir -p {BP}/OneStepEM/Output/{PDB_ID}'.format(BP=base_path, PDB_ID=PDB_ID)) runCommand('mkdir -p {BP}/OneStepEM/FSC/{PDB_ID}'.format(BP=base_path, PDB_ID=PDB_ID)) input_mrc_path = '{BP}/OneStepEM/Input/{PDB_ID}/{PDB_ID}_input.mrc'.format( BP=base_path, PDB_ID=PDB_ID) output_mrc_path = '{BP}/OneStepEM/Output/{PDB_ID}/{PDB_ID}_output.mrc'.format( BP=base_path, PDB_ID=PDB_ID) projs_star_path = '{BP}/projs/{PDB_ID}/{PDB_ID}_mult002.star'.format( BP=base_path, PDB_ID=PDB_ID) # Use this if data has been genereated using --split_random_halves input_star_path = '{BP}/EM/{PDB_ID}/{PDB_ID}_mult002_it{EIS}_optimiser.star'.format( BP=base_path, PDB_ID=PDB_ID, EIS=startingZeros(em_iter_start)) training_path = '/local/scratch/public/sl767/MRC_Data/org/training' if PDB_ID[0] == '9': training_path = '/local/scratch/public/sl767/MRC_Data/org/eval' fsc_path = '{BP}/OneStepEM/FSC/{PDB_ID}/{PDB_ID}_fsc.star'.format( BP=base_path, PDB_ID=PDB_ID) with mrcfile.new(input_mrc_path, overwrite=True) as mrc: mrc.set_data(x) mrc.voxel_size = 1.5 ### FSC fsc_cmd = 'relion_image_handler --i {IMP}' fsc_cmd += ' --fsc {TP}/{PDB_ID_first}/{PDB_ID}.mrc' # fsc_cmd += ' --o {FP}' # Problems with encoding if we write to .star here. Write instead during sp.call() fsc_cmd = fsc_cmd.format(IMP=input_mrc_path, TP=training_path, PDB_ID=PDB_ID, PDB_ID_first=PDB_ID[0], FP=fsc_path) runCommand(fsc_cmd, fsc_path) fsc_star_file = load_star(fsc_path) fsc = fsc_star_file['fsc']['rlnFourierShellCorrelation'] ### EM refine_cmd = 'mpirun -n 3 relion_refine_mpi' refine_cmd += ' --continue {ISP}' refine_cmd += ' --o {OMP}' refine_cmd += ' --auto_refine --split_random_halves' refine_cmd += ' --pad 1' refine_cmd += ' --particle_diameter 150 --flatten_solvent --zero_mask --oversampling 1' refine_cmd += ' --healpix_order 2 --offset_range 5' refine_cmd += ' --auto_local_healpix_order 4' refine_cmd += ' --offset_step 2 --sym C1' refine_cmd += ' --low_resol_join_halves 40' refine_cmd += ' --norm --scale' refine_cmd += ' --gpu "{GPU_ids}"' refine_cmd += ' --external_reconstruct' # --maximum_angular_sampling 1.8' refine_cmd += ' --j 6' # Number of threads to run in parallel (only useful on multi-core machines) refine_cmd += ' --pool 30' # Number of images to pool for each thread task refine_cmd += ' --dont_combine_weights_via_disc' # Send the large arrays of summed weights through the MPI network, # instead of writing large files to disc refine_cmd += ' --auto_iter_max {EIF}' # change this so that it's not hard coded refine_cmd = refine_cmd.format(OMP=output_mrc_path[:-4], GPU_ids=GPU_ids, ISP=input_star_path, EIF=em_iter_finish) runCommand(refine_cmd) ### Accuracy of rotations aux_star_path = '{BP}/OneStepEM/Output/{PDB_ID}/{PDB_ID}_output_it{EIS_plus_1}_half1_model.star' aux_star_path = aux_star_path.format( BP=base_path, PDB_ID=PDB_ID, EIS_plus_1=startingZeros(em_iter_start + 1)) # aux_star_path = '{BP}/OneStepEM/Output/{PDB_ID}/{PDB_ID}_output_it001_model.star'.format(BP=base_path, PDB_ID=PDB_ID) aux_star_file = load_star(aux_star_path) acc_rot = aux_star_file['model_classes']['rlnAccuracyRotations'] return fsc, acc_rot, output_mrc_path[: -4] + '_it{EIF}_half1_class001.mrc'.format( EIF=startingZeros(em_iter_finish))
### testing the up and downsampling methods test_im = np.random.normal(size=(TARGET_NUM_VOX, TARGET_NUM_VOX, TARGET_NUM_VOX // 2 + 1)) test_up = upsample(test_im) test_down = downsample(test_up) assert l2(test_down - test_im) == 0.0 print('Rescaling methods validated') # In[10]: ITERATE = '001' TIKHONOV_REGULARIZATION = 2e2 path = '/local/scratch/public/sl767/MRC_Data/RealLifeAR/A2A_VPP/ClassicalRelionWithExtReco/run_it{}_half2_class001_external_reconstruct.star'.format( ITERATE) file = load_star(path) with mrcfile.open( file['external_reconstruct_general']['rlnExtReconsDataReal']) as mrc: data_real = mrc.data.copy() with mrcfile.open( file['external_reconstruct_general']['rlnExtReconsDataImag']) as mrc: data_im = mrc.data.copy() with mrcfile.open( file['external_reconstruct_general']['rlnExtReconsWeight']) as mrc: kernel = mrc.data.copy() complex_data = data_real + 1j * data_im #### Rescaling kernels complex_data_norm = np.mean(irfft(complex_data, scaling=NUM_VOX**2))