Spaces:
Runtime error
Runtime error
# -*- coding: utf-8 -*- | |
"""Copy of Skin NTDs.ipynb | |
Automatically generated by Colaboratory. | |
Original file is located at | |
https://colab.research.google.com/drive/110qV0zVen9O-zjd-wOuHOKURfR0lUfc7 | |
""" | |
import numpy as np | |
import skfuzzy as fuzz | |
from skfuzzy import control as ctrl | |
import matplotlib.pyplot as plt | |
import streamlit as st | |
# Create fuzzy variables & membership functions for lump | |
lump_amount = ctrl.Antecedent(np.arange(0, 11, 1), 'lump_amount') | |
lump_amount['1 to 3'] = fuzz.trimf(lump_amount.universe, [0, 0, 3]) | |
lump_amount['more than 3'] = fuzz.trimf(lump_amount.universe, [3, 11, 11]) | |
lump_hardness = ctrl.Antecedent(np.arange(0, 11, 1), 'lump_hardness') | |
lump_hardness['soft'] = fuzz.trimf(lump_hardness.universe, [0, 0, 5]) | |
lump_hardness['solid'] = fuzz.trimf(lump_hardness.universe, [5, 11, 11]) | |
lump_size = ctrl.Antecedent(np.arange(0, 11, 1), 'lump_size') | |
lump_size['small'] = fuzz.trimf(lump_size.universe, [0, 0, 5]) | |
lump_size['large'] = fuzz.trimf(lump_size.universe, [5, 11, 11]) | |
lump_location = ctrl.Antecedent(np.arange(0, 11, 1), 'lump_location') | |
lump_location['localized'] = fuzz.trapmf(lump_location.universe, [0, 0, 3, 6]) | |
lump_location['widespread'] = fuzz.trapmf(lump_location.universe, [3, 6, 11, 11]) | |
lump_itch = ctrl.Antecedent(np.arange(0, 11, 1), 'lump_itch') | |
lump_itch['not itchy'] = fuzz.trapmf(lump_itch.universe, [0, 0, 3, 6]) | |
lump_itch['itchy'] = fuzz.trapmf(lump_itch.universe, [3, 6, 7, 8]) | |
lump_itch['very itchy'] = fuzz.trapmf(lump_itch.universe, [7, 8, 11, 11]) | |
lump_pus = ctrl.Antecedent(np.arange(0, 2, 1), 'lump_pus') | |
lump_pus['present'] = fuzz.trapmf(lump_pus.universe, [0, 0, 1, 1]) | |
lump_pus['absent'] = fuzz.trapmf(lump_pus.universe, [1, 1, 2, 2]) | |
# Create fuzzy variables for ulcer | |
ulcer_location = ctrl.Antecedent(np.arange(0, 3, 1), 'ulcer_location') | |
ulcer_location['feet or hands'] = fuzz.trapmf(ulcer_location.universe, [0, 0, 1, 1]) | |
ulcer_location['others'] = fuzz.trapmf(ulcer_location.universe, [1, 1, 2, 2]) | |
ulcer_edge = ctrl.Antecedent(np.arange(0, 2, 1), 'ulcer_edge') | |
ulcer_edge['irregular'] = fuzz.trapmf(ulcer_edge.universe, [0, 0, 1, 1]) | |
ulcer_edge['regular'] = fuzz.trapmf(ulcer_edge.universe, [1, 1, 2, 2]) | |
ulcer_pain = ctrl.Antecedent(np.arange(0, 2, 1), 'ulcer_pain') | |
ulcer_pain['painless'] = fuzz.trapmf(ulcer_pain.universe, [0, 0, 1, 1]) | |
ulcer_pain['painful'] = fuzz.trapmf(ulcer_pain.universe, [1, 1, 2, 2]) | |
# Create fuzzy variables for swollen limb | |
swollen_limb = ctrl.Antecedent(np.arange(0, 2, 1), 'swollen_limb') | |
swollen_limb['unilateral'] = fuzz.trapmf(swollen_limb.universe, [0, 0, 1, 1]) | |
swollen_limb['bilateral'] = fuzz.trapmf(swollen_limb.universe, [1, 1, 2, 2]) | |
swollen_location = ctrl.Antecedent(np.arange(0, 3, 1), 'swollen_location') | |
swollen_location['localised'] = fuzz.trapmf(swollen_location.universe, [0, 0, 1, 1]) | |
swollen_location['widespread'] = fuzz.trapmf(swollen_location.universe, [1, 1, 2, 2]) | |
swelling_sensation = ctrl.Antecedent(np.arange(0, 2, 1), 'swelling_sensation') | |
swelling_sensation['painless'] = fuzz.trapmf(swelling_sensation.universe, [0, 0, 1, 1]) | |
swelling_sensation['painful'] = fuzz.trapmf(swelling_sensation.universe, [1, 1, 2, 2]) | |
# Create fuzzy variables for patch | |
patch_itch = ctrl.Antecedent(np.arange(0, 11, 1), 'patch_itch') | |
patch_itch['itchy'] = fuzz.trimf(patch_itch.universe, [0, 0, 5]) | |
patch_itch['non-itchy'] = fuzz.trimf(patch_itch.universe, [5, 11, 11]) | |
patch_texture = ctrl.Antecedent(np.arange(0, 11, 1), 'patch_texture') | |
patch_texture['smooth'] = fuzz.trimf(patch_texture.universe, [0, 0, 5]) | |
patch_texture['scaly'] = fuzz.trimf(patch_texture.universe, [5, 11, 11]) | |
patch_wetness = ctrl.Antecedent(np.arange(0, 11, 1), 'patch_wetness') | |
patch_wetness['wet'] = fuzz.trimf(patch_wetness.universe, [0, 0, 5]) | |
patch_wetness['dry'] = fuzz.trimf(patch_wetness.universe, [5, 11, 11]) | |
# Create fuzzy variable for diagnosis | |
diagnosis = ctrl.Consequent(np.arange(0, 11, 1), 'diagnosis') | |
diagnosis['leprosy'] = fuzz.trapmf(diagnosis.universe, [0, 0, 2, 5]) | |
diagnosis['scabies'] = fuzz.trapmf(diagnosis.universe, [3, 5, 7, 8]) | |
diagnosis['lymphatic_filariasis'] = fuzz.trapmf(diagnosis.universe, [6, 8, 10, 10]) | |
# Fuzzy rules | |
rule1 = ctrl.Rule(lump_amount['more than 3'] & lump_hardness['solid'] & lump_size['large'] & patch_itch['non-itchy'] | patch_texture['scaly'] | patch_wetness['dry'], diagnosis['leprosy']) | |
rule2 = ctrl.Rule(lump_amount['more than 3'] & lump_hardness['soft'] & lump_size['small'] | lump_location['widespread'] | lump_itch['very itchy'] | lump_pus['present'], diagnosis['scabies']) | |
rule3 = ctrl.Rule(swollen_limb['unilateral'] | swollen_location['localised'] | swelling_sensation['painful'], diagnosis['lymphatic_filariasis']) | |
# Create control system | |
diagnosis_ctrl = ctrl.ControlSystem([rule1, rule2, rule3]) | |
# Create control system simulator | |
diagnosis_fuzzy = ctrl.ControlSystemSimulation(diagnosis_ctrl) | |
import streamlit as st | |
# Create the user interface | |
st.title("Disease Diagnosis") | |
lump_amount = st.selectbox("Lump amount", ["1 to 3", "more than 3"]) | |
lump_hardness = st.selectbox("Lump hardness", ["soft", "solid"]) | |
lump_size = st.selectbox("Lump size", ["small", "large"]) | |
lump_location = st.text_input("Lump location", ["localised", "widespread"]) | |
lump_itch = st.selectbox("Lump itch", ["not itchy", "slightly itchy", "very itchy"]) | |
lump_pus = st.selectbox("Lump pus", ["absent", "present"]) | |
ulcer_location = st.selectbox("Ulcer location", ["feet", "hands", "other"]) | |
ulcer_edge = st.selectbox("Ulcer edge", ["regular", "irregular"]) | |
ulcer_pain = st.selectbox("Ulcer pain", ["painful", "painless"]) | |
swollen_limb = st.selectbox("Swollen limb", ["unilateral", "bilateral"]) | |
swollen_location = st.text_input("Swollen location", ["localised", "widespread"]) | |
swelling_sensation = st.selectbox("Swelling sensation", ["painful", "painless"]) | |
patch_itch = st.selectbox("Patch itch", ["non-itchy", "itchy"]) | |
patch_texture = st.selectbox("Patch texture", ["smooth", "scaly"]) | |
patch_wetness = st.selectbox("Patch wetness", ["dry", "wet"]) | |
if st.button("Diagnose"): | |
#st.success(diagnosis(lump_amount, lump_hardness, lump_size, lump_location, lump_itch, lump_pus, ulcer_location, ulcer_edge, ulcer_pain, swollen_limb, swollen_location, swelling_sensation, patch_itch, patch_texture, patch_wetness)) | |
diagnosis_fuzzy.input['lump_amount'] = lump_amount | |
diagnosis_fuzzy.input['lump_hardness'] = lump_hardness | |
diagnosis_fuzzy.input['lump_size'] = lump_size | |
diagnosis_fuzzy.input['lump_location'] = lump_location | |
diagnosis_fuzzy.input['lump_itch'] = lump_itch | |
diagnosis_fuzzy.input['lump_pus'] = lump_pus | |
diagnosis_fuzzy.input['ulcer_location'] = ulcer_location | |
diagnosis_fuzzy.input['ulcer_edge'] = ulcer_edge | |
diagnosis_fuzzy.input['ulcer_pain'] = ulcer_pain | |
diagnosis_fuzzy.input['swollen_limb'] = swollen_limb | |
diagnosis_fuzzy.input['swollen_location'] = swollen_location | |
diagnosis_fuzzy.input['swelling_sensation'] = swelling_sensation | |
diagnosis_fuzzy.input['patch_itch'] = patch_itch | |
diagnosis_fuzzy.input['patch_texture'] = patch_texture | |
diagnosis_fuzzy.input['patch_wetness'] = patch_wetness | |
# Print the diagnosis | |
#print(diagnosis_fuzzy.output['diagnosis']) | |
# Defuzzification | |
# Pass crisp input values to the control system and get the diagnosis | |
# diagnosis_fuzzy.input['lump_amount'] = 5 | |
# diagnosis_fuzzy.input['lump_hardness'] = 7 | |
# diagnosis_fuzzy.input['lump_size'] = 6 | |
# diagnosis_fuzzy.input['lump_location'] = 8 | |
# diagnosis_fuzzy.input['lump_itch'] = 9 | |
# diagnosis_fuzzy.input['lump_pus'] = 1 | |
# diagnosis_fuzzy.input['swollen_limb'] = 1 | |
# diagnosis_fuzzy.input['swollen_location'] = 10 | |
# diagnosis_fuzzy.input['swelling_sensation'] = 8 | |
# diagnosis_fuzzy.input['swelling_color'] = 7 | |
diagnosis_fuzzy.compute() | |
# Print the diagnosis | |
print(diagnosis_fuzzy.output['diagnosis']) |