Skip to content

Commit

Permalink
falling back to fixed profile when interpolation fails during fuzzifi…
Browse files Browse the repository at this point in the history
…er fitting
  • Loading branch information
dariomalchiodi committed Nov 7, 2024
1 parent e34b1d7 commit d268c9f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
2 changes: 1 addition & 1 deletion mulearn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.0.6'
__version__ = '1.0.7'


import copy
Expand Down
90 changes: 58 additions & 32 deletions mulearn/fuzzifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,17 @@ def r2_to_mu(r, threshold):
result[r > threshold] = 0
return result

[t_opt], _ = curve_fit(r2_to_mu, squared_R, mu,
bounds=((0,), (np.inf,)))
self.threshold_ = t_opt

if self.threshold_ < 0:
logger.warning("Profile fit returned a negative parameter")
try:
[t_opt], _ = curve_fit(r2_to_mu, squared_R, mu,
bounds=((0,), (np.inf,)))
self.threshold_ = t_opt

if self.threshold_ < 0:
logger.warning("Profile fit returned a negative parameter")
except RuntimeError:
# interpolation could not take place, fall back to fixed profile
self.profile = 'fixed'
self.fit(squared_R, mu, squared_radius)

else:
raise ValueError("'profile' parameter should either be equal to "
Expand Down Expand Up @@ -279,25 +284,36 @@ def r2_to_mu(R_2, r_2_05):
0, 1)
for r_2 in R_2]

[r_2_05_opt], _ = curve_fit(r2_to_mu, squared_R, mu,
p0=(r_2_05_guess,),
bounds=((0,), (np.inf,)))
self.slope_ = -1 / (2 * r_2_05_opt)
self.intercept_ = 1
try:
[r_2_05_opt], _ = curve_fit(r2_to_mu, squared_R, mu,
p0=(r_2_05_guess,),
bounds=((0,), (np.inf,)))
self.slope_ = -1 / (2 * r_2_05_opt)
self.intercept_ = 1
except RuntimeError:
# interpolation could not take place, fall back to fixed profile
self.profile = 'fixed'
self.fit(squared_R, mu, squared_radius)

elif self.profile == 'infer':

def r2_to_mu(R_2, r_2_1, r_2_0):
return [np.clip(1 - (r_2 - r_2_1) / (r_2_0 - r_2_1), 0, 1)
for r_2 in R_2]

p_opt, _ = curve_fit(r2_to_mu, squared_R, mu,
p0=(r_2_1_guess, r_2_0_guess),
bounds=((-np.inf, -np.inf),
(np.inf, np.inf,)))
r_2_1_opt, r_2_0_opt = p_opt
self.slope_ = -1 / (r_2_0_opt - r_2_1_opt)
self.intercept_ = 1 + r_2_1_opt / (r_2_0_opt - r_2_1_opt)
try:
p_opt, _ = curve_fit(r2_to_mu, squared_R, mu,
p0=(r_2_1_guess, r_2_0_guess),
bounds=((-np.inf, -np.inf),
(np.inf, np.inf,)))
r_2_1_opt, r_2_0_opt = p_opt
self.slope_ = -1 / (r_2_0_opt - r_2_1_opt)
self.intercept_ = 1 + r_2_1_opt / (r_2_0_opt - r_2_1_opt)
except RuntimeError:
# interpolation could not take place, fall back to fixed profile
self.profile = 'fixed'
self.fit(squared_R, mu, squared_radius)

else:
raise ValueError("'profile' parameter should be equal to "
"'fixed' or 'infer' (provided value: {self.profile})")
Expand Down Expand Up @@ -403,13 +419,18 @@ def r2_to_mu(R_2, r_2_1, s):
return [np.clip(_safe_exp(-(r_2 - r_2_1) / s), 0, 1)
for r_2 in R_2]

p_opt, _ = curve_fit(r2_to_mu, squared_R, mu,
p0=(r_2_1_guess, s_guess),
# bounds=((0, 0), (np.inf, np.inf)),
maxfev=2000)
r_2_1_opt, s_opt = p_opt
self.slope_ = -1 / s_opt
self.intercept_ = r_2_1_opt / s_opt
try:
p_opt, _ = curve_fit(r2_to_mu, squared_R, mu,
p0=(r_2_1_guess, s_guess),
# bounds=((0, 0), (np.inf, np.inf)),
maxfev=2000)
r_2_1_opt, s_opt = p_opt
self.slope_ = -1 / s_opt
self.intercept_ = r_2_1_opt / s_opt
except RuntimeError:
# interpolation could not take place, fall back to fixed profile
self.profile = 'fixed'
self.fit(squared_R, mu, squared_radius)

elif isinstance(self.profile, (int, float)):
alpha = self.profile
Expand All @@ -423,13 +444,18 @@ def r2_to_mu(R_2, r_2_1):
# all points have within the sphere -> unit membership
return [1] * len(R_2)

[r_2_1_opt], _ = curve_fit(r2_to_mu, squared_R, mu,
p0=(r_2_1_guess,),
bounds=((0,), (np.inf,)))
inner = [r_2 - r_2_1_opt for r_2 in squared_R if r_2 > r_2_1_opt]
q = np.percentile(inner, 100 * alpha)
self.slope_ = np.log(alpha) / q
self.intercept_ = -r_2_1_opt * np.log(alpha) / q
try:
[r_2_1_opt], _ = curve_fit(r2_to_mu, squared_R, mu,
p0=(r_2_1_guess,),
bounds=((0,), (np.inf,)))
inner = [r_2 - r_2_1_opt for r_2 in squared_R if r_2 > r_2_1_opt]
q = np.percentile(inner, 100 * alpha)
self.slope_ = np.log(alpha) / q
self.intercept_ = -r_2_1_opt * np.log(alpha) / q
except RuntimeError:
# interpolation could not take place, fall back to fixed profile
self.profile = 'fixed'
self.fit(squared_R, mu, squared_radius)

else:
raise ValueError("'self.profile' attribute should be equal to "
Expand Down

0 comments on commit d268c9f

Please sign in to comment.