I have an excel sheet to magnify (replicate) a times series of daily sewage patterns with a certain peak factor to a similar pattern but with another peak factor; the X-axis has time in hours, and the Y-axis has the value of the peak factor. Usually, I change the peak factor value and get the new pattern. The average value has to equal 1, and the number of hours equals 24 based on the required new peak factor. I iterate the cell E30 to get an average of 1 and total hours of 24
Description of the Excel sheet: • Column A Hour: has time from 1 to 24 hours • Column B Original Pattern: has the value from the original pattern • Column C Unit Pattern: is used to make the peak factor (max. value at Y) equal to 1 • Column D Reduced: use of a function to reduce the error from the Unit Pattern • Column E Pattern: is for the new pattern of the new peak factor (modified max. value at Y) • Column F Required Peak Factor: the required peak factor (required max. value at Y). • The Link to Excel Sheet: https://docs.google.com/spreadsheets/d/1SBX2Xar1skDdgJS_eIiIG-6d8bEyhdcK/edit?usp=sharing&ouid=117376103914635182979&rtpof=true&sd=true
I have written the python code shown below, but the optimization part is not converging. I want someone to help me to correct the code.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import auc
plt.style.use(['science', 'notebook', 'dark_background'])
filename = './Magnify Patterns.xlsx'
# read excel file using pandas library
df = pd.read_excel(filename,nrows=24,usecols= range(0,5))
# convert to numpy array
data = df.to_numpy()
# Orginal pattern maximum value
org_maxvalue= data[:, 1].max()
# required pattern maximum value
mag_maxvalue=2.5
print("org_pf =", org_maxvalue)
print("mag_pf =", mag_maxvalue)
# Interval step along X axis
dx = 1
xx = np.arange(1, 25, dx)
# original pattern values
yy = data[:,1]
# normalize data and scale
y_norm = (data[:, 1] / data[:, 1].max())
exponentInitialValue=1
exponent = exponentInitialValue
y_Exponent= y_norm ** exponent
y_mag = y_Exponent * mag_maxvalue
# Area under the curve
org = auc(xx,yy) # area under the curve original pattern
mag = auc(xx, y_mag) # initial area under the curve magnified pattern
orgmean= np.mean(yy) # mean value of original pattern
magmean= np.mean(y_mag) # Initial mean value of magnified pattern
print("org mean",orgmean)
print("mag mean",magmean)
# optimization solver to make the mean value approx equal 1
while magmean != 0.999: #avg value aprox = 1
exponentInitialValue += 0.1
magmean= round((np.mean(y_mag)),3)
print("The final magnified mean value ="magmean)
print('computed AUC of original curve: {}'.format(auc(xx,yy)))
print('computed AUC of magnified curve: {}'.format(auc(xx,y_mag)))
sfs = [mag_maxvalue] # set scale factor (maximum value of the pattern)
# loop over scale factors
for sf in sfs:
s = 'scale factor = ' + str(sf) # legends
# normalize data and scale
y_scaled = (data[:, 1] / data[:, 1].max()) ** sf
# normalize so sum = 24
y_scaled_norm = 24 * y_scaled / y_scaled.sum()
plt.scatter(data[:, 0], y_scaled_norm, label=s)
plt.legend()
plt.show()
I have a problem with the while loop as it is not converging and keep continuous running. I would like any suggestion how to optimize it and make it converge.