Skip to content

Commit

Permalink
Introduce vFloat and float types in ParameterSets
Browse files Browse the repository at this point in the history
  • Loading branch information
felicepantaleo committed Jun 13, 2024
1 parent 23d93b4 commit 4e3d218
Show file tree
Hide file tree
Showing 16 changed files with 397 additions and 27 deletions.
8 changes: 8 additions & 0 deletions FWCore/ParameterSet/interface/Entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ namespace edm {
Entry(std::string const& name, std::vector<double> const& val, bool is_tracked);
std::vector<double> getVDouble() const;

// Float
Entry(std::string const& name, float val, bool is_tracked);
float getFloat() const;

// vFloat
Entry(std::string const& name, std::vector<float> const& val, bool is_tracked);
std::vector<float> getVFloat() const;

// String
Entry(std::string const& name, std::string const& val, bool is_tracked);
std::string getString() const;
Expand Down
4 changes: 4 additions & 0 deletions FWCore/ParameterSet/interface/ParameterDescription.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace edm {
ValueFormat format);
void writeValue(std::ostream& os, int indentation, double const& value_, ValueFormat format);
void writeValue(std::ostream& os, int indentation, std::vector<double> const& value_, ValueFormat format);
void writeValue(std::ostream& os, int indentation, float const& value_, ValueFormat format);
void writeValue(std::ostream& os, int indentation, std::vector<float> const& value_, ValueFormat format);
void writeValue(std::ostream& os, int indentation, bool const& value_, ValueFormat format);
void writeValue(std::ostream& os, int indentation, std::string const& value_, ValueFormat format);
void writeValue(std::ostream& os, int indentation, std::vector<std::string> const& value_, ValueFormat format);
Expand Down Expand Up @@ -88,6 +90,8 @@ namespace edm {
bool hasNestedContent(std::vector<unsigned long long> const& value);
bool hasNestedContent(double const& value);
bool hasNestedContent(std::vector<double> const& value);
bool hasNestedContent(float const& value);
bool hasNestedContent(std::vector<float> const& value);
bool hasNestedContent(bool const& value);
bool hasNestedContent(std::string const& value);
bool hasNestedContent(std::vector<std::string> const& value);
Expand Down
4 changes: 3 additions & 1 deletion FWCore/ParameterSet/interface/ParameterDescriptionNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ namespace edm {
k_EventRange = 'R',
k_VEventRange = 'r',
k_PSet = 'Q',
k_VPSet = 'q'
k_VPSet = 'q',
k_float = 'H',
k_vfloat = 'h'
};

std::string parameterTypeEnumToString(ParameterTypes iType);
Expand Down
50 changes: 50 additions & 0 deletions FWCore/ParameterSet/interface/ParameterSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,15 @@ namespace edm {
template <>
std::vector<double> ParameterSet::getParameter<std::vector<double>>(std::string const& name) const;

// ----------------------------------------------------------------------
// Float, vFloat

template <>
float ParameterSet::getParameter<float>(std::string const& name) const;

template <>
std::vector<float> ParameterSet::getParameter<std::vector<float>>(std::string const& name) const;

// ----------------------------------------------------------------------
// String, vString

Expand Down Expand Up @@ -618,6 +627,22 @@ namespace edm {
template <>
std::vector<double> ParameterSet::getUntrackedParameter<std::vector<double>>(std::string const& name) const;

// ----------------------------------------------------------------------
// Float, vFloat

template <>
float ParameterSet::getUntrackedParameter<float>(std::string const& name, float const& defaultValue) const;

template <>
float ParameterSet::getUntrackedParameter<float>(std::string const& name) const;

template <>
std::vector<float> ParameterSet::getUntrackedParameter<std::vector<float>>(
std::string const& name, std::vector<float> const& defaultValue) const;

template <>
std::vector<float> ParameterSet::getUntrackedParameter<std::vector<float>>(std::string const& name) const;

// ----------------------------------------------------------------------
// String, vString

Expand Down Expand Up @@ -778,6 +803,15 @@ namespace edm {
template <>
std::vector<double> ParameterSet::getParameter<std::vector<double>>(char const* name) const;

// ----------------------------------------------------------------------
// Float, vFloat

template <>
float ParameterSet::getParameter<float>(char const* name) const;

template <>
std::vector<float> ParameterSet::getParameter<std::vector<float>>(char const* name) const;

// ----------------------------------------------------------------------
// String, vString

Expand Down Expand Up @@ -969,6 +1003,22 @@ namespace edm {
template <>
std::vector<double> ParameterSet::getUntrackedParameter<std::vector<double>>(char const* name) const;

// ----------------------------------------------------------------------
// Float, vFloat

template <>
float ParameterSet::getUntrackedParameter<float>(char const* name, float const& defaultValue) const;

template <>
float ParameterSet::getUntrackedParameter<float>(char const* name) const;

template <>
std::vector<float> ParameterSet::getUntrackedParameter<std::vector<float>>(
char const* name, std::vector<float> const& defaultValue) const;

template <>
std::vector<float> ParameterSet::getUntrackedParameter<std::vector<float>>(char const* name) const;

// ----------------------------------------------------------------------
// String, vString

Expand Down
8 changes: 8 additions & 0 deletions FWCore/ParameterSet/interface/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ namespace edm {
bool decode(std::vector<double>&, std::string_view);
bool encode(std::string&, std::vector<double> const&);

// Float
bool decode(float&, std::string_view);
bool encode(std::string&, float);

// vFloat
bool decode(std::vector<float>&, std::string_view);
bool encode(std::string&, std::vector<float> const&);

// String
bool decode(std::string&, std::string_view);
bool encode(std::string&, std::string const&);
Expand Down
5 changes: 4 additions & 1 deletion FWCore/ParameterSet/python/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from .Options import Options
options = Options()


## imports
import sys
from typing import Union
Expand Down Expand Up @@ -2141,6 +2140,10 @@ def addDouble(self,tracked,label,value):
self.__insertValue(tracked,label,value)
def addVDouble(self,tracked,label,value):
self.__insertValue(tracked,label,value)
def addFloat(self,tracked,label,value):
self.__insertValue(tracked,label,value)
def addVFloat(self,tracked,label,value):
self.__insertValue(tracked,label,value)
def addBool(self,tracked,label,value):
self.__insertValue(tracked,label,value)
def addString(self,tracked,label,value):
Expand Down
2 changes: 2 additions & 0 deletions FWCore/ParameterSet/python/DummyCfis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
cms.string.dummyDefault="__default__"
cms.double.dummyDefault = 9999
cms.vdouble.dummyDefault = []
cms.float.dummyDefault = 9999
cms.vfloat.dummyDefault = []
cms.vint32.dummyDefault = []
cms.vuint32.dummyDefault = []
cms.vint64.dummyDefault = []
Expand Down
85 changes: 64 additions & 21 deletions FWCore/ParameterSet/python/Types.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,13 @@ def __nonzero__(self) -> bool:
class double(_SimpleParameterTypeBase):
@staticmethod
def _isValid(value) -> bool:
return isinstance(value, (int, long, float))
return isinstance(value, (int, long, builtins.float))
@staticmethod
def _valueFromString(value:str):
"""only used for cfg-parsing"""
return double(float(value))
return double(builtins.float(value))
def insertInto(self, parameterSet, myname:str):
parameterSet.addDouble(self.isTracked(), myname, float(self.value()))
parameterSet.addDouble(self.isTracked(), myname, builtins.float(self.value()))
def __nonzero__(self) -> bool:
return self.value()!=0.
def configValue(self, options:PrintOptions=PrintOptions()) -> str:
Expand All @@ -345,7 +345,30 @@ def _pythonValue(value) -> str:
return "float('nan')"
return str(value)


class float(_SimpleParameterTypeBase):
@staticmethod
def _isValid(value) -> bool:
return isinstance(value, (int, long, builtins.float))
@staticmethod
def _valueFromString(value:str):
"""only used for cfg-parsing"""
return float(builtins.float(value))
def insertInto(self, parameterSet, myname:str):
parameterSet.addFloat(self.isTracked(), myname, builtins.float(self.value()))
def __nonzero__(self) -> bool:
return self.value()!=0.
def configValue(self, options:PrintOptions=PrintOptions()) -> str:
return float._pythonValue(self._value)
@staticmethod
def _pythonValue(value) -> str:
if math.isinf(value):
if value > 0:
return "float('inf')"
else:
return "-float('inf')"
if math.isnan(value):
return "float('nan')"
return str(value)

class bool(_SimpleParameterTypeBase):
@staticmethod
Expand Down Expand Up @@ -1022,7 +1045,6 @@ def _place(self,name:str,proc):
proc._placePSet(name,self)
def __str__(self) -> str:
return object.__str__(self)

class PSet(_ParameterTypeBase,_Parameterizable,_ConfigureComponent,_Labelable):
def __init__(self,*arg,**args):
#need to call the inits separately
Expand Down Expand Up @@ -1157,7 +1179,19 @@ def insertInto(self, parameterSet, myname:str):
def pythonValueForItem(self,item, options) -> str:
return double._pythonValue(item)


class vfloat(_ValidatingParameterListBase):
def __init__(self,*arg,**args):
super(vfloat,self).__init__(*arg,**args)
@classmethod
def _itemIsValid(cls,item) -> builtins.bool:
return float._isValid(item)
@staticmethod
def _valueFromString(value:str):
return vfloat(*_ValidatingParameterListBase._itemsFromStrings(value,float._valueFromString))
def insertInto(self, parameterSet, myname:str):
parameterSet.addVFloat(self.isTracked(), myname, self.value())
def pythonValueForItem(self,item, options) -> str:
return float._pythonValue(item)


class vbool(_ValidatingParameterListBase):
Expand Down Expand Up @@ -1439,6 +1473,10 @@ def addDouble(self,tracked:bool,label:str,value):
v = double(value)
v.setIsTracked(tracked)
setattr(self.pset,label,v)
def addFloat(self,tracked:bool,label:str,value):
v = float(value)
v.setIsTracked(tracked)
setattr(self.pset,label,v)
def addString(self,tracked:bool,label:str,value):
v = string(value)
v.setIsTracked(tracked)
Expand Down Expand Up @@ -1491,6 +1529,11 @@ def addVDouble(self,tracked:bool,label:str,value):
v = vdouble(value)
v.setIsTracked(tracked)
setattr(self.pset,label,v)

def addVFloat(self,tracked:bool,label:str,value):
v = vfloat(value)
v.setIsTracked(tracked)
setattr(self.pset,label,v)
def addVString(self,tracked:bool,label:str,value):
v = vstring(value)
v.setIsTracked(tracked)
Expand Down Expand Up @@ -1680,26 +1723,26 @@ def testdouble(self):
d = double(1)
self.assertEqual(d.value(),1)
self.assertEqual(d.pythonValue(),'1')
d = double(float('Inf'))
self.assertEqual(d,float('Inf'))
d = double(builtins.float('Inf'))
self.assertEqual(d,builtins.float('Inf'))
self.assertEqual(d.pythonValue(),"float('inf')")
d = double(-float('Inf'))
self.assertEqual(d,-float('Inf'))
d = double(-builtins.float('Inf'))
self.assertEqual(d,-builtins.float('Inf'))
self.assertEqual(d.pythonValue(),"-float('inf')")
d = double(float('Nan'))
d = double(builtins.float('Nan'))
self.assertTrue(math.isnan(d.value()))
self.assertEqual(d.pythonValue(),"float('nan')")
def testvdouble(self):
d = vdouble(1)
self.assertEqual(d.value(),[1])
self.assertEqual(d.dumpPython(),'cms.vdouble(1)')
d = vdouble(float('inf'))
self.assertEqual(d,[float('inf')])
d = vdouble(builtins.float('inf'))
self.assertEqual(d,[builtins.float('inf')])
self.assertEqual(d.dumpPython(),"cms.vdouble(float('inf'))")
d = vdouble(-float('Inf'))
self.assertEqual(d,[-float('inf')])
d = vdouble(-builtins.float('Inf'))
self.assertEqual(d,[-builtins.float('inf')])
self.assertEqual(d.dumpPython(),"cms.vdouble(-float('inf'))")
d = vdouble(float('nan'))
d = vdouble(builtins.float('nan'))
self.assertTrue(math.isnan(d[0]))
self.assertEqual(d.dumpPython(),"cms.vdouble(float('nan'))")
def testvint32(self):
Expand Down Expand Up @@ -2539,12 +2582,12 @@ def testincompatibletypes(self):
with self.assertRaises(TypeError):
3 < string("I am a string")
def testinfinity(self):
self.assertLess(1e99, double(float("inf")))
self.assertLess(double(1e99), float("inf"))
self.assertGreater(1e99, double(float("-inf")))
self.assertEqual(double(float("inf")), float("inf"))
self.assertLess(1e99, double(builtins.float("inf")))
self.assertLess(double(1e99), builtins.float("inf"))
self.assertGreater(1e99, double(builtins.float("-inf")))
self.assertEqual(double(builtins.float("inf")), builtins.float("inf"))
def testnan(self):
nan = double(float("nan"))
nan = double(builtins.float("nan"))
self.assertNotEqual(nan, nan)
self.assertFalse(nan > 3 or nan < 3 or nan == 3)

Expand Down
Loading

0 comments on commit 4e3d218

Please sign in to comment.