Skip to content
Snippets Groups Projects
Commit 963da40c authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

correct Datafield::crop[2]

parent 31381d37
No related branches found
No related tags found
1 merge request!1974Pt fit example now loading data the ba way
...@@ -165,3 +165,9 @@ Frame* Frame::flat() const ...@@ -165,3 +165,9 @@ Frame* Frame::flat() const
outaxes.emplace_back(s->clone()); outaxes.emplace_back(s->clone());
return new Frame(std::move(outaxes)); return new Frame(std::move(outaxes));
} }
void Frame::setScale(size_t k_axis, Scale* scale)
{
m_axes.replace_at(k_axis, scale);
m_size = FrameUtil::product_size(m_axes.reference());
}
...@@ -83,6 +83,7 @@ public: ...@@ -83,6 +83,7 @@ public:
Frame* flat() const; Frame* flat() const;
#ifndef SWIG #ifndef SWIG
void setScale(size_t k_axis, Scale* scale);
std::vector<const Scale*> clonedAxes() const; std::vector<const Scale*> clonedAxes() const;
private: private:
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#ifndef BORNAGAIN_BASE_TYPES_OWNINGVECTOR_H #ifndef BORNAGAIN_BASE_TYPES_OWNINGVECTOR_H
#define BORNAGAIN_BASE_TYPES_OWNINGVECTOR_H #define BORNAGAIN_BASE_TYPES_OWNINGVECTOR_H
#include "Base/Util/Assert.h"
#include <cstddef> #include <cstddef>
#include <utility> #include <utility>
#include <vector> #include <vector>
...@@ -45,9 +46,6 @@ public: ...@@ -45,9 +46,6 @@ public:
OwningVector& operator=(OwningVector&& other) = default; OwningVector& operator=(OwningVector&& other) = default;
void reserve(size_t n) { m_v.reserve(n); } void reserve(size_t n) { m_v.reserve(n); }
void emplace_back(T* e) { m_v.emplace_back(e); }
void insert_at(size_t index, T* e) { m_v.insert(m_v.begin() + index, e); }
void clear() void clear()
{ {
for (T* e : *this) for (T* e : *this)
...@@ -82,6 +80,15 @@ public: ...@@ -82,6 +80,15 @@ public:
m_v.erase(m_v.begin() + i); m_v.erase(m_v.begin() + i);
return result; return result;
} }
void emplace_back(T* e) { m_v.emplace_back(e); }
void insert_at(size_t i, T* e) { m_v.insert(m_v.begin() + i, e); }
void replace_at(size_t i, T* e)
{
ASSERT(i < m_v.size());
delete m_v[i];
m_v[i] = e;
}
T* release_back() T* release_back()
{ {
......
...@@ -255,6 +255,7 @@ double Datafield::minVal() const ...@@ -255,6 +255,7 @@ double Datafield::minVal() const
Datafield* Datafield::crop(double xmin, double ymin, double xmax, double ymax) const Datafield* Datafield::crop(double xmin, double ymin, double xmax, double ymax) const
{ {
ASSERT(false); // needs renovation, similar to Datafield::crop(double xmin, double xmax)
const auto xclipped = std::make_unique<Scale>(xAxis().clipped(xmin, xmax)); const auto xclipped = std::make_unique<Scale>(xAxis().clipped(xmin, xmax));
const auto yclipped = std::make_unique<Scale>(yAxis().clipped(ymin, ymax)); const auto yclipped = std::make_unique<Scale>(yAxis().clipped(ymin, ymax));
...@@ -271,22 +272,30 @@ Datafield* Datafield::crop(double xmin, double ymin, double xmax, double ymax) c ...@@ -271,22 +272,30 @@ Datafield* Datafield::crop(double xmin, double ymin, double xmax, double ymax) c
Datafield* Datafield::crop(double xmin, double xmax) const Datafield* Datafield::crop(double xmin, double xmax) const
{ {
const auto xclipped = std::make_unique<Scale>(xAxis().clipped(xmin, xmax)); std::vector<Bin1D> outbins;
for (size_t i = 0; i < xAxis().size(); ++i) {
const double x = xAxis().binCenter(i);
if (xmin <= x && x <= xmax)
outbins.push_back(xAxis().bin(i));
}
auto* xclipped = new Scale(xAxis().axisLabel(), outbins);
const size_t N = size(); const size_t N = size();
std::vector<double> out(N); std::vector<double> out;
std::vector<double> errout(hasErrorSigmas() ? N : 0); std::vector<double> errout;
size_t iout = 0;
for (size_t i = 0; i < N; ++i) { for (size_t i = 0; i < N; ++i) {
const double x = frame().projectedCoord(i, 0); const double x = frame().projectedCoord(i, 0);
if (xclipped->rangeComprises(x)) { if (xmin <= x && x <= xmax) {
out[iout] = m_values[i]; out.push_back(m_values[i]);
if (hasErrorSigmas()) if (hasErrorSigmas())
errout[iout] = m_errSigmas[i]; errout.push_back(m_errSigmas[i]);
++iout;
} }
} }
return new Datafield(frame().clone(), out, errout); Frame* outframe = frame().clone();
outframe->setScale(0, xclipped);
ASSERT(outframe->xAxis().size() == out.size());
return new Datafield(outframe, out, errout);
} }
#ifdef BORNAGAIN_PYTHON #ifdef BORNAGAIN_PYTHON
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment