Ejemplo n.º 1
0
def save_fig(path=None,
             mean=None,
             sigma=None,
             x_data=None,
             y_data=None,
             y_value=None):
    try:
        path_list = os.listdir("{}/Membership".format(path))
        y_value = str(y_value).replace('.', '_')
        if len(path_list) == 0:
            next_number = '0_{}'.format(y_value)
        else:
            path_list = np.array([str_replace(value) for value in path_list])
            max_path = np.max(path_list) + 1
            next_number = str(max_path) + '_{}'.format(y_value)
        storage_fig_path = "{}/Membership/{}".format(path, next_number)
        gaussian_data_y = gaussian(x_data, mean, sigma)

        plt.plot(x_data, gaussian_data_y, label='Gaussian')
        plt.plot(x_data, y_data, label='Membership')
        plt.xlabel('X')
        plt.ylabel('Y')
        plt.legend()
        plt.savefig('{}'.format(storage_fig_path))
        plt.close()
    except Exception as e:
        print(e)
    return
    def fuzz(self, value):
        """
        Propagate crisp value down to adjectives by calculating membership.
        """
        if len(self.var.terms) == 0:
            raise ValueError("Set Term membership function(s) first")

        for label, term in self.var.terms.items():
            # input:
            # self.var.universe is the array of x input values
            # term.mf is the array of y output values
            # value is the value that you want to obtain an output from based on interpolation
            if self.analysis_function == 'gauss':
                # TODO: i think this function uses the wrong fuzzy membership since it's supposed to be the value of the defined gauss
                term.membership_value[self.sim] = gaussian(
                    value, self.analysis_params['mean'],
                    self.analysis_params['sigma'])
Ejemplo n.º 3
0
def graph_gaussian(path=None):
    data_x, data_y = open_data(path="{}normalized_peak.txt".format(path))
    granularity = 500
    tol_x = np.divide(np.subtract(np.max(data_x), np.min(data_x)), granularity)
    new_universe = np.arange(np.min(data_x), np.max(data_x) + tol_x, tol_x)

    std_dev = np.std(data_x)
    result = gaussian(new_universe, 2, std_dev)
    result_skew = skew_norm_pdf(new_universe, e=2)
    normalize_result = np.divide(np.subtract(result, np.min(result)),
                                 np.subtract(np.max(result), np.min(result)))
    normalize_result_skew = np.divide(
        np.subtract(result_skew, np.min(result_skew)),
        np.subtract(np.max(result_skew), np.min(result_skew)))

    plt.plot(new_universe, normalize_result)
    plt.plot(new_universe, normalize_result_skew)
Ejemplo n.º 4
0
    def create_membership(self, m_x=None, m_y=None):
        if self.analysis_function == 'composite_gauss':
            # here we will create a composite gaussian of two gaussians, with mean E(x) and E(m_x) so we can adjust
            # our centroid as necessary

            if m_x:
                # our m_x gaussian has to fit within the range
                self.x_antecedent['x'], sigma = gaussian_with_range(
                    self.x_antecedent.universe, m_x)
                self.analysis_params_antecedent = {
                    'mean':
                    m_x,
                    'sigma':
                    self.std_x_sigma,
                    'range':
                    np.arange(np.min(self.x_antecedent['x']),
                              np.max(self.x_antecedent['x']) + self.tol_x,
                              self.tol_x),
                    'path':
                    self.path
                }
            else:
                self.x_antecedent['x'], sigma = gaussian_with_range(
                    self.x_antecedent.universe,
                    float(np.mean(np.array(self.x_antecedent.universe))))
                self.analysis_params_antecedent = {
                    'mean':
                    float(np.mean(np.array(self.x_antecedent.universe))),
                    'sigma':
                    sigma,
                    'range':
                    np.arange(np.min(self.x_antecedent['x']),
                              np.max(self.x_antecedent['x']) + self.tol_x,
                              self.tol_x),
                    'path':
                    self.path
                }
            if m_y:
                self.y_consequent['y'] = gaussian(
                    self.y_consequent.universe, m_y,
                    float(np.std(np.array(self.y_consequent.universe))))
                self.analysis_params_consequent = {
                    'mean':
                    m_y,
                    'sigma':
                    float(np.std(np.array(self.y_consequent.universe))),
                    'range':
                    np.arange(np.min(self.data_y),
                              np.max(self.data_y) + self.tol_y, self.tol_y),
                    'path':
                    self.path
                }
            else:
                # We need to use a composite gaussian here to create our y
                self.y_consequent['y'] = gaussian(
                    self.y_consequent.universe,
                    float(np.mean(np.array(self.y_consequent.universe))),
                    self.std_y_sigma)
                self.analysis_params_consequent = {
                    'mean':
                    float(np.mean(np.array(self.y_consequent.universe))),
                    'sigma':
                    self.std_y_sigma,
                    'range':
                    np.arange(np.min(self.data_y),
                              np.max(self.data_y) + self.tol_y, self.tol_y),
                    'path':
                    self.path
                }
        if self.analysis_function == 'gauss':

            if m_x:
                self.x_antecedent['x'] = gaussian(
                    self.x_antecedent.universe, m_x,
                    float(np.std(np.array(self.x_antecedent.universe))))
                self.analysis_params_antecedent = {
                    'mean':
                    m_x,
                    'sigma':
                    self.std_x_sigma,
                    'range':
                    np.arange(np.min(self.data_x),
                              np.max(self.data_x) + self.tol_x, self.tol_x),
                    'path':
                    self.path
                }
            else:
                self.x_antecedent['x'] = gaussian(
                    self.x_antecedent.universe,
                    float(np.mean(np.array(self.x_antecedent.universe))),
                    float(np.std(np.array(self.x_antecedent.universe))))
                self.analysis_params_antecedent = {
                    'mean':
                    float(np.mean(np.array(self.x_antecedent.universe))),
                    'sigma':
                    float(np.std(np.array(self.x_antecedent.universe))),
                    'range':
                    np.arange(np.min(self.data_x),
                              np.max(self.data_x) + self.tol_x, self.tol_x),
                    'path':
                    self.path
                }
            if m_y:
                self.y_consequent['y'] = gaussian(
                    self.y_consequent.universe, m_y,
                    float(np.std(np.array(self.y_consequent.universe))))
                self.analysis_params_consequent = {
                    'mean':
                    m_y,
                    'sigma':
                    float(np.std(np.array(self.y_consequent.universe))),
                    'range':
                    np.arange(np.min(self.data_y),
                              np.max(self.data_y) + self.tol_y, self.tol_y),
                    'path':
                    self.path
                }
            else:
                self.y_consequent['y'] = gaussian(
                    self.y_consequent.universe,
                    float(np.mean(np.array(self.y_consequent.universe))),
                    self.std_y_sigma)
                self.analysis_params_consequent = {
                    'mean':
                    float(np.mean(np.array(self.y_consequent.universe))),
                    'sigma':
                    self.std_y_sigma,
                    'range':
                    np.arange(np.min(self.data_y),
                              np.max(self.data_y) + self.tol_y, self.tol_y),
                    'path':
                    self.path
                }
        elif self.analysis_function == 'trimf':
            if m_x:
                self.x_antecedent['x'] = trimf(
                    self.x_antecedent.universe,
                    [np.min(self.data_x), m_x,
                     np.max(self.data_x)])
            else:
                self.x_antecedent['x'] = trimf(
                    self.x_antecedent.universe,
                    [np.min(self.data_x), self.m_x,
                     np.max(self.data_x)])
            if m_y:
                self.y_consequent['y'] = trimf(
                    self.y_consequent.universe,
                    [np.min(self.data_y), m_y,
                     np.max(self.data_y)])
            else:
                self.y_consequent['y'] = trimf(
                    self.y_consequent.universe,
                    [np.min(self.data_y), self.m_y,
                     np.max(self.data_y)])
Ejemplo n.º 5
0
    def create_membership(self, m_x=None, m_y=None):
        # so we don't actually use the antecedent and consequent stuff here...i might scrap all of this for a new repo
        if self.analysis_function == 'composite_gauss':
            # here we will create a composite gaussian of two gaussians, with mean E(x) and E(m_x) so we can adjust
            # our centroid as necessary
            if m_x:
                # our m_x gaussian has to fit within the range
                self.x_antecedent['x'] = gaussian(
                    self.x_antecedent.universe, m_x,
                    float(np.std(np.array(self.x_antecedent.universe))))
                self.analysis_params_antecedent = {
                    'mean':
                    m_x,
                    'sigma':
                    self.std_x_sigma,
                    'data':
                    self.data_x,
                    'range':
                    np.arange(np.min(self.data_x),
                              np.max(self.data_x) + self.tol_x, self.tol_x),
                    'path':
                    self.path
                }
            else:
                self.x_antecedent['x'] = gaussian(
                    self.x_antecedent.universe,
                    float(np.mean(np.array(self.x_antecedent.universe))),
                    float(np.std(np.array(self.x_antecedent.universe))))
                self.analysis_params_antecedent = {
                    'mean':
                    float(np.mean(np.array(self.x_antecedent.universe))),
                    'sigma':
                    float(np.std(np.array(self.x_antecedent.universe))),
                    'data':
                    self.data_x,
                    'range':
                    np.arange(np.min(self.data_x),
                              np.max(self.data_x) + self.tol_x, self.tol_x),
                    'path':
                    self.path
                }
            if m_y:
                # this is just a placeholder, self.y_consequent['y'] doesn't seem to affect results
                self.y_consequent['y'] = gaussian(
                    self.y_consequent.universe,
                    float(np.mean(np.array(self.y_consequent.universe))),
                    self.std_y_sigma)
                self.analysis_params_consequent = {
                    'mean':
                    m_y,
                    'data':
                    self.data_y,
                    'range':
                    np.arange(np.min(self.data_y),
                              np.max(self.data_y) + self.tol_y, self.tol_y),
                    'path':
                    self.path
                }
            else:
                # this is just a placeholder, self.y_consequent['y'] doesn't seem to affect results
                self.y_consequent['y'] = gaussian(
                    self.y_consequent.universe,
                    float(np.mean(np.array(self.y_consequent.universe))),
                    self.std_y_sigma)
                self.analysis_params_consequent = {
                    'mean':
                    float(np.mean(np.array(self.y_consequent.universe))),
                    'data':
                    self.data_y,
                    'range':
                    np.arange(np.min(self.data_y),
                              np.max(self.data_y) + self.tol_y, self.tol_y),
                    'path':
                    self.path
                }
        if self.analysis_function == 'gauss':

            if m_x:
                self.x_antecedent['x'] = gaussian(
                    self.x_antecedent.universe, m_x,
                    float(np.std(np.array(self.x_antecedent.universe))))
                self.analysis_params_antecedent = {
                    'mean':
                    m_x,
                    'sigma':
                    self.std_x_sigma,
                    'range':
                    np.arange(np.min(self.data_x),
                              np.max(self.data_x) + self.tol_x, self.tol_x),
                    'path':
                    self.path
                }
            else:
                self.x_antecedent['x'] = gaussian(
                    self.x_antecedent.universe,
                    float(np.mean(np.array(self.x_antecedent.universe))),
                    float(np.std(np.array(self.x_antecedent.universe))))
                self.analysis_params_antecedent = {
                    'mean':
                    float(np.mean(np.array(self.x_antecedent.universe))),
                    'sigma':
                    float(np.std(np.array(self.x_antecedent.universe))),
                    'range':
                    np.arange(np.min(self.data_x),
                              np.max(self.data_x) + self.tol_x, self.tol_x),
                    'path':
                    self.path
                }
            if m_y:
                self.y_consequent['y'] = gaussian(
                    self.y_consequent.universe, m_y,
                    float(np.std(np.array(self.y_consequent.universe))))
                self.analysis_params_consequent = {
                    'mean':
                    m_y,
                    'sigma':
                    float(np.std(np.array(self.y_consequent.universe))),
                    'range':
                    np.arange(np.min(self.data_y),
                              np.max(self.data_y) + self.tol_y, self.tol_y),
                    'path':
                    self.path
                }
            else:
                self.y_consequent['y'] = gaussian(
                    self.y_consequent.universe,
                    float(np.mean(np.array(self.y_consequent.universe))),
                    self.std_y_sigma)
                self.analysis_params_consequent = {
                    'mean':
                    float(np.mean(np.array(self.y_consequent.universe))),
                    'sigma':
                    self.std_y_sigma,
                    'range':
                    np.arange(np.min(self.data_y),
                              np.max(self.data_y) + self.tol_y, self.tol_y),
                    'path':
                    self.path
                }
        elif self.analysis_function == 'trimf':
            if m_x:
                self.x_antecedent['x'] = trimf(
                    self.x_antecedent.universe,
                    [np.min(self.data_x), m_x,
                     np.max(self.data_x)])
            else:
                self.x_antecedent['x'] = trimf(
                    self.x_antecedent.universe,
                    [np.min(self.data_x), self.m_x,
                     np.max(self.data_x)])
            if m_y:
                self.y_consequent['y'] = trimf(
                    self.y_consequent.universe,
                    [np.min(self.data_y), m_y,
                     np.max(self.data_y)])
            else:
                self.y_consequent['y'] = trimf(
                    self.y_consequent.universe,
                    [np.min(self.data_y), self.m_y,
                     np.max(self.data_y)])