Skip to content

Commit

Permalink
Added the possibility to remove the path check option for PathPropert…
Browse files Browse the repository at this point in the history
…ies for those that are created by the code and that do not exist beforehand. The check is performed after their creation now
  • Loading branch information
WilfriedMercier committed Sep 3, 2022
1 parent 982b3f9 commit 2770a4a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 28 deletions.
54 changes: 40 additions & 14 deletions misc/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ class PathProperty(Property):
r'''
.. codeauthor:: Wilfried Mercier - IRAP <[email protected]>
Define a property which stores a str object which includes additional checks for paths.
Define a property which stores a str object and can include additional checks for paths.
:param default: default value used at init
:type default: :python:`str`
Expand All @@ -641,19 +641,23 @@ class PathProperty(Property):
:type testFunc: :python:`Callable`
:param testMsg: (**Optional**) a test message used to throw an error if **testFunc** returns :python:`False`
:type testMsg: :python:`str`
:param str path: (**Optional**) path to append each time to the new value
:param path: (**Optional**) path to append each time to the new value
:type path: :python:`str`
:param str ext: (**Optional**) extension to append at the end of the file name when checking the path
:param ext: (**Optional**) extension to append at the end of the file name when checking the path
:type ext: :python:`str`
:param skipCheckPath: (**Optional**) whether to skip the check of the existence of the path. It can be useful for paths that have not been created yet.
:type skipCheckPath: :python:`bool`
:raises TypeError: if neither **path** nor **ext** are :python:`str`
'''

def __init__(self, default: str,
testFunc: Callable[[str], bool] = lambda value: False,
testMsg: str ='',
path: str = '',
ext: str = '', **kwargs) -> None:
testMsg: str ='',
path: str = '',
ext: str = '',
skipCheckPath: bool = False,
**kwargs) -> None:

r'''Init method.'''

Expand All @@ -672,7 +676,7 @@ def __init__(self, default: str,
self.path = path

# Set the value to check data type (default property has already been set)
self.set(default)
self.set(default, skipCheckPath=skipCheckPath)

def __str__(self, *args, **kwargs) -> str:
r'''
Expand All @@ -686,24 +690,46 @@ def __str__(self, *args, **kwargs) -> str:

return self.value

def _checkPath(self, value: str, *args, **kwargs) -> bool:
r'''
.. codeauthor:: Wilfried Mercier - IRAP <[email protected]>
Check whether the path exists or not.
:param value: new value
:type value: :python:`str`
:returns: :python:`True` if the path is ok and :python:`False` otherwise
:rtype: :python:`bool`
'''

path = opath.join(self.path, value) + self.ext
epath = opath.expandvars(path)
if value.upper() != 'NONE' and (not opath.exists(epath) or not opath.isfile(epath)):
return False

return True


@check_type(str)
def set(self, value: str, *args, **kwargs) -> None:
def set(self, value: str, skipCheckPath: bool = False, **kwargs) -> None:
r'''
.. codeauthor:: Wilfried Mercier - IRAP <[email protected]>
Set the current value.
Set the current value if the tests are passed.
:param value: new value. Must be within bounds.
:param value: new value
:type value: :python:`str`
:param skipCheckPath: (**Optional**) whether to skip the check of the existence of the path. It can be useful for paths that have not been created yet.
:type skipCheckPath: :python:`bool`
:raises OSError: if the path does not exist
:raises ValueError: if the test function does not pass
'''

path = opath.join(self.path, value) + self.ext
epath = opath.expandvars(path)
if value.upper() != 'NONE' and not opath.exists(epath) and not opath.isfile(epath):
raise OSError(f'path {path} (expanded as {epath}) does not exist.')
if not skipCheckPath and not self._checkPath(value):
path = opath.join(self.path, value) + self.ext
raise OSError(f'path {path} (expanded as {opath.expandvars(path)}) does not exist.')

if self._testFunc(value):
raise ValueError(self._testMsg)
Expand Down
46 changes: 32 additions & 14 deletions sed.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from distutils.spawn import find_executable
from copy import deepcopy
from typing import List, Any, Optional, Dict, Union
from typing import List, Any, Optional, Dict, Union, Callable
from io import TextIOBase
from abc import ABC, abstractmethod
from textwrap import dedent, indent
Expand Down Expand Up @@ -749,19 +749,19 @@ def __init__(self, ID: Any, properties: dict = {}, **kwargs) -> None:

'STAR_FSCALE' : FloatProperty(3.432e-09, minBound=0),

'STAR_LIB' : PathProperty('LIB_STAR_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin'),
'STAR_LIB' : PathProperty('LIB_STAR_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin', skipCheckPath=True),

'QSO_SED' : PathProperty(opath.join('$LEPHAREDIR', 'sed', 'QSO', 'QSO_MOD.list')),

'QSO_FSCALE' : FloatProperty(1.0, minBound=0),

'QSO_LIB' : PathProperty('LIB_QSO_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin'),
'QSO_LIB' : PathProperty('LIB_QSO_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin', skipCheckPath=True),

'GAL_SED' : PathProperty(opath.join('$LEPHAREDIR', 'sed', 'GAL', 'BC03_CHAB', 'BC03_MOD.list')),

'GAL_FSCALE' : FloatProperty(1.0, minBound=0),

'GAL_LIB' : PathProperty('LIB_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin'),
'GAL_LIB' : PathProperty('LIB_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin', skipCheckPath=True),

'SEL_AGE' : PathProperty(opath.join('$LEPHAREDIR', 'sed', 'GAL', 'BC03_CHAB', 'BC03_AGE.list')),

Expand All @@ -776,17 +776,17 @@ def __init__(self, ID: Any, properties: dict = {}, **kwargs) -> None:

'FILTER_CALIB' : IntProperty(0, minBound=0, maxBound=4),

'FILTER_FILE' : PathProperty('HDF_bc03.filt', path=opath.join('$LEPHAREWORK', 'filt')),
'FILTER_FILE' : PathProperty('HDF_bc03.filt', path=opath.join('$LEPHAREWORK', 'filt'), skipCheckPath=True),

'STAR_LIB_IN' : PathProperty('LIB_STAR_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin'),
'STAR_LIB_IN' : PathProperty('LIB_STAR_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin', skipCheckPath=True),

'STAR_LIB_OUT' : StrProperty('STAR_HDF_bc03'),

'QSO_LIB_IN' : PathProperty('LIB_QSO_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin'),
'QSO_LIB_IN' : PathProperty('LIB_QSO_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin', skipCheckPath=True),

'QSO_LIB_OUT' : StrProperty('QSO_HDF_bc03'),

'GAL_LIB_IN' : PathProperty('LIB_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin'),
'GAL_LIB_IN' : PathProperty('LIB_bc03', path=opath.join('$LEPHAREWORK', 'lib_bin'), ext='.bin', skipCheckPath=True),

'GAL_LIB_OUT' : StrProperty('HDF_bc03'),

Expand Down Expand Up @@ -1181,7 +1181,13 @@ def parameters(self, *args, **kwargs) -> str:
# Run methods #
#############################

def runScript(self, commands: List[str], file: str = '', log: TextIOBase = None, errMsg: str = ''):
def runScript(self, commands: List[str],
file: str = '',
log: TextIOBase = None,
errMsg: str = '',
test: bool = True,
testMsg: str = ''
):
r'''
.. codeauthor:: Wilfried Mercier - IRAP <[email protected]>
Expand All @@ -1196,12 +1202,24 @@ def runScript(self, commands: List[str], file: str = '', log: TextIOBase = None,
:type log: `TextIOBase`_
:param errMsg: (**Optional**) message error to show if the process failed
:type errMsg: :python:`str`
:param test: (**Optional**) a test that must be passed at the end of the command for the program to continue
:type test: :python:`bool`
:param testMsg: (**Optional**) a test that must be passed at the end of the command for the program to continue
:type testMsg: :python:`str`
:raises TypeError: if **commands* if not of type :python:`list`
:raises ValueError: if **testFunc** returns :python:`False`
'''

if not isinstance(commands, list):
raise TypeError(f'commands parameter has type {type(commands)} but it must have type list.')
print('XXX:', file)
return self.startProcess(commands + [file], log=log, errMsg=errMsg)

res = self.startProcess(commands + [file], log=log, errMsg=errMsg)

if not test:
raise ValueError(testMsg)

return res

genQSOModel = partialmethod(runScript, ['$LEPHAREDIR/source/sedtolib', '-t', 'QSO', '-c'], errMsg='QSO models generation failed.')
genGalModel = partialmethod(runScript, ['$LEPHAREDIR/source/sedtolib', '-t', 'Galaxy', '-c'], errMsg='Galaxy models generation failed.')
Expand Down Expand Up @@ -1298,15 +1316,15 @@ def __call__(self, catalogue: LePhareCat,

# QSO magnitudes
if not skipMagQSO:
self.genMagQSO(file=pfile, log=log)
self.genMagQSO(file=pfile, log=log, test=self.prop['QSO_LIB' ]._checkPath(self.prop['QSO_LIB'].value ), testMsg=f'{ERROR} QSO library {opath.expandvars(opath.join(self.prop["QSO_LIB"].path, self.prop["QSO_LIB"].value) + self.prop["QSO_LIB"].ext)} not found.')

# Star magnitudes
if not skipMagStar:
self.genMagStar(file=pfile, log=log)
self.genMagStar(file=pfile, log=log, test=self.prop['STAR_LIB']._checkPath(self.prop['STAR_LIB'].value), testMsg=f'{ERROR} STAR library {opath.expandvars(opath.join(self.prop["STAR_LIB"].path, self.prop["STAR_LIB"].value) + self.prop["STAR_LIB"].ext)} not found.')

# Galaxy magnitudes
if not skipMagGal:
self.genMagGal(file=pfile, log=log)
self.genMagGal(file=pfile, log=log, test=self.prop['GAL_LIB' ]._checkPath(self.prop['GAL_LIB'].value ), testMsg=f'{ERROR} GAL library {opath.expandvars(opath.join(self.prop["GAL_LIB"].path, self.prop["GAL_LIB"].value) + self.prop["GAL_LIB"].ext)} not found.')

###################################
# Run SED fitting #
Expand Down

0 comments on commit 2770a4a

Please sign in to comment.