Skip to main content

src/WhiskerToolbox/Plots/HeatmapWidget/Core/HeatmapState.hpp

src/WhiskerToolbox/Plots/HeatmapWidget/Core/HeatmapState.hpp

State class for HeatmapWidget. More…

Classes

Name
struct HeatmapColorRangeConfig
Configuration for how the heatmap color range is determined.
struct HeatmapStateData
Serializable state data for HeatmapWidget.
class HeatmapState
State class for HeatmapWidget.

Types

Name
enum class HeatmapSortMode { Manual, TimeToPeak, PeakRate, MeanRate, Alphabetical}
Sorting modes for heatmap rows.

Functions

Name
char const * sortModeLabel(HeatmapSortMode mode)
Return a human-readable label for a HeatmapSortMode value.
std::vector< HeatmapSortMode > allSortModes()
All available sort modes (for populating combo boxes).

Detailed Description

State class for HeatmapWidget.

See: EditorState for base class documentation

HeatmapState manages the serializable state for the HeatmapWidget, enabling workspace save/restore and inter-widget communication via SelectionContext.

View state uses CorePlotting::ViewStateData as the single source of truth. Y bounds are stored in view state (world [-1, 1] for trial index axis) and kept in sync with the vertical axis state.

Types Documentation

enum HeatmapSortMode

Enumerator Value Description
Manual User-defined order (order of addition) — default.
TimeToPeak Sort by latency of peak rate relative to t=0 (ascending).
PeakRate Sort by maximum rate value (descending).
MeanRate Sort by mean rate across all bins (descending).
Alphabetical Sort by unit label / event key (ascending).

Sorting modes for heatmap rows.

Controls how unit rows are ordered in the heatmap display. Sorting is applied after rate estimation and scaling, before colormap mapping.

Functions Documentation

function sortModeLabel

char const * sortModeLabel(
    HeatmapSortMode mode
)

Return a human-readable label for a HeatmapSortMode value.

function allSortModes

inline std::vector< HeatmapSortMode > allSortModes()

All available sort modes (for populating combo boxes).

Source code

#ifndef HEATMAP_STATE_HPP
#define HEATMAP_STATE_HPP


#include "CorePlotting/Colormaps/Colormap.hpp"
#include "CorePlotting/CoordinateTransform/ViewStateData.hpp"
#include "EditorState/EditorState.hpp"
#include "Plots/Common/EventRateEstimation/EstimationParams.hpp"
#include "Plots/Common/EventRateEstimation/RateEstimate.hpp"
#include "Plots/Common/PlotAlignmentWidget/Core/PlotAlignmentData.hpp"
#include "Plots/Common/PlotAlignmentWidget/Core/PlotAlignmentState.hpp"
#include "Plots/Common/RelativeTimeAxisWidget/Core/RelativeTimeAxisState.hpp"
#include "Plots/Common/RelativeTimeAxisWidget/Core/RelativeTimeAxisStateData.hpp"
#include "Plots/Common/VerticalAxisWidget/Core/VerticalAxisState.hpp"
#include "Plots/Common/VerticalAxisWidget/Core/VerticalAxisStateData.hpp"

#include <rfl.hpp>
#include <rfl/json.hpp>

#include <memory>
#include <string>
#include <vector>

// =============================================================================
// Sort Mode (serializable)
// =============================================================================

enum class HeatmapSortMode {
    Manual,      
    TimeToPeak,  
    PeakRate,    
    MeanRate,    
    Alphabetical,
};

[[nodiscard]] constexpr char const * sortModeLabel(HeatmapSortMode mode) noexcept {
    switch (mode) {
        case HeatmapSortMode::Manual:
            return "Manual";
        case HeatmapSortMode::TimeToPeak:
            return "Time to Peak";
        case HeatmapSortMode::PeakRate:
            return "Peak Rate";
        case HeatmapSortMode::MeanRate:
            return "Mean Rate";
        case HeatmapSortMode::Alphabetical:
            return "Alphabetical";
    }
    return "Unknown";
}

[[nodiscard]] inline std::vector<HeatmapSortMode> allSortModes() {
    return {
            HeatmapSortMode::Manual,
            HeatmapSortMode::TimeToPeak,
            HeatmapSortMode::PeakRate,
            HeatmapSortMode::MeanRate,
            HeatmapSortMode::Alphabetical,
    };
}

// =============================================================================
// Color Range Configuration (serializable)
// =============================================================================

struct HeatmapColorRangeConfig {
    enum class Mode {
        Auto,     
        Manual,   
        Symmetric,
    };

    Mode mode = Mode::Auto;
    double vmin = 0.0;
    double vmax = 1.0;
};

// =============================================================================
// State Data
// =============================================================================

struct HeatmapStateData {
    std::string instance_id;
    std::string display_name = "Heatmap Plot";
    PlotAlignmentData alignment;
    CorePlotting::ViewStateData view_state;
    RelativeTimeAxisStateData time_axis;
    VerticalAxisStateData vertical_axis;
    std::string background_color = "#FFFFFF";
    std::vector<std::string> unit_keys;
    Neuralyzer::Plots::ScalingMode scaling =
            Neuralyzer::Plots::ScalingMode::FiringRateHz;
    Neuralyzer::Plots::EstimationParams estimation_params;
    HeatmapColorRangeConfig color_range;
    CorePlotting::Colormaps::ColormapPreset colormap =
            CorePlotting::Colormaps::ColormapPreset::Inferno;
    HeatmapSortMode sort_mode = HeatmapSortMode::Manual;
    bool sort_ascending = true;
};

class HeatmapState : public EditorState {
    Q_OBJECT

public:
    explicit HeatmapState(QObject * parent = nullptr);
    ~HeatmapState() override = default;

    [[nodiscard]] QString getTypeName() const override { return QStringLiteral("HeatmapWidget"); }
    [[nodiscard]] QString getDisplayName() const override;
    void setDisplayName(QString const & name) override;

    // === Alignment ===
    [[nodiscard]] QString getAlignmentEventKey() const;
    void setAlignmentEventKey(QString const & key);
    [[nodiscard]] IntervalAlignmentType getIntervalAlignmentType() const;
    void setIntervalAlignmentType(IntervalAlignmentType type);
    [[nodiscard]] double getOffset() const;
    void setOffset(double offset);
    [[nodiscard]] double getWindowSize() const;
    void setWindowSize(double window_size);
    [[nodiscard]] PlotAlignmentState * alignmentState() { return _alignment_state.get(); }
    [[nodiscard]] RelativeTimeAxisState * relativeTimeAxisState() {
        return _relative_time_axis_state.get();
    }
    [[nodiscard]] VerticalAxisState * verticalAxisState() { return _vertical_axis_state.get(); }

    // === View State ===
    [[nodiscard]] CorePlotting::ViewStateData const & viewState() const { return _data.view_state; }
    void setXZoom(double zoom);
    void setYZoom(double zoom);
    void setPan(double x_pan, double y_pan);
    void setXBounds(double x_min, double x_max);
    void setYBounds(double y_min, double y_max);

    // === Unit Management ===
    [[nodiscard]] std::vector<std::string> const & unitKeys() const { return _data.unit_keys; }
    void addUnit(std::string const & key);
    void removeUnit(std::string const & key);
    void addUnits(std::vector<std::string> const & keys);
    void removeUnits(std::vector<std::string> const & keys);
    [[nodiscard]] bool hasUnit(std::string const & key) const;
    [[nodiscard]] size_t unitCount() const { return _data.unit_keys.size(); }

    // === Scaling ===
    [[nodiscard]] Neuralyzer::Plots::ScalingMode scaling() const { return _data.scaling; }
    void setScaling(Neuralyzer::Plots::ScalingMode scaling);

    // === Estimation Parameters ===
    [[nodiscard]] Neuralyzer::Plots::EstimationParams const & estimationParams() const {
        return _data.estimation_params;
    }
    void setEstimationParams(Neuralyzer::Plots::EstimationParams const & params);

    // === Sorting ===
    [[nodiscard]] HeatmapSortMode sortMode() const { return _data.sort_mode; }
    void setSortMode(HeatmapSortMode mode);
    [[nodiscard]] bool sortAscending() const { return _data.sort_ascending; }
    void setSortAscending(bool ascending);

    // === Color Range ===
    [[nodiscard]] HeatmapColorRangeConfig const & colorRange() const { return _data.color_range; }
    void setColorRangeMode(HeatmapColorRangeConfig::Mode mode);
    void setColorRangeBounds(double vmin, double vmax);
    void setColorRange(HeatmapColorRangeConfig const & config);

    // === Colormap ===
    [[nodiscard]] CorePlotting::Colormaps::ColormapPreset colormapPreset() const {
        return _data.colormap;
    }
    void setColormapPreset(CorePlotting::Colormaps::ColormapPreset preset);

    // === Background Color ===
    [[nodiscard]] QString getBackgroundColor() const;
    void setBackgroundColor(QString const & hex_color);

    // === Direct Data Access ===
    [[nodiscard]] HeatmapStateData const & data() const { return _data; }

    // === Serialization ===
    [[nodiscard]] std::string toJson() const override;
    bool fromJson(std::string const & json) override;

signals:
    void alignmentEventKeyChanged(QString const & key);
    void intervalAlignmentTypeChanged(IntervalAlignmentType type);
    void offsetChanged(double offset);
    void windowSizeChanged(double window_size);
    void viewStateChanged();
    void backgroundColorChanged(QString const & hex_color);
    void unitsChanged();           
    void scalingChanged();         
    void colorRangeChanged();      
    void colormapChanged();        
    void estimationParamsChanged();
    void sortChanged();            

private:
    HeatmapStateData _data;
    std::unique_ptr<PlotAlignmentState> _alignment_state;
    std::unique_ptr<RelativeTimeAxisState> _relative_time_axis_state;
    std::unique_ptr<VerticalAxisState> _vertical_axis_state;
};

#endif// HEATMAP_STATE_HPP

Updated on 2026-11-13 at 20:25:45 +0000