Skip to content

Commit

Permalink
Genereal colocalization works
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Glowacki committed Mar 7, 2023
1 parent bdad89b commit d0ba235
Show file tree
Hide file tree
Showing 9 changed files with 314 additions and 43 deletions.
28 changes: 28 additions & 0 deletions src/gstar/ImageViewWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,34 @@ void ImageViewWidget::createSceneAndView(int rows, int cols)
}
}

//---------------------------------------------------------------------------

QImage ImageViewWidget::generate_img(ArrayXXr<float>& int_img, QVector<QRgb>& colormap)
{
float counts_max = int_img.maxCoeff();
float counts_min = int_img.minCoeff();
int width = static_cast<int>(int_img.cols());
int height = static_cast<int>(int_img.rows());
QImage image(width, height, QImage::Format_Indexed8);
image.setColorTable(colormap);

float max_min = counts_max - counts_min;
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
//first clamp the data to max min
float cnts = int_img(row, col);
cnts = std::min(counts_max, cnts);
cnts = std::max(counts_min, cnts);
//convert to pixel
byte data = static_cast<byte>(((cnts - counts_min) / max_min) * 255.0);
image.setPixel(col, row, (uint)data);
}
}
return image;
}

/*---------------------------------------------------------------------------*/

void ImageViewWidget::redrawSubWindows()
Expand Down
2 changes: 2 additions & 0 deletions src/gstar/ImageViewWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ class ImageViewWidget

void getMinMaxAt(int grid_idx, float &counts_min, float &counts_max);

QImage generate_img(ArrayXXr<float>& int_img, QVector<QRgb>& colormap);

public slots:

/**
Expand Down
257 changes: 248 additions & 9 deletions src/mvc/CoLocalizationWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
CoLocalizationWidget::CoLocalizationWidget(QWidget* parent) : gstar::AbstractImageWidget(1, 1, parent)
{
_model = nullptr;
_first_pixmap_set = true;
int r = 0;
for (int i = 0; i < 256; ++i)
{
_red_colormap.append(qRgb(i, 0, 0));
_green_colormap.append(qRgb(0, i, 0));
_blue_colormap.append(qRgb(0, 0, i));
}

setAnnotationsEnabled(false);
_createLayout();
createActions();
Expand Down Expand Up @@ -42,12 +51,18 @@ void CoLocalizationWidget::_createLayout()
m_imageViewWidget->setCountsVisible(false);

_cb_red_element = new QComboBox();

connect(_cb_red_element, SIGNAL(currentIndexChanged(QString)), this, SLOT(onColorSelected(QString)));
_cb_green_element = new QComboBox();

connect(_cb_green_element, SIGNAL(currentIndexChanged(QString)), this, SLOT(onColorSelected(QString)));
_cb_blue_element = new QComboBox();
connect(_cb_blue_element, SIGNAL(currentIndexChanged(QString)), this, SLOT(onColorSelected(QString)));

_ck_quad_view = new QCheckBox("Quad View");
// TODO: save and load from pref check value
connect(_ck_quad_view, &QCheckBox::stateChanged, this, &CoLocalizationWidget::onQuadViewChanged);

QHBoxLayout* hb = new QHBoxLayout();
hb->addStretch();
hb->addWidget(new QLabel("Red Element"));
hb->addWidget(_cb_red_element);

Expand All @@ -56,9 +71,12 @@ void CoLocalizationWidget::_createLayout()

hb->addWidget(new QLabel("Blue Element"));
hb->addWidget(_cb_blue_element);

hb->addWidget(_ck_quad_view);
hb->addStretch();
layout->addItem(hb);

appendAnnotationTab(false);
//appendAnnotationTab(false);

layout->addItem(gen_layout);
//layout->addWidget(m_imageViewWidget);
Expand Down Expand Up @@ -130,19 +148,106 @@ void CoLocalizationWidget::on_min_max_contrast_changed()
}
*/

//---------------------------------------------------------------------------

void CoLocalizationWidget::onQuadViewChanged(int value)
{
if (value == 0)
{
onNewGridLayout(1, 1);
}
else
{
onNewGridLayout(2, 2);
}
onColorSelected(" ");
}

//---------------------------------------------------------------------------

void CoLocalizationWidget::onColorSelected(QString name)
{

QImage red_img;
QImage green_img;
QImage blue_img;
QImage sum_img;

bool first = true;

ArrayXXr<float> int_img;

if (_fit_counts.count(_cb_red_element->currentText().toStdString()) > 0)
{
red_img = m_imageViewWidget->generate_img(_fit_counts.at(_cb_red_element->currentText().toStdString()), _red_colormap);
sum_img = red_img.convertToFormat(QImage::Format_RGB32);
first = false;
}
if (_fit_counts.count(_cb_green_element->currentText().toStdString()) > 0)
{
green_img = m_imageViewWidget->generate_img(_fit_counts.at(_cb_green_element->currentText().toStdString()), _green_colormap);
if (first)
{
sum_img = green_img.convertToFormat(QImage::Format_RGB32);
first = false;
}
else
{
QPainter p(&sum_img);
p.setCompositionMode(QPainter::CompositionMode_Plus);
p.drawImage(0, 0, green_img.convertToFormat(QImage::Format_RGB32));
p.end();
}
}
if (_fit_counts.count(_cb_blue_element->currentText().toStdString()) > 0)
{
blue_img = m_imageViewWidget->generate_img(_fit_counts.at(_cb_blue_element->currentText().toStdString()), _blue_colormap);
if (first)
{
sum_img = blue_img.convertToFormat(QImage::Format_RGB32);;
first = false;
}
else
{
QPainter p(&sum_img);
p.setCompositionMode(QPainter::CompositionMode_Plus);
p.drawImage(0, 0, blue_img.convertToFormat(QImage::Format_RGB32));
p.end();
}
}

if (_ck_quad_view->isChecked())
{
m_imageViewWidget->scene(0)->setPixmap(QPixmap::fromImage(red_img.convertToFormat(QImage::Format_RGB32)));
m_imageViewWidget->scene(1)->setPixmap(QPixmap::fromImage(green_img.convertToFormat(QImage::Format_RGB32)));
m_imageViewWidget->scene(2)->setPixmap(QPixmap::fromImage(blue_img.convertToFormat(QImage::Format_RGB32)));
m_imageViewWidget->scene(3)->setPixmap(QPixmap::fromImage(sum_img.convertToFormat(QImage::Format_RGB32)));
}
else
{
m_imageViewWidget->scene(0)->setPixmap(QPixmap::fromImage(sum_img.convertToFormat(QImage::Format_RGB32)));
}
if (_first_pixmap_set)
{
m_imageViewWidget->clickFill(true);
_first_pixmap_set = false;
}
}

//---------------------------------------------------------------------------

void CoLocalizationWidget::onNewGridLayout(int rows, int cols)
{
const std::vector<QString> element_view_list = m_imageViewWidget->getLabelList();
//const std::vector<QString> element_view_list = m_imageViewWidget->getLabelList();
m_imageViewWidget->setSceneModelAndSelection(nullptr, nullptr);
m_imageViewWidget->newGridLayout(rows, cols);

m_imageViewWidget->setCoordsVisible(false);
m_imageViewWidget->setSelectorVisible(false);
m_imageViewWidget->setCountsVisible(false);
//model_updated();
m_imageViewWidget->restoreLabels(element_view_list);
redrawCounts();
Preferences::inst()->setValue(STR_GRID_ROWS,rows);
Preferences::inst()->setValue(STR_GRID_COLS,cols);
Preferences::inst()->save();
//m_imageViewWidget->restoreLabels(element_view_list);
}

//---------------------------------------------------------------------------
Expand Down Expand Up @@ -282,6 +387,139 @@ void CoLocalizationWidget::onSelectNormalizer(QString name)
*/
//---------------------------------------------------------------------------

void CoLocalizationWidget::onSetAnalysisType(QString name)
{
_curAnalysis = name;

_cb_red_element->clear();
_cb_green_element->clear();
_cb_blue_element->clear();

_cb_red_element->addItem(QString(" "));
_cb_green_element->addItem(QString(" "));
_cb_blue_element->addItem(QString(" "));

if (_model != nullptr)
{
data_struct::Fit_Count_Dict<float> fit_counts;
_model->getAnalyzedCounts(name.toStdString(), fit_counts);
std::map<std::string, data_struct::ArrayXXr<float>>* scalers = _model->getScalers();
if (scalers != nullptr)
{
for (auto& itr : *scalers)
{
fit_counts.insert(itr);
}
}

//copy
_fit_counts = fit_counts;

//create save ordered vector by element Z number with K , L, M lines
std::vector<std::string> element_lines;
for (std::string el_name : data_struct::Element_Symbols)
{
element_lines.push_back(el_name);
}
for (std::string el_name : data_struct::Element_Symbols)
{
element_lines.push_back(el_name + "_L");
}
for (std::string el_name : data_struct::Element_Symbols)
{
element_lines.push_back(el_name + "_M");
}

std::vector<std::string> final_counts_to_add_before_scalers;
final_counts_to_add_before_scalers.push_back(STR_COHERENT_SCT_AMPLITUDE);
final_counts_to_add_before_scalers.push_back(STR_COMPTON_AMPLITUDE);
final_counts_to_add_before_scalers.push_back(STR_SUM_ELASTIC_INELASTIC_AMP);
final_counts_to_add_before_scalers.push_back(STR_TOTAL_FLUORESCENCE_YIELD);
final_counts_to_add_before_scalers.push_back(STR_NUM_ITR);
final_counts_to_add_before_scalers.push_back(STR_RESIDUAL);

std::vector<std::string> scalers_to_add_first;
scalers_to_add_first.push_back(STR_SR_CURRENT);
scalers_to_add_first.push_back(STR_US_IC);
scalers_to_add_first.push_back(STR_DS_IC);
scalers_to_add_first.push_back(STR_ELT);
scalers_to_add_first.push_back(STR_ELAPSED_LIVE_TIME);
scalers_to_add_first.push_back(STR_ERT);
scalers_to_add_first.push_back(STR_ELAPSED_REAL_TIME);
scalers_to_add_first.push_back(STR_INPUT_COUNTS);
scalers_to_add_first.push_back(STR_ICR);
scalers_to_add_first.push_back("INCNT");
scalers_to_add_first.push_back(STR_OUTPUT_COUNTS);
scalers_to_add_first.push_back(STR_OCR);
scalers_to_add_first.push_back("OUTCNT");
scalers_to_add_first.push_back(STR_DEAD_TIME);
scalers_to_add_first.push_back("abs_cfg");
scalers_to_add_first.push_back("abs_ic");
scalers_to_add_first.push_back("H_dpc_cfg");
scalers_to_add_first.push_back("V_dpc_cfg");
scalers_to_add_first.push_back("DPC1_IC");
scalers_to_add_first.push_back("DPC2_IC");
scalers_to_add_first.push_back("dia1_dpc_cfg");
scalers_to_add_first.push_back("dia2_dpc_cfg");
scalers_to_add_first.push_back("CFG_1");
scalers_to_add_first.push_back(STR_CFG_2);
scalers_to_add_first.push_back(STR_CFG_3);
scalers_to_add_first.push_back(STR_CFG_4);
scalers_to_add_first.push_back(STR_CFG_5);
scalers_to_add_first.push_back("CFG_6");
scalers_to_add_first.push_back("CFG_7");
scalers_to_add_first.push_back("CFG_8");
scalers_to_add_first.push_back("CFG_9");

// insert in z order
for (std::string el_name : element_lines)
{
if (fit_counts.count(el_name) > 0)
{
_cb_red_element->addItem(QString(el_name.c_str()));
_cb_green_element->addItem(QString(el_name.c_str()));
_cb_blue_element->addItem(QString(el_name.c_str()));
fit_counts.erase(el_name);
}

}

// add end of element list that are not elements
for (auto& itr : final_counts_to_add_before_scalers)
{
if (fit_counts.count(itr) > 0)
{
_cb_red_element->addItem(QString(itr.c_str()));
_cb_green_element->addItem(QString(itr.c_str()));
_cb_blue_element->addItem(QString(itr.c_str()));
fit_counts.erase(itr);
}
}

// add scalers in certain order
for (auto& itr : scalers_to_add_first)
{
if (fit_counts.count(itr) > 0)
{
_cb_red_element->addItem(QString(itr.c_str()));
_cb_green_element->addItem(QString(itr.c_str()));
_cb_blue_element->addItem(QString(itr.c_str()));
fit_counts.erase(itr);
}
}

// add rest of scalers
for (auto& itr : fit_counts)
{
_cb_red_element->addItem(QString(itr.first.c_str()));
_cb_green_element->addItem(QString(itr.first.c_str()));
_cb_blue_element->addItem(QString(itr.first.c_str()));
}
}
}

//---------------------------------------------------------------------------

void CoLocalizationWidget::setModel(MapsH5Model* model)
{
if (_model != model)
Expand All @@ -290,6 +528,7 @@ void CoLocalizationWidget::setModel(MapsH5Model* model)
//model_updated();
if (_model != nullptr)
{
onSetAnalysisType(_curAnalysis);
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/mvc/CoLocalizationWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public slots:

void onNewGridLayout(int rows, int cols);

void onSetAnalysisType(QString name);

void onColorSelected(QString);

void onQuadViewChanged(int);

protected:

/**
Expand Down Expand Up @@ -79,6 +85,19 @@ public slots:

QComboBox* _cb_blue_element;

QCheckBox* _ck_quad_view;

QString _curAnalysis;

data_struct::Fit_Count_Dict<float> _fit_counts;

QVector<QRgb> _red_colormap;

QVector<QRgb> _green_colormap;

QVector<QRgb> _blue_colormap;

bool _first_pixmap_set;
};


Expand Down
Loading

0 comments on commit d0ba235

Please sign in to comment.