Skip to content

Commit

Permalink
Expand ~ into the home directory on Linux and MacOS (#6531)
Browse files Browse the repository at this point in the history
  • Loading branch information
hcho3 authored Dec 20, 2020
1 parent cd08215 commit fbb980d
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions R-package/R/xgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ xgb.Booster.handle <- function(params = list(), cachelist = list(),
if (typeof(modelfile) == "character") {
## A filename
handle <- .Call(XGBoosterCreate_R, cachelist)
modelfile <- path.expand(modelfile)
.Call(XGBoosterLoadModel_R, handle, modelfile[1])
class(handle) <- "xgb.Booster.handle"
if (length(params) > 0) {
Expand Down
2 changes: 2 additions & 0 deletions R-package/R/xgb.DMatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ xgb.DMatrix <- function(data, info = list(), missing = NA, silent = FALSE, ...)
if (length(data) > 1)
stop("'data' has class 'character' and length ", length(data),
".\n 'data' accepts either a numeric matrix or a single filename.")
data <- path.expand(data)
handle <- .Call(XGDMatrixCreateFromFile_R, data, as.integer(silent))
} else if (is.matrix(data)) {
handle <- .Call(XGDMatrixCreateFromMat_R, data, missing)
Expand Down Expand Up @@ -65,6 +66,7 @@ xgb.get.DMatrix <- function(data, label = NULL, missing = NA, weight = NULL) {
warning("xgboost: label will be ignored.")
}
if (is.character(data)) {
data <- path.expand(data)
dtrain <- xgb.DMatrix(data[1])
} else if (inherits(data, "xgb.DMatrix")) {
dtrain <- data
Expand Down
1 change: 1 addition & 0 deletions R-package/R/xgb.DMatrix.save.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ xgb.DMatrix.save <- function(dmatrix, fname) {
if (!inherits(dmatrix, "xgb.DMatrix"))
stop("dmatrix must be xgb.DMatrix")

fname <- path.expand(fname)
.Call(XGDMatrixSaveBinary_R, dmatrix, fname[1], 0L)
return(TRUE)
}
1 change: 1 addition & 0 deletions R-package/R/xgb.dump.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ xgb.dump <- function(model, fname = NULL, fmap = "", with_stats=FALSE,
if (is.null(fname)) {
return(model_dump)
} else {
fname <- path.expand(fname)
writeLines(model_dump, fname[1])
return(TRUE)
}
Expand Down
1 change: 1 addition & 0 deletions R-package/R/xgb.save.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ xgb.save <- function(model, fname) {
if (inherits(model, "xgb.DMatrix")) " Use xgb.DMatrix.save to save an xgb.DMatrix object." else "")
}
model <- xgb.Booster.complete(model, saveraw = FALSE)
fname <- path.expand(fname)
.Call(XGBoosterSaveModel_R, model$handle, fname[1])
return(TRUE)
}
18 changes: 11 additions & 7 deletions python-package/xgboost/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,9 @@ def save_binary(self, fname, silent=True):
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):
Expand Down Expand Up @@ -1677,8 +1678,9 @@ def save_model(self, fname):
"""
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")

Expand Down Expand Up @@ -1717,8 +1719,9 @@ def load_model(self, fname):
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))
Expand Down Expand Up @@ -1756,7 +1759,8 @@ def dump_model(self, fout, fmap='', with_stats=False, dump_format="text"):
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
Expand Down Expand Up @@ -1790,7 +1794,7 @@ def get_dump(self, fmap='', with_stats=False, dump_format="text"):
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 == '':
Expand Down Expand Up @@ -1870,7 +1874,7 @@ def get_score(self, fmap='', importance_type='weight'):
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))
Expand Down Expand Up @@ -1963,7 +1967,7 @@ def trees_to_dataframe(self, fmap=''):
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.'))
Expand Down
3 changes: 2 additions & 1 deletion python-package/xgboost/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fbb980d

Please sign in to comment.