Files
Jungfraujoch/viewer/JFJochImageReadingWorker.h
T
leonarski_fandClaude Opus 4.8 56ddfaef96
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 11m38s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 11m49s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 11m55s
Build Packages / build:rpm (rocky8) (push) Successful in 12m55s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 13m4s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 13m16s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 14m12s
Build Packages / XDS test (durin plugin) (push) Successful in 8m5s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 8m1s
Build Packages / Generate python client (push) Successful in 19s
Build Packages / Create release (push) Skipped
Build Packages / build:rpm (ubuntu2404) (push) Successful in 9m55s
Build Packages / Build documentation (push) Successful in 45s
Build Packages / XDS test (neggia plugin) (push) Successful in 7m58s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 10m56s
Build Packages / build:rpm (rocky9) (push) Successful in 11m58s
Build Packages / DIALS test (push) Successful in 11m59s
Build Packages / Unit tests (push) Successful in 55m33s
viewer: live detector status bar + dataset-follow sync mode
Add a status-bar cluster, shown when connected over HTTP, that surfaces
the broker state and live acquisition info:
- broker state box (QProgressBar) with progress drawn as the bar fill,
  polled ~1 Hz on its own timer independent of image sync
- Live/Disconnected connection badge (host:port in tooltip)
- "+N new" badge and effective live-rate (Hz) readout
All widgets are fixed-width and only blanked in place, so the bar never
reflows when things appear/disappear.

Add a third autoload mode (HTTPSyncDataset): manually selecting an image
while following live now freezes the displayed image but keeps the dataset,
plots and image count updating. Surfaced via a tri-state HTTP Sync button
(off / live / data-only).

Also fix GetBrokerStatus() dropping the message field and a couple of
copy-pasted exception strings in JFJochHttpReader.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-18 08:13:07 +02:00

175 lines
6.3 KiB
C++

// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <QObject>
#include <QString>
#include <QVector>
#include <QRect>
#include <QMutex>
#include <QTimer>
#include <QElapsedTimer>
#include "../reader/JFJochHDF5Reader.h"
#include "../common/Logger.h"
#include "../reader/JFJochHttpReader.h"
#include "../image_analysis/MXAnalysisWithoutFPGA.h"
#include "../image_analysis/bragg_prediction/BraggPrediction.h"
#include "SimpleImage.h"
#include "../common/MovingAverage.h"
Q_DECLARE_METATYPE(std::shared_ptr<const JFJochReaderDataset>)
Q_DECLARE_METATYPE(std::shared_ptr<const JFJochReaderImage>)
Q_DECLARE_METATYPE(std::shared_ptr<const JFJochReaderSpots>)
Q_DECLARE_METATYPE(DiffractionExperiment)
Q_DECLARE_METATYPE(SpotFindingSettings)
Q_DECLARE_METATYPE(IndexingSettings)
Q_DECLARE_METATYPE(AzimuthalIntegrationSettings)
Q_DECLARE_METATYPE(UnitCell)
Q_DECLARE_METATYPE(std::shared_ptr<const SimpleImage>)
Q_DECLARE_METATYPE(BrokerStatus)
class JFJochImageReadingWorker : public QObject {
Q_OBJECT
public:
// HTTPSync: follow latest image + dataset. HTTPSyncDataset: follow dataset/plots/image count
// but keep the displayed image frozen (entered by manually selecting an image while following).
enum class AutoloadMode {HTTPSync, HTTPSyncDataset, Movie, None};
Q_ENUM(AutoloadMode)
private:
mutable QMutex m;
bool http_mode = false;
AutoloadMode autoload_mode = AutoloadMode::None;
JFJochHDF5Reader file_reader;
JFJochHttpReader http_reader;
QString current_file;
DiffractionExperiment curr_experiment;
IndexingSettings indexing_settings;
AzimuthalIntegrationSettings azint_settings;
std::unique_ptr<IndexerThreadPool> indexing;
std::shared_ptr<JFJochReaderImage> current_image_ptr;
std::unique_ptr<AzimuthalIntegrationMapping> azint_mapping;
std::unique_ptr<MXAnalysisWithoutFPGA> image_analysis;
std::unique_ptr<IndexAndRefine> index_and_refine;
// Azimuthal profile buffer of the most recently analyzed image (filled by Analyze).
std::unique_ptr<AzimuthalIntegrationProfile> last_profile_;
std::unique_ptr<ROIElement> roi;
SpotFindingSettings spot_finding_settings;
std::optional<int64_t> current_image;
int64_t current_summation = 1;
int64_t total_images = 0;
int64_t jump_value = 1;
Logger logger{"jfjoch_viewer"};
bool auto_reanalyze = false;
QTimer *autoload_timer;
int autoload_interval = 500; // milliseconds
// Broker status polling (independent of image sync, runs whenever connected over HTTP)
QTimer *status_timer = nullptr;
int status_interval = 1000; // milliseconds
bool http_connected = false;
// Adaptive autoload interval based on recent load+analysis time
MovingAverage autoload_ms_ma{8}; // window size (tune as needed)
int autoload_interval_min_ms = 50; // 20 Hz is the top performance!
int autoload_interval_max_ms = 2000; // 0.5 Hz as bottom limit
float autoload_safety_factor = 2.0f;
// File open retry/back-off (GPFS via NFSv4 visibility lag)
struct PendingLoadRequest {
QString filename;
qint64 image_number = 0;
qint64 summation = 1;
};
QTimer *file_open_retry_timer = nullptr;
PendingLoadRequest pending_load;
bool file_open_retry_active = false;
bool file_open_retry_warned = false;
int file_open_retry_attempts = 0;
int file_open_retry_delay_ms = 100;
int file_open_retry_delay_max_ms = 10000;
QElapsedTimer file_open_retry_elapsed;
void ResetFileOpenRetry_i();
void ScheduleFileOpenRetry_i(const QString& reason);
void LoadFile_i(const QString &filename, qint64 image_number, qint64 summation, bool retry);
void LoadImage_i(int64_t image_number, int64_t summation);
void RefreshDatasetOnly_i();
void SetHttpConnected_i(bool connected, const QString &addr);
void ReanalyzeImage_i();
void UpdateDataset_i(const std::optional<DiffractionExperiment>& experiment);
void UpdateAzint_i(const JFJochReaderDataset *dataset);
void UpdateUserMask_i(const std::vector<uint32_t> &mask);
void setAutoLoadMode_i(AutoloadMode mode);
signals:
void datasetLoaded(std::shared_ptr<const JFJochReaderDataset>);
void imageLoaded(std::shared_ptr<const JFJochReaderImage>);
void spotsLoaded(std::shared_ptr<const JFJochReaderSpots>);
void imageStatsUpdated(std::shared_ptr<const JFJochReaderImage>);
void imageNumberChanged(int64_t total_images, int64_t current_image);
void setRings(const QVector<float> &v);
void simpleImageLoaded(std::shared_ptr<const SimpleImage> image);
void autoloadChanged(AutoloadMode mode);
void fileLoadError(QString title, QString message);
void fileLoadRetryStatus(bool active, QString message);
void brokerStatusUpdated(BrokerStatus status);
void httpConnectionChanged(bool connected, QString addr);
void liveRateChanged(double hz);
public:
JFJochImageReadingWorker(const SpotFindingSettings &settings, const DiffractionExperiment& experiment, QObject *parent = nullptr);
~JFJochImageReadingWorker() override = default;
private slots:
void AutoLoadTimerExpired();
void StatusTimerExpired();
void FileOpenRetryTimerExpired();
public slots:
void LoadFile(const QString &filename, qint64 image_number, qint64 summation, bool retry);
void CloseFile();
void LoadImage(int64_t image_number, int64_t summation);
void LoadSpots(int64_t start_image, int64_t end_image, int64_t stride);
void SetROIBox(QRect box);
void SetROICircle(double x, double y, double radius);
void UpdateDataset(const DiffractionExperiment& experiment);
void FindCenter(const UnitCell& calibrant, bool guess);
void Analyze();
void UpdateSpotFindingSettings(const SpotFindingSettings &settings, const IndexingSettings &indexing, int64_t max_spots);
void ReanalyzeImages(bool input);
void UpdateAzintSettings(const AzimuthalIntegrationSettings& settings);
void AddROIToUserMask();
void SubtractROIFromUserMask();
void SaveUserMaskTIFF(QString filename);
void UploadUserMask();
void ClearUserMask();
void LoadCalibration(QString dataset);
void setAutoLoadMode(AutoloadMode mode);
void setAutoLoadJump(int64_t val);
};