-
Notifications
You must be signed in to change notification settings - Fork 1
/
quantification-multi.go
163 lines (130 loc) · 5.22 KB
/
quantification-multi.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
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
package wsHandler
import (
"context"
"errors"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/api/quantification"
"github.com/pixlise/core/v4/api/ws/wsHelpers"
"github.com/pixlise/core/v4/core/errorwithstatus"
protos "github.com/pixlise/core/v4/generated-protos"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Anyone can retrieve a quant z-stack if they have quant messaging permissions
func HandleQuantCombineListGetReq(req *protos.QuantCombineListGetReq, hctx wsHelpers.HandlerContext) (*protos.QuantCombineListGetResp, error) {
if err := wsHelpers.CheckStringField(&req.ScanId, "ScanId", 1, wsHelpers.IdFieldMaxLength); err != nil {
return nil, err
}
zId := hctx.SessUser.User.Id + "_" + req.ScanId
ctx := context.TODO()
coll := hctx.Svcs.MongoDB.Collection(dbCollections.QuantificationZStacksName)
filter := bson.M{"_id": zId}
result := coll.FindOne(ctx, filter)
if result.Err() != nil {
if result.Err() == mongo.ErrNoDocuments {
return nil, errorwithstatus.MakeNotFoundError(req.ScanId)
}
return nil, result.Err()
}
resultItem := &protos.QuantCombineItemListDB{}
if err := result.Decode(&resultItem); err != nil {
return nil, err
}
return &protos.QuantCombineListGetResp{
List: resultItem.List,
}, nil
}
// Anyone can save a quant z-stack if they have quant messaging permissions
func HandleQuantCombineListWriteReq(req *protos.QuantCombineListWriteReq, hctx wsHelpers.HandlerContext) (*protos.QuantCombineListWriteResp, error) {
if err := wsHelpers.CheckStringField(&req.ScanId, "ScanId", 1, wsHelpers.IdFieldMaxLength); err != nil {
return nil, err
}
if req.List == nil || len(req.List.RoiZStack) <= 0 {
return nil, errorwithstatus.MakeBadRequestError(errors.New("List cannot be empty"))
}
zId := hctx.SessUser.User.Id + "_" + req.ScanId
ctx := context.TODO()
coll := hctx.Svcs.MongoDB.Collection(dbCollections.QuantificationZStacksName)
doc := &protos.QuantCombineItemListDB{
Id: zId,
UserId: hctx.SessUser.User.Id,
ScanId: req.ScanId,
List: req.List,
}
opt := options.Update().SetUpsert(true)
result, err := coll.UpdateByID(ctx, zId, bson.D{{Key: "$set", Value: doc}}, opt)
if err != nil {
return nil, err
}
if result.UpsertedCount != 1 {
hctx.Svcs.Log.Errorf("MultiQuant Z-Stack write for: %v got unexpected DB write result: %+v", req.ScanId, result)
}
return &protos.QuantCombineListWriteResp{}, nil
}
func HandleMultiQuantCompareReq(req *protos.MultiQuantCompareReq, hctx wsHelpers.HandlerContext) (*protos.MultiQuantCompareResp, error) {
// req.ScanId is checked in beginDatasetFileReq
if len(req.QuantIds) <= 0 {
return nil, errorwithstatus.MakeBadRequestError(errors.New("Requested with 0 quant IDs"))
}
// If we're requesting for RemainingPoints ROI, mandate that the PMC list is not empty, otherwise it should be
if req.ReqRoiId == "RemainingPoints" && len(req.RemainingPointsPMCs) <= 0 {
return nil, errorwithstatus.MakeBadRequestError(errors.New("No PMCs supplied for RemainingPoints ROI"))
} else if req.ReqRoiId != "RemainingPoints" && len(req.RemainingPointsPMCs) > 0 {
return nil, errorwithstatus.MakeBadRequestError(errors.New("Unexpected PMCs supplied for ROI: " + req.ReqRoiId))
}
exprPB, err := beginDatasetFileReq(req.ScanId, hctx)
if err != nil {
return nil, err
}
tables, err := quantification.MultiQuantCompare(req.ReqRoiId, req.RemainingPointsPMCs, req.QuantIds, exprPB, hctx)
if err != nil {
return nil, err
}
return &protos.MultiQuantCompareResp{
RoiId: req.ReqRoiId,
QuantTables: tables,
}, nil
}
func HandleQuantCombineReq(req *protos.QuantCombineReq, hctx wsHelpers.HandlerContext) (*protos.QuantCombineResp, error) {
// Simple validation
// NOTE: if only asking for a summary, we don't care about name being empty
if !req.SummaryOnly && len(req.Name) <= 0 {
return nil, errorwithstatus.MakeBadRequestError(errors.New("Name cannot be empty"))
}
if len(req.RoiZStack) <= 1 {
return nil, errorwithstatus.MakeBadRequestError(errors.New("Must reference more than 1 ROI"))
}
exprPB, err := beginDatasetFileReq(req.ScanId, hctx)
if err != nil {
return nil, err
}
multiQuantData, err := quantification.MultiQuantCombinedCSV(req.Name, req.ScanId, req.RoiZStack, exprPB, hctx)
if err != nil {
return nil, err
}
if req.SummaryOnly {
// We return a summary instead of forming a CSV
summary := quantification.FormMultiQuantSummary(multiQuantData.DataPerDetectorPerPMC, multiQuantData.AllColumns, multiQuantData.PMCCount)
return &protos.QuantCombineResp{
CombineResult: &protos.QuantCombineResp_Summary{
Summary: summary,
},
}, nil
}
// Form a CSV
csv := quantification.FormCombinedCSV(multiQuantData.QuantIds, multiQuantData.DataPerDetectorPerPMC, multiQuantData.AllColumns)
quantMode := quantification.QuantModeCombinedMultiQuant
if len(multiQuantData.Detectors) > 1 {
quantMode = quantification.QuantModeABMultiQuant
}
quantId, err := quantification.ImportQuantCSV(hctx, req.ScanId, hctx.SessUser.User, csv, "combined-multi", "multi", req.Name, quantMode, req.Description)
if err != nil {
return nil, err
}
return &protos.QuantCombineResp{
CombineResult: &protos.QuantCombineResp_JobId{
JobId: quantId,
},
}, nil
}