-
Notifications
You must be signed in to change notification settings - Fork 7
/
diagnosis.go
126 lines (110 loc) · 3.18 KB
/
diagnosis.go
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
package infermedica
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/pkg/errors"
)
// DiagnosisReq is a struct to request diagnosis
type DiagnosisReq struct {
Sex Sex `json:"sex"`
Age int `json:"age"`
Evidences []Evidence `json:"evidence"`
Extras DiagnosisReqExras `json:"extras"`
}
type DiagnosisReqCovid struct {
Sex Sex `json:"sex"`
Age int `json:"age"`
Evidences []EvidenceCovid `json:"evidence"`
Extras DiagnosisReqExras `json:"extras"`
}
// DiagnosisReqExras contains extra params for DiagnosisReq
type DiagnosisReqExras struct {
DisableGroups bool `json:"disable_groups"`
}
// DiagnosisRes is a response struct for diagnosis
type DiagnosisRes struct {
Question Question `json:"question"`
Conditions []DiagnosisConditionRes `json:"conditions"`
ShouldStop bool `json:"should_stop"`
Extras interface{} `json:"extras"`
}
// QuestionType is a list of question types
type QuestionType string
const (
// QuestionTypeSingle single question
QuestionTypeSingle QuestionType = "single"
// QuestionTypeGroupSingle question group
QuestionTypeGroupSingle QuestionType = "group_single"
// QuestionTypeGroupMultiple multiple question groups
QuestionTypeGroupMultiple QuestionType = "group_multiple"
)
func (qt QuestionType) Ptr() *QuestionType { return &qt }
func (qt QuestionType) String() string { return string(qt) }
func (qt *QuestionType) IsValid() bool {
_, err := QuestionTypeFromString(qt.String())
if err != nil {
return false
}
return true
}
func QuestionTypeFromString(x string) (QuestionType, error) {
switch strings.ToLower(x) {
case "single":
return QuestionTypeSingle, nil
case "group_single":
return QuestionTypeGroupSingle, nil
case "group_multiple":
return QuestionTypeGroupMultiple, nil
default:
return "", fmt.Errorf("Unexpected value for Question Type: %q", x)
}
}
// Question struct
type Question struct {
Type QuestionType `json:"type"`
Text string `json:"text"`
Items []QuestionItem `json:"items"`
}
// QuestionItem question item struct
type QuestionItem struct {
ID string `json:"id"`
Name string `json:"name"`
Choices []QuestionItemChoice `json:"choices"`
}
// QuestionItemChoice question item choice struct
type QuestionItemChoice struct {
ID EvidenceChoiceID `json:"id"`
Label string `json:"label"`
}
// DiagnosisConditionRes is a response struct for condition + probability
type DiagnosisConditionRes struct {
Condition
Probability float64 `json:"probability"`
}
// Diagnosis is a func to request diagnosis for given data
func (a *App) Diagnosis(dr DiagnosisReq) (*DiagnosisRes, error) {
if !dr.Sex.IsValid() {
return nil, errors.New("Unexpected value for Sex")
}
req, err := a.prepareRequest("POST", "diagnosis", dr)
if err != nil {
return nil, err
}
client := &http.Client{
Timeout: time.Second * 5,
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
r := DiagnosisRes{}
err = json.NewDecoder(res.Body).Decode(&r)
if err != nil {
return nil, err
}
return &r, nil
}