|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Thu Nov 4 18:19:28 2021 |
| 4 | +
|
| 5 | +@author: Ani Chattaraj |
| 6 | +""" |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import json |
| 12 | +from numpy import array |
| 13 | + |
| 14 | +font = {'family' : 'Arial', |
| 15 | + 'size' : 16} |
| 16 | +plt.rc('font', **font) |
| 17 | + |
| 18 | + |
| 19 | +def getColumns(txtfile): |
| 20 | + # name of observables in gdat file |
| 21 | + with open(txtfile,'r') as tf: |
| 22 | + lines = tf.readlines() |
| 23 | + columns = lines[0].replace('#','').split() |
| 24 | + return columns |
| 25 | + |
| 26 | + |
| 27 | +def plotTimeCourse(path, obsList=[]): |
| 28 | + # plotting the observable time course |
| 29 | + txtfile = path + '/pyStat/Mean_Observable_Counts.txt' |
| 30 | + mean_data = np.loadtxt(path + '/pyStat/Mean_Observable_Counts.txt') |
| 31 | + std_data = np.loadtxt(path + '/pyStat/Stdev_Observable_Counts.txt') |
| 32 | + |
| 33 | + _, numVar = mean_data.shape |
| 34 | + colNames = getColumns(txtfile) |
| 35 | + if len(obsList) == 0: |
| 36 | + for i in range(1, numVar): |
| 37 | + x, y, yerr = mean_data[:,0], mean_data[:,int(i)], std_data[:,int(i)] |
| 38 | + plt.plot(x,y, label=f'{colNames[i]}') |
| 39 | + plt.fill_between(x, y-yerr, y+yerr, alpha=0.2) |
| 40 | + else: |
| 41 | + for i in obsList: |
| 42 | + x, y, yerr = mean_data[:,0], mean_data[:,int(i)], std_data[:,int(i)] |
| 43 | + plt.plot(x,y, label=f'{colNames[i]}') |
| 44 | + plt.fill_between(x, y-yerr, y+yerr, alpha=0.2) |
| 45 | + |
| 46 | + plt.legend() |
| 47 | + plt.xlabel('Time (seconds)') |
| 48 | + plt.ylabel('Observable Counts') |
| 49 | + plt.show() |
| 50 | + |
| 51 | +def plotClusterDist(path, sizeRange=[]): |
| 52 | + # plotting the cluster size distribution (ACO: average cluster occupancy) |
| 53 | + plt.subplots(figsize=(7,4)) |
| 54 | + df = pd.read_csv(path + '/pyStat/SteadyState_distribution.csv') |
| 55 | + cs, foTM = df['Cluster size'], df['foTM'] |
| 56 | + |
| 57 | + if len(sizeRange) == 0: |
| 58 | + aco = sum(cs*foTM) |
| 59 | + plt.bar(cs, height=foTM, fc='grey',ec='k', label=f'ACO = {aco:.2f}') |
| 60 | + plt.axvline(aco, ls='dashed', lw=1.5, color='k') |
| 61 | + plt.xlabel('Cluster Size (molecules)') |
| 62 | + plt.ylabel('Fraction of total molecules') |
| 63 | + plt.legend() |
| 64 | + plt.show() |
| 65 | + else: |
| 66 | + # sizeRange = [1,10,20] |
| 67 | + # clusters : 1-10, 10-20, >20 |
| 68 | + idList = [0] |
| 69 | + #xbar = np.arange(1, len(sizeRange)+1, 1) |
| 70 | + xLab = [f'{sizeRange[i]} - {sizeRange[i+1]}' for i in range(len(sizeRange) - 1)] |
| 71 | + xLab.append(f'> {sizeRange[-1]}') |
| 72 | + |
| 73 | + for size in sizeRange[1:]: |
| 74 | + i = 0 |
| 75 | + while cs[i] < size: |
| 76 | + i += 1 |
| 77 | + if cs[i] == size: |
| 78 | + idList.append(i+1) |
| 79 | + else: |
| 80 | + idList.append(i) |
| 81 | + |
| 82 | + |
| 83 | + foTM_binned = [sum(foTM[idList[i]: idList[i+1]]) for i in range(len(idList)-1)] |
| 84 | + foTM_binned.append(sum(foTM[idList[-1]:])) |
| 85 | + |
| 86 | + try: |
| 87 | + plt.bar(xLab, foTM_binned, color='grey', ec='k') |
| 88 | + plt.xlabel('Cluster size range (molecules)') |
| 89 | + plt.ylabel('Fraction of total molecules') |
| 90 | + plt.ylim(0,1) |
| 91 | + plt.show() |
| 92 | + except: |
| 93 | + print('Invalid size range!! Maximal size range might be higher than largest cluster!') |
| 94 | + |
| 95 | + |
| 96 | +def plotBondsPerMolecule(path): |
| 97 | + # plotting the bond count distribution per molecule |
| 98 | + df = pd.read_csv(path + '/pyStat/Bonds_per_single_molecule.csv') |
| 99 | + fig, ax = plt.subplots(figsize=(7,4)) |
| 100 | + bonds, freq = df['BondCounts'], df['frequency'] |
| 101 | + m_bf = sum(bonds*freq) |
| 102 | + ax.bar(bonds, freq, width=0.3, color='b') |
| 103 | + ax.axvline(m_bf, ls='dashed', c='k', lw=2, label=f'Mean = {m_bf:.2f}') |
| 104 | + plt.legend() |
| 105 | + ax.set_xlabel('Bonds per molecule') |
| 106 | + ax.set_ylabel('Frequency') |
| 107 | + plt.show() |
| 108 | + |
| 109 | + |
| 110 | +def plotBondCounts(path, molecules=[]): |
| 111 | + if len(molecules) > 0: |
| 112 | + for mol in molecules: |
| 113 | + df = pd.read_csv(path + f'/pyStat/{mol}_bonds_per_molecule.csv') |
| 114 | + plt.bar(df['BondCounts'], df['frequency'], width=0.3, color='b') |
| 115 | + plt.xlabel('Bonds per molecule') |
| 116 | + plt.ylabel('Frequency') |
| 117 | + plt.title(mol) |
| 118 | + plt.ylim(0,1) |
| 119 | + plt.show() |
| 120 | + else: |
| 121 | + print('Please pass on the molecular names!') |
| 122 | + |
| 123 | +def plotBoundFraction(path): |
| 124 | + #df = pd.read_csv(path + '/pyStat/Cluster_composition.csv') |
| 125 | + jdict = json.load(open(path + '/pyStat/BoundFraction.json')) |
| 126 | + csList, bfList, freqList = [], [], [] |
| 127 | + |
| 128 | + for cs, bf in jdict.items(): |
| 129 | + for item, freq in bf.items(): |
| 130 | + csList.append(float(cs)) |
| 131 | + bfList.append(float(item)) |
| 132 | + freqList.append(float(freq)) |
| 133 | + |
| 134 | + plt.subplots(figsize=(7,4)) |
| 135 | + cm = plt.cm.get_cmap('rainbow') |
| 136 | + sc = plt.scatter(csList, bfList, c = freqList, cmap=cm) |
| 137 | + cbar = plt.colorbar(sc) |
| 138 | + cbar.ax.set_ylabel('Frequency') |
| 139 | + plt.xlabel('Cluster size (molecules)') |
| 140 | + plt.ylabel('Bound fraction') |
| 141 | + plt.show() |
| 142 | + |
| 143 | +def plotBarGraph(xdata, yList, yLabels, title='', width=0.1, alpha=0.5): |
| 144 | + N_entry = len(yList) |
| 145 | + midVarId = N_entry//2 |
| 146 | + if N_entry % 2 == 1: |
| 147 | + # odd number |
| 148 | + plt.bar(xdata, yList[midVarId], width=width, alpha=alpha, label=yLabels[midVarId]) |
| 149 | + idx = 1 |
| 150 | + for id_lh in range(0, midVarId): |
| 151 | + plt.bar(xdata - 0.15*idx, yList[id_lh], width=width, alpha=alpha, label=yLabels[id_lh]) |
| 152 | + idx += 1 |
| 153 | + idx = 1 |
| 154 | + for id_rh in range(midVarId+1, N_entry): |
| 155 | + plt.bar(xdata + 0.15*idx, yList[id_rh], width=width, alpha=alpha, label=yLabels[id_rh]) |
| 156 | + idx += 1 |
| 157 | + else: |
| 158 | + # even number |
| 159 | + shiftIndex = [0.06] + [0.1]*midVarId |
| 160 | + |
| 161 | + idx = 1 |
| 162 | + for id_lh in range(0, midVarId): |
| 163 | + plt.bar(xdata - idx*shiftIndex[idx-1], yList[id_lh], width=width, alpha=alpha, label=yLabels[id_lh]) |
| 164 | + idx += 1 |
| 165 | + |
| 166 | + idx = 1 |
| 167 | + for id_rh in range(midVarId, N_entry): |
| 168 | + plt.bar(xdata + idx*shiftIndex[idx-1], yList[id_rh], width=width, alpha=alpha, label=yLabels[id_rh]) |
| 169 | + idx += 1 |
| 170 | + pass |
| 171 | + |
| 172 | + plt.legend(ncol=2) |
| 173 | + plt.xlabel('Cluster size (molecules)') |
| 174 | + plt.ylabel('Frequency') |
| 175 | + plt.title(title, pad=12) |
| 176 | + plt.show() |
| 177 | + |
| 178 | + |
| 179 | +def plotMolecularDistribution(path, molecules=[], width=0.1, alpha=0.6): |
| 180 | + df = pd.read_csv(path + '/pyStat/Molecular_distribution.csv') |
| 181 | + csList = df['Clusters'] |
| 182 | + if len(molecules) == 0: |
| 183 | + mols = df.columns[2:] |
| 184 | + freqList = [df[mol] for mol in mols] |
| 185 | + plotBarGraph(csList, freqList, mols, width=width, alpha=alpha, title='Molecular Distribution') |
| 186 | + else: |
| 187 | + freqList = [df[mol] for mol in molecules] |
| 188 | + plotBarGraph(csList, freqList, molecules, width=width, alpha=alpha, title='Molecular Distribution') |
| 189 | + |
| 190 | + |
| 191 | +def plotClusterComposition(path, specialClusters=[], width=0.1, alpha=0.6): |
| 192 | + df = pd.read_csv(path + '/pyStat/Cluster_composition.csv') |
| 193 | + csList = df['Clusters'] |
| 194 | + if len(specialClusters) == 0: |
| 195 | + mols = df.columns[2:] |
| 196 | + freqList = [df[mol] for mol in mols] |
| 197 | + plotBarGraph(csList, freqList, mols, width=width, alpha=alpha, title='Cluster Composition') |
| 198 | + else: |
| 199 | + idx = [i for i in range(len(csList)) if csList[i] in specialClusters] |
| 200 | + df2 = df.iloc[idx] |
| 201 | + mols = df.columns[2:] |
| 202 | + freqList = [df2[mol] for mol in mols] |
| 203 | + plotBarGraph(df2['Clusters'], freqList, mols, width=width, alpha=alpha, title='Cluster Composition') |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | + |
0 commit comments