Skip to content
Snippets Groups Projects
Commit 633de0f2 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

New methods in IAxis to get bin centers and boundaries.

parent 4c7414d7
No related branches found
No related tags found
No related merge requests found
...@@ -126,6 +126,30 @@ struct AngleBinAxis_wrapper : AngleBinAxis, bp::wrapper< AngleBinAxis > { ...@@ -126,6 +126,30 @@ struct AngleBinAxis_wrapper : AngleBinAxis, bp::wrapper< AngleBinAxis > {
return IAxis::createDoubleBinSize( ); return IAxis::createDoubleBinSize( );
} }
virtual ::std::vector< double > getBinBoundaries( ) const {
if( bp::override func_getBinBoundaries = this->get_override( "getBinBoundaries" ) )
return func_getBinBoundaries( );
else
return this->IAxis::getBinBoundaries( );
}
::std::vector< double > default_getBinBoundaries( ) const {
return IAxis::getBinBoundaries( );
}
virtual ::std::vector< double > getBinCenters( ) const {
if( bp::override func_getBinCenters = this->get_override( "getBinCenters" ) )
return func_getBinCenters( );
else
return this->IAxis::getBinCenters( );
}
::std::vector< double > default_getBinCenters( ) const {
return IAxis::getBinCenters( );
}
}; };
void register_AngleBinAxis_class(){ void register_AngleBinAxis_class(){
...@@ -237,6 +261,28 @@ void register_AngleBinAxis_class(){ ...@@ -237,6 +261,28 @@ void register_AngleBinAxis_class(){
, default_createDoubleBinSize_function_type(&AngleBinAxis_wrapper::default_createDoubleBinSize) , default_createDoubleBinSize_function_type(&AngleBinAxis_wrapper::default_createDoubleBinSize)
, bp::return_value_policy< bp::manage_new_object >() ); , bp::return_value_policy< bp::manage_new_object >() );
}
{ //::IAxis::getBinBoundaries
typedef ::std::vector< double > ( ::IAxis::*getBinBoundaries_function_type )( ) const;
typedef ::std::vector< double > ( AngleBinAxis_wrapper::*default_getBinBoundaries_function_type )( ) const;
AngleBinAxis_exposer.def(
"getBinBoundaries"
, getBinBoundaries_function_type(&::IAxis::getBinBoundaries)
, default_getBinBoundaries_function_type(&AngleBinAxis_wrapper::default_getBinBoundaries) );
}
{ //::IAxis::getBinCenters
typedef ::std::vector< double > ( ::IAxis::*getBinCenters_function_type )( ) const;
typedef ::std::vector< double > ( AngleBinAxis_wrapper::*default_getBinCenters_function_type )( ) const;
AngleBinAxis_exposer.def(
"getBinCenters"
, getBinCenters_function_type(&::IAxis::getBinCenters)
, default_getBinCenters_function_type(&AngleBinAxis_wrapper::default_getBinCenters) );
} }
AngleBinAxis_exposer.staticmethod( "createIsGISAXSAxis" ); AngleBinAxis_exposer.staticmethod( "createIsGISAXSAxis" );
} }
......
...@@ -133,6 +133,30 @@ struct AxisBin_wrapper : AxisBin, bp::wrapper< AxisBin > { ...@@ -133,6 +133,30 @@ struct AxisBin_wrapper : AxisBin, bp::wrapper< AxisBin > {
return AxisBin::operator[]( index ); return AxisBin::operator[]( index );
} }
virtual ::std::vector< double > getBinBoundaries( ) const {
if( bp::override func_getBinBoundaries = this->get_override( "getBinBoundaries" ) )
return func_getBinBoundaries( );
else
return this->IAxis::getBinBoundaries( );
}
::std::vector< double > default_getBinBoundaries( ) const {
return IAxis::getBinBoundaries( );
}
virtual ::std::vector< double > getBinCenters( ) const {
if( bp::override func_getBinCenters = this->get_override( "getBinCenters" ) )
return func_getBinCenters( );
else
return this->IAxis::getBinCenters( );
}
::std::vector< double > default_getBinCenters( ) const {
return IAxis::getBinCenters( );
}
}; };
void register_AxisBin_class(){ void register_AxisBin_class(){
...@@ -264,6 +288,28 @@ void register_AxisBin_class(){ ...@@ -264,6 +288,28 @@ void register_AxisBin_class(){
, ( bp::arg("limit") ) ); , ( bp::arg("limit") ) );
} }
{ //::IAxis::getBinBoundaries
typedef ::std::vector< double > ( ::IAxis::*getBinBoundaries_function_type )( ) const;
typedef ::std::vector< double > ( AxisBin_wrapper::*default_getBinBoundaries_function_type )( ) const;
AxisBin_exposer.def(
"getBinBoundaries"
, getBinBoundaries_function_type(&::IAxis::getBinBoundaries)
, default_getBinBoundaries_function_type(&AxisBin_wrapper::default_getBinBoundaries) );
}
{ //::IAxis::getBinCenters
typedef ::std::vector< double > ( ::IAxis::*getBinCenters_function_type )( ) const;
typedef ::std::vector< double > ( AxisBin_wrapper::*default_getBinCenters_function_type )( ) const;
AxisBin_exposer.def(
"getBinCenters"
, getBinCenters_function_type(&::IAxis::getBinCenters)
, default_getBinCenters_function_type(&AxisBin_wrapper::default_getBinCenters) );
}
} }
} }
...@@ -116,6 +116,30 @@ struct AxisDouble_wrapper : AxisDouble, bp::wrapper< AxisDouble > { ...@@ -116,6 +116,30 @@ struct AxisDouble_wrapper : AxisDouble, bp::wrapper< AxisDouble > {
return AxisDouble::operator[]( index ); return AxisDouble::operator[]( index );
} }
virtual ::std::vector< double > getBinBoundaries( ) const {
if( bp::override func_getBinBoundaries = this->get_override( "getBinBoundaries" ) )
return func_getBinBoundaries( );
else
return this->IAxis::getBinBoundaries( );
}
::std::vector< double > default_getBinBoundaries( ) const {
return IAxis::getBinBoundaries( );
}
virtual ::std::vector< double > getBinCenters( ) const {
if( bp::override func_getBinCenters = this->get_override( "getBinCenters" ) )
return func_getBinCenters( );
else
return this->IAxis::getBinCenters( );
}
::std::vector< double > default_getBinCenters( ) const {
return IAxis::getBinCenters( );
}
}; };
void register_AxisDouble_class(){ void register_AxisDouble_class(){
...@@ -244,6 +268,28 @@ void register_AxisDouble_class(){ ...@@ -244,6 +268,28 @@ void register_AxisDouble_class(){
, ( bp::arg("element") ) ); , ( bp::arg("element") ) );
} }
{ //::IAxis::getBinBoundaries
typedef ::std::vector< double > ( ::IAxis::*getBinBoundaries_function_type )( ) const;
typedef ::std::vector< double > ( AxisDouble_wrapper::*default_getBinBoundaries_function_type )( ) const;
AxisDouble_exposer.def(
"getBinBoundaries"
, getBinBoundaries_function_type(&::IAxis::getBinBoundaries)
, default_getBinBoundaries_function_type(&AxisDouble_wrapper::default_getBinBoundaries) );
}
{ //::IAxis::getBinCenters
typedef ::std::vector< double > ( ::IAxis::*getBinCenters_function_type )( ) const;
typedef ::std::vector< double > ( AxisDouble_wrapper::*default_getBinCenters_function_type )( ) const;
AxisDouble_exposer.def(
"getBinCenters"
, getBinCenters_function_type(&::IAxis::getBinCenters)
, default_getBinCenters_function_type(&AxisDouble_wrapper::default_getBinCenters) );
}
} }
} }
...@@ -73,6 +73,30 @@ struct FixedBinAxis_wrapper : FixedBinAxis, bp::wrapper< FixedBinAxis > { ...@@ -73,6 +73,30 @@ struct FixedBinAxis_wrapper : FixedBinAxis, bp::wrapper< FixedBinAxis > {
return FixedBinAxis::getBin( index ); return FixedBinAxis::getBin( index );
} }
virtual ::std::vector< double > getBinBoundaries( ) const {
if( bp::override func_getBinBoundaries = this->get_override( "getBinBoundaries" ) )
return func_getBinBoundaries( );
else
return this->FixedBinAxis::getBinBoundaries( );
}
::std::vector< double > default_getBinBoundaries( ) const {
return FixedBinAxis::getBinBoundaries( );
}
virtual ::std::vector< double > getBinCenters( ) const {
if( bp::override func_getBinCenters = this->get_override( "getBinCenters" ) )
return func_getBinCenters( );
else
return this->FixedBinAxis::getBinCenters( );
}
::std::vector< double > default_getBinCenters( ) const {
return FixedBinAxis::getBinCenters( );
}
virtual double getMax( ) const { virtual double getMax( ) const {
if( bp::override func_getMax = this->get_override( "getMax" ) ) if( bp::override func_getMax = this->get_override( "getMax" ) )
return func_getMax( ); return func_getMax( );
...@@ -177,6 +201,28 @@ void register_FixedBinAxis_class(){ ...@@ -177,6 +201,28 @@ void register_FixedBinAxis_class(){
, default_getBin_function_type(&FixedBinAxis_wrapper::default_getBin) , default_getBin_function_type(&FixedBinAxis_wrapper::default_getBin)
, ( bp::arg("index") ) ); , ( bp::arg("index") ) );
}
{ //::FixedBinAxis::getBinBoundaries
typedef ::std::vector< double > ( ::FixedBinAxis::*getBinBoundaries_function_type )( ) const;
typedef ::std::vector< double > ( FixedBinAxis_wrapper::*default_getBinBoundaries_function_type )( ) const;
FixedBinAxis_exposer.def(
"getBinBoundaries"
, getBinBoundaries_function_type(&::FixedBinAxis::getBinBoundaries)
, default_getBinBoundaries_function_type(&FixedBinAxis_wrapper::default_getBinBoundaries) );
}
{ //::FixedBinAxis::getBinCenters
typedef ::std::vector< double > ( ::FixedBinAxis::*getBinCenters_function_type )( ) const;
typedef ::std::vector< double > ( FixedBinAxis_wrapper::*default_getBinCenters_function_type )( ) const;
FixedBinAxis_exposer.def(
"getBinCenters"
, getBinCenters_function_type(&::FixedBinAxis::getBinCenters)
, default_getBinCenters_function_type(&FixedBinAxis_wrapper::default_getBinCenters) );
} }
{ //::FixedBinAxis::getMax { //::FixedBinAxis::getMax
......
...@@ -50,6 +50,30 @@ struct IAxis_wrapper : IAxis, bp::wrapper< IAxis > { ...@@ -50,6 +50,30 @@ struct IAxis_wrapper : IAxis, bp::wrapper< IAxis > {
return func_getBin( index ); return func_getBin( index );
} }
virtual ::std::vector< double > getBinBoundaries( ) const {
if( bp::override func_getBinBoundaries = this->get_override( "getBinBoundaries" ) )
return func_getBinBoundaries( );
else
return this->IAxis::getBinBoundaries( );
}
::std::vector< double > default_getBinBoundaries( ) const {
return IAxis::getBinBoundaries( );
}
virtual ::std::vector< double > getBinCenters( ) const {
if( bp::override func_getBinCenters = this->get_override( "getBinCenters" ) )
return func_getBinCenters( );
else
return this->IAxis::getBinCenters( );
}
::std::vector< double > default_getBinCenters( ) const {
return IAxis::getBinCenters( );
}
virtual double getMax( ) const { virtual double getMax( ) const {
bp::override func_getMax = this->get_override( "getMax" ); bp::override func_getMax = this->get_override( "getMax" );
return func_getMax( ); return func_getMax( );
...@@ -124,6 +148,28 @@ void register_IAxis_class(){ ...@@ -124,6 +148,28 @@ void register_IAxis_class(){
, bp::pure_virtual( getBin_function_type(&::IAxis::getBin) ) , bp::pure_virtual( getBin_function_type(&::IAxis::getBin) )
, ( bp::arg("index") ) ); , ( bp::arg("index") ) );
}
{ //::IAxis::getBinBoundaries
typedef ::std::vector< double > ( ::IAxis::*getBinBoundaries_function_type )( ) const;
typedef ::std::vector< double > ( IAxis_wrapper::*default_getBinBoundaries_function_type )( ) const;
IAxis_exposer.def(
"getBinBoundaries"
, getBinBoundaries_function_type(&::IAxis::getBinBoundaries)
, default_getBinBoundaries_function_type(&IAxis_wrapper::default_getBinBoundaries) );
}
{ //::IAxis::getBinCenters
typedef ::std::vector< double > ( ::IAxis::*getBinCenters_function_type )( ) const;
typedef ::std::vector< double > ( IAxis_wrapper::*default_getBinCenters_function_type )( ) const;
IAxis_exposer.def(
"getBinCenters"
, getBinCenters_function_type(&::IAxis::getBinCenters)
, default_getBinCenters_function_type(&IAxis_wrapper::default_getBinCenters) );
} }
{ //::IAxis::getMax { //::IAxis::getMax
......
...@@ -126,6 +126,30 @@ struct VariableBinAxis_wrapper : VariableBinAxis, bp::wrapper< VariableBinAxis > ...@@ -126,6 +126,30 @@ struct VariableBinAxis_wrapper : VariableBinAxis, bp::wrapper< VariableBinAxis >
return IAxis::createDoubleBinSize( ); return IAxis::createDoubleBinSize( );
} }
virtual ::std::vector< double > getBinBoundaries( ) const {
if( bp::override func_getBinBoundaries = this->get_override( "getBinBoundaries" ) )
return func_getBinBoundaries( );
else
return this->IAxis::getBinBoundaries( );
}
::std::vector< double > default_getBinBoundaries( ) const {
return IAxis::getBinBoundaries( );
}
virtual ::std::vector< double > getBinCenters( ) const {
if( bp::override func_getBinCenters = this->get_override( "getBinCenters" ) )
return func_getBinCenters( );
else
return this->IAxis::getBinCenters( );
}
::std::vector< double > default_getBinCenters( ) const {
return IAxis::getBinCenters( );
}
}; };
void register_VariableBinAxis_class(){ void register_VariableBinAxis_class(){
...@@ -227,6 +251,28 @@ void register_VariableBinAxis_class(){ ...@@ -227,6 +251,28 @@ void register_VariableBinAxis_class(){
, bp::return_value_policy< bp::manage_new_object >() ); , bp::return_value_policy< bp::manage_new_object >() );
} }
{ //::IAxis::getBinBoundaries
typedef ::std::vector< double > ( ::IAxis::*getBinBoundaries_function_type )( ) const;
typedef ::std::vector< double > ( VariableBinAxis_wrapper::*default_getBinBoundaries_function_type )( ) const;
VariableBinAxis_exposer.def(
"getBinBoundaries"
, getBinBoundaries_function_type(&::IAxis::getBinBoundaries)
, default_getBinBoundaries_function_type(&VariableBinAxis_wrapper::default_getBinBoundaries) );
}
{ //::IAxis::getBinCenters
typedef ::std::vector< double > ( ::IAxis::*getBinCenters_function_type )( ) const;
typedef ::std::vector< double > ( VariableBinAxis_wrapper::*default_getBinCenters_function_type )( ) const;
VariableBinAxis_exposer.def(
"getBinCenters"
, getBinCenters_function_type(&::IAxis::getBinCenters)
, default_getBinCenters_function_type(&VariableBinAxis_wrapper::default_getBinCenters) );
}
} }
} }
...@@ -50,6 +50,10 @@ public: ...@@ -50,6 +50,10 @@ public:
size_t findClosestIndex(double value) const; size_t findClosestIndex(double value) const;
std::vector<double > getBinCenters() const;
std::vector<double > getBinBoundaries() const;
protected: protected:
void print(std::ostream& ostr) const; void print(std::ostream& ostr) const;
virtual bool equals(const IAxis& other) const; virtual bool equals(const IAxis& other) const;
......
...@@ -64,13 +64,13 @@ public: ...@@ -64,13 +64,13 @@ public:
//! test for equality //! test for equality
friend bool operator==(const IAxis& left, const IAxis& right); friend bool operator==(const IAxis& left, const IAxis& right);
// friend bool operator==(const IAxis& left, const IAxis& right) {
// return left.equals(right);
// }
friend std::ostream& operator<<(std::ostream& ostr, const IAxis& m) friend std::ostream& operator<<(std::ostream& ostr, const IAxis& m)
{ m.print(ostr); return ostr; } { m.print(ostr); return ostr; }
virtual std::vector<double > getBinCenters() const;
virtual std::vector<double > getBinBoundaries() const;
protected: protected:
virtual void print(std::ostream& ostr) const=0; virtual void print(std::ostream& ostr) const=0;
virtual bool equals(const IAxis& other) const; virtual bool equals(const IAxis& other) const;
...@@ -95,6 +95,16 @@ inline bool operator!=(const IAxis& left, const IAxis& right) { ...@@ -95,6 +95,16 @@ inline bool operator!=(const IAxis& left, const IAxis& right) {
return !(left == right); return !(left == right);
} }
inline std::vector<double> IAxis::getBinCenters() const
{
throw Exceptions::NotImplementedException("IAxis::getBinCenters() -> Error. Not implemented.");
}
inline std::vector<double> IAxis::getBinBoundaries() const
{
throw Exceptions::NotImplementedException("IAxis::getBinBoundaries() -> Error. Not implemented.");
}
//! global helper function for comparison of axes //! global helper function for comparison of axes
inline bool HaveSameNameAndShape(const IAxis& left, const IAxis& right) inline bool HaveSameNameAndShape(const IAxis& left, const IAxis& right)
{ {
......
...@@ -80,6 +80,27 @@ size_t FixedBinAxis::findClosestIndex(double value) const ...@@ -80,6 +80,27 @@ size_t FixedBinAxis::findClosestIndex(double value) const
return int( (value-m_start)/step); return int( (value-m_start)/step);
} }
std::vector<double> FixedBinAxis::getBinCenters() const
{
std::vector<double> result;
result.resize(getSize(), 0.0);
for(size_t i=0; i<getSize(); ++i) {
result[i] = getBin(i).getMidPoint();
}
return result;
}
std::vector<double> FixedBinAxis::getBinBoundaries() const
{
std::vector<double> result;
result.resize(getSize()+1, 0.0);
for(size_t i=0; i<getSize(); ++i) {
result[i] = getBin(i).m_lower;
}
result[getSize()] = getBin(getSize()-1).m_upper;
return result;
}
void FixedBinAxis::print(std::ostream& ostr) const void FixedBinAxis::print(std::ostream& ostr) const
{ {
......
...@@ -29,8 +29,29 @@ class AxesTest(unittest.TestCase): ...@@ -29,8 +29,29 @@ class AxesTest(unittest.TestCase):
def test_FixedBinAxis_equality(self): def test_FixedBinAxis_equality(self):
b1 = FixedBinAxis("axis", 99, -1.01, 3.3) b1 = FixedBinAxis("axis", 99, -1.01, 3.3)
b2 = FixedBinAxis("axis", 99, -1.01, 3.3) b2 = FixedBinAxis("axis", 99, -1.01, 3.3)
self.assertTrue( b1 == b2) self.assertTrue( b1 == b2)
b3 = FixedBinAxis("axissss", 99, -1.01, 3.3)
b4 = FixedBinAxis("axis", 99, -1.0, 3.3)
b5 = FixedBinAxis("axis", 99, -1.01, 3.29)
b6 = FixedBinAxis("axiss", 98, -1.01, 3.3)
self.assertFalse( b1 == b3)
self.assertFalse( b1 == b4)
self.assertFalse( b1 == b5)
self.assertFalse( b1 == b6)
def test_FixedBinAxis_boundaries(self):
axis = FixedBinAxis("name", 3, -1.5, 1.5)
centers = axis.getBinCenters()
self.assertEqual(3, len(centers))
self.assertAlmostEqual(-1.0, centers[0])
self.assertAlmostEqual(0.0, centers[1])
self.assertAlmostEqual(1.0, centers[2])
boundaries = axis.getBinBoundaries()
self.assertEqual(4, len(boundaries))
self.assertAlmostEqual(-1.5, boundaries[0])
self.assertAlmostEqual(-0.5, boundaries[1])
self.assertAlmostEqual(0.5, boundaries[2])
self.assertAlmostEqual(1.5, boundaries[3])
if __name__ == '__main__': if __name__ == '__main__':
......
# tests of IntensityData object # tests of IO operations with the IntensityData object
import sys import sys
import os import os
...@@ -13,27 +13,31 @@ from libBornAgainCore import * ...@@ -13,27 +13,31 @@ from libBornAgainCore import *
def fill_data(data): def fill_data(data):
"""Fills intensity data with some numbers""" """
Fills intensity data with some numbers
"""
for i in range(0, data.getAllocatedSize()): for i in range(0, data.getAllocatedSize()):
data[i] = i data[i] = i
def is_the_same(data1, data2): def is_the_same(data1, data2):
"""
Checks if two data are identical
"""
if data1.getAllocatedSize() != data2.getAllocatedSize(): if data1.getAllocatedSize() != data2.getAllocatedSize():
return False return False
if data1.getRank() != data2.getRank(): if data1.getRank() != data2.getRank():
return False return False
for i in range(0, data1.getRank()): for i in range(0, data1.getRank()):
if data1.getAxis(i) == data2.getAxis(i): if data1.getAxis(i) != data2.getAxis(i):
print "xxx" return False
else: for i in range(0, data1.getAllocatedSize()):
print "yyy" if data1[i] != data2[i]:
return False
return True return True
class OutputDataIOTest(unittest.TestCase): class OutputDataIOTest(unittest.TestCase):
""" """
Test serialization of IntensityData Test serialization of IntensityData
...@@ -44,8 +48,27 @@ class OutputDataIOTest(unittest.TestCase): ...@@ -44,8 +48,27 @@ class OutputDataIOTest(unittest.TestCase):
fill_data(data) fill_data(data)
OutputDataIOFactory.writeIntensityData(data, "tmp.baint") OutputDataIOFactory.writeIntensityData(data, "tmp.baint")
newdata = OutputDataIOFactory.readIntensityData("tmp.baint") newdata = OutputDataIOFactory.readIntensityData("tmp.baint")
self.assertTrue(is_the_same(data, newdata))
def test_2D_FixedBinAxis(self):
data = IntensityData()
data.addAxis(FixedBinAxis("axis0", 9, -1.00000001, 1.0))
data.addAxis(FixedBinAxis("axis1", 3, -4.0, 5.0))
fill_data(data)
OutputDataIOFactory.writeIntensityData(data, "tmp.baint")
newdata = OutputDataIOFactory.readIntensityData("tmp.baint")
self.assertTrue(is_the_same(data, newdata))
def test_3D_FixedBinAxis(self):
data = IntensityData()
data.addAxis(FixedBinAxis("axis0", 9, -1.00000001, 1.0))
data.addAxis(FixedBinAxis("axis1", 1, -4.0, 5.0))
data.addAxis(FixedBinAxis("axis2", 3, 0.0, 1.0))
fill_data(data)
OutputDataIOFactory.writeIntensityData(data, "tmp.baint")
newdata = OutputDataIOFactory.readIntensityData("tmp.baint")
self.assertTrue(is_the_same(data, newdata)) self.assertTrue(is_the_same(data, newdata))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -142,5 +142,26 @@ TEST_F(FixedBinAxisTest, IOStream) ...@@ -142,5 +142,26 @@ TEST_F(FixedBinAxisTest, IOStream)
delete result; delete result;
} }
TEST_F(FixedBinAxisTest, BinCenters)
{
FixedBinAxis axis("name", 3, -1.5, 1.5);
std::vector<double> centers = axis.getBinCenters();
EXPECT_EQ(3, centers.size());
EXPECT_DOUBLE_EQ(-1.0, centers[0]);
EXPECT_DOUBLE_EQ(0.0, centers[1]);
EXPECT_DOUBLE_EQ(1.0, centers[2]);
}
TEST_F(FixedBinAxisTest, BinBoundaries)
{
FixedBinAxis axis("name", 3, -1.5, 1.5);
std::vector<double> boundaries = axis.getBinBoundaries();
EXPECT_EQ(4, boundaries.size());
EXPECT_DOUBLE_EQ(-1.5, boundaries[0]);
EXPECT_DOUBLE_EQ(-0.5, boundaries[1]);
EXPECT_DOUBLE_EQ(0.5, boundaries[2]);
EXPECT_DOUBLE_EQ(1.5, boundaries[3]);
}
#endif #endif
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