secretunicornx commited on
Commit
fc601fe
1 Parent(s): 053e811

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +149 -0
app.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Copy of Skin NTDs.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/110qV0zVen9O-zjd-wOuHOKURfR0lUfc7
8
+ """
9
+
10
+
11
+
12
+ import numpy as np
13
+ import skfuzzy as fuzz
14
+ from skfuzzy import control as ctrl
15
+ import matplotlib.pyplot as plt
16
+ import streamlit as st
17
+
18
+ # Create fuzzy variables & membership functions for lump
19
+ lump_amount = ctrl.Antecedent(np.arange(0, 11, 1), 'lump_amount')
20
+ lump_amount['1 to 3'] = fuzz.trimf(lump_amount.universe, [0, 0, 3])
21
+ lump_amount['more than 3'] = fuzz.trimf(lump_amount.universe, [3, 11, 11])
22
+
23
+ lump_hardness = ctrl.Antecedent(np.arange(0, 11, 1), 'lump_hardness')
24
+ lump_hardness['soft'] = fuzz.trimf(lump_hardness.universe, [0, 0, 5])
25
+ lump_hardness['solid'] = fuzz.trimf(lump_hardness.universe, [5, 11, 11])
26
+
27
+ lump_size = ctrl.Antecedent(np.arange(0, 11, 1), 'lump_size')
28
+ lump_size['small'] = fuzz.trimf(lump_size.universe, [0, 0, 5])
29
+ lump_size['large'] = fuzz.trimf(lump_size.universe, [5, 11, 11])
30
+
31
+ lump_location = ctrl.Antecedent(np.arange(0, 11, 1), 'lump_location')
32
+ lump_location['localized'] = fuzz.trapmf(lump_location.universe, [0, 0, 3, 6])
33
+ lump_location['widespread'] = fuzz.trapmf(lump_location.universe, [3, 6, 11, 11])
34
+
35
+ lump_itch = ctrl.Antecedent(np.arange(0, 11, 1), 'lump_itch')
36
+ lump_itch['not itchy'] = fuzz.trapmf(lump_itch.universe, [0, 0, 3, 6])
37
+ lump_itch['itchy'] = fuzz.trapmf(lump_itch.universe, [3, 6, 7, 8])
38
+ lump_itch['very itchy'] = fuzz.trapmf(lump_itch.universe, [7, 8, 11, 11])
39
+
40
+ lump_pus = ctrl.Antecedent(np.arange(0, 2, 1), 'lump_pus')
41
+ lump_pus['present'] = fuzz.trapmf(lump_pus.universe, [0, 0, 1, 1])
42
+ lump_pus['absent'] = fuzz.trapmf(lump_pus.universe, [1, 1, 2, 2])
43
+
44
+ # Create fuzzy variables for ulcer
45
+ ulcer_location = ctrl.Antecedent(np.arange(0, 3, 1), 'ulcer_location')
46
+ ulcer_location['feet or hands'] = fuzz.trapmf(ulcer_location.universe, [0, 0, 1, 1])
47
+ ulcer_location['others'] = fuzz.trapmf(ulcer_location.universe, [1, 1, 2, 2])
48
+
49
+ ulcer_edge = ctrl.Antecedent(np.arange(0, 2, 1), 'ulcer_edge')
50
+ ulcer_edge['irregular'] = fuzz.trapmf(ulcer_edge.universe, [0, 0, 1, 1])
51
+ ulcer_edge['regular'] = fuzz.trapmf(ulcer_edge.universe, [1, 1, 2, 2])
52
+
53
+ ulcer_pain = ctrl.Antecedent(np.arange(0, 2, 1), 'ulcer_pain')
54
+ ulcer_pain['painless'] = fuzz.trapmf(ulcer_pain.universe, [0, 0, 1, 1])
55
+ ulcer_pain['painful'] = fuzz.trapmf(ulcer_pain.universe, [1, 1, 2, 2])
56
+
57
+ # Create fuzzy variables for swollen limb
58
+ swollen_limb = ctrl.Antecedent(np.arange(0, 2, 1), 'swollen_limb')
59
+ swollen_limb['unilateral'] = fuzz.trapmf(swollen_limb.universe, [0, 0, 1, 1])
60
+ swollen_limb['bilateral'] = fuzz.trapmf(swollen_limb.universe, [1, 1, 2, 2])
61
+
62
+ swollen_location = ctrl.Antecedent(np.arange(0, 3, 1), 'swollen_location')
63
+ swollen_location['localised'] = fuzz.trapmf(swollen_location.universe, [0, 0, 1, 1])
64
+ swollen_location['widespread'] = fuzz.trapmf(swollen_location.universe, [1, 1, 2, 2])
65
+
66
+ swelling_sensation = ctrl.Antecedent(np.arange(0, 2, 1), 'swelling_sensation')
67
+ swelling_sensation['painless'] = fuzz.trapmf(swelling_sensation.universe, [0, 0, 1, 1])
68
+ swelling_sensation['painful'] = fuzz.trapmf(swelling_sensation.universe, [1, 1, 2, 2])
69
+
70
+ # Create fuzzy variables for patch
71
+ patch_itch = ctrl.Antecedent(np.arange(0, 11, 1), 'patch_itch')
72
+ patch_itch['itchy'] = fuzz.trimf(patch_itch.universe, [0, 0, 5])
73
+ patch_itch['non-itchy'] = fuzz.trimf(patch_itch.universe, [5, 11, 11])
74
+
75
+ patch_texture = ctrl.Antecedent(np.arange(0, 11, 1), 'patch_texture')
76
+ patch_texture['smooth'] = fuzz.trimf(patch_texture.universe, [0, 0, 5])
77
+ patch_texture['scaly'] = fuzz.trimf(patch_texture.universe, [5, 11, 11])
78
+
79
+ patch_wetness = ctrl.Antecedent(np.arange(0, 11, 1), 'patch_wetness')
80
+ patch_wetness['wet'] = fuzz.trimf(patch_wetness.universe, [0, 0, 5])
81
+ patch_wetness['dry'] = fuzz.trimf(patch_wetness.universe, [5, 11, 11])
82
+
83
+ # Create fuzzy variable for diagnosis
84
+ diagnosis = ctrl.Consequent(np.arange(0, 11, 1), 'diagnosis')
85
+ diagnosis['leprosy'] = fuzz.trapmf(diagnosis.universe, [0, 0, 2, 5])
86
+ diagnosis['scabies'] = fuzz.trapmf(diagnosis.universe, [3, 5, 7, 8])
87
+ diagnosis['lymphatic_filariasis'] = fuzz.trapmf(diagnosis.universe, [6, 8, 10, 10])
88
+
89
+ # Fuzzy rules
90
+ 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'])
91
+ 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'])
92
+ rule3 = ctrl.Rule(swollen_limb['unilateral'] | swollen_location['localised'] | swelling_sensation['painful'], diagnosis['lymphatic filariasis'])
93
+
94
+
95
+ # Create control system
96
+ diagnosis_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
97
+
98
+ # Create control system simulator
99
+ diagnosis_fuzzy = ctrl.ControlSystemSimulation(diagnosis_ctrl)
100
+
101
+ import streamlit as st
102
+
103
+ # Create the user interface
104
+ st.title("Disease Diagnosis")
105
+
106
+ lump_amount = st.selectbox("Lump amount", ["1 to 3", "more than 3"])
107
+ lump_hardness = st.selectbox("Lump hardness", ["soft", "solid"])
108
+ lump_size = st.selectbox("Lump size", ["small", "large"])
109
+ lump_location = st.text_input("Lump location", ["localised", "widespread"])
110
+ lump_itch = st.selectbox("Lump itch", ["not itchy", "slightly itchy", "very itchy"])
111
+ lump_pus = st.selectbox("Lump pus", ["absent", "present"])
112
+
113
+ ulcer_location = st.selectbox("Ulcer location", ["feet", "hands", "other"])
114
+ ulcer_edge = st.selectbox("Ulcer edge", ["regular", "irregular"])
115
+ ulcer_pain = st.selectbox("Ulcer pain", ["painful", "painless"])
116
+
117
+ swollen_limb = st.selectbox("Swollen limb", ["unilateral", "bilateral"])
118
+ swollen_location = st.text_input("Swollen location", ["localised", "widespread"])
119
+ swelling_sensation = st.selectbox("Swelling sensation", ["painful", "painless"])
120
+
121
+ patch_itch = st.selectbox("Patch itch", ["non-itchy", "itchy"])
122
+ patch_texture = st.selectbox("Patch texture", ["smooth", "scaly"])
123
+ patch_wetness = st.selectbox("Patch wetness", ["dry", "wet"])
124
+
125
+
126
+ if st.button("Diagnose"):
127
+ 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)
128
+ st.success(diagnosis)
129
+
130
+ # Print the diagnosis
131
+ print(diagnosis_fuzzy.output['diagnosis'])
132
+
133
+ # Defuzzification
134
+ # Pass crisp input values to the control system and get the diagnosis
135
+ diagnosis_fuzzy.input['lump_amount'] = 5
136
+ diagnosis_fuzzy.input['lump_hardness'] = 7
137
+ diagnosis_fuzzy.input['lump_size'] = 6
138
+ diagnosis_fuzzy.input['lump_location'] = 8
139
+ diagnosis_fuzzy.input['lump_itch'] = 9
140
+ diagnosis_fuzzy.input['lump_pus'] = 1
141
+ diagnosis_fuzzy.input['swollen_limb'] = 1
142
+ diagnosis_fuzzy.input['swollen_location'] = 10
143
+ diagnosis_fuzzy.input['swelling_sensation'] = 8
144
+ diagnosis_fuzzy.input['swelling_color'] = 7
145
+
146
+ diagnosis_fuzzy.compute()
147
+
148
+ # Print the diagnosis
149
+ print(diagnosis_fuzzy.output['diagnosis'])