Skip to content

Commit

Permalink
refactor: move drawing intergrals to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan Le committed Apr 22, 2024
1 parent 51e3c80 commit f56f9d8
Showing 1 changed file with 55 additions and 41 deletions.
96 changes: 55 additions & 41 deletions chem_spectra/lib/composer/ni.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,9 @@ def tf_img(self):
markersize=50,
)

# ----- PLOT integration -----
# ----- Calculate integration -----
cys = []
refShift, refArea = self.refShift, self.refArea
itg_h = y_max + h * 0.6
itg_h = itg_h + itg_h * 0.1
itg_value_position_x = y_min - h * 0.4
y_boundary_min += itg_value_position_x
if (len(self.all_itgs) == 0 and len(self.core.itg_table) > 0 and not self.core.params['integration'].get('edited') and ('originStack' not in self.core.params['integration'])):
core_itg_table = self.core.itg_table[0]
itg_table = core_itg_table.split('\n')
Expand All @@ -384,40 +380,9 @@ def tf_img(self):
xUStr = split_itg[1].strip()
areaStr = split_itg[2].strip()
self.all_itgs.append({'xL': float(xLStr), 'xU': float(xUStr), 'area': float(areaStr)}) # noqa: E501
for itg in self.all_itgs:
# integration marker
xL, xU, area = itg['xL'] - refShift, itg['xU'] - refShift, itg['area'] * refArea # noqa: E501
# integration curve
ks = calc_ks(self.core.ys, y_max, h)
iL, iU = get_curve_endpoint(self.core.xs, self.core.ys, xL, xU)
ref = ks[iL]
cxs = self.core.xs[iL:iU]

if self.core.is_hplc_uv_vis:
# fill area under curve
cys = self.core.ys[iL:iU]
slope = cal_slope(cxs[0], cys[0], cxs[len(cxs)-1], cys[len(cys)-1]) # noqa: E501
last_y = cys[0]
last_x = cxs[0]
aucys = [last_y]
for i in range(1, len(cys)):
curr_x = cxs[i]
curr_y = slope*(curr_x-last_x) + last_y
aucys.append(curr_y)
last_x = curr_x
last_y = curr_y
plt.fill_between(cxs, y1=cys, y2=aucys, alpha=0.2, color='#FF0000') # noqa: E501
else:
# display integration
plt.plot([xL, xU], [itg_value_position_x, itg_value_position_x], color='#228B22')
plt.plot([xL, xL], [itg_value_position_x + h * 0.01, itg_value_position_x - h * 0.01], color='#228B22') # noqa: E501
plt.plot([xU, xU], [itg_value_position_x + h * 0.01, itg_value_position_x - h * 0.01], color='#228B22') # noqa: E501
plt.text((xL + xU) / 2, itg_value_position_x + h * 0.02, '{:0.2f}'.format(area), color='#228B22', ha='center', size=10, rotation=90.) # noqa: E501
# cys = (ks[iL:iU] - ref) * 1.5 + (y_max - h * 0.4)
cys = (ks[iL:iU] - ref) * 1.5 + itg_h + itg_h * 0.1
plt.plot(cxs, cys, color='#228B22')


# ----- PLOT multiplicity -----
# ----- Calculate multiplicity -----
if (len(self.mpys) == 0 and len(self.core.mpy_itg_table) > 0 and not self.core.params['integration'].get('edited') and ('originStack' not in self.core.params['integration'])):
core_mpy_pks_table = self.core.mpy_pks_table[0]
mpy_pks_table = core_mpy_pks_table.split('\n')
Expand Down Expand Up @@ -455,14 +420,25 @@ def tf_img(self):
mpy_item['mpyType'] = typeStr
mpy_item['peaks'] = tmp_dic_mpy_peaks[idxStr]
self.mpys.append(mpy_item)
mpy_h = y_min - h * 0.08

# ----- PLOT integration -----
itg_h = y_max + h * 0.6
itg_h = itg_h + itg_h * 0.1
itg_value_position_x = y_min - h * 0.15
y_boundary_min += itg_value_position_x
if (len(self.mpys) == 0):
itg_value_position_x = y_min - h * 0.05
self.__draw_integrals(plt, refShift, refArea, y_max, h, itg_value_position_x, itg_h)

# ----- PLOT multiplicity -----
mpy_h = y_min - h * 0.06
for mpy in self.mpys:
xL, xU, area, typ, peaks = mpy['xExtent']['xL'] - refShift, mpy['xExtent']['xU'] - refShift, mpy['area'] * refArea, mpy['mpyType'], mpy['peaks'] # noqa: E501
plt.plot([xL, xU], [mpy_h, mpy_h], color='#DA70D6')
plt.plot([xL, xL], [mpy_h + h * 0.01, mpy_h - h * 0.01], color='#DA70D6') # noqa: E501
plt.plot([xU, xU], [mpy_h + h * 0.01, mpy_h - h * 0.01], color='#DA70D6') # noqa: E501
plt.text((xL + xU) / 2, mpy_h - h * 0.1, '({})'.format(typ), color='#DA70D6', ha='center', size=12) # noqa: E501
plt.text((xL + xU) / 2, mpy_h - h * 0.06, '{:0.3f}'.format(calc_mpy_center(mpy['peaks'], refShift, mpy['mpyType'])), color='#DA70D6', ha='center', size=12) # noqa: E501
plt.text((xL + xU) / 2, mpy_h - h * 0.08, '({})'.format(typ), color='#DA70D6', ha='center', size=10) # noqa: E501
plt.text((xL + xU) / 2, mpy_h - h * 0.05, '{:0.3f}'.format(calc_mpy_center(mpy['peaks'], refShift, mpy['mpyType'])), color='#DA70D6', ha='center', size=10) # noqa: E501
for p in peaks:
x = p['x']
plt.plot([x - refShift, x - refShift], [mpy_h, mpy_h + h * 0.05], color='#DA70D6') # noqa: E501
Expand Down Expand Up @@ -503,6 +479,44 @@ def tf_img(self):
plt.clf()
plt.cla()
return tf_img


def __draw_integrals(self, plt, refShift, refArea, y_max, h, itg_value_position_x, itg_h):
for itg in self.all_itgs:
# integration marker
xL, xU, area = itg['xL'] - refShift, itg['xU'] - refShift, itg['area'] * refArea # noqa: E501
# integration curve
ks = calc_ks(self.core.ys, y_max, h)
iL, iU = get_curve_endpoint(self.core.xs, self.core.ys, xL, xU)
ref = ks[iL]
cxs = self.core.xs[iL:iU]

if self.core.is_hplc_uv_vis:
# fill area under curve
cys = self.core.ys[iL:iU]
slope = cal_slope(cxs[0], cys[0], cxs[len(cxs)-1], cys[len(cys)-1]) # noqa: E501
last_y = cys[0]
last_x = cxs[0]
aucys = [last_y]
for i in range(1, len(cys)):
curr_x = cxs[i]
curr_y = slope*(curr_x-last_x) + last_y
aucys.append(curr_y)
last_x = curr_x
last_y = curr_y
plt.fill_between(cxs, y1=cys, y2=aucys, alpha=0.2, color='#FF0000') # noqa: E501
else:
# display integration
plt.plot([xL, xU], [itg_value_position_x, itg_value_position_x], color='#228B22')
plt.plot([xL, xL], [itg_value_position_x + h * 0.01, itg_value_position_x - h * 0.01], color='#228B22') # noqa: E501
plt.plot([xU, xU], [itg_value_position_x + h * 0.01, itg_value_position_x - h * 0.01], color='#228B22') # noqa: E501
plt.text((xL + xU) / 2, itg_value_position_x - h * 0.08, '{:0.2f}'.format(area), color='#228B22', ha='center', size=10, rotation=90.) # noqa: E501
# cys = (ks[iL:iU] - ref) * 1.5 + (y_max - h * 0.4)
cys = (ks[iL:iU] - ref) * 1.5 + itg_h + itg_h * 0.1
plt.plot(cxs, cys, color='#228B22')

return plt


def __draw_peaks(self, plt, x_peaks, y_peaks, h, w):
if self.core.non_nmr == True or len(x_peaks) == 0:
Expand Down

0 comments on commit f56f9d8

Please sign in to comment.