Expand ~ into the home directory on Linux and MacOS (#6531)

This commit is contained in:
Philip Hyunsu Cho
2020-12-19 23:35:13 -08:00
committed by GitHub
parent cd0821500c
commit fbb980d9d3
7 changed files with 19 additions and 8 deletions

View File

@@ -645,8 +645,9 @@ class DMatrix: # pylint: disable=too-many-instance-attributes
silent : bool (optional; default: True)
If set, the output is suppressed.
"""
fname = os.fspath(os.path.expanduser(fname))
_check_call(_LIB.XGDMatrixSaveBinary(self.handle,
c_str(os.fspath(fname)),
c_str(fname),
ctypes.c_int(silent)))
def set_label(self, label):
@@ -1677,8 +1678,9 @@ class Booster(object):
"""
if isinstance(fname, (STRING_TYPES, os.PathLike)): # assume file name
fname = os.fspath(os.path.expanduser(fname))
_check_call(_LIB.XGBoosterSaveModel(
self.handle, c_str(os.fspath(fname))))
self.handle, c_str(fname)))
else:
raise TypeError("fname must be a string or os PathLike")
@@ -1717,8 +1719,9 @@ class Booster(object):
if isinstance(fname, (STRING_TYPES, os.PathLike)):
# assume file name, cannot use os.path.exist to check, file can be
# from URL.
fname = os.fspath(os.path.expanduser(fname))
_check_call(_LIB.XGBoosterLoadModel(
self.handle, c_str(os.fspath(fname))))
self.handle, c_str(fname)))
elif isinstance(fname, bytearray):
buf = fname
length = c_bst_ulong(len(buf))
@@ -1756,7 +1759,8 @@ class Booster(object):
Format of model dump file. Can be 'text' or 'json'.
"""
if isinstance(fout, (STRING_TYPES, os.PathLike)):
fout = open(os.fspath(fout), 'w')
fout = os.fspath(os.path.expanduser(fout))
fout = open(fout, 'w')
need_close = True
else:
need_close = False
@@ -1790,7 +1794,7 @@ class Booster(object):
Format of model dump. Can be 'text', 'json' or 'dot'.
"""
fmap = os.fspath(fmap)
fmap = os.fspath(os.path.expanduser(fmap))
length = c_bst_ulong()
sarr = ctypes.POINTER(ctypes.c_char_p)()
if self.feature_names is not None and fmap == '':
@@ -1870,7 +1874,7 @@ class Booster(object):
importance_type: str, default 'weight'
One of the importance types defined above.
"""
fmap = os.fspath(fmap)
fmap = os.fspath(os.path.expanduser(fmap))
if getattr(self, 'booster', None) is not None and self.booster not in {'gbtree', 'dart'}:
raise ValueError('Feature importance is not defined for Booster type {}'
.format(self.booster))
@@ -1963,7 +1967,7 @@ class Booster(object):
The name of feature map file.
"""
# pylint: disable=too-many-locals
fmap = os.fspath(fmap)
fmap = os.fspath(os.path.expanduser(fmap))
if not PANDAS_INSTALLED:
raise Exception(('pandas must be available to use this method.'
'Install pandas before calling again.'))

View File

@@ -486,7 +486,8 @@ def _is_uri(data):
def _from_uri(data, missing, feature_names, feature_types):
_warn_unused_missing(data, missing)
handle = ctypes.c_void_p()
_check_call(_LIB.XGDMatrixCreateFromFile(c_str(os.fspath(data)),
data = os.fspath(os.path.expanduser(data))
_check_call(_LIB.XGDMatrixCreateFromFile(c_str(data),
ctypes.c_int(1),
ctypes.byref(handle)))
return handle, feature_names, feature_types