-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeartTestViewController.swift
More file actions
212 lines (164 loc) · 6 KB
/
Copy pathHeartTestViewController.swift
File metadata and controls
212 lines (164 loc) · 6 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// HeartTestViewController.swift
// vitalizer
//
// Created by Ritik Suryawanshi on 11/10/20.
// Copyright © 2020 riddhi gupta. All rights reserved.
//
import UIKit
import CoreML
import Firebase
import FirebaseDatabase
class HeartTestViewController: UIViewController {
@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var yesbutton: UIButton!
@IBOutlet weak var nobutton: UIButton!
@IBOutlet weak var exitButton: UIButton!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var Done: UIButton!
@IBOutlet weak var answer: UITextField!
var model = final_heart_model()
let allQuestions = HeartQuestion()
var pickedAnswer : Int = 0
var questionNumber : Int = 1
var arr = [Int]()
var ref : DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
questionLabel.text=allQuestions.list[questionNumber].questionText
Done.isHidden = true
// Do any additional setup after loading the view.
}
@IBAction func yesButtonPressed(_ sender: UIButton) {
arr.append(1)
}
@IBAction func noButtonPressed(_ sender: UIButton) {
arr.append(0)
}
@IBAction func nextButtonPressed(_ sender: UIButton) {
questionNumber+=1
nextQuestion()
}
func nextQuestion() {
if questionNumber < 14
{
if questionNumber == 0
{
questionLabel.text=allQuestions.list[questionNumber].questionText
guard let uid = Auth.auth().currentUser?.uid else
{
return
}
ref = Database.database().reference()
let userReference = ref.child("users").child(uid)
userReference.observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]
{
let ageText = dictionary["age"] as! String
let genderText = dictionary["gender"] as! String
if genderText == "Male"
{
self.arr.append(1)
self.questionNumber+=1
self.nextQuestion()
}
else{
self.arr.append(0)
self.questionNumber+=1
self.nextQuestion()
}
self.arr.append(Int(ageText)!)
self.questionNumber+=1
self.nextQuestion()
}
})
}
if questionNumber == 1
{
questionLabel.text=allQuestions.list[questionNumber].questionText
guard let uid = Auth.auth().currentUser?.uid else
{
return
}
ref = Database.database().reference()
let userReference = ref.child("users").child(uid)
userReference.observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject]
{
let ageText = dictionary["age"] as! String
let genderText = dictionary["gender"] as! String
if genderText == "Male"
{
self.arr.append(1)
self.questionNumber+=1
self.nextQuestion()
}
else{
self.arr.append(0)
self.questionNumber+=1
self.nextQuestion()
}
self.arr.append(Int(ageText)!)
self.questionNumber+=1
self.nextQuestion()
}
})
}
if questionNumber == 4 || questionNumber > 7
{
questionLabel.text=allQuestions.list[questionNumber].questionText
var bcd = Int(answer.text ?? "hi") ?? 20
arr.append(bcd ?? 20)
}
else{
questionLabel.text=allQuestions.list[questionNumber].questionText
}
}
else
{
print(arr)
mlModel()
Done.isHidden = false
//performSegue(withIdentifier: "covidtoresult", sender: nil)
}}
func mlModel()
{
do{
var muArray = try? MLMultiArray(shape: [1,14], dataType: MLMultiArrayDataType.float32)
muArray = convertToMLArray(arr)
let res = try model.prediction(dense_7_input: muArray! )
print(res.classLabel)
print(res.Identity["TenYearCHD"])
var abc = res.Identity["TenYearCHD"]! * 100
var str = String(abc)
let values = ["heartprob": str]
Database.database().reference().child("users").child("\(Auth.auth().currentUser!.uid)").updateChildValues(values) {
(error:Error?, ref:DatabaseReference) in
if let error = error {
print("Data could not be saved: \(error).")
} else {
print("Data saved successfully!")
}
}
print(abc)
}
catch {
print("got exception")
}
}
func convertToMLArray(_ input: [Int]) -> MLMultiArray {
let mlArray = try? MLMultiArray(shape: [1,14], dataType: MLMultiArrayDataType.float32)
for i in 0...13 {
mlArray?[i] = NSNumber(value: input[i])
}
return mlArray!
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}