Skip to content

Commit

Permalink
added test for sigmoid plot
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Eggimann committed Oct 14, 2017
1 parent 783fb16 commit 82131dd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
50 changes: 31 additions & 19 deletions energy_demand/scripts/s_generate_sigmoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This script calculates the three parameters of a sigmoid diffusion
for every technology which is diffused and has a larger service
fraction at the model end year
fraction at the model end year
"""
import os
import sys
Expand All @@ -17,8 +17,22 @@
def calc_sigmoid_parameters(l_value, xdata, ydata, fit_crit_a=200, fit_crit_b=0.001):
"""Calculate sigmoid parameters
I
TODO
Arguments
----------
l_value : float
xdata : array
ydata : array
fit_crit_a : float
fit_crit_b : float
Returns
------
fit_parameter : array
Parameters (first position: midpoint, second position: slope)
"""

# Generate possible starting parameters for fit
Expand All @@ -42,22 +56,23 @@ def calc_sigmoid_parameters(l_value, xdata, ydata, fit_crit_a=200, fit_crit_b=0.
fit_parameter[1] < 0) or (
fit_parameter[0] == start_parameters[0]) or (
fit_parameter[1] == start_parameters[1]):
# or(round(fit_parameter[0], 2) == round(fit_parameter[1], 2)): #NEW RULE successfull = False
# or(round(fit_parameter[0], 2) == round(fit_parameter[1], 2)): #NEW RULE
# successfull = False
cnt += 1
if cnt >= len(start_param_list):
sys.exit("Error2: CURVE FITTING DID NOT WORK")
else:
successfull = True
logging.debug("Fit successful {} for with fitting parameters: {} ".format(successfull, fit_parameter))
except:
#logging.debug("Tried unsuccessfully to do the fit with the following parameters: " + str(start_parameters[1]))

except RuntimeError:
logging.debug("Unsuccessful fit" + str(start_parameters[1]))
cnt += 1

if cnt >= len(start_param_list):
sys.exit("Error: CURVE FITTING DID NOT WORK. Try changing fit_crit_a and fit_crit_b")
sys.exit("Sigmoid fit error: Try changing fit_crit_a and fit_crit_b")

return fit_parameter

#OLD VERSION
def tech_sigmoid_parameters(data, enduse, crit_switch_service, installed_tech, l_values, service_tech_by_p, service_tech_switched_p, fuel_switches):
"""Calculate diffusion parameters based on energy service demand in base year and projected future energy service demand
Expand Down Expand Up @@ -154,7 +169,7 @@ def tech_sigmoid_parameters(data, enduse, crit_switch_service, installed_tech, l
# ----------------
fit_parameter = calc_sigmoid_parameters(l_value, xdata, ydata)
logging.debug(" ... Result fit: Midpoint:{} steepness: {}".format(fit_parameter[0], fit_parameter[1]))

# Insert parameters
sigmoid_parameters[tech]['midpoint'] = fit_parameter[0] #midpoint (x0)
sigmoid_parameters[tech]['steepness'] = fit_parameter[1] #Steepnes (k)
Expand Down Expand Up @@ -281,13 +296,12 @@ def sigmoid_fitting_function(x_value, x0_value, k_value):

return y_value

# Define parameter bound
popt, _ = curve_fit(
sigmoid_fitting_function,
x_data,
y_data,
p0=start_parameters
#maxfev=10000 #Numer of iterations
p0=start_parameters,
maxfev=10000 # Numer of iterations
) #bounds=([-np.inf, -np.inf, -np.inf], [np.inf, np.inf, np.inf]))

return popt
Expand Down Expand Up @@ -319,7 +333,7 @@ def tech_l_sigmoid(enduses, fuel_switches, installed_tech, service_fueltype_p, s
#logging.debug("No technologies to calculate sigmoid {}".format(enduse))
pass
else:
logging.debug("Technologes it calculate sigmoid {} {}".format(enduse, installed_tech[enduse]))
logging.debug("Technologes it calculate sigmoid {} {}".format(enduse, installed_tech[enduse]))

# Iterite list with enduses where fuel switches are defined
for technology in installed_tech[enduse]:
Expand Down Expand Up @@ -553,13 +567,11 @@ def write_installed_tech(path_to_txt, data):
for enduse, technologies in data.items():
if str(technologies) == "[]":
file.write("{}, {}".format(
str.strip(enduse), str(technologies) + '\n')
)
str.strip(enduse), str(technologies) + '\n'))
else:
for technology in technologies:
file.write("{}, {}".format(
str.strip(enduse), str.strip(technology) + '\n')
)
str.strip(enduse), str.strip(technology) + '\n'))
file.close()

return
Expand Down Expand Up @@ -610,7 +622,7 @@ def write_tech_increased_service(path_to_txt, data):
file.close()

return

def run(data):
"""Function run script
"""
Expand Down
17 changes: 11 additions & 6 deletions tests/scripts/test_s_generate_sigmoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from energy_demand.scripts import s_generate_sigmoid
import numpy as np
from energy_demand.plotting import plotting_program
from energy_demand.technologies import diffusion_technologies

def test_calc_sigmoid_parameters():
"""Testing
"""
l_value = 1.0
xdata = np.array([2010.0, 2100.0]) #[point_x_by, point_x_projected]
ydata = np.array([0.1, 0.9]) #[point_y_by, point_y_projected]
xdata = np.array([2020.0, 2050.0]) #[point_x_by, point_x_projected]
ydata = np.array([0.1, 0.2]) #[point_y_by, point_y_projected]

# fit parameters
fit_parameter = s_generate_sigmoid.calc_sigmoid_parameters(
Expand All @@ -20,7 +21,7 @@ def test_calc_sigmoid_parameters():
fit_crit_b=0.001)

print("Plot graph: " + str(fit_parameter))
plotting_program.plotout_sigmoid_tech_diff(
'''plotting_program.plotout_sigmoid_tech_diff(
l_value,
"testtech",
"test_enduse",
Expand All @@ -29,8 +30,13 @@ def test_calc_sigmoid_parameters():
fit_parameter,
False # Close windows
)
'''

expected_midpoint = 4.4
x_to_test = xdata[1]
y_calculated = diffusion_technologies.sigmoid_function(x_to_test, l_value, *fit_parameter)

assert round(y_calculated, 3) == round(ydata[1], 3)
'''expected_midpoint = 4.4
expected_slope = None
# call function
Expand All @@ -39,5 +45,4 @@ def test_calc_sigmoid_parameters():
assert out_midpoint == out_midpoint
assert expected_slope == out_slope

test_calc_sigmoid_parameters()
'''

0 comments on commit 82131dd

Please sign in to comment.