Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic conversion of integers to doubles when setting parameters #2024

Merged
merged 13 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions nestkernel/nestmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,27 +222,6 @@ NestModule::create_mask( const Token& t )
// The anchor may be an array of doubles (a spatial position).
// For grid layers only, it is also possible to provide an array of longs.
try
{

std::vector< double > anchor = getValue< std::vector< double > >( anchor_token );
AbstractMask* amask;

switch ( anchor.size() )
{
case 2:
amask = new AnchoredMask< 2 >( dynamic_cast< Mask< 2 >& >( *mask ), anchor );
break;
case 3:
amask = new AnchoredMask< 3 >( dynamic_cast< Mask< 3 >& >( *mask ), anchor );
break;
default:
throw BadProperty( "Anchor must be 2- or 3-dimensional." );
}

delete mask;
mask = amask;
}
catch ( TypeMismatch& e )
{
std::vector< long > anchor = getValue< std::vector< long > >( anchor_token );

Expand Down Expand Up @@ -272,6 +251,26 @@ NestModule::create_mask( const Token& t )
break;
}
}
catch ( TypeMismatch& e )
{
std::vector< double > anchor = getValue< std::vector< double > >( anchor_token );
AbstractMask* amask;

switch ( anchor.size() )
{
case 2:
amask = new AnchoredMask< 2 >( dynamic_cast< Mask< 2 >& >( *mask ), anchor );
break;
case 3:
amask = new AnchoredMask< 3 >( dynamic_cast< Mask< 3 >& >( *mask ), anchor );
break;
default:
throw BadProperty( "Anchor must be 2- or 3-dimensional." );
}

delete mask;
mask = amask;
}
}

return mask;
Expand Down
2 changes: 0 additions & 2 deletions pynest/nest/tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ def test_erroneous_param_to_create(self):
params = [(tuple(), TypeError),
({'V_m': [-50]}, IndexError),
({'V_mm': num_nodes*[-50.]}, nest_errors.DictError),
({'V_m': num_nodes*[-50]}, nest_errors.TypeMismatch),
({'V_m': -50, 'C_m': num_nodes*[20.]}, nest_errors.TypeMismatch),
]

for p, err in params:
Expand Down
4 changes: 2 additions & 2 deletions sli/sliarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ SLIArrayModule::SortFunction::execute( SLIInterpreter* i ) const

try
{
std::vector< double > vd;
std::vector< long > vd;
td.toVector( vd );
std::sort( vd.begin(), vd.end() );
i->OStack.pop();
Expand All @@ -571,7 +571,7 @@ SLIArrayModule::SortFunction::execute( SLIInterpreter* i ) const

try
{
std::vector< long > vd;
std::vector< double > vd;
td.toVector( vd );
std::sort( vd.begin(), vd.end() );
i->OStack.pop();
Expand Down
5 changes: 0 additions & 5 deletions sli/token.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ Token::operator double() const
return getValue< double >( *this );
}

Token::operator float() const
{
return getValue< float >( *this );
}

Token::operator bool() const
{
return getValue< bool >( *this );
Expand Down
14 changes: 11 additions & 3 deletions sli/tokenarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,21 @@ TokenArray::toVector( std::vector< double >& a ) const
a.reserve( size() );
for ( Token* idx = begin(); idx != end(); ++idx )
{
DoubleDatum* targetid = dynamic_cast< DoubleDatum* >( idx->datum() );
if ( targetid == NULL )
DoubleDatum* targetdd = dynamic_cast< DoubleDatum* >( idx->datum() );
if ( targetdd )
{
a.push_back( targetdd->get() );
}
else if ( IntegerDatum* targetid = dynamic_cast< IntegerDatum* >( idx->datum() ) )
{
a.push_back( static_cast< double >( targetid->get() ) );
}
else
{
DoubleDatum const d;
throw TypeMismatch( d.gettypename().toString(), idx->datum()->gettypename().toString() );
}
a.push_back( targetid->get() );

}
}

Expand Down
88 changes: 36 additions & 52 deletions sli/tokenutils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ getValue< long >( const Token& t )
{
const IntegerDatum* id = dynamic_cast< const IntegerDatum* >( t.datum() );
if ( id == NULL )
{ // we have to create a Datum object to get the name...
{ // We have to create a Datum object to get the name...
IntegerDatum const d;
throw TypeMismatch( d.gettypename().toString(), t.datum()->gettypename().toString() );
}
Expand All @@ -54,7 +54,7 @@ setValue< long >( const Token& t, long const& value )
{
IntegerDatum* id = dynamic_cast< IntegerDatum* >( t.datum() );
if ( id == NULL )
{ // we have to create a Datum object to get the name...
{ // We have to create a Datum object to get the name...
IntegerDatum const d;
throw TypeMismatch( d.gettypename().toString(), t.datum()->gettypename().toString() );
}
Expand All @@ -73,50 +73,34 @@ template <>
double
getValue< double >( const Token& t )
{
DoubleDatum* id = dynamic_cast< DoubleDatum* >( t.datum() );
if ( id == NULL )
{ // we have to create a Datum object to get the name...
DoubleDatum const d;
throw TypeMismatch( d.gettypename().toString(), t.datum()->gettypename().toString() );
}
return id->get();
}
template <>
void
setValue< double >( const Token& t, double const& value )
{
DoubleDatum* id = dynamic_cast< DoubleDatum* >( t.datum() );
if ( id == NULL )
{ // we have to create a Datum object to get the name...
DoubleDatum const d;
throw TypeMismatch( d.gettypename().toString(), t.datum()->gettypename().toString() );

DoubleDatum* dd = dynamic_cast< DoubleDatum* >( t.datum() );
if ( dd )
{
return dd->get();
}
( *id ) = value;
}
template <>
float
getValue< float >( const Token& t )
{
DoubleDatum* id = dynamic_cast< DoubleDatum* >( t.datum() );
if ( id == NULL )
{ // we have to create a Datum object to get the name...
DoubleDatum const d;
throw TypeMismatch( d.gettypename().toString(), t.datum()->gettypename().toString() );
IntegerDatum* id = dynamic_cast< IntegerDatum* >( t.datum() );
if ( id )
{
return static_cast< double >( id->get() );
}
return ( float ) id->get();

// We have to create a Datum object to get the name...
DoubleDatum const d;
throw TypeMismatch( d.gettypename().toString(), t.datum()->gettypename().toString() );
}

template <>
void
setValue< float >( const Token& t, float const& value )
setValue< double >( const Token& t, double const& value )
{
DoubleDatum* id = dynamic_cast< DoubleDatum* >( t.datum() );
if ( id == NULL )
{ // we have to create a Datum object to get the name...
{ // We have to create a Datum object to get the name...
DoubleDatum const d;
throw TypeMismatch( d.gettypename().toString(), t.datum()->gettypename().toString() );
}
( *id ) = ( double ) value;
( *id ) = value;
}

template <>
Expand All @@ -132,25 +116,25 @@ getValue< bool >( const Token& t )
{
BoolDatum* bd = dynamic_cast< BoolDatum* >( t.datum() );
if ( bd == NULL )
{ // we have to create a Datum object to get the name...
{ // We have to create a Datum object to get the name...
BoolDatum const d( false );
throw TypeMismatch( d.gettypename().toString(), t.datum()->gettypename().toString() );
}
return static_cast< bool >( *bd );
// we should have used i->true_name, bit we don't know the interpreter here.
// We should have used i->true_name, bit we don't know the interpreter here.
}
template <>
void
setValue< bool >( const Token& t, bool const& value )
{
BoolDatum* bd = dynamic_cast< BoolDatum* >( t.datum() );
if ( bd == NULL )
{ // we have to create a Datum object to get the name...
{ // We have to create a Datum object to get the name...
BoolDatum const d( false );
throw TypeMismatch( d.gettypename().toString(), t.datum()->gettypename().toString() );
}
*bd = BoolDatum( value );
// we should have used i->true_name, bit we don't know the interpreter here.
// We should have used i->true_name, bit we don't know the interpreter here.
}


Expand All @@ -159,7 +143,7 @@ Token
newToken< bool >( bool const& value )
{
return Token( new BoolDatum( value ) );
// we should have used i->true_name, bit we don't know the interpreter here.
// We should have used i->true_name, bit we don't know the interpreter here.
}


Expand Down Expand Up @@ -187,7 +171,7 @@ getValue< std::string >( const Token& t )
else
{
// The given token can never yield a string!
// we have to create Datum objects to get the expected names...
// We have to create Datum objects to get the expected names...
StringDatum const d1;
NameDatum const d2( "dummy" );
LiteralDatum const d3( "dummy" );
Expand Down Expand Up @@ -215,7 +199,7 @@ setValue< std::string >( const Token& t, std::string const& value )
BoolDatum* b = dynamic_cast< BoolDatum* >( t.datum() );
if ( b != NULL )
{
// we have to create Datum objects to get the expected names...
// We have to create Datum objects to get the expected names...
StringDatum const d1;
NameDatum const d2( "dummy" );
LiteralDatum const d3( "dummy" );
Expand All @@ -236,7 +220,7 @@ setValue< std::string >( const Token& t, std::string const& value )
else
{
// The given token can never hold a string!
// we have to create Datum objects to get the expected names...
// We have to create Datum objects to get the expected names...
StringDatum const d1;
NameDatum const d2( "dummy" );
LiteralDatum const d3( "dummy" );
Expand Down Expand Up @@ -265,14 +249,14 @@ template <>
std::vector< double >
getValue< std::vector< double > >( const Token& t )
{
// try DoubleVectorDatum first
// Try DoubleVectorDatum first
DoubleVectorDatum* dvd = dynamic_cast< DoubleVectorDatum* >( t.datum() );
if ( dvd )
{
return **dvd;
}

// ok, try ArrayDatum
// Ok, try ArrayDatum
ArrayDatum* ad = dynamic_cast< ArrayDatum* >( t.datum() );
if ( ad )
{
Expand All @@ -281,7 +265,7 @@ getValue< std::vector< double > >( const Token& t )
return data;
}

// out of options
// Out of options
throw TypeMismatch( DoubleVectorDatum().gettypename().toString() + " or " + ArrayDatum().gettypename().toString(),
t.datum()->gettypename().toString() );
}
Expand All @@ -292,14 +276,14 @@ setValue< std::vector< double > >( const Token& t, std::vector< double > const&
{
ArrayDatum* ad = dynamic_cast< ArrayDatum* >( t.datum() );
if ( ad == NULL )
{ // we have to create a Datum object to get the name...
{ // We have to create a Datum object to get the name...
ArrayDatum const d;
throw TypeMismatch( d.gettypename().toString(), t.datum()->gettypename().toString() );
}
// ArrayDatum is an AggregateDatum, which means, it is derived from
// TokenArray. Hence, we can use ad just like a TokenArray:
if ( ad->size() != value.size() )
{ // arrays have incompatible size
{ // Arrays have incompatible size
throw RangeCheck( value.size() );
}
for ( size_t i = 0; i < ad->size(); ++i )
Expand All @@ -322,14 +306,14 @@ template <>
std::vector< long >
getValue< std::vector< long > >( const Token& t )
{
// try IntVectorDatum first
// Try IntVectorDatum first
IntVectorDatum* ivd = dynamic_cast< IntVectorDatum* >( t.datum() );
if ( ivd )
{
return **ivd;
}

// ok, try ArrayDatum
// Ok, try ArrayDatum
ArrayDatum* ad = dynamic_cast< ArrayDatum* >( t.datum() );
if ( ad )
{
Expand All @@ -338,7 +322,7 @@ getValue< std::vector< long > >( const Token& t )
return data;
}

// out of options
// Out of options
throw TypeMismatch( IntVectorDatum().gettypename().toString() + " or " + ArrayDatum().gettypename().toString(),
t.datum()->gettypename().toString() );
}
Expand All @@ -349,14 +333,14 @@ setValue< std::vector< long > >( const Token& t, std::vector< long > const& valu
{
ArrayDatum* ad = dynamic_cast< ArrayDatum* >( t.datum() );
if ( ad == NULL )
{ // we have to create a Datum object to get the name...
{ // We have to create a Datum object to get the name...
ArrayDatum const d;
throw TypeMismatch( d.gettypename().toString(), t.datum()->gettypename().toString() );
}
// ArrayDatum is an AggregateDatum, which means, it is derived from
// TokenArray. Hence, we can use ad just like a TokenArray:
if ( ad->size() != value.size() )
{ // arrays have incompatible size
{ // Arrays have incompatible size
throw RangeCheck( value.size() );
}
for ( size_t i = 0; i < ad->size(); ++i )
Expand Down
6 changes: 0 additions & 6 deletions sli/tokenutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,6 @@ double getValue< double >( const Token& );
template <>
void setValue< double >( const Token&, double const& value );

template <>
float getValue< float >( const Token& );

template <>
void setValue< float >( const Token&, float const& value );


template <>
Token newToken< double >( double const& value );
Expand Down
Loading