|
| 1 | +import argparse |
| 2 | +# from pathlib import Path |
| 3 | + |
| 4 | +from mpl_toolkits.axes_grid1 import host_subplot |
| 5 | +import mpl_toolkits.axisartist as AA |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +# ^^^ = from matplotlib.pyplot import axvline, savefig, subplots, annotate |
| 8 | + |
| 9 | +from pycorn import pc_res3 |
| 10 | + |
| 11 | +parser = argparse.ArgumentParser() |
| 12 | + |
| 13 | +parser.add_argument("inp_res", |
| 14 | + help="Input .res file(s)", |
| 15 | + nargs='+', |
| 16 | + metavar="<file>.res") |
| 17 | +parser.add_argument("-e", "--ext", default='.pdf', |
| 18 | + help="Image type to use, e.g. 'jpg', 'png', 'eps', or 'pdf' (default: pdf)") |
| 19 | +parser.add_argument("--xmin", default=None, type=float, |
| 20 | + help="Lower bound on the x-axis") |
| 21 | +parser.add_argument("--xmax", default=None, type=float, |
| 22 | + help="Upper bound on the x-axis") |
| 23 | +parser.add_argument("--dpi", default=None, type=int, |
| 24 | + help="DPI (dots per inch) for raster images (png, jpg, etc.)") |
| 25 | +parser.add_argument("--par1", default=None, type=str, |
| 26 | + help="First parasite") |
| 27 | +parser.add_argument("--par2", default=None, type=str, |
| 28 | + help="Second parasite") |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +args = parser.parse_args() |
| 33 | + |
| 34 | + |
| 35 | +def mapper(min_val, max_val, perc): |
| 36 | + ''' |
| 37 | + calculate relative position in delta min/max |
| 38 | + ''' |
| 39 | + x = abs(max_val - min_val) * perc |
| 40 | + if min_val < 0: |
| 41 | + return (x - abs(min_val)) |
| 42 | + else: |
| 43 | + return (x + min_val) |
| 44 | + |
| 45 | + |
| 46 | +def expander(min_val, max_val, perc): |
| 47 | + ''' |
| 48 | + expand -/+ direction of two values by a percentage of their delta |
| 49 | + ''' |
| 50 | + delta = abs(max_val - min_val) |
| 51 | + x = delta * perc |
| 52 | + return (min_val - x, max_val + x) |
| 53 | + |
| 54 | + |
| 55 | +def xy_data(inp): |
| 56 | + ''' |
| 57 | + Takes a data block and returns two lists with x- and y-data |
| 58 | + ''' |
| 59 | + x_data = [x[0] for x in inp] |
| 60 | + y_data = [x[1] for x in inp] |
| 61 | + return x_data, y_data |
| 62 | + |
| 63 | + |
| 64 | +def uvdata(inp): |
| 65 | + ''' |
| 66 | + helps in finding the useful data |
| 67 | + ''' |
| 68 | + UV_blocks = [i for i in inp if i.startswith('UV') or i.endswith('nm')] |
| 69 | + for i in UV_blocks: |
| 70 | + if i.endswith("_0nm"): |
| 71 | + UV_blocks.remove(i) |
| 72 | + |
| 73 | + |
| 74 | +def smartscale(inp): |
| 75 | + ''' |
| 76 | + input is the entire fdata block |
| 77 | + checks user input/fractions to determine scaling of x/y-axis |
| 78 | + returns min/max for x/y |
| 79 | + ''' |
| 80 | + UV_blocks = [i for i in inp.keys() if i.startswith('UV') and not i.endswith('_0nm')] |
| 81 | + uv1_data = inp[UV_blocks[0]]['data'] |
| 82 | + uv1_x, uv1_y = xy_data(uv1_data) |
| 83 | + try: |
| 84 | + uv2_data = inp[UV_blocks[1]]['data'] |
| 85 | + uv2_x, uv2_y = xy_data(uv2_data) |
| 86 | + uv3_data = inp[UV_blocks[2]]['data'] |
| 87 | + uv3_x, uv3_y = xy_data(uv3_data) |
| 88 | + except: |
| 89 | + KeyError |
| 90 | + uv2_data = None |
| 91 | + uv3_data = None |
| 92 | + frac_delta = [] |
| 93 | + try: |
| 94 | + frac_data = inp['Fractions']['data'] |
| 95 | + frac_x, frac_y = xy_data(frac_data) |
| 96 | + frac_delta = [abs(a - b) for a, b in zip(frac_x, frac_x[1:])] |
| 97 | + frac_delta.append(frac_delta[-1]) |
| 98 | + except: |
| 99 | + KeyError |
| 100 | + frac_data = None |
| 101 | + if args.xmin: |
| 102 | + plot_x_min = args.xmin |
| 103 | + else: |
| 104 | + if frac_data: |
| 105 | + plot_x_min = frac_data[0][0] |
| 106 | + else: |
| 107 | + plot_x_min = uv1_x[0] |
| 108 | + if args.xmax: |
| 109 | + plot_x_max = args.xmax |
| 110 | + else: |
| 111 | + if frac_data: |
| 112 | + plot_x_max = frac_data[-1][0] + frac_delta[-1]*2 # recheck |
| 113 | + else: |
| 114 | + plot_x_max = uv1_x[-1] |
| 115 | + if plot_x_min > plot_x_max: |
| 116 | + print("Warning: xmin bigger than xmax - adjusting...") |
| 117 | + plot_x_min = uv1_x[0] |
| 118 | + if plot_x_max < plot_x_min: |
| 119 | + print("Warning: xmax smaller than xmin - adjusting...") |
| 120 | + plot_x_max = uv1_x[-1] |
| 121 | + # optimize y_scaling |
| 122 | + min_y_values = [] |
| 123 | + max_y_values = [] |
| 124 | + for i in UV_blocks: |
| 125 | + tmp_x, tmp_y = xy_data(inp[i]['data']) |
| 126 | + range_min_lst = [abs(a - plot_x_min) for a in tmp_x] |
| 127 | + range_min_idx = range_min_lst.index(min(range_min_lst)) |
| 128 | + range_max_lst = [abs(a - plot_x_max) for a in tmp_x] |
| 129 | + range_max_idx = range_max_lst.index(min(range_max_lst)) |
| 130 | + values_in_range = tmp_y[range_min_idx:range_max_idx] |
| 131 | + min_y_values.append(min(values_in_range)) |
| 132 | + max_y_values.append(max(values_in_range)) |
| 133 | + plot_y_min_tmp = min(min_y_values) |
| 134 | + plot_y_max_tmp = max(max_y_values) |
| 135 | + plot_y_min, plot_y_max = expander(plot_y_min_tmp, plot_y_max_tmp, 0.085) |
| 136 | + return plot_x_min, plot_x_max, plot_y_min, plot_y_max |
| 137 | + |
| 138 | +def plotterX(inp,fname): |
| 139 | + plot_x_min, plot_x_max, plot_y_min, plot_y_max = smartscale(inp) |
| 140 | + host = host_subplot(111, axes_class=AA.Axes) |
| 141 | + host.set_xlabel("Elution volume (ml)") |
| 142 | + host.set_ylabel("Absorbance (mAu)") |
| 143 | + host.set_xlim(plot_x_min, plot_x_max) |
| 144 | + host.set_ylim(plot_y_min, plot_y_max) |
| 145 | + for i in inp.keys(): |
| 146 | + if i.startswith('UV') and not i.endswith('_0nm'): |
| 147 | + x_dat, y_dat = xy_data(inp[i]['data']) |
| 148 | + print("Plotting: " + inp[i]['data_name']) |
| 149 | + stl = styles[i[:4]] |
| 150 | + p0, = host.plot(x_dat, y_dat, label=inp[i]['data_name'], color=stl['color'], |
| 151 | + ls=stl['ls'], lw=stl['lw'],alpha=stl['alpha']) |
| 152 | + if args.par1: |
| 153 | + par1_inp = args.par1 |
| 154 | + par1 = host.twinx() |
| 155 | + par1_data = inp[par1_inp] |
| 156 | + stl = styles[par1_inp] |
| 157 | + par1.set_ylabel(par1_data['data_name'] + " (" + par1_data['unit'] + ")", color=stl['color']) |
| 158 | + x_dat_p1, y_dat_p1 = xy_data(par1_data['data']) |
| 159 | + p1_ymin, p1_ymax = expander(min(y_dat_p1), max(y_dat_p1), 0.085) |
| 160 | + par1.set_ylim(p1_ymin, p1_ymax) |
| 161 | + print("Plotting: " + par1_data['data_name']) |
| 162 | + p1, = par1.plot(x_dat_p1, y_dat_p1, label=par1_data['data_name'], color=stl['color'], |
| 163 | + ls=stl['ls'], lw=stl['lw'], alpha=stl['alpha']) |
| 164 | + if args.par2: |
| 165 | + par2_inp = args.par2 |
| 166 | + par2 = host.twinx() |
| 167 | + offset = 60 |
| 168 | + new_fixed_axis = par2.get_grid_helper().new_fixed_axis |
| 169 | + par2.axis["right"] = new_fixed_axis(loc="right", axes=par2, offset=(offset, 0)) |
| 170 | + par2.axis["right"].toggle(all=True) |
| 171 | + par2_data = inp[par2_inp] |
| 172 | + stl = styles[par2_inp] |
| 173 | + par2.set_ylabel(par2_data['data_name'] + " (" + par2_data['unit'] + ")", color=stl['color']) |
| 174 | + x_dat_p2, y_dat_p2 = xy_data(par2_data['data']) |
| 175 | + p2_ymin, p2_ymax = expander(min(y_dat_p2), max(y_dat_p2), 0.075) |
| 176 | + par2.set_ylim(p2_ymin, p2_ymax) |
| 177 | + print("Plotting: " + par2_data['data_name']) |
| 178 | + p2, = par2.plot(x_dat_p2, y_dat_p2, label=par2_data['data_name'], color=stl['color'], |
| 179 | + ls=stl['ls'], lw=stl['lw'], alpha=stl['alpha']) |
| 180 | + try: |
| 181 | + frac_data = inp['Fractions']['data'] |
| 182 | + frac_x, frac_y = xy_data(frac_data) |
| 183 | + frac_delta = [abs(a - b) for a, b in zip(frac_x, frac_x[1:])] |
| 184 | + frac_delta.append(frac_delta[-1]) |
| 185 | + frac_y_pos = mapper(host.get_ylim()[0], host.get_ylim()[1], 0.015) |
| 186 | + for i in frac_data: |
| 187 | + host.axvline(x=i[0], ymin=0.065, ymax=0.0, color='r', linewidth=0.85) |
| 188 | + host.annotate(str(i[1]), xy=(i[0] + frac_delta[frac_data.index(i)] * 0.55, frac_y_pos), |
| 189 | + horizontalalignment='center', verticalalignment='bottom', size=8, rotation=90) |
| 190 | + except: |
| 191 | + KeyError |
| 192 | + host.set_xlim(plot_x_min, plot_x_max) |
| 193 | + host.legend(fontsize=8, fancybox=True, labelspacing=0.4) |
| 194 | + plt.title(fname, loc='left') |
| 195 | + internal_run_name = str(inp['Logbook']['run_name']) |
| 196 | + plot_file = fname[:-4] + "_" + internal_run_name + "_plot" + args.ext |
| 197 | + plt.savefig(plot_file, bbox_inches='tight', dpi=args.dpi) |
| 198 | + print("Plot saved to: " + plot_file) |
| 199 | +#4e62ff |
| 200 | +styles = {'UV':{'color': '#1919FF', 'lw': 1.6, 'ls': "-", 'alpha':1.0}, |
| 201 | +'UV1_':{'color': '#1919FF', 'lw': 1.6, 'ls': "-", 'alpha':1.0}, |
| 202 | +'UV2_':{'color': '#e51616', 'lw': 1.4, 'ls': "-", 'alpha':1.0}, |
| 203 | +'UV3_':{'color': '#c73de6', 'lw': 1.2, 'ls': "-", 'alpha':1.0}, |
| 204 | +'Cond':{'color': '#62181A', 'lw': 1.4, 'ls': "-", 'alpha':0.75}, |
| 205 | +'Conc':{'color': '#0F990F', 'lw': 1.0, 'ls': "-", 'alpha':0.75}, |
| 206 | +'Pres':{'color': '#3E1719', 'lw': 1.0, 'ls': "-", 'alpha':0.75}, |
| 207 | +'Temp':{'color': '#e04730', 'lw': 1.0, 'ls': "-", 'alpha':0.75}, |
| 208 | +'Inje':{'color': '#d56d9d', 'lw': 1.0, 'ls': "-", 'alpha':0.75}, |
| 209 | +'pH':{'color': '#0C7F7F', 'lw': 1.0, 'ls': "-", 'alpha':0.75},} |
| 210 | + |
| 211 | +def main2(): |
| 212 | + for fname in args.inp_res: |
| 213 | + fdata = pc_res3(fname) |
| 214 | + fdata.load() |
| 215 | + plotterX(fdata, fname) |
| 216 | + |
| 217 | +main2() |
0 commit comments