Skip to content

Commit

Permalink
Merge pull request #260 from pixlise/bugfix/autoquant
Browse files Browse the repository at this point in the history
Bugfix/autoquant
  • Loading branch information
pnemere authored Jul 4, 2024
2 parents 1723b79 + 13fbf70 commit 64247ae
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 2 deletions.
8 changes: 8 additions & 0 deletions api/dataimport/internal/output/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ func makeSummaryFileContent(
s.Title = prevSavedScan.Title
jobLog.Infof(" Preserved previous title=\"%v\"", s.Title)
}

// If we have been marked as having RGBU imagery, preserve this
for _, item := range prevSavedScan.DataTypes {
if item.DataType == protos.ScanDataType_SD_RGBU {
s.DataTypes = append(s.DataTypes, item)
break
}
}
}

return s
Expand Down
Binary file not shown.
6 changes: 4 additions & 2 deletions api/notificationSender/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ func (n *NotificationSender) saveNotificationToDB(notifId string, destUserId str
}

// Make a copy which has the user id set
filter := bson.D{{"id", toSave.Id}}
_, err := n.db.Collection(dbCollections.NotificationsName).ReplaceOne(context.TODO(), filter, toSave, options.Replace().SetUpsert(true))
filter := bson.D{{"_id", toSave.Id}}
opt := options.Replace().SetUpsert(true)

_, err := n.db.Collection(dbCollections.NotificationsName).ReplaceOne(context.TODO(), filter, toSave, opt)
return err
}
38 changes: 38 additions & 0 deletions api/notificationSender/notifications_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package notificationSender

import (
"context"
"fmt"

"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/core/logger"
"github.com/pixlise/core/v4/core/wstestlib"
protos "github.com/pixlise/core/v4/generated-protos"
)

func Example_notificationDBSave() {
db := wstestlib.GetDB()
ctx := context.TODO()

// Clear relevant collections
db.Collection(dbCollections.NotificationsName).Drop(ctx)

notif := &protos.Notification{
Id: "ajm5d5bjs4vq7bkc-auth0|5de45d85ca40070f421a3a34",
DestUserId: "auth0|5de45d85ca40070f421a3a34",
Subject: "Quantification Peters combined v4 test quant has completed with status: Complete",
Contents: "A quantification named Peters combined v4 test quant (id: quant-wqp66sayhowj41ej) has completed with status Complete. This quantification is for the scan named: Castle Geyser",
From: "Data Importer",
TimeStampUnixSec: 1710831761,
ActionLink: "?q=393871873&quant=quant-wqp66sayhowj41ej",
NotificationType: protos.NotificationType_NT_USER_MESSAGE,
}

n := MakeNotificationSender("abc123", db, nil, nil, &logger.StdOutLoggerForTest{}, "unittest", nil, nil)
fmt.Printf("Notification write to empty DB: %v\n", n.saveNotificationToDB("notif123", "destuser123", notif))
fmt.Printf("Notification overwrite: %v", n.saveNotificationToDB("notif123", "destuser123", notif))

// Output:
// Notification write to empty DB: <nil>
// Notification overwrite: <nil>
}
97 changes: 97 additions & 0 deletions api/quantification/autoQuantification_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package quantification

import (
"context"
"fmt"
"strings"

"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/core/logger"
"github.com/pixlise/core/v4/core/wstestlib"
protos "github.com/pixlise/core/v4/generated-protos"
"go.mongodb.org/mongo-driver/mongo/options"
)

func Example_getExistingAutoQuants() {
db := wstestlib.GetDB()

// Ensure none
ctx := context.TODO()
coll := db.Collection(dbCollections.QuantificationsName)
fmt.Printf("Drop: %v\n", coll.Drop(ctx))

names := []string{"AutoQuant-PDS(AB)", "AutoQuant-PIXL(AB)", "AutoQuant-PDS(Combined)", "AutoQuant-PIXL(Combined)"}
existing, err := getExistingAutoQuants("123", names, db)
fmt.Println("Test missing")
fmt.Printf("%v\n", err)
fmt.Printf("Read:%v\n\n", strings.Join(existing, ","))

autoQuant := &protos.QuantificationSummary{
Id: "PIXLAB123",
ScanId: "123",
Params: &protos.QuantStartingParameters{
UserParams: &protos.QuantCreateParams{
Command: "map",
Name: "AutoQuant-PIXL(AB)",
ScanId: "123",
Elements: []string{"Na", "Mg"},
DetectorConfig: "PIXL/PiquantConfigs/v7",
Parameters: "-Fe,1",
QuantMode: "Combined",
},
PmcCount: 51,
ScanFilePath: "Datasets/104202753/dataset.bin",
},
Elements: []string{"Na2O", "MgO"},
Status: &protos.JobStatus{
JobId: "PIXLAB123",
Status: 5,
Message: "Nodes ran: 1",
EndUnixTimeSec: 1670988052,
OutputFilePath: "Quantifications/104202753/auth0|62eda29040fd995f305e2322",
OtherLogFiles: []string{"node00001_piquant.log", "node00001_stdout.log"},
},
}

_, err = coll.InsertOne(ctx, autoQuant, options.InsertOne())
fmt.Printf("Insert: %v\n", err)

//names := []string{"AutoQuant-PIXL(AB)"}
existing, err = getExistingAutoQuants("123", names, db)
fmt.Println("Test exists")
fmt.Printf("%v\n", err)
fmt.Printf("%v\n", strings.Join(existing, ","))

// Output:
// Drop: <nil>
// Test missing
// <nil>
// Read:
//
// Insert: <nil>
// Test exists
// <nil>
// AutoQuant-PIXL(AB) (id: PIXLAB123)
}

func Example_readQuantifiablePMCs() {
expr, err := readDatasetFile("./testdata/LagunaSalinasdataset.bin")
fmt.Printf("Read Laguna: %v\n", err)
pmcs, err := readQuantifiablePMCs(expr, "123", &logger.StdOutLoggerForTest{})
fmt.Printf("PMCRead: %v\n", err)
fmt.Printf("PMCs: %v\n", pmcs)

expr, err = readDatasetFile("./testdata/Naltsosdataset.bin")
fmt.Printf("Read Naltsos: %v\n", err)
pmcs, err = readQuantifiablePMCs(expr, "123", &logger.StdOutLoggerForTest{})
fmt.Printf("PMCRead: %v\n", err)
fmt.Printf("PMCs: %v\n", pmcs)

// Output:
// Read Laguna: <nil>
// PMCRead: <nil>
// PMCs: []
// Read Naltsos: <nil>
// PMCRead: <nil>
// PMCs: [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 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 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 213 214 216]
}
Binary file added api/quantification/testdata/Naltsosdataset.bin
Binary file not shown.
12 changes: 12 additions & 0 deletions api/ws/handlers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ func HandleImageDeleteReq(req *protos.ImageDeleteReq, hctx wsHelpers.HandlerCont

//Verify delImgResult.DeletedCount == 1 ???

// Finally, update the scan if needed
err = wsHelpers.UpdateScanImageDataTypes(img.OriginScanId, hctx.Svcs.MongoDB, hctx.Svcs.Log)
if err != nil {
hctx.Svcs.Log.Errorf("UpdateScanImageDataTypes Failed for scan: %v, when uploading image: %v. DataType counts may not be accurate on Scan Item, RGBU icon may not show correctly.", img.OriginScanId, img.ImagePath)
}

// For any associated scans or origin scans, we send notify out
scanIds := []string{}
for _, assocScanId := range img.AssociatedScanIds {
Expand Down Expand Up @@ -424,6 +430,12 @@ func HandleImageUploadReq(req *protos.ImageUploadReq, hctx wsHelpers.HandlerCont
}
}

// Finally, update the scan if needed
err = wsHelpers.UpdateScanImageDataTypes(req.OriginScanId, hctx.Svcs.MongoDB, hctx.Svcs.Log)
if err != nil {
hctx.Svcs.Log.Errorf("UpdateScanImageDataTypes Failed for scan: %v, when uploading image: %v. DataType counts may not be accurate on Scan Item, RGBU icon may not show correctly.", req.OriginScanId, scanImage.ImagePath)
}

// Notify of our successful image addition
hctx.Svcs.Notifier.NotifyNewScanImage(req.OriginScanId, req.OriginScanId, scanImage.ImagePath)
hctx.Svcs.Notifier.SysNotifyScanImagesChanged([]string{req.OriginScanId})
Expand Down
89 changes: 89 additions & 0 deletions api/ws/wsHelpers/scan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package wsHelpers

import (
"context"
"errors"

"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/core/logger"
"github.com/pixlise/core/v4/core/scan"
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"
)

func UpdateScanImageDataTypes(scanId string, db *mongo.Database, logger logger.ILogger) error {
if len(scanId) <= 0 {
return errors.New("No scanId specified for UpdateScanImageDataTypes")
}

// Check how many RGBU images and normal images this scan has
ctx := context.TODO()

coll := db.Collection(dbCollections.ImagesName)
filter := bson.M{"originscanid": scanId}
opt := options.Find()
cursor, err := coll.Find(ctx, filter, opt)
if err != nil {
return err
}

images := []*protos.ScanImage{}
err = cursor.All(ctx, &images)
if err != nil {
return err
}

imageCount := uint32(0)
rgbuCount := uint32(0)

for _, img := range images {
if img.Purpose == protos.ScanImagePurpose_SIP_MULTICHANNEL {
rgbuCount++
} else {
imageCount++
}
}

// Update the scan item
summary, err := scan.ReadScanItem(scanId, db)
if err != nil {
return err
}

saveDataTypes := []*protos.ScanItem_ScanTypeCount{}
for _, item := range summary.DataTypes {
if item.DataType == protos.ScanDataType_SD_XRF {
saveDataTypes = append(saveDataTypes, item)
}
}

// Add these if needed
if imageCount > 0 {
saveDataTypes = append(saveDataTypes, &protos.ScanItem_ScanTypeCount{
DataType: protos.ScanDataType_SD_IMAGE,
Count: imageCount,
})
}
if rgbuCount > 0 {
saveDataTypes = append(saveDataTypes, &protos.ScanItem_ScanTypeCount{
DataType: protos.ScanDataType_SD_RGBU,
Count: rgbuCount,
})
}

summary.DataTypes = saveDataTypes

coll = db.Collection(dbCollections.ScansName)
filter = bson.M{"_id": scanId}
replaceResult, err := coll.ReplaceOne(ctx, filter, summary, options.Replace())
if err != nil {
return err
}

if replaceResult.ModifiedCount != 1 {
logger.Infof("UpdateScanImageDataTypes for scan: %v expected ModifiedCount 1, got: %v", scanId, replaceResult.ModifiedCount)
}
return nil
}
Loading

0 comments on commit 64247ae

Please sign in to comment.