Spaces:
Runtime error
Runtime error
File size: 7,006 Bytes
fc601fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# -*- 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"):
diagnosis = diagnose(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)
st.success(diagnosis)
# 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']) |