text:
# coding: utf-8

# # COM 466 Fuzzy Logic Project

# Credit decision system

# In[20]:

# needed libraries
import numpy as np
import skfuzzy as fuzz
import matplotlib.pyplot as plt

# ## Fuzzy Input Set Ranges

# In[21]:

# ranges of the sets
x_house_market = np.arange(0, 1000, 1) # Market value $ x10^3
x_house_location = np.arange(0, 10, .01) # location of house
x_person_asset = np.arange(0,1000, 1) # Asset $ x10^3
x_person_income = np.arange(0,100, .1) # income $ x10^3
x_interest = np.arange(0, 10, .01) # Interest %

# ## Defining the Fuzzy Inputs Sets

# In[22]:

# house market value sets
market_low = fuzz.trapmf(x_house_market, [0, 0, 75, 100])
market_medium = fuzz.trapmf(x_house_market, [50, 100, 200, 250])
market_high = fuzz.trapmf(x_house_market, [200, 300, 650, 850])
market_very_high = fuzz.trapmf(x_house_market, [650, 850, 1000, 1000])

# house location sets
location_bad = fuzz.trapmf(x_house_location, [0, 0, 1.5, 4])
location_fair = fuzz.trapmf(x_house_location, [2.5, 5, 6, 8.5])
location_excellent = fuzz.trapmf(x_house_location, [6, 8.5, 10, 10])

# person asset sets
p_asset_low = fuzz.trimf(x_person_asset, [0, 0, 150])
p_asset_medium = fuzz.trapmf(x_person_asset, [50, 250, 450, 650])
p_asset_high = fuzz.trapmf(x_person_asset, [500, 700, 1000, 1000])

# person income sets
p_income_low = fuzz.trapmf(x_person_income, [0, 0, 10, 25])
p_income_medium = fuzz.trimf(x_person_income, [15, 35, 55])
p_income_high = fuzz.trimf(x_person_income, [40, 60, 80])
p_income_very_high = fuzz.trapmf(x_person_income, [60, 80, 100, 100])

# interest sets
b_interest_low = fuzz.trapmf(x_interest, [0, 0, 2, 5])
b_interest_medium = fuzz.trapmf(x_interest, [2, 4, 6, 8])
b_interest_high = fuzz.trapmf(x_interest, [6, 8.5, 10, 10])

# ## Showing the Defined Fuzzy Inputs

# In[23]:

plt.rcParams["figure.figsize"] = 15, 20
# house market value
plt.subplot(5,1,1), plt.plot(x_house_market, market_low, 'b', linewidth=1.5, label='Low')
plt.subplot(5,1,1), plt.plot(x_house_market, market_medium, 'g', linewidth=1.5, label='Medium')
plt.subplot(5,1,1), plt.plot(x_house_market, market_high, 'r', linewidth=1.5, label='High')
plt.subplot(5,1,1), plt.plot(x_house_market, market_very_high, 'y', linewidth=1.5, label='Very High'),plt.title("House Market Value $ x10^3")
plt.legend()

# house location
plt.subplot(5,1,2), plt.plot(x_house_location, location_bad, 'b', linewidth=1.5, label='Bad')
plt.subplot(5,1,2), plt.plot(x_house_location, location_fair, 'g', linewidth=1.5, label='Fair')
plt.subplot(5,1,2), plt.plot(x_house_location, location_excellent, 'r', linewidth=1.5, label='Excellent'),plt.title("House Location")
plt.legend()

# person Assets
plt.subplot(5,1,3), plt.plot(x_person_asset, p_asset_low, 'b', linewidth=1.5, label='Low')
plt.subplot(5,1,3), plt.plot(x_person_asset, p_asset_medium, 'g', linewidth=1.5, label='Medium')
plt.subplot(5,1,3), plt.plot(x_person_asset, p_asset_high, 'r', linewidth=1.5, label='High'),plt.title("Person Assets $ x10^3")
plt.legend()

# person income
plt.subplot(5,1,4), plt.plot(x_person_income, p_income_low, 'b', linewidth=1.5, label='Low')
plt.subplot(5,1,4), plt.plot(x_person_income, p_income_medium, 'g', linewidth=1.5, label='Medium')
plt.subplot(5,1,4), plt.plot(x_person_income, p_income_high, 'r', linewidth=1.5, label='High')
plt.subplot(5,1,4), plt.plot(x_person_income, p_income_very_high, 'y', linewidth=1.5, label='Very High'),plt.title("Person income $ x10^3")
plt.legend()

# Interest
plt.subplot(5,1,5), plt.plot(x_interest, b_interest_low, 'b', linewidth=1.5, label='Low')
plt.subplot(5,1,5), plt.plot(x_interest, b_interest_medium, 'g', linewidth=1.5, label='Medium')
plt.subplot(5,1,5), plt.plot(x_interest, b_interest_high, 'r', linewidth=1.5, label='High'),plt.title("Interest %")
plt.legend()

plt.show()

# ## Defining Fuzzy Output Set Ranges

# In[24]:

x_house = np.arange(0, 10, .01) # House evaluation range
x_applicant = np.arange(0, 10, .01) # applicant evalutaion range
x_credit = np.arange(0, 500, .5) # Credit evalutaion Range $ x10^3

# ## Defining the Fuzzy Output Sets

# In[25]:

# house evalutation output fuzzy sets
house_very_low = fuzz.trimf(x_house, [0, 0, 3])
house_low = fuzz.trimf(x_house, [0, 3, 6])
house_medium = fuzz.trimf(x_house, [2, 5, 8])
house_high = fuzz.trimf(x_house, [4, 7, 10])
house_very_high = fuzz.trimf(x_house, [7, 10, 10])

# applicant evalutation output fuzzy sets
applicant_low = fuzz.trapmf(x_applicant, [0, 0, 2, 4])
applicant_medium = fuzz.trimf(x_applicant, [2, 5, 8])
applicant_high = fuzz.trapmf(x_applicant, [6, 8, 10, 10])

# credit evalutation output fuzzy sets
credit_very_low = fuzz.trimf(x_credit, [0, 0, 125])
credit_low = fuzz.trimf(x_credit, [0, 125, 250])
credit_medium = fuzz.trimf(x_credit, [125, 250, 375])
credit_high = fuzz.trimf(x_credit, [250, 375, 500])
credit_very_high = fuzz.trimf(x_credit, [375, 500, 500])

# ## Showing the Defined Fuzzy Outputs

# In[26]:

plt.rcParams["figure.figsize"] = 15, 12
# house evaluation
plt.subplot(3,1,1), plt.plot(x_house, house_very_low, 'c', linewidth=1.5, label='Very Low')
plt.subplot(3,1,1), plt.plot(x_house, house_low, 'b', linewidth=1.5, label='Low')
plt.subplot(3,1,1), plt.plot(x_house, house_medium, 'g', linewidth=1.5, label='Medium')
plt.subplot(3,1,1), plt.plot(x_house, house_high, 'r', linewidth=1.5, label='High')
plt.subplot(3,1,1), plt.plot(x_house, house_very_high, 'y', linewidth=1.5, label='Very High'),plt.title("House Evaluation Output")
plt.legend()

# applicant evaluation
plt.subplot(3,1,2), plt.plot(x_applicant, applicant_low, 'b', linewidth=1.5, label='Low')
plt.subplot(3,1,2), plt.plot(x_applicant, applicant_medium, 'g', linewidth=1.5, label='Medium')
plt.subplot(3,1,2), plt.plot(x_applicant, applicant_high, 'r', linewidth=1.5, label='High'),plt.title("Applicant Evalutaion Output")
plt.legend()

# credit evaluation
plt.subplot(3,1,3), plt.plot(x_credit, credit_very_low, 'c', linewidth=1.5, label='Very Low')
plt.subplot(3,1,3), plt.plot(x_credit, credit_low, 'b', linewidth=1.5, label='Low')
plt.subplot(3,1,3), plt.plot(x_credit, credit_medium, 'g', linewidth=1.5, label='Medium')
plt.subplot(3,1,3), plt.plot(x_credit, credit_high, 'r', linewidth=1.5, label='High')
plt.subplot(3,1,3), plt.plot(x_credit, credit_very_high, 'y', linewidth=1.5, label='Very High'),plt.title("Credit Value $ x10^3")
plt.legend()

plt.show()

# ## AND and OR helper functions
# - For "and" we used minimum fonction to apply rules
# - For "or" we used maximum function to apply rules

# In[27]:

def and_rule(x, y, z):
    rule = np.fmin(x, y)
    act = np.fmin(rule, z)
    return act

def or_rule(x, y, z):
    rule = np.fmax(x, y)
    act = np.fmax(rule, z)
    return act

# ## House Evaluation Rule base 1
# 1. If (Market_value is Low) then (House is Low)
#     - Market_value == Low AND House == Low ==> C1
# 2. If (Location is Bad) then (House is Low)
#     - Location == Bad AND House == Low ==> C2
# 3. If (Location is Bad) and (Market_value is Low) then (House is Very_low)
#     - ( Location == Bad AND Market_value == Low ) AND House == Very_Low ==> C3
# 4. If (Location is Bad) and (Market_value is Medium) then (House is Low)
#     - ( Location == Bad AND Market_value == Medium ) AND House == Low ==> C4
# 5. If (Location is Bad) and (Market_value is High) then (House is Medium)
#     - ( Location == Bad AND Market_value == High ) AND House == Medium ==> C5
# 6. If (Location is Bad) and (Market_value is Very_high) then (House is High)
#     - ( Location == Bad AND Market_value == Very_high ) AND House == High ==> C6
# 7. If (Location is Fair) and (Market_value is Low) then (House is Low)
#     - ( Location == Fair AND Market_value == Low ) AND House == Low ==> C7
# 8. If (Location is Fair) and (Market_value is Medium) then (House is Medium)
#     - ( Location == Fair AND Market_value == Medium ) AND House == Medium ==> C8
# 9. If (Location is Fair) and (Market_value is High) then (House is High)
#     - ( Location == Fair AND Market_value == High ) AND House == High ==> C9
# 10. If (Location is Fair) and (Market_value is Very_high) then (House is Very_high)
#     - ( Location == Fair AND Market_value == Very_high ) AND House == Very_high ==> C10
# 11. If (Location is Excellent) and (Market_value is Low) then (House is Medium)
#     - ( Location == Excellent AND Market_value == Low ) AND House == Medium ==> C11
# 12. If (Location is Excellent) and (Market_value is Medium) then (House is High)
#     - ( Location == Excellent AND Market_value == Medium ) AND House == High ==> C12
# 13. If (Location is Excellent) and (Market_value is High) then (House is Very_high)
#     - ( Location == Excellent AND Market_value == high ) AND House == Very_high ==> C13
# 14. If (Location is Excellent) and (Market_value is Very_high) then (House is Very_high)
#     - ( Location == Excellent AND Market_value == Very_high ) AND House == Very_high ==> C14
#
# ### Rule Base 1 Combining:
# - => Rule = C1 OR C2 OR C3 OR C4 OR C5 OR C6 OR C7 OR C8 OR C9 OR C10 OR C11 OR C12 OR C13 OR C14

# In[28]:

def apply_house_rules(market_value, location, verbose=0):
    # house market value functions
    market_level_low = fuzz.interp_membership(x_house_market, market_low, market_value)
    market_level_medium = fuzz.interp_membership(x_house_market, market_medium, market_value)
    market_level_high = fuzz.interp_membership(x_house_market, market_high, market_value)
    market_level_very_high = fuzz.interp_membership(x_house_market, market_very_high, market_value)

    # house location
    location_level_bad = fuzz.interp_membership(x_house_location, location_bad, location)
    location_level_fair = fuzz.interp_membership(x_house_location, location_fair, location)
    location_level_excellent = fuzz.interp_membership(x_house_location, location_excellent, location)

    ### rules
    # 1. If (Market_value is Low) then (House is Low)
    house_act_low1 = np.fmin(market_level_low, house_low)
    # 2. If (Location is Bad) then (House is Low)
    house_act_low2 = np.fmin(location_level_bad, house_low)
    # 3. If (Location is Bad) and (Market_value is Low) then (House is Very_low)
    house_act_very_low = and_rule(location_level_bad, market_level_low, house_very_low)
    # 4. If (Location is Bad) and (Market_value is Medium) then (House is Low)
    house_act_low3 = and_rule(location_level_bad, market_level_medium, house_low)
    # 5. If (Location is Bad) and (Market_value is High) then (House is Medium)
    house_act_medium1 = and_rule(location_level_bad, market_level_high, house_medium)
    # 6. If (Location is Bad) and (Market_value is Very_high) then (House is High)
    house_act_high1 = and_rule(location_level_bad, market_level_very_high, house_high)
    # 7. If (Location is Fair) and (Market_value is Low) then (House is Low)
    house_act_low4 = and_rule(location_level_fair, market_level_low, house_low)
    # 8. If (Location is Fair) and (Market_value is Medium) then (House is Medium)
    house_act_medium2 = and_rule(location_level_fair, market_level_medium, house_medium)
    # 9. If (Location is Fair) and (Market_value is High) then (House is High)
    house_act_high2 = and_rule(location_level_fair, market_level_high, house_high)
    # 10. If (Location is Fair) and (Market_value is Very_high) then (House is Very_high)
    house_act_very_high1 = and_rule(location_level_fair, market_level_very_high, house_very_high)
    # 11. If (Location is Excellent) and (Market_value is Low) then (House is Medium)
    house_act_medium3 = and_rule(location_level_excellent, market_level_low, house_medium)
    # 12. If (Location is Excellent) and (Market_value is Medium) then (House is High)
    house_act_high3 = and_rule(location_level_excellent, market_level_medium, house_high)
    # 13. If (Location is Excellent) and (Market_value is High) then (House is Very_high)
    house_act_very_high2 = and_rule(location_level_excellent, market_level_high, house_very_high)
    # 14. If (Location is Excellent) and (Market_value is Very_high) then (House is Very_high)
    house_act_very_high3 = and_rule(location_level_excellent, market_level_very_high, house_very_high)

    # combine the rules
    step = or_rule(house_act_low1, house_act_low2, house_act_low3)
    house_act_low = np.fmax(step, house_act_low4)

    house_act_medium = or_rule(house_act_medium1, house_act_medium2, house_act_medium3)

    house_act_high = or_rule(house_act_high1, house_act_high2, house_act_high3)

    house_act_very_high = or_rule(house_act_very_high1, house_act_very_high2, house_act_very_high3)

    step = or_rule(house_act_very_low, house_act_low, house_act_medium)
    house = or_rule(step, house_act_high, house_act_very_high)

    # if we want to see the graph of the output
    if verbose == 1:
        plt.rcParams["figure.figsize"] = 15, 4
        plt.plot(x_house, house_very_low, 'c', linestyle='--', linewidth=1.5, label='Very Low')
        plt.plot(x_house, house_low, 'b', linestyle='--', linewidth=1.5, label='Low')
        plt.plot(x_house, house_medium, 'g', linestyle='--', linewidth=1.5, label='Medium')
        plt.plot(x_house, house_high, 'r', linestyle='--', linewidth=1.5, label='High')
        plt.plot(x_house, house_very_high, 'y', linestyle='--', linewidth=1.5, label='Very High'),plt.title("House Evaluation Output")
        plt.legend()

        plt.fill_between(x_house, house, color='r')
        plt.ylim(-0.1, 1.1)
        plt.grid(True)
        plt.show()

    return house

# ## Example Output For Rule Base 1

# In[29]:

## triying the function with example
h_eval = apply_house_rules(250, 4, verbose=1)

# ## Applicant Evaluation Rule base 2
# 1. If (Asset is Low) and (Income is Low) then (Applicant is Low)
#     - ( Asset == Low AND Income == Low ) AND Applicant == Low ==> C1
# 2. If (Asset is Low) and (Income is Medium) then (Applicant is Low)
#     - ( Asset == Low AND Income == Medium ) AND Applicant == Low ==> C2
# 3. If (Asset is Low) and (Income is High) then (Applicant is Medium)
#     - ( Asset == Low AND Income == High ) AND Applicant == Medium ==> C3
# 4. If (Asset is Low) and (Income is Very_high) then (Applicant is High)
#     - ( Asset == Low AND Income == Very_high ) AND Applicant == High ==> C4
# 5. If (Asset is Medium) and (Income is Low) then (Applicant is Low)
#     - ( Asset == Medium AND Income == Low ) AND Applicant == Low ==> C5
# 6. If (Asset is Medium) and (Income is Medium) then (Applicant is Medium)
#     - ( Asset == Medium AND Income == Medium ) AND Applicant == Medium ==> C6
# 7. If (Asset is Medium) and (Income is High) then (Applicant is High)
#     - ( Asset == Medium AND Income == High ) AND Applicant == High ==> C7
# 8. If (Asset is Medium) and (Income is Very_high) then (Applicant is High)
#     - ( Asset == Medium AND Income == Very_high ) AND Applicant == High ==> C8
# 9. If (Asset is High) and (Income is Low) then (Applicant is Medium)
#     - ( Asset == High AND Income == Low ) AND Applicant == Medium ==> C9
# 10. If (Asset is High) and (Income is Medium) then (Applicant is Medium)
#     - ( Asset == High AND Income == Medium ) AND Applicant == Medium ==> C10
# 11. If (Asset is High) and (Income is High) then (Applicant is High)
#     - ( Asset == High AND Income == High ) AND Applicant == High ==> C11
# 12. If (Asset is High) and (Income is Very_high) then (Applicant is High)
#     - ( Asset == High AND Income == Very_high ) AND Applicant == High ==> C12
#
# ### Rule Base 2 Combining
# - => Rule = C1 OR C2 OR C3 OR C4 OR C5 OR C6 OR C7 OR C8 OR C9 OR C10 OR C11 OR C12

# In[30]:

def apply_applicant_rules(assets, income, verbose=0):
    # person asset
    p_asset_level_low = fuzz.interp_membership(x_person_asset, p_asset_low, assets)
    p_asset_level_medium = fuzz.interp_membership(x_person_asset, p_asset_medium, assets)
    p_asset_level_high = fuzz.interp_membership(x_person_asset, p_asset_high, assets)

    # person income
    p_income_level_low = fuzz.interp_membership(x_person_income, p_income_low, income)
    p_income_level_medium = fuzz.interp_membership(x_person_income, p_income_medium, income)
    p_income_level_high = fuzz.interp_membership(x_person_income, p_income_high, income)
    p_income_level_very_high = fuzz.interp_membership(x_person_income, p_income_very_high, income)

    # 1. If (Asset is Low) and (Income is Low) then (Applicant is Low)
    applicant_act_low1 = and_rule(p_asset_level_low, p_income_level_low, applicant_low)
    # 2. If (Asset is Low) and (Income is Medium) then (Applicant is Low)
    applicant_act_low2 = and_rule(p_asset_level_low, p_income_level_medium, applicant_low)
    # 3. If (Asset is Low) and (Income is High) then (Applicant is Medium)
    applicant_act_medium1 = and_rule(p_asset_level_low, p_income_level_high, applicant_medium)
    # 4. If (Asset is Low) and (Income is Very_high) then (Applicant is High)
    applicant_act_high1 = and_rule(p_asset_level_low, p_income_level_very_high, applicant_high)
    # 5. If (Asset is Medium) and (Income is Low) then (Applicant is Low)
    applicant_act_low3 = and_rule(p_asset_level_medium, p_income_level_low, applicant_low)
    # 6. If (Asset is Medium) and (Income is Medium) then (Applicant is Medium)
    applicant_act_medium2 = and_rule(p_asset_level_medium, p_income_level_medium, applicant_medium)
    # 7. If (Asset is Medium) and (Income is High) then (Applicant is High)
    applicant_act_high2 = and_rule(p_asset_level_medium, p_income_level_high, applicant_high)
    # 8. If (Asset is Medium) and (Income is Very_high) then (Applicant is High)
    applicant_act_high3 = and_rule(p_asset_level_medium, p_income_level_very_high, applicant_high)
    # 9. If (Asset is High) and (Income is Low) then (Applicant is Medium)
    applicant_act_medium3 = and_rule(p_asset_level_high, p_income_level_low, applicant_medium)
    # 10. If (Asset is High) and (Income is Medium) then (Applicant is Medium)
    applicant_act_medium4 = and_rule(p_asset_level_high, p_income_level_medium, applicant_medium)
    # 11. If (Asset is High) and (Income is High) then (Applicant is High)
    applicant_act_high4 = and_rule(p_asset_level_high, p_income_level_high, applicant_high)
    # 12. If (Asset is High) and (Income is Very_high) then (Applicant is High)
    applicant_act_high5 = and_rule(p_asset_level_high, p_income_level_very_high, applicant_high)

    # combine the rules
    applicant_act_low = or_rule(applicant_act_low1, applicant_act_low2, applicant_act_low3)

    step = or_rule(applicant_act_medium1, applicant_act_medium2, applicant_act_medium3)
    applicant_act_medium = np.fmax(step, applicant_act_medium4)

    step = or_rule(applicant_act_high1, applicant_act_high2, applicant_act_high3)
    applicant_act_high = or_rule(step, applicant_act_high4, applicant_act_high5)

    applicant = or_rule(applicant_act_low, applicant_act_medium, applicant_act_high)

    # if we want to see the graph of the output
    if verbose == 1:
        plt.rcParams["figure.figsize"] = 15, 4
        plt.plot(x_applicant, applicant_low, 'b', linestyle='--', linewidth=1.5, label='Low')
        plt.plot(x_applicant, applicant_medium, 'g', linestyle='--', linewidth=1.5, label='Medium')
        plt.plot(x_applicant, applicant_high, 'r', linestyle='--', linewidth=1.5, label='High'),plt.title("Applicant Evalutaion Output")
        plt.legend()

        plt.fill_between(x_applicant, applicant, color='r')
        plt.ylim(-0.1, 1.1)
        plt.grid(True)
        plt.show()

    return applicant

# ## Example Output For Rule Base 2

# In[31]:

## triying the function with example
a_eval = apply_applicant_rules(550, 45, verbose=1)

# ## Credit Evaluation Rule Base 3
# 1. If (Income is Low) and (Interest is Medium) then (Credit is Very_low)
#     - ( Income == Low AND Interest == Medium ) AND Credit == Very_Low ==> C1
# 2. If (Income is Low) and (Interest is High) then (Credit is Very_low)
#     - ( Income == Low AND Interest == High ) AND Credit == Very_low ==> C2
# 3. If (Income is Medium) and (Interest is High) then (Credit is Low)
#     - ( Income == Medium AND Interest == High ) AND Credit == Low ==> C3
# 4. If (Applicant is Low) then (Credit is Very_low)
#     - Applicant == Low AND Credit == Very_low ==> C4
# 5. If (House is Very_low) then (Credit is Very_low)
#     - House == Very_low AND Credit == Very_low ==> C5
# 6. If (Applicant is Medium) and (House is Very_low) then (Credit is Low)
#     - ( Applicant == Medium AND House == Very_low ) AND Credit == Low ==> C6
# 7. If (Applicant is Medium) and (House is Low) then (Credit is Low)
#     - ( Applicant == Medium AND House == Low ) AND Credit == Low ==> C7
# 8. If (Applicant is Medium) and (House is Medium) then (Credit is Medium)
#     - ( Applicant == Medium AND House == Medium ) AND Credit == Medium ==> C8
# 9. If (Applicant is Medium) and (House is High) then (Credit is High)
#     - ( Applicant == Medium AND House == High ) AND Credit == High ==> C9
# 10. If (Applicant is Medium) and (House is Very_high) then (Credit is High)
#     - ( Applicant == Medium AND House == Very_high ) AND Credit == High ==> C10
# 11. If (Applicant is High) and (House is Very_low) then (Credit is Low)
#     - ( Applicant == High AND House == Very_low ) AND Credit == Low ==> C11
# 12. If (Applicant is High) and (House is Low) then (Credit is Medium)
#     - ( Applicant == High AND House == Low ) AND Credit == Medium ==> C12
# 13. If (Applicant is High) and (House is Medium) then (Credit is High)
#     - ( Applicant == High AND House == Medium ) AND Credit == High ==> C13
# 14. If (Applicant is High) and (House is High) then (Credit is High)
#     - ( Applicant == High AND House == High ) AND Credit == High ==> C14
# 15. If (Applicant is High) and (House is Very_high) then (Credit is Very_high)
#     - ( Applicant == High AND House == Very_high ) AND Credit == Very_high ==> C15
#
# ### Rule Base 3 Combining
# - => Rule = C1 OR C2 OR C3 OR C4 OR C5 OR C6 OR C7 OR C8 OR C9 OR C10 OR C11 OR C12 OR C13 OR C14 OR C15

# In[32]:

def apply_credit_rules(house, income, interest, applicant):
    # house
    house_level_very_low = np.fmin(house, house_low)
    house_level_low = np.fmin(house, house_low)
    house_level_medium = np.fmin(house, house_medium)
    house_level_high = np.fmin(house, house_high)
    house_level_very_high = np.fmin(house, house_very_high)

    # person income
    p_income_level_low = fuzz.interp_membership(x_person_income, p_income_low, income)
    p_income_level_medium = fuzz.interp_membership(x_person_income, p_income_medium, income)
    p_income_level_high = fuzz.interp_membership(x_person_income, p_income_high, income)
    p_income_level_very_high = fuzz.interp_membership(x_person_income, p_income_very_high, income)

    # interest
    b_interest_level_low = fuzz.interp_membership(x_interest, b_interest_low, interest)
    b_interest_level_medium = fuzz.interp_membership(x_interest, b_interest_medium, interest)
    b_interest_level_high = fuzz.interp_membership(x_interest, b_interest_high, interest)

    # applicant
    applicant_level_low = np.fmin(applicant, applicant_low)
    applicant_level_medium = np.fmin(applicant, applicant_medium)
    applicant_level_high = np.fmin(applicant, applicant_high)

    # 1. If (Income is Low) and (Interest is Medium) then (Credit is Very_low)
    credit_act_very_low1 = and_rule(p_income_level_low, b_interest_level_medium, credit_very_low)
    # 2. If (Income is Low) and (Interest is High) then (Credit is Very_low)
    credit_act_very_low2 = and_rule(p_income_level_low, b_interest_level_high, credit_very_low)
    # 3. If (Income is Medium) and (Interest is High) then (Credit is Low)
    credit_act_low1 = and_rule(p_income_level_medium, b_interest_level_high, credit_low)
    # 4. If (Applicant is Low) then (Credit is Very_low)
    credit_act_very_low3 = np.fmin(applicant_level_low, credit_very_low)
    # 5. If (House is Very_low) then (Credit is Very_low)
    credit_act_very_low4 = np.fmin(house_level_very_low, credit_very_low)
    # 6. If (Applicant is Medium) and (House is Very_low) then (Credit is Low)
    credit_act_low2 = and_rule(applicant_level_medium, house_level_very_low, credit_low)
    # 7. If (Applicant is Medium) and (House is Low) then (Credit is Low)
    credit_act_low3 = and_rule(applicant_level_medium, house_level_low, credit_low)
    # 8. If (Applicant is Medium) and (House is Medium) then (Credit is Medium)
    credit_act_medium1 = and_rule(applicant_level_medium, house_level_medium, credit_medium)
    # 9. If (Applicant is Medium) and (House is High) then (Credit is High)
    credit_act_high1 = and_rule(applicant_level_medium, house_level_high, credit_high)
    # 10. If (Applicant is Medium) and (House is Very_high) then (Credit is High)
    credit_act_high2 = and_rule(applicant_level_medium, house_level_very_high, credit_high)
    # 11. If (Applicant is High) and (House is Very_low) then (Credit is Low)
    credit_act_low4 = and_rule(applicant_level_high, house_level_very_low, credit_low)
    # 12. If (Applicant is High) and (House is Low) then (Credit is Medium)
    credit_act_medium2 = and_rule(applicant_level_high, house_level_low, credit_medium)
    # 13. If (Applicant is High) and (House is Medium) then (Credit is High)
    credit_act_high3 = and_rule(applicant_level_high, house_level_medium, credit_high)
    # 14. If (Applicant is High) and (House is High) then (Credit is High)
    credit_act_high4 = and_rule(applicant_level_high, house_level_high, credit_high)
    # 15. If (Applicant is High) and (House is Very_high) then (Credit is Very_high)
    credit_act_very_high = and_rule(applicant_level_high, house_level_very_high, credit_very_high)

    step = or_rule(credit_act_very_low1, credit_act_very_low2, credit_act_very_low3)
    credit_act_very_low = np.fmax(step, credit_act_very_low4)

    step = or_rule(credit_act_low1, credit_act_low2, credit_act_low3)
    credit_act_low = np.fmax(step, credit_act_low4)

    credit_act_medium = np.fmax(credit_act_medium1, credit_act_medium2)

    step = or_rule(credit_act_high1, credit_act_high2, credit_act_high3)
    credit_act_high = np.fmax(step, credit_act_high4)
    # credit_act_very_high there is just one

    step = or_rule(credit_act_very_low, credit_act_low, credit_act_medium)
    credit = or_rule(step, credit_act_high, credit_act_very_high)

    return credit

# ## Example Output For Base 3

# In[33]:

# triying the function with example
credit_eval = apply_credit_rules(h_eval, 45, 4, a_eval)

plt.rcParams["figure.figsize"] = 15, 4
plt.plot(x_credit, credit_very_low, 'c', linestyle='--', linewidth=1.5, label='Very Low')
plt.plot(x_credit, credit_low, 'b', linestyle='--', linewidth=1.5, label='Low')
plt.plot(x_credit, credit_medium, 'g', linestyle='--', linewidth=1.5, label='Medium')
plt.plot(x_credit, credit_high, 'r', linestyle='--', linewidth=1.5, label='High')
plt.plot(x_credit, credit_very_high, 'y', linestyle='--', linewidth=1.5, label='Very High'),plt.title("Credit Value $ x10^3")
plt.legend()

plt.fill_between(x_credit, credit_eval)
plt.ylim(-0.1, 1.1)
plt.grid(True)
plt.show()

# ## Calling the ruleing functions sequentialy

# In[34]:

def apply_all_rules(market_value, location, assets, income, interest, verbose=0):
    house = apply_house_rules(market_value, location, verbose)
    applicant = apply_applicant_rules(assets,income, verbose)
    credit = apply_credit_rules(house, income, interest, applicant)
    return credit

# ## Example Output After All Bases

# In[35]:

credit = apply_all_rules(150, 3, 550, 45, 4, verbose=1)

plt.rcParams["figure.figsize"] = 15, 4
plt.plot(x_credit, credit_very_low, 'c', linestyle='--', linewidth=1.5, label='Very Low')
plt.plot(x_credit, credit_low, 'b', linestyle='--', linewidth=1.5, label='Low')
plt.plot(x_credit, credit_medium, 'g', linestyle='--', linewidth=1.5, label='Medium')
plt.plot(x_credit, credit_high, 'r', linestyle='--', linewidth=1.5, label='High')
plt.plot(x_credit, credit_very_high, 'y', linestyle='--', linewidth=1.5, label='Very High'),plt.title("Credit Value $ x10^3")
plt.legend()

plt.fill_between(x_credit, credit, color='b')
plt.ylim(-0.1, 1.1)
plt.grid(True)
plt.show()

# ## Making a Decision
# - After all the rules applied, we defuzify the output of the rules with mean of maximum and we generate a single value as a decision of the system

# In[36]:

def make_decision(market_value, location, assets, income, interest, verbose=0):
    credit = apply_all_rules(market_value, location, assets, income, interest, verbose)
    # defuzzification with mean of maximum
    defuzz_credit = fuzz.defuzz(x_credit, credit, 'mom')
    max_n = np.max(credit)

    if (verbose == 1):
        plt.rcParams["figure.figsize"] = 15, 5
        plt.plot(x_credit, credit_very_low, 'c', linestyle='--', linewidth=1.5, label='Very Low')
        plt.plot(x_credit, credit_low, 'b', linestyle='--', linewidth=1.5, label='Low')
        plt.plot(x_credit, credit_medium, 'g', linestyle='--', linewidth=1.5, label='Medium')
        plt.plot(x_credit, credit_high, 'r', linestyle='--', linewidth=1.5, label='High')
        plt.plot(x_credit, credit_very_high, 'y', linestyle='--', linewidth=1.5, label='Very High'),plt.title("Credit Value $ x10^3")
        plt.legend()

        plt.fill_between(x_credit, credit, color='b')
        plt.ylim(-0.1, 1.1)
        plt.grid(True)

        plt.plot(defuzz_credit, max_n, 'X', color='r')
        plt.show()

    print "Output: ", defuzz_credit, "x10^3 $"
    return defuzz_credit

# In[37]:

credit_decision = make_decision(150, 3, 550, 45, 4, verbose=1)


text:
# Main process
#importing some useful packages
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2

from moviepy.editor import VideoFileClip
from cameraCalibration   import get_undistorted_image
from combiningThresholds import filter_white_lane, filter_yellow_lane
# from adjust_filter_params import adjuct_filter_parameters
from geometries import get_perspective, draw_lanes, clear_lane_fifos

images_file_names = glob.glob('test_images/test*.jpg')
# images_file_names = glob.glob('video_images/vlcsnap-0000*.jpg')

# single_image = mpimg.imread('test_images/straight_lines1.jpg')
single_image = mpimg.imread('video_images/vlcsnap-00001.jpg')
# single_image = mpimg.imread('test_images/test1.jpg')

# def alphaBetaAuto_correction(img):
#     inputRange = np.amax(img) - np.amin(img)
#     wantedrange = 255.0
#     alpha = wantedrange / inputRange
#     beta = - alpha * np.amin(img)
#     return (img * alpha + beta).astype("uint8")

frame_counter = 0

def process_image(input_image):
    global frame_counter
    __image = np.copy(input_image)
    __image = get_undistorted_image(__image)
    # image = draw_region_of_interest(image)
    __image = cv2.blur(__image, (5, 5))
    __image = get_perspective(__image, 'b')

    white_line_binary = filter_white_lane(__image)
    yellow_line_binary = filter_yellow_lane(input_image)
    # __image[:, :, 0] = white_line_binary
    # __image[:, :, 1] = yellow_line_binary
    # __image[:, :, 2] = 0
    # __image[__image > 0] = 255
    binary_warped = np.uint8(white_line_binary + yellow_line_binary)

    __image, road = draw_lanes(binary_warped, input_image)
    __image = cv2.addWeighted(input_image, 1, __image, 1, 0)

    # __image = np.concatenate((__image, road), axis=1)
    frame_counter += 1

    # if frameCounter >= 120:
    #     plt.imshow(image)
    #     plt.show()

    return __image

# # mutiple image
# # --------------------------------------------------------------------
img = mpimg.imread(images_file_names[0])
disp_img_row1 = process_image(img)
for i in range(1, 3): # len(images_file_names)):
    filename = images_file_names[i]
    img = mpimg.imread(filename)
    clear_lane_fifos()
    img = process_image(img)
    disp_img_row1 = np.concatenate((disp_img_row1, img), axis=1)

img = mpimg.imread(images_file_names[3])
disp_img_row2 = process_image(img)
for i in range(4, len(images_file_names)):
    filename = images_file_names[i]
    img = mpimg.imread(filename)
    clear_lane_fifos()
    img = process_image(img)
    disp_img_row2 = np.concatenate((disp_img_row2, img), axis=1)

image = np.concatenate((disp_img_row1, disp_img_row2), axis=0)
# # --------------------------------------------------------------------

clear_lane_fifos()
image = process_image(single_image)
plt.imshow(image)
plt.show()
# # single_image = draw_region_of_interest(single_image, top_left, top_right, bottom_left, bottom_right)

# # offset = 150
# # top_left = (offset, 0)
# # top_right = (1280-offset , 0)
# # bottom_left = (offset, 721)
# # bottom_right = (1280-offset, 721)
# # image = draw_region_of_interest(image, top_left, top_right, bottom_left, bottom_right)

# plt.imshow(image)
# plt.show()

# adjuct_filter_parameters(image)

# f, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 20))
# ax1.set_title("Normal image")
# ax1.imshow(single_image)
# ax2.set_title("Bird's eye view")
# ax2.imshow(image)
# plt.show()
frame_counter = 0
clear_lane_fifos()

videoName = 'project_video.mp4'
# videoName = 'challenge_video.mp4'
# videoName = 'harder_challenge_video.mp4'
white_output = 'test_videos_output/' + videoName
clip1 = VideoFileClip(videoName)
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!
white_clip.write_videofile(white_output, audio=False)

ext: py

reason: import numpy


text:
"""
Tests for the aperture function of a spherical loudspeaker array
"""

import sys
from filehandling import read_matrix_from_mat

sys.path.append('./')

import pytest
import spharpy.spherical as sh
import numpy as np

def test_aperture_diag():
    n_max = 10
    rad_cap = 0.1
    rad_sphere = 1
    aperture = sh.aperture_vibrating_spherical_cap(n_max, rad_sphere, rad_cap)
    reference = read_matrix_from_mat('./tests/data/aperture_function.mat')
    np.testing.assert_allclose(aperture, reference)

max_stars_repo_name: mberz/spharpy

max_issues_repo_name: mberz/spharpy

max_forks_repo_name: mberz/spharpy


text:
import numpy as np
import ephem
from scipy.stats import norm

def gaussian(x, a, b, c, d):
    val = d + (a * np.exp(-(x - b)**2 / c**2))
    return val

def fehprior(feh):
    _fehprior = (0.8/0.15*np.exp(-0.5*(feh-0.016)**2./0.15**2.)
                 +0.2/0.22*np.exp(-0.5*(feh+0.15)**2./0.22**2.))
    return _fehprior

def avprior(av_in,data,i,dust,dist):
    # conversion from E(B-V) to Av from Schlafly & Finkbeiner
    fac = 2.742
    ebv_in = av_in/fac
    dm = 5.*np.log10(dist)-5.

    ra = data['ra'][i]*np.pi/180.
    dec = data['dec'][i]*np.pi/180.
    equ = ephem.Equatorial(ra, dec, epoch=ephem.J2000)
    gal = ephem.Galactic(equ)

    dsquared = ((gal.lon*180./np.pi - dust['lon'])**2
                + (gal.lat*180./np.pi - dust['lat'])**2 )
    pos = np.argmin(dsquared)
    ebv = dust['vals'][pos,:,:]
    dms =np.arange(4.0,19.5,0.5)
    ebvs = np.zeros(20)
    for j in range(0,len(ebvs)):
        ebvs[j] = np.interp(dm,dms,ebv[j,:])

    med = np.median(ebvs)
    std = np.std(ebvs)
    #print gal.lon*180./np.pi,gal.lat*180./np.pi,dm,med
    #pdb.set_trace()
    return fac * gaussian(ebv_in,1.,np.median(ebvs),np.std(ebvs),0.)

def getav(data,i,dust,dist):
    # conversion from E(B-V) to Av from Schlafly & Finkbeiner
    fac = 2.742
    dm = 5.*np.log10(dist)-5.
    ra = data['ra'][i]*np.pi/180.
    dec = data['dec'][i]*np.pi/180.
    equ = ephem.Equatorial(ra, dec, epoch=ephem.J2000)
    gal = ephem.Galactic(equ)

    dsquared = ((gal.lon*180./np.pi - dust['lon'])**2
                + (gal.lat*180./np.pi - dust['lat'])**2 )
    pos = np.argmin(dsquared)

    ebv = dust['vals'][pos,:,:]
    dms = np.arange(4.0,19.5,0.5)
    ebvs = np.zeros(20)
    for j in range(0,len(ebvs)):
        ebvs[j] = np.interp(dm,dms,ebv[j,:])

    med = np.median(ebvs)
    std = np.std(ebvs)
    #print gal.lon*180./np.pi,gal.lat*180./np.pi,dm,med
    #pdb.set_trace()

    #return fac*gaussian(ebv_in,1.,np.median(ebvs),np.std(ebvs),0.)
    return med,std

max_stars_repo_name: cosmicoder/isoclassify

max_issues_repo_name: cosmicoder/isoclassify

max_forks_repo_name: petigura/isoclassify

reason: import numpy,from scipy


text:
import glob
import io
import math
import time

import keras.backend as K
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from PIL import Image
from keras import Sequential, Input, Model
from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input
from keras.callbacks import TensorBoard
from keras.layers import Conv2D
from keras.layers import Dense
from keras.layers import ReLU
from keras.layers import Reshape
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.normalization import BatchNormalization
from keras.layers.pooling import MaxPooling2D
from keras.optimizers import Adam, SGD
from keras.preprocessing import image
from scipy.misc import imread, imsave
from scipy.stats import entropy

K.set_image_dim_ordering('tf')

np.random.seed(1337)

def build_generator():
    gen_model = Sequential()

    gen_model.add(Dense(input_dim=100, output_dim=2048))
    gen_model.add(ReLU())

    gen_model.add(Dense(256 * 8 * 8))
    gen_model.add(BatchNormalization())
    gen_model.add(ReLU())
    gen_model.add(Reshape((8, 8, 256), input_shape=(256 * 8 * 8,)))
    gen_model.add(UpSampling2D(size=(2, 2)))

    gen_model.add(Conv2D(128, (5, 5), padding='same'))
    gen_model.add(ReLU())

    gen_model.add(UpSampling2D(size=(2, 2)))

    gen_model.add(Conv2D(64, (5, 5), padding='same'))
    gen_model.add(ReLU())

    gen_model.add(UpSampling2D(size=(2, 2)))

    gen_model.add(Conv2D(3, (5, 5), padding='same'))
    gen_model.add(Activation('tanh'))
    return gen_model

def build_discriminator():
    dis_model = Sequential()
    dis_model.add(
        Conv2D(128, (5, 5),
               padding='same',
               input_shape=(64, 64, 3))
    )
    dis_model.add(LeakyReLU(alpha=0.2))
    dis_model.add(MaxPooling2D(pool_size=(2, 2)))

    dis_model.add(Conv2D(256, (3, 3)))
    dis_model.add(LeakyReLU(alpha=0.2))
    dis_model.add(MaxPooling2D(pool_size=(2, 2)))

    dis_model.add(Conv2D(512, (3, 3)))
    dis_model.add(LeakyReLU(alpha=0.2))
    dis_model.add(MaxPooling2D(pool_size=(2, 2)))

    dis_model.add(Flatten())
    dis_model.add(Dense(1024))
    dis_model.add(LeakyReLU(alpha=0.2))

    dis_model.add(Dense(1))
    dis_model.add(Activation('sigmoid'))

    return dis_model

def build_adversarial_model(gen_model, dis_model):
    model = Sequential()
    model.add(gen_model)
    dis_model.trainable = False
    model.add(dis_model)
    return model

def write_log(callback, name, loss, batch_no):
    """
    Write training summary to TensorBoard
    """
    # for name, value in zip(names, logs):
    summary = tf.Summary()
    summary_value = summary.value.add()
    summary_value.simple_value = loss
    summary_value.tag = name
    callback.writer.add_summary(summary, batch_no)
    callback.writer.flush()

def calculate_inception_score(images_path, batch_size=1, splits=10):
    # Create an instance of InceptionV3
    model = InceptionResNetV2()

    images = None
    for image_ in glob.glob(images_path):
        # Load image
        loaded_image = image.load_img(image_, target_size=(299, 299))

        # Convert PIL image to numpy ndarray
        loaded_image = image.img_to_array(loaded_image)

        # Another another dimension (Add batch dimension)
        loaded_image = np.expand_dims(loaded_image, axis=0)

        # Concatenate all images into one tensor
        if images is None:
            images = loaded_image
        else:
            images = np.concatenate([images, loaded_image], axis=0)

    # Calculate number of batches
    num_batches = (images.shape[0] + batch_size - 1) // batch_size

    probs = None

    # Use InceptionV3 to calculate probabilities
    for i in range(num_batches):
        image_batch = images[i * batch_size:(i + 1) * batch_size, :, :, :]
        prob = model.predict(preprocess_input(image_batch))

        if probs is None:
            probs = prob
        else:
            probs = np.concatenate([prob, probs], axis=0)

    # Calculate Inception scores
    divs = []
    split_size = probs.shape[0] // splits

    for i in range(splits):
        prob_batch = probs[(i * split_size):((i + 1) * split_size), :]
        p_y = np.expand_dims(np.mean(prob_batch, 0), 0)
        div = prob_batch * (np.log(prob_batch / p_y))
        div = np.mean(np.sum(div, 1))
        divs.append(np.exp(div))

    return np.mean(divs), np.std(divs)

def calculate_mode_score(gen_images_path, real_images_path, batch_size=32, splits=10):
    # Create an instance of InceptionV3
    model = InceptionResNetV2()

    # Load real images
    real_images = None
    for image_ in glob.glob(real_images_path):
        # Load image
        loaded_image = image.load_img(image_, target_size=(299, 299))

        # Convert PIL image to numpy ndarray
        loaded_image = image.img_to_array(loaded_image)

        # Another another dimension (Add batch dimension)
        loaded_image = np.expand_dims(loaded_image, axis=0)

        # Concatenate all images into one tensor
        if real_images is None:
            real_images = loaded_image
        else:
            real_images = np.concatenate([real_images, loaded_image], axis=0)

    # Load generated images
    gen_images = None
    for image_ in glob.glob(gen_images_path):
        # Load image
        loaded_image = image.load_img(image_, target_size=(299, 299))

        # Convert PIL image to numpy ndarray
        loaded_image = image.img_to_array(loaded_image)

        # Another another dimension (Add batch dimension)
        loaded_image = np.expand_dims(loaded_image, axis=0)

        # Concatenate all images into one tensor
        if gen_images is None:
            gen_images = loaded_image
        else:
            gen_images = np.concatenate([gen_images, loaded_image], axis=0)

    # Calculate number of batches for generated images
    gen_num_batches = (gen_images.shape[0] + batch_size - 1) // batch_size
    gen_images_probs = None
    # Use InceptionV3 to calculate probabilities of generated images
    for i in range(gen_num_batches):
        image_batch = gen_images[i * batch_size:(i + 1) * batch_size, :, :, :]
        prob = model.predict(preprocess_input(image_batch))

        if gen_images_probs is None:
            gen_images_probs = prob
        else:
            gen_images_probs = np.concatenate([prob, gen_images_probs], axis=0)

    # Calculate number of batches for real images
    real_num_batches = (real_images.shape[0] + batch_size - 1) // batch_size
    real_images_probs = None
    # Use InceptionV3 to calculate probabilities of real images
    for i in range(real_num_batches):
        image_batch = real_images[i * batch_size:(i + 1) * batch_size, :, :, :]
        prob = model.predict(preprocess_input(image_batch))

        if real_images_probs is None:
            real_images_probs = prob
        else:
            real_images_probs = np.concatenate([prob, real_images_probs], axis=0)

    # KL-Divergence: compute kl-divergence and mean of it
    num_gen_images = len(gen_images)
    split_scores = []

    for j in range(splits):
        gen_part = gen_images_probs[j * (num_gen_images // splits): (j + 1) * (num_gen_images // splits), :]
        real_part = real_images_probs[j * (num_gen_images // splits): (j + 1) * (num_gen_images // splits), :]
        gen_py = np.mean(gen_part, axis=0)
        real_py = np.mean(real_part, axis=0)
        scores = []
        for i in range(gen_part.shape[0]):
            scores.append(entropy(gen_part[i, :], gen_py))

        split_scores.append(np.exp(np.mean(scores) - entropy(gen_py, real_py)))

    final_mean = np.mean(split_scores)
    final_std = np.std(split_scores)

    return final_mean, final_std

def denormalize(img):
    img = (img + 1) * 127.5
    return img.astype(np.uint8)

def normalize(img):
    return (img - 127.5) / 127.5

def visualize_rgb(img):
    """
    Visualize a rgb image
    :param img: RGB image
    """
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(img)
    ax.axis("off")
    ax.set_title("Image")
    plt.show()

def save_rgb_img(img, path):
    """
    Save a rgb image
    """
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(img)
    ax.axis("off")
    ax.set_title("RGB Image")

    plt.savefig(path)
    plt.close()

def train():
    start_time = time.time()
    dataset_dir = "data/*.*"
    batch_size = 128
    z_shape = 100
    epochs = 10000
    dis_learning_rate = 0.005
    gen_learning_rate = 0.005
    dis_momentum = 0.5
    gen_momentum = 0.5
    dis_nesterov = True
    gen_nesterov = True

    dis_optimizer = SGD(lr=dis_learning_rate, momentum=dis_momentum, nesterov=dis_nesterov)
    gen_optimizer = SGD(lr=gen_learning_rate, momentum=gen_momentum, nesterov=gen_nesterov)

    # Load images
    all_images = []
    for index, filename in enumerate(glob.glob(dataset_dir)):
        all_images.append(imread(filename, flatten=False, mode='RGB'))

    X = np.array(all_images)
    X = (X - 127.5) / 127.5
    X = X.astype(np.float32)

    dis_model = build_discriminator()
    dis_model.compile(loss='binary_crossentropy', optimizer=dis_optimizer)

    gen_model = build_generator()
    gen_model.compile(loss='mse', optimizer=gen_optimizer)

    adversarial_model = build_adversarial_model(gen_model, dis_model)
    adversarial_model.compile(loss='binary_crossentropy', optimizer=gen_optimizer)

    tensorboard = TensorBoard(log_dir="logs/{}".format(time.time()), write_images=True, write_grads=True, write_graph=True)
    tensorboard.set_model(gen_model)
    tensorboard.set_model(dis_model)

    for epoch in range(epochs):
        print("--------------------------")
        print("Epoch:{}".format(epoch))

        dis_losses = []
        gen_losses = []

        num_batches = int(X.shape[0] / batch_size)

        print("Number of batches:{}".format(num_batches))
        for index in range(num_batches):
            print("Batch:{}".format(index))

            z_noise = np.random.normal(0, 1, size=(batch_size, z_shape))
            # z_noise = np.random.uniform(-1, 1, size=(batch_size, 100))

            generated_images = gen_model.predict_on_batch(z_noise)

            # visualize_rgb(generated_images[0])

            """
            Train the discriminator model
            """

            dis_model.trainable = True

            image_batch = X[index * batch_size:(index + 1) * batch_size]

            y_real = np.ones((batch_size, )) * 0.9
            y_fake = np.zeros((batch_size, )) * 0.1

            dis_loss_real = dis_model.train_on_batch(image_batch, y_real)
            dis_loss_fake = dis_model.train_on_batch(generated_images, y_fake)

            d_loss = (dis_loss_real+dis_loss_fake)/2
            print("d_loss:", d_loss)

            dis_model.trainable = False

            """
            Train the generator model(adversarial model)
            """
            z_noise = np.random.normal(0, 1, size=(batch_size, z_shape))
            # z_noise = np.random.uniform(-1, 1, size=(batch_size, 100))

            g_loss = adversarial_model.train_on_batch(z_noise, y_real)
            print("g_loss:", g_loss)

            dis_losses.append(d_loss)
            gen_losses.append(g_loss)

        """
        Sample some images and save them
        """
        if epoch % 100 == 0:
            z_noise = np.random.normal(0, 1, size=(batch_size, z_shape))
            gen_images1 = gen_model.predict_on_batch(z_noise)

            for img in gen_images1[:2]:
                save_rgb_img(img, "results/one_{}.png".format(epoch))

        print("Epoch:{}, dis_loss:{}".format(epoch, np.mean(dis_losses)))
        print("Epoch:{}, gen_loss: {}".format(epoch, np.mean(gen_losses)))

        """
        Save losses to Tensorboard after each epoch
        """
        write_log(tensorboard, 'discriminator_loss', np.mean(dis_losses), epoch)
        write_log(tensorboard, 'generator_loss', np.mean(gen_losses), epoch)

    """
    Save models
    """
    gen_model.save("generator_model.h5")
    dis_model.save("generator_model.h5")

    print("Time:", (time.time() - start_time))

if __name__ == '__main__':
    train()

max_stars_repo_name: dolgogae/GAN-projects

max_issues_repo_name: dolgogae/GAN-projects

max_forks_repo_name: dolgogae/GAN-projects


text:
from collections import namedtuple
from typing import Dict, List, Any

import numpy as np

from vcap import (
    Resize,
    DETECTION_NODE_TYPE,
    OPTION_TYPE,
    BaseStreamState)
from vcap_utils import BaseOpenVINOBackend

EMOTION_TYPES = ['neutral', 'happy', 'sad', 'surprise', 'anger']

class Backend(BaseOpenVINOBackend):

    def process_frame(self, frame: np.ndarray,
                      detection_node: DETECTION_NODE_TYPE,
                      options: Dict[str, OPTION_TYPE],
                      state: BaseStreamState) -> DETECTION_NODE_TYPE:
        crop = Resize(frame).crop_bbox(detection_node.bbox).frame

        input_dict, _ = self.prepare_inputs(crop)
        prediction = self.send_to_batch(input_dict).result()

        emotion_id = int(prediction["prob_emotion"].argmax())
        emotion = EMOTION_TYPES[emotion_id]
        emotion_score = float(prediction["prob_emotion"].flatten()[emotion_id])

        detection_node.attributes["emotion"] = emotion
        detection_node.extra_data["emotion_confidence"] = emotion_score

max_stars_repo_name: aotuai/capsule-zoo

max_issues_repo_name: aotuai/capsule-zoo

max_forks_repo_name: aotuai/capsule-zoo


text:
# remove this for windows platforms (resource only works on unix)
import resource
import heapq
import sys
import numpy as np
from abc import ABC, abstractmethod
from collections import deque

# Board Representation

class Board(object):
    # obj globals
    origin = None
    state = None
    operation = None
    n = 0
    zero = None
    cost = 0

    def __init__(self, state, origin=None, operation=None, n=0):
        # the original board
        self.origin = origin
        # the state it is in now
        self.state = np.array(state)
        # the operation done last
        self.operation = operation
        # the depth of the search
        self.n = n
        # find the starting state
        self.zero = self.discover_start()
        # calculate the cost based on the current state and the goal defined + the current depth
        self.cost = self.n + self.calc_manhattan_dist()

    def __lt__(self, other):
        # function to compare costs
        if self.cost != other.cost:
            return self.cost < other.cost
        else:
            operat = {'Up': 0, 'Down': 1, 'Left': 2, 'Right': 3}
            return operat[self.operation] < operat[other.operation]

    def __str__(self):
        # str method to nicely display the board
        return str(self.state[:3]) + '\n' \
               + str(self.state[3:6]) + '\n' \
               + str(self.state[6:]) + ' ' + str(self.n) + str(self.operation) + '\n'

    def check_obj(self):
        # function to check if the objective is reached
        if np.array_equal(self.state, np.arange(9)):
            return True
        else:
            return False

    def discover_start(self):
        # state start
        for i in range(9):
            if self.state[i] == 0:
                return i

    def calc_manhattan_dist(self):
        # manhattan dist calculator
        state = self.calc_index(self.state)
        goal = self.calc_index(np.arange(9))
        return sum((abs(state // 3 - goal // 3) + abs(state % 3 - goal % 3))[1:])

    @staticmethod
    def calc_index(state):
        indexes = np.array(range(9))
        for x, y in enumerate(state):
            indexes[y] = x
        return indexes

    def change_state(self, i, j):
        changed_state = np.array(self.state)
        changed_state[i], changed_state[j] = changed_state[j], changed_state[i]
        return changed_state

    # define how the moves affect the blocks.

    def move_up(self):
        if self.zero > 2:
            return Board(self.change_state(self.zero, self.zero - 3), self, 'Up', self.n + 1)
        else:
            return None

    def move_down(self):
        if self.zero < 6:
            return Board(self.change_state(self.zero, self.zero + 3), self, 'Down', self.n + 1)
        else:
            return None

    def move_left(self):
        if self.zero % 3 != 0:
            return Board(self.change_state(self.zero, self.zero - 1), self, 'Left', self.n + 1)
        else:
            return None

    def move_right(self):
        if (self.zero + 1) % 3 != 0:
            return Board(self.change_state(self.zero, self.zero + 1), self, 'Right', self.n + 1)
        else:
            return None

    # get the neighbors given the current state
    def neighbors(self):
        neighbors = [self.move_up(), self.move_down(), self.move_left(), self.move_right()]
        return list(filter(None, neighbors))

    __repr__ = __str__

# Create A base class for Search methods

class Search(ABC):
    solution = None
    frontier = None
    nodes_expanded = 0
    max_n = 0
    explored_nodes = set()
    initial_state = None

    def __init__(self, initial_state):
        self.initial_state = initial_state

    def parent_node(self):
        current = self.solution
        chain = [current]
        while current.origin is not None:
            chain.append(current.origin)
            current = current.origin
        return chain

    @property
    def path(self):
        path = [node.operation for node in self.parent_node()[-2::-1]]
        return path

    @abstractmethod
    def search(self):
        pass

    def set_solution(self, board):
        self.solution = board
        self.nodes_expanded = len(self.explored_nodes) - len(self.frontier) - 1
        # return self.solution

# Search Methods

class BreadthFirstSearch(Search):
    def __init__(self, initial_state):
        super(BreadthFirstSearch, self).__init__(initial_state)
        self.frontier = deque()

    def search(self):
        self.frontier.append(self.initial_state)
        while self.frontier:
            board = self.frontier.popleft()
            self.explored_nodes.add(tuple(board.state))
            if board.check_obj():
                self.set_solution(board)
                break
            # breadth search
            for neighbor in board.neighbors():
                if tuple(neighbor.state) not in self.explored_nodes:
                    self.frontier.append(neighbor)
                    self.explored_nodes.add(tuple(neighbor.state))
                    self.max_n = max(self.max_n, neighbor.n)
        return

class DepthFirstSearch(Search):
    def __init__(self, initial_state):
        super(DepthFirstSearch, self).__init__(initial_state)
        self.frontier = []

    def search(self):
        self.frontier.append(self.initial_state)
        while self.frontier:
            board = self.frontier.pop()
            # add the new tried states to the explored nodes
            self.explored_nodes.add(tuple(board.state))
            # if the explored node matches the solution stop the algorithm
            if board.check_obj():
                self.set_solution(board)
                break
            # depth search
            for neighbor in board.neighbors()[::-1]:
                if tuple(neighbor.state) not in self.explored_nodes:
                    self.frontier.append(neighbor)
                    self.explored_nodes.add(tuple(neighbor.state))
                    self.max_n = max(self.max_n, neighbor.n)
        return

class AStarSearch(Search):
    def __init__(self, initial_state):
        super(AStarSearch, self).__init__(initial_state)
        self.frontier = []

    def search(self):
        heapq.heappush(self.frontier, self.initial_state)
        # start with the initial state
        while self.frontier:
            board = heapq.heappop(self.frontier)
            # add the new tried states to the explored nodes
            self.explored_nodes.add(tuple(board.state))
            # if the explored node matches the solution stop the algorithm
            if board.check_obj():
                self.set_solution(board)
                break
            for neighbor in board.neighbors():
                if tuple(neighbor.state) not in self.explored_nodes:
                    heapq.heappush(self.frontier, neighbor)
                    self.explored_nodes.add(tuple(neighbor.state))
                    self.max_n = max(self.max_n, neighbor.n)
        return

def driver():
    problem = Board(np.array(eval(sys.argv[2])))
    method = sys.argv[1]
    if 'bfs' in method:
        solver = BreadthFirstSearch(problem)
    elif 'dfs' in method:
        solver = DepthFirstSearch(problem)
    elif 'ast' in method:
        solver = AStarSearch(problem)
    else:
        print("Invalid Arguments, call the application like this:\npython3 driver.py <method> <board>")
        solver = None
        exit(0)
    solver.search()

    file = open(f'output.txt', 'w')
    # paths we went to find the goal
    file.write('path_to_goal: ' + str(solver.path) + '\n')
    # how many pathing were used
    file.write('cost_of_path: ' + str(len(solver.path)) + '\n')
    file.write('nodes_expanded: ' + str(solver.nodes_expanded) + '\n')
    file.write('nodes_explored: ' + str(len(solver.explored_nodes)) + '\n')
    # how much the search went on
    file.write('search_depth: ' + str(solver.solution.n) + '\n')
    # maximum searching depth
    file.write('max_search_depth: ' + str(solver.max_n) + '\n')
    # resources used
    file.write('running_time: ' + str(resource.getrusage(resource.RUSAGE_SELF).ru_utime + \
                                      resource.getrusage(resource.RUSAGE_SELF).ru_stime) + '\n')
    file.write('max_ram_usage: ' + str(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024))
    file.close()

if __name__ == "__main__":
    driver()


text:
import os
import math
import torch
import shutil
import logging
import transformers
from datasets import load_dataset
import pandas as pd
import numpy as np
from theconf import Config as C
from transformers import BertForSequenceClassification, Trainer, TrainingArguments, BertTokenizerFast
import joblib

try:
    from .data import augment
    from .train import compute_metrics
    from .search import search_policy
    from .utils.raw_data_utils import get_examples
    from .custom_dataset import general_dataset
    from .text_networks import get_model, num_class, get_num_class
    from .common import get_logger, add_filehandler
    from .utils.train_tfidf import train_tfidf
    from .transforms import Compose
    from .data import Augmentation
    from .custom_dataset import general_dataset
    from .utils.raw_data_utils import get_examples, general_split

except:
    from data import augment
    from train import compute_metrics
    from search import search_policy
    from utils.raw_data_utils import get_examples
    from custom_dataset import general_dataset
    from text_networks import get_model, num_class, get_num_class
    from common import get_logger, add_filehandler
    from utils.train_tfidf import train_tfidf
    from transforms import Compose
    from data import Augmentation
    from custom_dataset import general_dataset
    from utils.raw_data_utils import get_examples, general_split

def get_all_datasets(dataset, n_aug, policy, test_size):
    """ get augmented train, valid and full test datasets """
    C.get()['n_aug'] = n_aug
    text_key = C.get()['dataset']['text_key']

    transform_train = Compose([])
    tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')
    transform_train.transforms.insert(0, Augmentation(policy))

    train_dataset = load_dataset(dataset, split='train')
    test_dataset = load_dataset(dataset, split='test')

    class_num = train_dataset.features['label'].num_classes
    all_train_examples = get_examples(train_dataset, text_key)

    train_examples, valid_examples = general_split(all_train_examples, test_size=test_size, train_size=1-test_size)

    test_examples = get_examples(test_dataset, text_key)

    train_dataset = general_dataset(train_examples, tokenizer, text_transform=transform_train)
    val_dataset = general_dataset(valid_examples, tokenizer, text_transform=None)
    test_dataset = general_dataset(test_examples, tokenizer, text_transform=None)

    return train_dataset, val_dataset, test_dataset

def train_bert(tag, augmented_train_dataset, valid_dataset, test_dataset, policy_opt, save_path=None, only_eval=False):
    transformers.logging.set_verbosity_info()
    logger = get_logger('Text AutoAugment')
    logger.setLevel(logging.INFO)

    config = C.get()
    dataset_type = config['dataset']['name']
    model_type = config['model']['type']
    C.get()['tag'] = tag
    text_key = C.get()['dataset']['text_key']

    tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')
    train_dataset = augmented_train_dataset

    do_train = True
    logging_dir = os.path.join('logs/%s_%s/%s' % (dataset_type, model_type, tag))

    if save_path and os.path.exists(save_path):
        model_name_or_path = save_path
    else:
        model_name_or_path = 'bert-base-uncased'
        only_eval = False
        logger.info('"%s" file not found. skip to pretrain weights...' % save_path)

    if only_eval:
        do_train = False

    if policy_opt:
        logging_dir = None
        per_device_train_batch_size = config['per_device_train_batch_size']
    else:
        per_device_train_batch_size = int(config[
                                              'per_device_train_batch_size'] / torch.cuda.device_count())  # To make batch size equal to that in policy opt phase

    print('per_device_train_batch_size: ', per_device_train_batch_size)
    warmup_steps = math.ceil(len(train_dataset) / config['per_device_train_batch_size'] * config[
        'epoch'] * 0.1)  # number of warmup steps for learning rate scheduler
    print('warmup_steps: ', warmup_steps)
    # create model
    training_args = TrainingArguments(
        output_dir=save_path,  # output directory
        do_train=do_train,
        do_eval=True,
        evaluation_strategy="epoch",
        num_train_epochs=config['epoch'],  # total number of training epochs
        per_device_train_batch_size=per_device_train_batch_size,  # batch size per device during training
        per_device_eval_batch_size=config['per_device_eval_batch_size'],  # batch size for evaluation
        warmup_steps=warmup_steps,
        learning_rate=float(config['lr']),
        logging_dir=logging_dir,  # directory for storing logs
        load_best_model_at_end=True,
        metric_for_best_model="eval_accuracy",
        seed=config['seed']
    )
    model = BertForSequenceClassification.from_pretrained(model_name_or_path, num_labels=get_num_class(dataset_type))

    trainer = Trainer(
        model=model,  # the instantiated 🤗 Transformers model to be trained
        args=training_args,  # training arguments, defined above
        train_dataset=train_dataset,  # training dataset
        eval_dataset=valid_dataset,  # evaluation dataset
        compute_metrics=compute_metrics,
    )

    # logger.info("evaluating on train set")
    # result = trainer.evaluate(eval_dataset=train_dataset)

    if do_train:
        trainer.train()
        if not policy_opt:
            trainer.save_model()
            dirs = os.listdir(save_path)
            for file in dirs:
                if file.startswith("checkpoint-"):
                    shutil.rmtree(os.path.join(save_path, file))

    logger.info("evaluating on test set")
    # note that when policy_opt, the test_dataset is only a subset of true test_dataset, used for evaluating policy
    result = trainer.evaluate(eval_dataset=test_dataset)

    result['n_dist'] = train_dataset.aug_n_dist
    result['opt_object'] = result['eval_accuracy']

    # logger.info("Predicting on test set")
    # result = trainer.predict(test_dataset)
    # logits = result.predictions
    # predictions = np.argmax(logits, axis=1)

    # predict_df = pd.read_csv('%s.tsv' % dataset_type, sep='\t')
    # predict_df['prediction'] = predictions
    # predict_df.to_csv('%s.tsv' % dataset_type, sep='\t', index=False)

    if policy_opt:
        shutil.rmtree(save_path)
    return result

if __name__ == '__main__':
    _ = C('confs/bert_imdb_example.yaml')
    dataset_type = C.get()['dataset']['name']
    model_type = C.get()['model']['type']

    # search augmentation policy for specific dataset
    # search_policy(dataset='imdb', abspath='/home/renshuhuai/text-autoaugment' )

    # get augmented train dataset, valid and full test datasets
    configfile = 'bert_imdb_example.yaml'
    policy_path = '/home/wangyuxiang/Text_AutoAugment/text-autoaugment/final_policy/imdb_Bert_seed59_train-npc50_n-aug4_ir1.00_taa.pkl'
    policy = joblib.load(policy_path)
    augmented_train_dataset, valid_dataset, test_dataset = get_all_datasets(dataset_type, n_aug=4, policy=policy, test_size=0.3)

    # training
    train_tfidf(dataset_type)  # calculate tf-idf score for TS and TI operations

    tag = '%s_%s_with_found_policy' % (dataset_type, model_type)
    save_path = os.path.join('models', tag)

    result = train_bert(tag, augmented_train_dataset, valid_dataset, test_dataset, policy_opt=False,
                        save_path=save_path, only_eval=False)

    for k,v in result.items():
        print('%s:%s' % (k,v))

max_stars_repo_name: wolvecap/text-autoaugment

max_issues_repo_name: wolvecap/text-autoaugment

max_forks_repo_name: wolvecap/text-autoaugment


text:
import os
import subprocess
import platform
import numpy as np
import distutils.dir_util, os
import tempfile
import time

def str2floatTrap(someStr):
    """
    Checks if there is either a starting '('  or an ending ')' around the input string and returns a string without them.
    :param someStr: input string
    :return:
    """

    tempStr = someStr

    if tempStr.startswith('('):
        tempStr = tempStr[1:]

    if tempStr.endswith(')'):
        tempStr = tempStr[:len(tempStr) - 1]

    return float(tempStr)

def removeFileIfExists(fName, tries=10, delay=0.2):
    if os.path.isfile(fName):
        i = 0
        while i < tries:
            try:
                os.remove(fName)
            except:  # Probably a file in use error
                i += 1
                time.sleep(delay)
            else:
                break

def chunks(l, n):
    ''' Split a list into smaller n-sized lists'''
    n = max(1, n)
    return (l[i:i+n] for i in range(0, len(l), n))

class LMInput:

    def __init__(self, swcFileNames, measure1names, average=False, nBins=10, measure2names=None, PCA=False, specificity=None):

        for measure in measure1names:
            assert not measure == 'XYZ', 'Measure \'XYZ\' cannot be used with getMeasure()'

        if measure2names is not None:
            for measure in measure2names:
                assert not measure == 'XYZ', 'Measure \'XYZ\' cannot be used with getMeasure()'

        for name in swcFileNames:
            if not os.path.exists(name):
                raise IOError("File cannot be found:" + os.path.join(os.getcwd(),name))

        for name in swcFileNames:
            if " " in name:
                raise IOError("L-Measure does not support spaces in file paths:" + os.path.join(os.getcwd(),name))

        self.swcFileNames = swcFileNames
        self.measure1names = measure1names
        self.measure2names = measure2names
        self.numberOfMeasures = len(self.measure1names)
        self.numberOfSWCFiles = len(self.swcFileNames)
        self.nBins = nBins
        self.average = average
        self.PCA = PCA
        self.specificity = specificity

        self.functionRef = {'Soma_Surface'           :0,
                            'N_stems'                :1,
                            'N_bifs'                 :2,
                            'N_branch'               :3,
                            'N_tips'                 :4,
                            'Width'                  :5,
                            'Height'                 :6,
                            'Depth'                  :7,
                            'Type'                   :8,
                            'Diameter'               :9,
                            'Diameter_pow'           :10,
                            'Length'                 :11,
                            'Surface'                :12,
                            'SectionArea'            :13,
                            'Volume'                 :14,
                            'EucDistance'            :15,
                            'PathDistance'           :16,
                            'XYZ'                    :17,
                            'Branch_Order'           :18,
                            'Terminal_degree'        :19,
                            'TerminalSegment'        :20,
                            'Taper_1'                :21,
                            'Taper_2'                :22,
                            'Branch_pathlength'      :23,
                            'Contraction'            :24,
                            'Fragmentation'          :25,
                            'Daughter_Ratio'         :26,
                            'Parent_Daughter_Ratio'  :27,
                            'Partition_asymmetry'    :28,
                            'Rall_Power'             :29,
                            'Pk'                     :30,
                            'Pk_classic'             :31,
                            'Pk_2'                   :32,
                            'Bif_ampl_local'         :33,
                            'Bif_ampl_remote'        :34,
                            'Bif_tilt_local'         :35,
                            'Bif_tilt_remote'        :36,
                            'Bif_torque_local'       :37,
                            'Bif_torque_remote'      :38,
                            'Last_parent_diam'       :39,
                            'Diam_threshold'         :40,
                            'HillmanThreshold'       :41,
                            'Helix'                  :43,
                            'Fractal_Dim'            :44}

        self.parse_specificity()

    def parse_specificity(self):
        if self.specificity is not None:
            from pyparsing import Word, alphanums, nums, ParseException

            # Match patterns like: 'Type > 1 and EucDistance > 100.0'
            pattern = (Word("andxornt") + Word(alphanums + "_") + Word("<=>") + Word(nums + ".")) * (1, None)

            try:
                parsed = pattern.parseString("and " + self.specificity) # L-measure requires the first logical to be "AND"

            except ParseException:
                raise Exception("Could not parse specificity expression: '" + self.specificity +
                                "'. Examples of allowed expressions: 'Type > 1' or 'Type > 1 and EucDistance > 100.0'")

            # Each condition has exactly 4 elements
            conditions = chunks(parsed, 4)

            conditions_parsed = []

            # Validated each conditional
            for cond in conditions:

                # Parse the condition elements
                logical = cond[0]
                func = cond[1]
                comparison = cond[2]

                try:
                    value = float(cond[3])
                except:
                    raise Exception("Specificity: Found: '" + value + "', but expected a number")

                # Validate the condition terms
                if logical not in ('and', 'or'):
                    raise Exception("Specificity: Found: '"+logical+"', but expected either 'and' or 'or'")

                if func not in self.functionRef:
                    raise Exception("Specificity: Found: '"+func+"', but expected one of " + str(self.functionRef.keys()))
                elif func == "XYZ":
                    raise Exception("Specificity: Function 'XYZ' is not allowed")

                if comparison not in ("<", "==", ">"):
                    raise Exception("Specificity: Found: '" + comparison + "', but expected either '<', '>', or '=='")

                # Save the parsed condition
                conditions_parsed.append({"logical":logical, "func":func, "comparison":comparison, "value":value})

            # Replace the original with the parsed
            self.specificity = conditions_parsed

    def get_specificity_string(self):
        '''
        L-Measure specificity strings look like this:

            -l1,1,8,1714.0 -l2,1,0,2714.0 -l1,1,1,3714.0

        Where, each "-l...." is a separate condition
        The integers represent logical operator, comparison operator, and function in that order

        Logical Operator        or:2, and:1, first always 1/and
        Comparison operator:    1: <, 2: =, 3: >
        Function:  		int
        Value: 			float
        '''

        # L-measure specificity condition constants
        logical_map = { "or": "2", "and": "1" }
        comparison_map = { "<": "1", "==": "2", ">": "3" }

        result = ""
        for cond in self.specificity:
            result += ("-l" +
                       logical_map[cond["logical"]] + "," +
                       comparison_map[cond["comparison"]] + "," +
                       str(self.functionRef[cond["func"]]) + "," +
                       str(cond["value"]) + " ")

        return result

    def validate_measure_name(self, name):
        if name is not None:
            if  name not in self.functionRef:
                from pprint import pformat as pf
                raise Exception("Unknown measure: '"+str(name)+"'. Must be one of the following: \n" + pf(self.functionRef.keys(), indent=4))

        return True

    def getFunctionString(self):

        functionString = ''

        for measureInd, measure1Name in enumerate(self.measure1names):

            if self.measure2names is None:
                self.validate_measure_name(measure1Name)
                functionString += '-f' + str(self.functionRef[measure1Name]) + ',0,0,' + str(self.nBins) + ' '

            else:
                self.validate_measure_name(measure1Name)
                self.validate_measure_name(self.measure2names[measureInd])

                measure2 = self.functionRef[self.measure2names[measureInd]]

                functionString += '-f' + str(self.functionRef[measure1Name]) + ',f' + str(measure2) + ',' \
                                      + str(int(self.average)) + ',0,' + str(self.nBins) + ' '

        return functionString

    def writeLMIn(self, inputFName,  outputFileName):
        """
        Write the input file for L-measure.

        :param inputFName: Name of the input file for L-Measure
        :param outputFileName: Name of the output file of L-Measure
        :param PCA: Whether to rotate the points along the principal component axes (used for height, width, depth)
        :rtype: None
        """

        outputLine = '-s' + outputFileName + (" -C" if self.PCA else "")

        # Create parent folders if they don't exist
        distutils.dir_util.mkpath(os.path.dirname(inputFName))

        with open(inputFName, 'w') as LMInputFile:
            if self.specificity is not None:
                LMInputFile.write(self.get_specificity_string() + '\n')

            LMInputFile.write(self.getFunctionString() + '\n')
            LMInputFile.write(outputLine + '\n')

            for swcFileName in self.swcFileNames:
                LMInputFile.write(swcFileName + '\n')

            LMInputFile.flush()

class LMRun():

    def __init__(self):

        osName = platform.system()
        if osName == 'Linux':
            (bit, linkage) = platform.architecture()
            self.LMPath = 'LMLinux' + bit[:2] + '/'
            self.LMExec = 'lmeasure'

        if osName == 'Darwin':
            self.LMPath = 'LMMac'
            self.LMExec = 'lmeasure'

        if osName == 'Windows':
            self.LMPath = 'LMwin'
            self.LMExec = 'Lm.exe'

        self.packagePrefix = os.path.split(__file__)[0]

    def runLM(self, LMInputFName, LMOutputFName, LMLogFName):
        """
        Runs the appropriate L-measure executable with the required arguments.

        """

        removeFileIfExists(LMOutputFName)
        removeFileIfExists(LMLogFName)

        with open(LMLogFName, 'w') as LMLogFle:
            lm_path = os.path.join(self.packagePrefix, self.LMPath, self.LMExec)

            if 'Darwin' in platform.system():
                os.system('chmod +x "%s"' % lm_path)

            subprocess.call([lm_path, LMInputFName],
                            stdout=LMLogFle, stderr=LMLogFle)

            try:
                self.LMOutputFile = open(LMOutputFName, 'r')
                self.LMOutputFile.close()

            except: raise(Exception('No Output file created by Lmeasure. Check ' + LMLogFName))

class BasicLMOutput:

    def __init__(self, lmInput):

        # WholeCellMeasures is a (# of swc files given)x7 numpy array. The seven entries along the
        # second dimension correspond respectively to
        # TotalSum, CompartmentsConsidered, Compartments Discarded, Minimum, Average, Maximum, StdDev

        self.LMOutput = []
        self.lmInput = lmInput
        self.LMOutputTemplate = dict(measure1BinCentres=None,
                                     measure1BinCounts=None,
                                     measure2BinAverages=None,
                                     measure2BinStdDevs=None,
                                     measure2BinSums=None,
                                     WholeCellMeasures=None,
                                     WholeCellMeasuresDict=None)

        self.WholeCellMeasuresDictTemplate = {
            "TotalSum":None,
            "CompartmentsConsidered":None,
            "CompartmentsDiscarded":None,
            "Minimum":None,
            "Average":None,
            "Maximum":None,
            "StdDev":None
        }

    def saveOneLine(self, measureInd, swcFileInd):
        return

    def readOutput(self, outputFile):

        self.outputFile = outputFile

        for swcFileInd in range(self.lmInput.numberOfSWCFiles):
            for measureInd in range(self.lmInput.numberOfMeasures):
                self.saveOneLine(measureInd, swcFileInd)

    def readOneLine(self, start=0, end=None):

        tempStr = self.outputFile.readline()
        tempWords = tempStr.split('\t')
        if end is None:
            return np.asarray([str2floatTrap(x) for x in tempWords[start:]])
        else:
            return np.asarray([str2floatTrap(x) for x in tempWords[start:end]])

class getMeasureLMOutput(BasicLMOutput):

    def __init__(self, lmInput):

        BasicLMOutput.__init__(self, lmInput)

        for x in self.lmInput.measure1names:
            tempCopy = self.LMOutputTemplate.copy()
            tempCopy['WholeCellMeasures'] = np.zeros([self.lmInput.numberOfSWCFiles, 7])

            # Pre-populate with blank copies of the output dictionary
            tempCopy['WholeCellMeasuresDict'] = [self.WholeCellMeasuresDictTemplate.copy() for i in range(self.lmInput.numberOfSWCFiles)]

            self.LMOutput.append(tempCopy)

    def saveOneLine(self, measureInd, swcFileInd):

        line = self.readOneLine(2)

        self.LMOutput[measureInd]['WholeCellMeasures'][swcFileInd, :] = line

        # Parse the values from the line into corresponding dict keys
        swcDict = self.LMOutput[measureInd]['WholeCellMeasuresDict'][swcFileInd]

        swcDict["TotalSum"] = line[0]
        swcDict["CompartmentsConsidered"] = line[1]
        swcDict["CompartmentsDiscarded"] = line[2]
        swcDict["Minimum"] = line[3]
        swcDict["Average"] = line[4]
        swcDict["Maximum"] = line[5]
        swcDict["StdDev"] = line[6]

class getMeasureDistLMOutput(BasicLMOutput):

    def __init__(self, lmInput):

        BasicLMOutput.__init__(self, lmInput)

        for x in self.lmInput.measure1names:
            tempCopy = self.LMOutputTemplate.copy()
            tempCopy['measure1BinCentres'] = np.zeros([self.lmInput.numberOfSWCFiles, self.lmInput.nBins])
            tempCopy['measure1BinCounts'] = np.zeros([self.lmInput.numberOfSWCFiles, self.lmInput.nBins])
            self.LMOutput.append(tempCopy)

    def saveOneLine(self, measureInd, swcFileInd):

        self.LMOutput[measureInd]['measure1BinCentres'][swcFileInd, :] = self.readOneLine(2, self.lmInput.nBins + 2)
        self.LMOutput[measureInd]['measure1BinCounts'][swcFileInd, :] = self.readOneLine(2, self.lmInput.nBins + 2)

class getMeasureDepLMOutput(BasicLMOutput):

    def __init__(self, lmInput):

        BasicLMOutput.__init__(self, lmInput)

        for x in self.lmInput.measure1names:

            tempCopy = self.LMOutputTemplate.copy()
            tempCopy['measure1BinCentres'] = np.zeros([self.lmInput.numberOfSWCFiles, self.lmInput.nBins])

            if self.lmInput.average:

                tempCopy['measure2BinAverages'] = np.zeros([self.lmInput.numberOfSWCFiles, self.lmInput.nBins])
                tempCopy['measure2BinStdDevs'] = np.zeros([self.lmInput.numberOfSWCFiles, self.lmInput.nBins])

            else:

                tempCopy['measure2BinSums'] = np.zeros([self.lmInput.numberOfSWCFiles, self.lmInput.nBins])

            self.LMOutput.append(tempCopy)

    def saveOneLine(self, measureInd, swcFileInd):

        self.LMOutput[measureInd]['measure1BinCentres'][swcFileInd, :] = self.readOneLine(2, self.lmInput.nBins + 2)

        if self.lmInput.average:

            self.LMOutput[measureInd]['measure2BinAverages'][swcFileInd, :] = \
                self.readOneLine(2, self.lmInput.nBins + 2)
            self.LMOutput[measureInd]['measure2BinStdDevs'][swcFileInd, :] = self.readOneLine(1, self.lmInput.nBins + 1)

        else:

            self.LMOutput[measureInd]['measure2BinSums'][swcFileInd, :] = self.readOneLine(2, self.lmInput.nBins + 2)

def LMIOFunction(mode, swcFileNames, measure1Names, measure2Names=None, average=False, nBins=10, PCA=False, specificity=None):

    tempDir = tempfile.TemporaryDirectory()

    LMInputFName = os.path.join(tempDir.name, 'LMInput.txt')
    LMOutputFName = os.path.join(tempDir.name, 'LMOutput.txt')
    LMLogFName = os.path.join(tempDir.name, 'LMLog.txt')

    lmInput = LMInput(swcFileNames, measure1Names, average, nBins, measure2Names, PCA, specificity)
    lmInput.writeLMIn(LMInputFName, LMOutputFName)

    if mode == 'getMeasure':
        lmOutput = getMeasureLMOutput(lmInput)

    if mode == 'getDist':
        lmOutput = getMeasureDistLMOutput(lmInput)

    if mode == 'getDep':
        lmOutput = getMeasureDepLMOutput(lmInput)

    lmRun = LMRun()
    lmRun.runLM(LMInputFName, LMOutputFName, LMLogFName)

    with open(LMOutputFName, 'r') as outputFile:
        lmOutput.readOutput(outputFile)

    tempDir.cleanup()

    return lmOutput.LMOutput

def getMeasure(measureNames, swcFileNames, PCA=False, specificity=None):
    '''
    Computes a list of measures of a list of SWC files.

    :param measureNames: A list of measures. See "Function list" in: http://cng.gmu.edu:8080/Lm/help/index.htm
    :param swcFileNames: A list of paths to SWC files
    :param PCA: If True, rotates the cell to "stand up right". See: http://cng.gmu.edu:8080/Lm/help/Width.htm
    :param specificity: An expression of filter conditions. E.g. 'Type > 1 and EucDistance > 100.0'. See:
    http://cng.gmu.edu:8080/Lm/help/speci.htm
    :return: A list in the form of:
                                                       V-- measure index           V-- file index
        print("Surface area of first file:",    result[0]["WholeCellMeasuresDict"][0]["TotalSum"])
        print("Mean diameter in first file:",   result[1]["WholeCellMeasuresDict"][0]["Average"])
        print("Surface area of 2nd file:",      result[0]["WholeCellMeasuresDict"][1]["TotalSum"])
        print("Mean diameter in 2nd file:",     result[1]["WholeCellMeasuresDict"][1]["Average"])

    '''
    return LMIOFunction('getMeasure', swcFileNames, measureNames, PCA=PCA, specificity=specificity)

def getOneMeasure(measure, swcFile, PCA=False, specificity=None):
    '''
    Computes one measure statistics of one SWC file

    :param measure: Name of the measure to use. See "Function list" in: http://cng.gmu.edu:8080/Lm/help/index.htm
    :param swcFile: Path to SWC file
    :param PCA: If True, rotates the cell to "stand up right". See: http://cng.gmu.edu:8080/Lm/help/Width.htm
    :param specificity: An expression of filter conditions. E.g. 'Type > 1 and EucDistance > 100.0'. See:
    :return: A dictionary with measure statistics
    '''
    result = getMeasure([measure], [swcFile], PCA=PCA, specificity=specificity)

    return result[0]["WholeCellMeasuresDict"][0]

def getMeasureDistribution(measureNames, swcFileNames, nBins=10, PCA=False, specificity=None):
    '''

    :param measureNames:
    :param swcFileNames:
    :param nBins:
    :param PCA: If True, rotates the cell to "stand up right". See: http://cng.gmu.edu:8080/Lm/help/Width.htm
    :param specificity: An expression of filter conditions. E.g. 'Type > 1 and EucDistance > 100.0'. See:
    :return:
    '''
    return LMIOFunction('getDist', swcFileNames, measureNames, measureNames, nBins=nBins, PCA=PCA, specificity=specificity)

def getMeasureDependence(measure1Names, measure2Names, swcFileNames, nBins=10, average=True, PCA=False, specificity=None):
    '''

    :param measure1Names:
    :param measure2Names:
    :param swcFileNames:
    :param nBins:
    :param average:
    :param PCA: If True, rotates the cell to "stand up right". See: http://cng.gmu.edu:8080/Lm/help/Width.htm
    :param specificity: An expression of filter conditions. E.g. 'Type > 1 and EucDistance > 100.0'. See:
    :return:
    '''
    return LMIOFunction('getDep', swcFileNames, measure1Names, measure2Names, average, nBins, PCA=PCA, specificity=specificity)

max_stars_repo_name: rgerkin/pylmeasure

max_issues_repo_name: rgerkin/pylmeasure

max_forks_repo_name: rgerkin/pylmeasure


text:
import tflib as lib

import numpy as np
import tensorflow as tf

def Batchnorm(name, axes, inputs, is_training=None, stats_iter=None, update_moving_stats=True, fused=True, labels=None, n_labels=None, extensible=False):
    """conditional batchnorm (dumoulin et al 2016) for BCHW conv filtermaps"""
    if axes != [0,2,3]:
        raise Exception('unsupported')
    mean, var = tf.nn.moments(inputs, axes, keep_dims=True)
    shape = mean.get_shape().as_list() # shape is [1,n,1,1]
    if extensible:
        offset_m = lib.extensible_param(name + '.offset', np.zeros([n_labels, shape[1]], dtype='float32'))
        scale_m = lib.extensible_param(name + '.scale', np.ones([n_labels, shape[1]], dtype='float32'))
    else:
        offset_m = lib.param(name+'.offset', np.zeros([n_labels,shape[1]], dtype='float32'))
        scale_m = lib.param(name+'.scale', np.ones([n_labels,shape[1]], dtype='float32'))
    offset = tf.nn.embedding_lookup(offset_m, labels)
    scale = tf.nn.embedding_lookup(scale_m, labels)
    result = tf.nn.batch_normalization(inputs, mean, var, offset[:,:,None,None], scale[:,:,None,None], 1e-5)
    return result, offset_m, scale_m

def BatchnormB(name, axes, inputs, is_training=None, stats_iter=None, update_moving_stats=True, fused=True, labels=None, n_labels=None, n_start_labels=None):
    """ based on conditional batchnorm (dumoulin et al 2016) for BCHW conv filtermaps"""
    if axes != [0,2,3]:
        raise Exception('unsupported')
    mean, var = tf.nn.moments(inputs, axes, keep_dims=True)
    shape = mean.get_shape().as_list() # shape is [1,n,1,1]
    init_scale = np.zeros([n_labels, shape[1]], dtype='float32')
    init_scale[:n_start_labels] = 1.0
    offset_m = lib.param(name + '.offset', np.zeros([n_labels, shape[1]], dtype='float32'))
    scale_m = lib.param(name + '.scale', init_scale)
    offset = tf.matmul(labels, offset_m)
    scale = tf.matmul(labels, scale_m)
    result = tf.nn.batch_normalization(inputs, mean, var, offset[:,:,None,None], scale[:,:,None,None], 1e-5)
    return result, offset_m, scale_m