Advertisement
shihab2196

heatmap_species_BASE_OIL

Jan 25th, 2024
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Author: Shihab Ahmed
  4. Created on Fri Oct 27 11:19:49 2023
  5. """
  6. ############################################
  7. ##
  8. #   Heatmap of most abundance species
  9. #   Abundance based on mean number of species
  10. #   at the last 100 ps.
  11. #   Basically, it show only the stable products not intermediates
  12. #   Using species file
  13. ##
  14. ############################################
  15.  
  16.  
  17.  
  18. import magnolia.bondfile_parser as bfp
  19. import sys
  20. import matplotlib.pyplot as plt
  21. from matplotlib.colors import LogNorm
  22. import random
  23. import pandas as pd
  24. import magnolia.speciesfile_parser as sfp
  25. import seaborn as sns
  26. import numpy as np
  27.  
  28. baseoil = 'PAO4'
  29.  
  30. savedir = r'C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Post Processing\lammps_processing\python_outputs\heatmap_of_species'
  31.  
  32. commmon = r"C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Base_Oil\{}\25_{}_200_O2_Soria\Production\1600".format(baseoil,baseoil)
  33.  
  34. flag = True
  35. cutoff = 0.45
  36. for sim in ['\\Sim-1','\\Sim-2','\\Sim-3']:
  37.     directory = commmon+sim
  38.     speciesfile_path = directory+f'\\species_{cutoff}.out'
  39.     if cutoff == 0.30: speciesfile_path = directory+'\\species.out'
  40.     current = sfp.get_species_count(speciesfile_path)
  41.     if flag:
  42.         output_species = current
  43.         flag = False
  44.     else:
  45.         output_species = output_species.add(current,fill_value=0)
  46.  
  47. #%%
  48. # User Input
  49. number_of_main = 25  
  50. timestep = 0.25
  51. skipts   = (1600-300)/4
  52. nspecies = 15
  53. tailps   = 10  #ps
  54. exclude  = ['O2','H62C30']
  55. n_xticks = 4  # how many xtick division in x axis i.e. 1+n_xticks ticklabels
  56. details  = ('\n\nThis heatmap is generated using species.out file.\nNumber of '
  57.             'species are average over three simulations.\n'
  58.             'Species are sorted by mean species count of last {} ps.\n'
  59.             'First {} species are showing here.\n'.format(tailps, nspecies))
  60. title    = commmon[commmon.find('Base'):]+ details+f"(cutoff: {cutoff})\n"
  61.  
  62. ## plot parameter
  63. label_fontsize  = 18
  64. tick_fontsize   = 14
  65.  
  66.  
  67.  
  68. species = output_species.copy()/3 # copy is important
  69. species.replace(0,1e-20,inplace=True)
  70. species.columns = species.columns*timestep/1000-skipts
  71. species    = species.loc[:,0:] # skip the ramping steps
  72. species    = species.drop(exclude)
  73.  
  74. ## mean of last {tailps} ps
  75. means          = species.loc[:,species.columns.max()-tailps:].mean(axis=1)
  76. sorted_index   = means.sort_values(ascending=False).index.tolist()
  77. species        = species.loc[sorted_index]
  78.  
  79. species       = species.head(nspecies)
  80. species.index = bfp.make_molecular_formula_latex(species.index,sort=True)
  81.  
  82. xticks_freq = int(species.columns.size/n_xticks)
  83. norm = None#LogNorm(vmin=1, vmax=60)
  84.  
  85. plt.style.use('default')
  86. fig, ax = plt.subplots(figsize=[8,6])
  87.  
  88. heatmap = sns.heatmap(species,cmap='jet',xticklabels=xticks_freq,
  89.                  ax=ax,norm=norm,vmax=50)
  90.  
  91. # make the xticks integers
  92. xticklabels = ax.get_xticklabels()
  93. xticks = np.array([float(label.get_text()) for label in xticklabels])
  94. ax.set_xticklabels(xticks.astype(int))
  95. ax.tick_params(axis='both', labelsize=tick_fontsize)
  96.  
  97. ## Color Bar
  98. cbar = heatmap.collections[0].colorbar  # Get the colorbar object
  99. cbar.set_label('Number of species', fontsize=label_fontsize)  # Set the label and fontsize
  100. # Customize other colorbar properties if needed
  101. cbar.ax.tick_params(labelsize=tick_fontsize)  # Set tick label size
  102.  
  103. ax.set_xlabel('Time (ps)',fontsize=label_fontsize)
  104. plt.tick_params(left=False)
  105. plt.title(title,fontsize=6)
  106. plt.yticks(rotation=0)
  107. plt.savefig(savedir+'\\hm_species_{}'.format(random.randint(0,9999999999)),dpi=500, bbox_inches='tight')
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement