after_merge
stringlengths 28
79.6k
| before_merge
stringlengths 20
79.6k
| url
stringlengths 38
71
| full_traceback
stringlengths 43
922k
| traceback_type
stringclasses 555
values |
|---|---|---|---|---|
def make_file_dict(filename):
"""Generate the data dictionary for the given RPath
This is a global function so that os.name can be called locally,
thus avoiding network lag and so that we only need to send the
filename over the network, thus avoiding the need to pickle an
(incomplete) rpath object.
"""
def _readlink(filename):
"""FIXME wrapper function to workaround a bug in os.readlink on Windows
not accepting bytes path. This function can be removed once pyinstaller
supports Python 3.8 and a new release can be made.
See https://github.com/pyinstaller/pyinstaller/issues/4311
"""
if os.name == "nt" and not isinstance(filename, str):
# we assume a bytes representation
return os.fsencode(os.readlink(os.fsdecode(filename)))
else:
return os.readlink(filename)
try:
statblock = os.lstat(filename)
except (FileNotFoundError, NotADirectoryError):
# FIXME not sure if this shouldn't trigger a warning but doing it
# generates (too) many messages during the tests
# log.Log("Warning: missing file '%s' couldn't be assessed." % filename, 2)
return {"type": None}
data = {}
mode = statblock[stat.ST_MODE]
if stat.S_ISREG(mode):
type_ = "reg"
elif stat.S_ISDIR(mode):
type_ = "dir"
elif stat.S_ISCHR(mode):
type_ = "dev"
s = statblock.st_rdev
data["devnums"] = ("c",) + (s >> 8, s & 0xFF)
elif stat.S_ISBLK(mode):
type_ = "dev"
s = statblock.st_rdev
data["devnums"] = ("b",) + (s >> 8, s & 0xFF)
elif stat.S_ISFIFO(mode):
type_ = "fifo"
elif stat.S_ISLNK(mode):
type_ = "sym"
# FIXME reverse once Python 3.8 can be used under Windows
# data['linkname'] = os.readlink(filename)
data["linkname"] = _readlink(filename)
elif stat.S_ISSOCK(mode):
type_ = "sock"
else:
raise C.UnknownFileError(filename)
data["type"] = type_
data["size"] = statblock[stat.ST_SIZE]
data["perms"] = stat.S_IMODE(mode)
data["uid"] = statblock[stat.ST_UID]
data["gid"] = statblock[stat.ST_GID]
data["inode"] = statblock[stat.ST_INO]
data["devloc"] = statblock[stat.ST_DEV]
data["nlink"] = statblock[stat.ST_NLINK]
if os.name == "nt":
try:
attribs = win32api.GetFileAttributes(os.fsdecode(filename))
except pywintypes.error as exc:
if exc.args[0] == 32: # file in use
# we could also ignore with: return {'type': None}
# but this approach seems to be better handled
attribs = 0
else:
# we replace the specific Windows exception by a generic
# one also understood by a potential Linux client/server
raise OSError(
None, exc.args[1] + " - " + exc.args[2], filename, exc.args[0]
) from None
if attribs & win32con.FILE_ATTRIBUTE_REPARSE_POINT:
data["type"] = "sym"
data["linkname"] = None
if not (type_ == "sym" or type_ == "dev"):
# mtimes on symlinks and dev files don't work consistently
data["mtime"] = int(statblock[stat.ST_MTIME])
data["atime"] = int(statblock[stat.ST_ATIME])
data["ctime"] = int(statblock[stat.ST_CTIME])
return data
|
def make_file_dict(filename):
"""Generate the data dictionary for the given RPath
This is a global function so that os.name can be called locally,
thus avoiding network lag and so that we only need to send the
filename over the network, thus avoiding the need to pickle an
(incomplete) rpath object.
"""
def _readlink(filename):
"""FIXME wrapper function to workaround a bug in os.readlink on Windows
not accepting bytes path. This function can be removed once pyinstaller
supports Python 3.8 and a new release can be made.
See https://github.com/pyinstaller/pyinstaller/issues/4311
"""
if os.name == "nt" and not isinstance(filename, str):
# we assume a bytes representation
return os.fsencode(os.readlink(os.fsdecode(filename)))
else:
return os.readlink(filename)
try:
statblock = os.lstat(filename)
except (FileNotFoundError, NotADirectoryError):
return {"type": None}
data = {}
mode = statblock[stat.ST_MODE]
if stat.S_ISREG(mode):
type_ = "reg"
elif stat.S_ISDIR(mode):
type_ = "dir"
elif stat.S_ISCHR(mode):
type_ = "dev"
s = statblock.st_rdev
data["devnums"] = ("c",) + (s >> 8, s & 0xFF)
elif stat.S_ISBLK(mode):
type_ = "dev"
s = statblock.st_rdev
data["devnums"] = ("b",) + (s >> 8, s & 0xFF)
elif stat.S_ISFIFO(mode):
type_ = "fifo"
elif stat.S_ISLNK(mode):
type_ = "sym"
# FIXME reverse once Python 3.8 can be used under Windows
# data['linkname'] = os.readlink(filename)
data["linkname"] = _readlink(filename)
elif stat.S_ISSOCK(mode):
type_ = "sock"
else:
raise C.UnknownFileError(filename)
data["type"] = type_
data["size"] = statblock[stat.ST_SIZE]
data["perms"] = stat.S_IMODE(mode)
data["uid"] = statblock[stat.ST_UID]
data["gid"] = statblock[stat.ST_GID]
data["inode"] = statblock[stat.ST_INO]
data["devloc"] = statblock[stat.ST_DEV]
data["nlink"] = statblock[stat.ST_NLINK]
if os.name == "nt":
attribs = win32api.GetFileAttributes(os.fsdecode(filename))
if attribs & win32con.FILE_ATTRIBUTE_REPARSE_POINT:
data["type"] = "sym"
data["linkname"] = None
if not (type_ == "sym" or type_ == "dev"):
# mtimes on symlinks and dev files don't work consistently
data["mtime"] = int(statblock[stat.ST_MTIME])
data["atime"] = int(statblock[stat.ST_ATIME])
data["ctime"] = int(statblock[stat.ST_CTIME])
return data
|
https://github.com/rdiff-backup/rdiff-backup/issues/392
|
2020-06-05 11:41:40.304281 +0530 <CLIENT-8640> Exception '(32, 'GetFileAttributes', 'The process cannot access the file because it is being used by another process.')' raised of class '<class 'pywintypes.error'>':
File "rdiff_backup\robust.py", line 35, in check_common_error
File "rdiff_backup\rpath.py", line 1412, in append
File "rdiff_backup\rpath.py", line 1082, in __init__
File "rdiff_backup\rpath.py", line 1108, in setdata
File "rdiff_backup\rpath.py", line 425, in make_file_dict
2020-06-05 11:41:40.304281 +0530 <CLIENT-8640> Exception '(32, 'GetFileAttributes', 'The process cannot access the file because it is being used by another process.')' raised of class '<class 'pywintypes.error'>':
File "rdiff_backup\Main.py", line 393, in error_check_Main
File "rdiff_backup\Main.py", line 415, in Main
File "rdiff_backup\Main.py", line 351, in take_action
File "rdiff_backup\Main.py", line 440, in Backup
File "rdiff_backup\backup.py", line 39, in Mirror
File "rdiff_backup\backup.py", line 267, in patch
File "rdiff_backup\rorpiter.py", line 202, in FillInIter
File "rdiff_backup\backup.py", line 114, in get_diffs
File "rdiff_backup\backup.py", line 196, in get_sigs
File "rdiff_backup\backup.py", line 361, in __next__
File "rdiff_backup\rorpiter.py", line 109, in Collate2Iters
File "rdiff_backup\rorpiter.py", line 376, in __next__
File "rdiff_backup\selection.py", line 138, in Iterate_fast
File "rdiff_backup\selection.py", line 122, in diryield
File "rdiff_backup\robust.py", line 35, in check_common_error
File "rdiff_backup\rpath.py", line 1412, in append
File "rdiff_backup\rpath.py", line 1082, in __init__
File "rdiff_backup\rpath.py", line 1108, in setdata
File "rdiff_backup\rpath.py", line 425, in make_file_dict
Traceback (most recent call last):
File "rdiff_backup\rorpiter.py", line 105, in Collate2Iters
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "rdiff-backup", line 32, in <module>
File "rdiff_backup\Main.py", line 393, in error_check_Main
File "rdiff_backup\Main.py", line 415, in Main
File "rdiff_backup\Main.py", line 351, in take_action
File "rdiff_backup\Main.py", line 440, in Backup
File "rdiff_backup\backup.py", line 39, in Mirror
File "rdiff_backup\backup.py", line 267, in patch
File "rdiff_backup\rorpiter.py", line 202, in FillInIter
File "rdiff_backup\backup.py", line 114, in get_diffs
File "rdiff_backup\backup.py", line 196, in get_sigs
File "rdiff_backup\backup.py", line 361, in __next__
File "rdiff_backup\rorpiter.py", line 109, in Collate2Iters
File "rdiff_backup\rorpiter.py", line 376, in __next__
File "rdiff_backup\selection.py", line 138, in Iterate_fast
File "rdiff_backup\selection.py", line 122, in diryield
File "rdiff_backup\robust.py", line 35, in check_common_error
File "rdiff_backup\rpath.py", line 1412, in append
File "rdiff_backup\rpath.py", line 1082, in __init__
File "rdiff_backup\rpath.py", line 1108, in setdata
File "rdiff_backup\rpath.py", line 425, in make_file_dict
pywintypes.error: (32, 'GetFileAttributes', 'The process cannot access the file because it is being used by another process.')
[8640] Failed to execute script rdiff-backup
|
pywintypes.error
|
def open_logfile_local(self, rpath):
"""Open logfile locally - should only be run on one connection"""
assert rpath.conn is Globals.local_connection
try:
self.logfp = rpath.open("ab")
except (OSError, IOError) as e:
raise LoggerError("Unable to open logfile %s: %s" % (rpath.path, e))
self.log_file_local = 1
self.logrp = rpath
|
def open_logfile_local(self, rpath):
"""Open logfile locally - should only be run on one connection"""
assert rpath.conn is Globals.local_connection
try:
self.logfp = rpath.open("a")
except (OSError, IOError) as e:
raise LoggerError("Unable to open logfile %s: %s" % (rpath.path, e))
self.log_file_local = 1
self.logrp = rpath
|
https://github.com/rdiff-backup/rdiff-backup/issues/356
|
2020-05-11 21:04:14.547611 +0000 <CLIENT-286> Using rdiff-backup version 2.0.1rc0
2020-05-11 21:04:14.547685 +0000 <CLIENT-286> with cpython /usr/bin/python3 version 3.6.8
2020-05-11 21:04:14.550694 +0000 <CLIENT-286> on Linux-4.19.37-x86_64-with-centos-7.8.2003-Core, fs encoding ascii
2020-05-11 21:04:14.552330 +0000 <CLIENT-286> Using mirror root directory /testcases
2020-05-11 21:04:14.552817 +0000 <CLIENT-286> Unable to import win32security module. Windows ACLs
not supported by filesystem at /testcases/rdiff-backup-data
2020-05-11 21:04:14.553562 +0000 <CLIENT-286> -----------------------------------------------------------------
Detected abilities for rdiff-backup repository (read only) file system:
Access control lists On
Extended attributes On
Windows access control lists Off
Case sensitivity On
Escape DOS devices Off
Escape trailing spaces Off
Mac OS X style resource forks Off
Mac OS X Finder information Off
-----------------------------------------------------------------
2020-05-11 21:04:14.553638 +0000 <CLIENT-286> Making directory /tmp/coucou/rdiff-backup.tmp.0
2020-05-11 21:04:14.553802 +0000 <CLIENT-286> Touching /tmp/coucou/rdiff-backup.tmp.0/5-_ a.snapshot.gz
2020-05-11 21:04:14.553948 +0000 <CLIENT-286> Deleting /tmp/coucou/rdiff-backup.tmp.0/5-_ a.snapshot.gz
2020-05-11 21:04:14.554872 +0000 <CLIENT-286> Exception ''ascii' codec can't encode characters in position 91-93: ordinal not in range(128)' raised of class '<class 'UnicodeEncodeError'>':
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 393, in error_check_Main
Main(arglist)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 415, in Main
take_action(rps)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 371, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 697, in Restore
dest_rp.conn.fs_abilities.restore_set_globals(dest_rp)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 1072, in restore_set_globals
dest_fsa = FSAbilities('restore target').init_readwrite(rpout)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 157, in init_readwrite
self.set_extended_filenames(subdir)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 241, in set_extended_filenames
ext_rp.touch()
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/rpath.py", line 1248, in touch
log.Log("Touching %s" % self.get_safepath(), 7)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 150, in __call__
self.log_to_term(message, verbosity)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 173, in log_to_term
termfp.write(tmpstr)
Traceback (most recent call last):
File "/usr/local/bin/rdiff-backup", line 32, in <module>
rdiff_backup.Main.error_check_Main(sys.argv[1:])
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 393, in error_check_Main
Main(arglist)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 415, in Main
take_action(rps)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 371, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 697, in Restore
dest_rp.conn.fs_abilities.restore_set_globals(dest_rp)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 1072, in restore_set_globals
dest_fsa = FSAbilities('restore target').init_readwrite(rpout)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 157, in init_readwrite
self.set_extended_filenames(subdir)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 241, in set_extended_filenames
ext_rp.touch()
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/rpath.py", line 1248, in touch
log.Log("Touching %s" % self.get_safepath(), 7)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 150, in __call__
self.log_to_term(message, verbosity)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 173, in log_to_term
termfp.write(tmpstr)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 91-93: ordinal not in range(128)
|
UnicodeEncodeError
|
def log_to_file(self, message):
"""Write the message to the log file, if possible"""
if self.log_file_open:
if self.log_file_local:
tmpstr = self.format(message, self.verbosity)
if type(tmpstr) == str: # transform string in bytes
tmpstr = tmpstr.encode("utf-8", "backslashreplace")
self.logfp.write(tmpstr)
self.logfp.flush()
else:
self.log_file_conn.log.Log.log_to_file(message)
|
def log_to_file(self, message):
"""Write the message to the log file, if possible"""
if self.log_file_open:
if self.log_file_local:
tmpstr = self.format(message, self.verbosity)
if type(tmpstr) != str: # transform bytes into string
tmpstr = str(tmpstr, "utf-8")
self.logfp.write(tmpstr)
self.logfp.flush()
else:
self.log_file_conn.log.Log.log_to_file(message)
|
https://github.com/rdiff-backup/rdiff-backup/issues/356
|
2020-05-11 21:04:14.547611 +0000 <CLIENT-286> Using rdiff-backup version 2.0.1rc0
2020-05-11 21:04:14.547685 +0000 <CLIENT-286> with cpython /usr/bin/python3 version 3.6.8
2020-05-11 21:04:14.550694 +0000 <CLIENT-286> on Linux-4.19.37-x86_64-with-centos-7.8.2003-Core, fs encoding ascii
2020-05-11 21:04:14.552330 +0000 <CLIENT-286> Using mirror root directory /testcases
2020-05-11 21:04:14.552817 +0000 <CLIENT-286> Unable to import win32security module. Windows ACLs
not supported by filesystem at /testcases/rdiff-backup-data
2020-05-11 21:04:14.553562 +0000 <CLIENT-286> -----------------------------------------------------------------
Detected abilities for rdiff-backup repository (read only) file system:
Access control lists On
Extended attributes On
Windows access control lists Off
Case sensitivity On
Escape DOS devices Off
Escape trailing spaces Off
Mac OS X style resource forks Off
Mac OS X Finder information Off
-----------------------------------------------------------------
2020-05-11 21:04:14.553638 +0000 <CLIENT-286> Making directory /tmp/coucou/rdiff-backup.tmp.0
2020-05-11 21:04:14.553802 +0000 <CLIENT-286> Touching /tmp/coucou/rdiff-backup.tmp.0/5-_ a.snapshot.gz
2020-05-11 21:04:14.553948 +0000 <CLIENT-286> Deleting /tmp/coucou/rdiff-backup.tmp.0/5-_ a.snapshot.gz
2020-05-11 21:04:14.554872 +0000 <CLIENT-286> Exception ''ascii' codec can't encode characters in position 91-93: ordinal not in range(128)' raised of class '<class 'UnicodeEncodeError'>':
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 393, in error_check_Main
Main(arglist)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 415, in Main
take_action(rps)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 371, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 697, in Restore
dest_rp.conn.fs_abilities.restore_set_globals(dest_rp)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 1072, in restore_set_globals
dest_fsa = FSAbilities('restore target').init_readwrite(rpout)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 157, in init_readwrite
self.set_extended_filenames(subdir)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 241, in set_extended_filenames
ext_rp.touch()
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/rpath.py", line 1248, in touch
log.Log("Touching %s" % self.get_safepath(), 7)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 150, in __call__
self.log_to_term(message, verbosity)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 173, in log_to_term
termfp.write(tmpstr)
Traceback (most recent call last):
File "/usr/local/bin/rdiff-backup", line 32, in <module>
rdiff_backup.Main.error_check_Main(sys.argv[1:])
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 393, in error_check_Main
Main(arglist)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 415, in Main
take_action(rps)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 371, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 697, in Restore
dest_rp.conn.fs_abilities.restore_set_globals(dest_rp)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 1072, in restore_set_globals
dest_fsa = FSAbilities('restore target').init_readwrite(rpout)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 157, in init_readwrite
self.set_extended_filenames(subdir)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 241, in set_extended_filenames
ext_rp.touch()
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/rpath.py", line 1248, in touch
log.Log("Touching %s" % self.get_safepath(), 7)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 150, in __call__
self.log_to_term(message, verbosity)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 173, in log_to_term
termfp.write(tmpstr)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 91-93: ordinal not in range(128)
|
UnicodeEncodeError
|
def log_to_term(self, message, verbosity):
"""Write message to stdout/stderr"""
if verbosity <= 2 or Globals.server:
termfp = sys.stderr.buffer
else:
termfp = sys.stdout.buffer
tmpstr = self.format(message, self.term_verbosity)
if type(tmpstr) == str: # transform string in bytes
tmpstr = tmpstr.encode(sys.stdout.encoding, "backslashreplace")
termfp.write(tmpstr)
|
def log_to_term(self, message, verbosity):
"""Write message to stdout/stderr"""
if verbosity <= 2 or Globals.server:
termfp = sys.stderr
else:
termfp = sys.stdout
tmpstr = self.format(message, self.term_verbosity)
if type(tmpstr) != str: # transform bytes in string
tmpstr = str(tmpstr, "utf-8")
termfp.write(tmpstr)
|
https://github.com/rdiff-backup/rdiff-backup/issues/356
|
2020-05-11 21:04:14.547611 +0000 <CLIENT-286> Using rdiff-backup version 2.0.1rc0
2020-05-11 21:04:14.547685 +0000 <CLIENT-286> with cpython /usr/bin/python3 version 3.6.8
2020-05-11 21:04:14.550694 +0000 <CLIENT-286> on Linux-4.19.37-x86_64-with-centos-7.8.2003-Core, fs encoding ascii
2020-05-11 21:04:14.552330 +0000 <CLIENT-286> Using mirror root directory /testcases
2020-05-11 21:04:14.552817 +0000 <CLIENT-286> Unable to import win32security module. Windows ACLs
not supported by filesystem at /testcases/rdiff-backup-data
2020-05-11 21:04:14.553562 +0000 <CLIENT-286> -----------------------------------------------------------------
Detected abilities for rdiff-backup repository (read only) file system:
Access control lists On
Extended attributes On
Windows access control lists Off
Case sensitivity On
Escape DOS devices Off
Escape trailing spaces Off
Mac OS X style resource forks Off
Mac OS X Finder information Off
-----------------------------------------------------------------
2020-05-11 21:04:14.553638 +0000 <CLIENT-286> Making directory /tmp/coucou/rdiff-backup.tmp.0
2020-05-11 21:04:14.553802 +0000 <CLIENT-286> Touching /tmp/coucou/rdiff-backup.tmp.0/5-_ a.snapshot.gz
2020-05-11 21:04:14.553948 +0000 <CLIENT-286> Deleting /tmp/coucou/rdiff-backup.tmp.0/5-_ a.snapshot.gz
2020-05-11 21:04:14.554872 +0000 <CLIENT-286> Exception ''ascii' codec can't encode characters in position 91-93: ordinal not in range(128)' raised of class '<class 'UnicodeEncodeError'>':
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 393, in error_check_Main
Main(arglist)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 415, in Main
take_action(rps)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 371, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 697, in Restore
dest_rp.conn.fs_abilities.restore_set_globals(dest_rp)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 1072, in restore_set_globals
dest_fsa = FSAbilities('restore target').init_readwrite(rpout)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 157, in init_readwrite
self.set_extended_filenames(subdir)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 241, in set_extended_filenames
ext_rp.touch()
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/rpath.py", line 1248, in touch
log.Log("Touching %s" % self.get_safepath(), 7)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 150, in __call__
self.log_to_term(message, verbosity)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 173, in log_to_term
termfp.write(tmpstr)
Traceback (most recent call last):
File "/usr/local/bin/rdiff-backup", line 32, in <module>
rdiff_backup.Main.error_check_Main(sys.argv[1:])
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 393, in error_check_Main
Main(arglist)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 415, in Main
take_action(rps)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 371, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/Main.py", line 697, in Restore
dest_rp.conn.fs_abilities.restore_set_globals(dest_rp)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 1072, in restore_set_globals
dest_fsa = FSAbilities('restore target').init_readwrite(rpout)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 157, in init_readwrite
self.set_extended_filenames(subdir)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/fs_abilities.py", line 241, in set_extended_filenames
ext_rp.touch()
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/rpath.py", line 1248, in touch
log.Log("Touching %s" % self.get_safepath(), 7)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 150, in __call__
self.log_to_term(message, verbosity)
File "/usr/local/lib64/python3.6/site-packages/rdiff_backup/log.py", line 173, in log_to_term
termfp.write(tmpstr)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 91-93: ordinal not in range(128)
|
UnicodeEncodeError
|
def parse_cmdlineoptions(arglist): # noqa: C901
"""Parse argument list and set global preferences"""
global args, action, create_full_path, force, restore_timestr, remote_cmd
global remote_schema, remove_older_than_string
global user_mapping_filename, group_mapping_filename, preserve_numerical_ids
def sel_fl(filename):
"""Helper function for including/excluding filelists below"""
try:
return open(filename, "rb") # files match paths hence bytes/bin
except IOError:
Log.FatalError("Error opening file %s" % filename)
def normalize_path(path):
"""Used below to normalize the security paths before setting"""
return rpath.RPath(Globals.local_connection, path).normalize().path
try:
optlist, args = getopt.getopt(
arglist,
"blr:sv:V",
[
"allow-duplicate-timestamps",
"backup-mode",
"calculate-average",
"carbonfile",
"check-destination-dir",
"compare",
"compare-at-time=",
"compare-hash",
"compare-hash-at-time=",
"compare-full",
"compare-full-at-time=",
"create-full-path",
"current-time=",
"exclude=",
"exclude-device-files",
"exclude-fifos",
"exclude-filelist=",
"exclude-symbolic-links",
"exclude-sockets",
"exclude-filelist-stdin",
"exclude-globbing-filelist=",
"exclude-globbing-filelist-stdin",
"exclude-mirror=",
"exclude-other-filesystems",
"exclude-regexp=",
"exclude-if-present=",
"exclude-special-files",
"force",
"group-mapping-file=",
"include=",
"include-filelist=",
"include-filelist-stdin",
"include-globbing-filelist=",
"include-globbing-filelist-stdin",
"include-regexp=",
"include-special-files",
"include-symbolic-links",
"list-at-time=",
"list-changed-since=",
"list-increments",
"list-increment-sizes",
"never-drop-acls",
"max-file-size=",
"min-file-size=",
"no-acls",
"no-carbonfile",
"no-compare-inode",
"no-compression",
"no-compression-regexp=",
"no-eas",
"no-file-statistics",
"no-hard-links",
"null-separator",
"override-chars-to-quote=",
"parsable-output",
"preserve-numerical-ids",
"print-statistics",
"remote-cmd=",
"remote-schema=",
"remote-tempdir=",
"remove-older-than=",
"restore-as-of=",
"restrict=",
"restrict-read-only=",
"restrict-update-only=",
"server",
"ssh-no-compression",
"tempdir=",
"terminal-verbosity=",
"test-server",
"use-compatible-timestamps",
"user-mapping-file=",
"verbosity=",
"verify",
"verify-at-time=",
"version",
"no-fsync",
],
)
except getopt.error as e:
commandline_error("Bad commandline options: " + str(e))
for opt, arg in optlist:
if opt == "-b" or opt == "--backup-mode":
action = "backup"
elif opt == "--calculate-average":
action = "calculate-average"
elif opt == "--carbonfile":
Globals.set("carbonfile_active", 1)
elif opt == "--check-destination-dir":
action = "check-destination-dir"
elif opt in (
"--compare",
"--compare-at-time",
"--compare-hash",
"--compare-hash-at-time",
"--compare-full",
"--compare-full-at-time",
):
if opt[-8:] == "-at-time":
restore_timestr, opt = arg, opt[:-8]
else:
restore_timestr = "now"
action = opt[2:]
elif opt == "--create-full-path":
create_full_path = 1
elif opt == "--current-time":
Globals.set_integer("current_time", arg)
elif (
opt == "--exclude"
or opt == "--exclude-device-files"
or opt == "--exclude-fifos"
or opt == "--exclude-other-filesystems"
or opt == "--exclude-regexp"
or opt == "--exclude-if-present"
or opt == "--exclude-special-files"
or opt == "--exclude-sockets"
or opt == "--exclude-symbolic-links"
):
select_opts.append((opt, arg))
elif opt == "--exclude-filelist":
select_opts.append((opt, arg))
select_files.append(sel_fl(arg))
elif opt == "--exclude-filelist-stdin":
select_opts.append(("--exclude-filelist", "standard input"))
select_files.append(sys.stdin.buffer)
elif opt == "--exclude-globbing-filelist":
select_opts.append((opt, arg))
select_files.append(sel_fl(arg))
elif opt == "--exclude-globbing-filelist-stdin":
select_opts.append(("--exclude-globbing-filelist", "standard input"))
select_files.append(sys.stdin.buffer)
elif opt == "--force":
force = 1
elif opt == "--group-mapping-file":
group_mapping_filename = os.fsencode(arg)
elif (
opt == "--include"
or opt == "--include-special-files"
or opt == "--include-symbolic-links"
):
select_opts.append((opt, arg))
elif opt == "--include-filelist":
select_opts.append((opt, arg))
select_files.append(sel_fl(arg))
elif opt == "--include-filelist-stdin":
select_opts.append(("--include-filelist", "standard input"))
select_files.append(sys.stdin.buffer)
elif opt == "--include-globbing-filelist":
select_opts.append((opt, arg))
select_files.append(sel_fl(arg))
elif opt == "--include-globbing-filelist-stdin":
select_opts.append(("--include-globbing-filelist", "standard input"))
select_files.append(sys.stdin.buffer)
elif opt == "--include-regexp":
select_opts.append((opt, arg))
elif opt == "--list-at-time":
restore_timestr, action = arg, "list-at-time"
elif opt == "--list-changed-since":
restore_timestr, action = arg, "list-changed-since"
elif opt == "-l" or opt == "--list-increments":
action = "list-increments"
elif opt == "--list-increment-sizes":
action = "list-increment-sizes"
elif opt == "--max-file-size":
select_opts.append((opt, arg))
elif opt == "--min-file-size":
select_opts.append((opt, arg))
elif opt == "--never-drop-acls":
Globals.set("never_drop_acls", 1)
elif opt == "--no-acls":
Globals.set("acls_active", 0)
Globals.set("win_acls_active", 0)
elif opt == "--no-carbonfile":
Globals.set("carbonfile_active", 0)
elif opt == "--no-compare-inode":
Globals.set("compare_inode", 0)
elif opt == "--no-compression":
Globals.set("compression", None)
elif opt == "--no-compression-regexp":
Globals.set("no_compression_regexp_string", os.fsencode(arg))
elif opt == "--no-eas":
Globals.set("eas_active", 0)
elif opt == "--no-file-statistics":
Globals.set("file_statistics", 0)
elif opt == "--no-hard-links":
Globals.set("preserve_hardlinks", 0)
elif opt == "--null-separator":
Globals.set("null_separator", 1)
elif opt == "--override-chars-to-quote":
Globals.set("chars_to_quote", os.fsencode(arg))
elif opt == "--parsable-output":
Globals.set("parsable_output", 1)
elif opt == "--preserve-numerical-ids":
preserve_numerical_ids = 1
elif opt == "--print-statistics":
Globals.set("print_statistics", 1)
elif opt == "-r" or opt == "--restore-as-of":
restore_timestr, action = arg, "restore-as-of"
elif opt == "--remote-cmd":
remote_cmd = os.fsencode(arg)
elif opt == "--remote-schema":
remote_schema = os.fsencode(arg)
elif opt == "--remote-tempdir":
Globals.remote_tempdir = os.fsencode(arg)
elif opt == "--remove-older-than":
remove_older_than_string = arg
action = "remove-older-than"
elif opt == "--no-resource-forks":
Globals.set("resource_forks_active", 0)
elif opt == "--restrict":
Globals.restrict_path = normalize_path(arg)
elif opt == "--restrict-read-only":
Globals.security_level = "read-only"
Globals.restrict_path = normalize_path(arg)
elif opt == "--restrict-update-only":
Globals.security_level = "update-only"
Globals.restrict_path = normalize_path(arg)
elif opt == "-s" or opt == "--server":
action = "server"
Globals.server = 1
elif opt == "--ssh-no-compression":
Globals.set("ssh_compression", None)
elif opt == "--tempdir":
if not os.path.isdir(arg):
Log.FatalError("Temporary directory '%s' doesn't exist." % arg)
tempfile.tempdir = os.fsencode(arg)
elif opt == "--terminal-verbosity":
Log.setterm_verbosity(arg)
elif opt == "--test-server":
action = "test-server"
elif opt == "--use-compatible-timestamps":
Globals.set("use_compatible_timestamps", 1)
elif opt == "--allow-duplicate-timestamps":
Globals.set("allow_duplicate_timestamps", True)
elif opt == "--user-mapping-file":
user_mapping_filename = os.fsencode(arg)
elif opt == "-v" or opt == "--verbosity":
Log.setverbosity(arg)
elif opt == "--verify":
action, restore_timestr = "verify", "now"
elif opt == "--verify-at-time":
action, restore_timestr = "verify", arg
elif opt == "-V" or opt == "--version":
print("rdiff-backup " + Globals.version)
sys.exit(0)
elif opt == "--no-fsync":
Globals.do_fsync = False
else:
Log.FatalError("Unknown option %s" % opt)
Log("Using rdiff-backup version %s" % (Globals.version), 4)
Log(
"\twith %s %s version %s"
% (sys.implementation.name, sys.executable, platform.python_version()),
4,
)
Log(
"\ton %s, fs encoding %s" % (platform.platform(), sys.getfilesystemencoding()),
4,
)
|
def parse_cmdlineoptions(arglist): # noqa: C901
"""Parse argument list and set global preferences"""
global args, action, create_full_path, force, restore_timestr, remote_cmd
global remote_schema, remove_older_than_string
global user_mapping_filename, group_mapping_filename, preserve_numerical_ids
def sel_fl(filename):
"""Helper function for including/excluding filelists below"""
try:
return open(filename, "rb") # files match paths hence bytes/bin
except IOError:
Log.FatalError("Error opening file %s" % filename)
def normalize_path(path):
"""Used below to normalize the security paths before setting"""
return rpath.RPath(Globals.local_connection, path).normalize().path
try:
optlist, args = getopt.getopt(
arglist,
"blr:sv:V",
[
"allow-duplicate-timestamps",
"backup-mode",
"calculate-average",
"carbonfile",
"check-destination-dir",
"compare",
"compare-at-time=",
"compare-hash",
"compare-hash-at-time=",
"compare-full",
"compare-full-at-time=",
"create-full-path",
"current-time=",
"exclude=",
"exclude-device-files",
"exclude-fifos",
"exclude-filelist=",
"exclude-symbolic-links",
"exclude-sockets",
"exclude-filelist-stdin",
"exclude-globbing-filelist=",
"exclude-globbing-filelist-stdin",
"exclude-mirror=",
"exclude-other-filesystems",
"exclude-regexp=",
"exclude-if-present=",
"exclude-special-files",
"force",
"group-mapping-file=",
"include=",
"include-filelist=",
"include-filelist-stdin",
"include-globbing-filelist=",
"include-globbing-filelist-stdin",
"include-regexp=",
"include-special-files",
"include-symbolic-links",
"list-at-time=",
"list-changed-since=",
"list-increments",
"list-increment-sizes",
"never-drop-acls",
"max-file-size=",
"min-file-size=",
"no-acls",
"no-carbonfile",
"no-compare-inode",
"no-compression",
"no-compression-regexp=",
"no-eas",
"no-file-statistics",
"no-hard-links",
"null-separator",
"override-chars-to-quote=",
"parsable-output",
"preserve-numerical-ids",
"print-statistics",
"remote-cmd=",
"remote-schema=",
"remote-tempdir=",
"remove-older-than=",
"restore-as-of=",
"restrict=",
"restrict-read-only=",
"restrict-update-only=",
"server",
"ssh-no-compression",
"tempdir=",
"terminal-verbosity=",
"test-server",
"use-compatible-timestamps",
"user-mapping-file=",
"verbosity=",
"verify",
"verify-at-time=",
"version",
"no-fsync",
],
)
except getopt.error as e:
commandline_error("Bad commandline options: " + str(e))
for opt, arg in optlist:
if opt == "-b" or opt == "--backup-mode":
action = "backup"
elif opt == "--calculate-average":
action = "calculate-average"
elif opt == "--carbonfile":
Globals.set("carbonfile_active", 1)
elif opt == "--check-destination-dir":
action = "check-destination-dir"
elif opt in (
"--compare",
"--compare-at-time",
"--compare-hash",
"--compare-hash-at-time",
"--compare-full",
"--compare-full-at-time",
):
if opt[-8:] == "-at-time":
restore_timestr, opt = arg, opt[:-8]
else:
restore_timestr = "now"
action = opt[2:]
elif opt == "--create-full-path":
create_full_path = 1
elif opt == "--current-time":
Globals.set_integer("current_time", arg)
elif (
opt == "--exclude"
or opt == "--exclude-device-files"
or opt == "--exclude-fifos"
or opt == "--exclude-other-filesystems"
or opt == "--exclude-regexp"
or opt == "--exclude-if-present"
or opt == "--exclude-special-files"
or opt == "--exclude-sockets"
or opt == "--exclude-symbolic-links"
):
select_opts.append((opt, arg))
elif opt == "--exclude-filelist":
select_opts.append((opt, arg))
select_files.append(sel_fl(arg))
elif opt == "--exclude-filelist-stdin":
select_opts.append(("--exclude-filelist", "standard input"))
select_files.append(sys.stdin.buffer)
elif opt == "--exclude-globbing-filelist":
select_opts.append((opt, arg))
select_files.append(sel_fl(arg))
elif opt == "--exclude-globbing-filelist-stdin":
select_opts.append(("--exclude-globbing-filelist", "standard input"))
select_files.append(sys.stdin.buffer)
elif opt == "--force":
force = 1
elif opt == "--group-mapping-file":
group_mapping_filename = os.fsencode(arg)
elif (
opt == "--include"
or opt == "--include-special-files"
or opt == "--include-symbolic-links"
):
select_opts.append((opt, arg))
elif opt == "--include-filelist":
select_opts.append((opt, arg))
select_files.append(sel_fl(arg))
elif opt == "--include-filelist-stdin":
select_opts.append(("--include-filelist", "standard input"))
select_files.append(sys.stdin.buffer)
elif opt == "--include-globbing-filelist":
select_opts.append((opt, arg))
select_files.append(sel_fl(arg))
elif opt == "--include-globbing-filelist-stdin":
select_opts.append(("--include-globbing-filelist", "standard input"))
select_files.append(sys.stdin.buffer)
elif opt == "--include-regexp":
select_opts.append((opt, arg))
elif opt == "--list-at-time":
restore_timestr, action = arg, "list-at-time"
elif opt == "--list-changed-since":
restore_timestr, action = arg, "list-changed-since"
elif opt == "-l" or opt == "--list-increments":
action = "list-increments"
elif opt == "--list-increment-sizes":
action = "list-increment-sizes"
elif opt == "--max-file-size":
select_opts.append((opt, arg))
elif opt == "--min-file-size":
select_opts.append((opt, arg))
elif opt == "--never-drop-acls":
Globals.set("never_drop_acls", 1)
elif opt == "--no-acls":
Globals.set("acls_active", 0)
Globals.set("win_acls_active", 0)
elif opt == "--no-carbonfile":
Globals.set("carbonfile_active", 0)
elif opt == "--no-compare-inode":
Globals.set("compare_inode", 0)
elif opt == "--no-compression":
Globals.set("compression", None)
elif opt == "--no-compression-regexp":
Globals.set("no_compression_regexp_string", os.fsencode(arg))
elif opt == "--no-eas":
Globals.set("eas_active", 0)
elif opt == "--no-file-statistics":
Globals.set("file_statistics", 0)
elif opt == "--no-hard-links":
Globals.set("preserve_hardlinks", 0)
elif opt == "--null-separator":
Globals.set("null_separator", 1)
elif opt == "--override-chars-to-quote":
Globals.set("chars_to_quote", os.fsencode(arg))
elif opt == "--parsable-output":
Globals.set("parsable_output", 1)
elif opt == "--preserve-numerical-ids":
preserve_numerical_ids = 1
elif opt == "--print-statistics":
Globals.set("print_statistics", 1)
elif opt == "-r" or opt == "--restore-as-of":
restore_timestr, action = arg, "restore-as-of"
elif opt == "--remote-cmd":
remote_cmd = os.fsencode(arg)
elif opt == "--remote-schema":
remote_schema = os.fsencode(arg)
elif opt == "--remote-tempdir":
Globals.remote_tempdir = os.fsencode(arg)
elif opt == "--remove-older-than":
remove_older_than_string = arg
action = "remove-older-than"
elif opt == "--no-resource-forks":
Globals.set("resource_forks_active", 0)
elif opt == "--restrict":
Globals.restrict_path = normalize_path(arg)
elif opt == "--restrict-read-only":
Globals.security_level = "read-only"
Globals.restrict_path = normalize_path(arg)
elif opt == "--restrict-update-only":
Globals.security_level = "update-only"
Globals.restrict_path = normalize_path(arg)
elif opt == "-s" or opt == "--server":
action = "server"
Globals.server = 1
elif opt == "--ssh-no-compression":
Globals.set("ssh_compression", None)
elif opt == "--tempdir":
tempfile.tempdir = os.fsencode(arg)
elif opt == "--terminal-verbosity":
Log.setterm_verbosity(arg)
elif opt == "--test-server":
action = "test-server"
elif opt == "--use-compatible-timestamps":
Globals.set("use_compatible_timestamps", 1)
elif opt == "--allow-duplicate-timestamps":
Globals.set("allow_duplicate_timestamps", True)
elif opt == "--user-mapping-file":
user_mapping_filename = os.fsencode(arg)
elif opt == "-v" or opt == "--verbosity":
Log.setverbosity(arg)
elif opt == "--verify":
action, restore_timestr = "verify", "now"
elif opt == "--verify-at-time":
action, restore_timestr = "verify", arg
elif opt == "-V" or opt == "--version":
print("rdiff-backup " + Globals.version)
sys.exit(0)
elif opt == "--no-fsync":
Globals.do_fsync = False
else:
Log.FatalError("Unknown option %s" % opt)
Log("Using rdiff-backup version %s" % (Globals.version), 4)
Log(
"\twith %s %s version %s"
% (sys.implementation.name, sys.executable, platform.python_version()),
4,
)
Log(
"\ton %s, fs encoding %s" % (platform.platform(), sys.getfilesystemencoding()),
4,
)
|
https://github.com/rdiff-backup/rdiff-backup/issues/367
|
Warning: Access Control List file not found
Warning: Extended Attributes file not found
Exception 'Can't mix strings and bytes in path components' raised of class '<class 'TypeError'>':
File "/usr/lib64/python3.8/site-packages/rdiff_backup/robust.py", line 35, in check_common_error
return function(*args)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/restore.py", line 528, in get_fp
current_fp = self.get_first_fp()
File "/usr/lib64/python3.8/site-packages/rdiff_backup/restore.py", line 568, in get_first_fp
current_fp = tempfile.TemporaryFile()
File "/usr/lib64/python3.8/tempfile.py", line 614, in TemporaryFile
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
File "/usr/lib64/python3.8/tempfile.py", line 247, in _mkstemp_inner
file = _os.path.join(dir, pre + name + suf)
File "/usr/lib64/python3.8/posixpath.py", line 90, in join
genericpath._check_arg_types('join', a, *p)
File "/usr/lib64/python3.8/genericpath.py", line 155, in _check_arg_types
raise TypeError("Can't mix strings and bytes in path components") from None
Exception 'Can't mix strings and bytes in path components' raised of class '<class 'TypeError'>':
File "/usr/lib64/python3.8/site-packages/rdiff_backup/Main.py", line 393, in error_check_Main
Main(arglist)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/Main.py", line 415, in Main
take_action(rps)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/Main.py", line 355, in take_action
CheckDest(rps[0])
File "/usr/lib64/python3.8/site-packages/rdiff_backup/Main.py", line 1073, in CheckDest
dest_rp.conn.regress.Regress(dest_rp)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/regress.py", line 71, in Regress
ITR(rf.index, rf)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/rorpiter.py", line 313, in __call__
last_branch.fast_process(*args)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/regress.py", line 295, in fast_process
self.restore_orig_regfile(rf)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/regress.py", line 326, in restore_orig_regfile
rf.mirror_rp.write_from_fileobj(rf.get_restore_fp())
File "/usr/lib64/python3.8/site-packages/rdiff_backup/restore.py", line 558, in get_restore_fp
return robust.check_common_error(error_handler, get_fp)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/robust.py", line 35, in check_common_error
return function(*args)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/restore.py", line 528, in get_fp
current_fp = self.get_first_fp()
File "/usr/lib64/python3.8/site-packages/rdiff_backup/restore.py", line 568, in get_first_fp
current_fp = tempfile.TemporaryFile()
File "/usr/lib64/python3.8/tempfile.py", line 614, in TemporaryFile
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
File "/usr/lib64/python3.8/tempfile.py", line 247, in _mkstemp_inner
file = _os.path.join(dir, pre + name + suf)
File "/usr/lib64/python3.8/posixpath.py", line 90, in join
genericpath._check_arg_types('join', a, *p)
File "/usr/lib64/python3.8/genericpath.py", line 155, in _check_arg_types
raise TypeError("Can't mix strings and bytes in path components") from None
Traceback (most recent call last):
File "/usr/bin/rdiff-backup", line 32, in <module>
rdiff_backup.Main.error_check_Main(sys.argv[1:])
File "/usr/lib64/python3.8/site-packages/rdiff_backup/Main.py", line 393, in error_check_Main
Main(arglist)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/Main.py", line 415, in Main
take_action(rps)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/Main.py", line 355, in take_action
CheckDest(rps[0])
File "/usr/lib64/python3.8/site-packages/rdiff_backup/Main.py", line 1073, in CheckDest
dest_rp.conn.regress.Regress(dest_rp)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/regress.py", line 71, in Regress
ITR(rf.index, rf)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/rorpiter.py", line 313, in __call__
last_branch.fast_process(*args)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/regress.py", line 295, in fast_process
self.restore_orig_regfile(rf)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/regress.py", line 326, in restore_orig_regfile
rf.mirror_rp.write_from_fileobj(rf.get_restore_fp())
File "/usr/lib64/python3.8/site-packages/rdiff_backup/restore.py", line 558, in get_restore_fp
return robust.check_common_error(error_handler, get_fp)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/robust.py", line 35, in check_common_error
return function(*args)
File "/usr/lib64/python3.8/site-packages/rdiff_backup/restore.py", line 528, in get_fp
current_fp = self.get_first_fp()
File "/usr/lib64/python3.8/site-packages/rdiff_backup/restore.py", line 568, in get_first_fp
current_fp = tempfile.TemporaryFile()
File "/usr/lib64/python3.8/tempfile.py", line 614, in TemporaryFile
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)
File "/usr/lib64/python3.8/tempfile.py", line 247, in _mkstemp_inner
file = _os.path.join(dir, pre + name + suf)
File "/usr/lib64/python3.8/posixpath.py", line 90, in join
genericpath._check_arg_types('join', a, *p)
File "/usr/lib64/python3.8/genericpath.py", line 155, in _check_arg_types
raise TypeError("Can't mix strings and bytes in path components") from None
TypeError: Can't mix strings and bytes in path components
|
TypeError
|
def catch_error(exc):
"""Return true if exception exc should be caught"""
for exception_class in (
rpath.SkipFileException,
rpath.RPathException,
librsync.librsyncError,
C.UnknownFileTypeError,
zlib.error,
):
if isinstance(exc, exception_class):
return 1
if (
isinstance(exc, EnvironmentError)
# the invalid mode shows up in backups of /proc for some reason
and (
"invalid mode: rb" in str(exc)
or "Not a gzipped file" in str(exc)
or exc.errno in _robust_errno_list
)
):
return 1
return 0
|
def catch_error(exc):
"""Return true if exception exc should be caught"""
for exception_class in (
rpath.SkipFileException,
rpath.RPathException,
librsync.librsyncError,
C.UnknownFileTypeError,
zlib.error,
):
if isinstance(exc, exception_class):
return 1
if (
isinstance(exc, EnvironmentError)
# the invalid mode shows up in backups of /proc for some reason
and (
"invalid mode: rb" in str(exc)
or "Not a gzipped file" in str(exc)
or exc.errno
in (
errno.EPERM,
errno.ENOENT,
errno.EACCES,
errno.EBUSY,
errno.EEXIST,
errno.ENOTDIR,
errno.EILSEQ,
errno.ENAMETOOLONG,
errno.EINTR,
errno.ESTALE,
errno.ENOTEMPTY,
errno.EIO,
errno.ETXTBSY,
errno.ESRCH,
errno.EINVAL,
errno.EDEADLOCK,
errno.EDEADLK,
errno.EOPNOTSUPP,
errno.ETIMEDOUT,
)
)
):
return 1
return 0
|
https://github.com/rdiff-backup/rdiff-backup/issues/366
|
$ python3
Python 3.7.7 (default, Mar 10 2020, 15:43:27)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import errno
errno.EDEADLOCK
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'errno' has no attribute 'EDEADLOCK'
errno.EDEADLK
11
|
AttributeError
|
def isincfile(self):
"""Return true if path indicates increment, sets various variables"""
if not self.index: # consider the last component as quoted
dirname, basename = self.dirsplit()
temp_rp = rpath.RPath(self.conn, dirname, (unquote(basename),))
result = temp_rp.isincfile()
if result:
self.inc_basestr = unquote(temp_rp.inc_basestr)
self.inc_timestr = unquote(temp_rp.inc_timestr)
else:
result = rpath.RPath.isincfile(self)
return result
|
def isincfile(self):
"""Return true if path indicates increment, sets various variables"""
if not self.index: # consider the last component as quoted
dirname, basename = self.dirsplit()
temp_rp = rpath.RPath(self.conn, dirname, (unquote(basename),))
result = temp_rp.isincfile()
if result:
self.inc_basestr = unquote(temp_rp.inc_basestr)
self.inc_timestr = unquote(temp_rp.inc_timestr)
else:
result = rpath.RPath.isincfile(self)
if result:
self.inc_basestr = unquote(self.inc_basestr)
return result
|
https://github.com/rdiff-backup/rdiff-backup/issues/266
|
$ wget https://git.patrikdufresne.com/pdsl/rdiffweb/raw/master/rdiffweb/tests/testcases.tar.gz
$ tar -zxvf testcases.tar.gz
$ rdiff-backup --version
rdiff-backup 1.9.0b0
$ rdiff-backup --restore-as-of=1414921853 '/testcases/Fichier @ <root>'
Fatal Error: Wrong number of arguments given.
See the rdiff-backup manual page for more information.
root@b94a5e7b3814:/# rdiff-backup --restore-as-of=1414921853 '/testcases/Fichier @ <root>' /tmp
Warning: special_escapes file not found,
will assume need to escape DOS devices and trailing spaces based on file systems.
Exception ''<' not supported between instances of 'QuotedRPath' and 'QuotedRPath'' raised of class '<class 'TypeError'>':
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 390, in error_check_Main
Main(arglist)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 412, in Main
take_action(rps)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 368, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 713, in Restore
restore_root.new_index(restore_index), inc_rpath, dest_rp, time)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 39, in Restore
TargetS.patch(target, diff_iter)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 331, in patch
for diff in rorpiter.FillInIter(diff_iter, target):
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/rorpiter.py", line 191, in FillInIter
first_rp = next(rpiter)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 279, in get_diffs_from_collated
diff = cls.get_diff(mir_rorp, target_rorp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 296, in get_diff
file_fp = cls.rf_cache.get_fp(expanded_index, mir_rorp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 391, in get_fp
self.get_rf(index, mir_rorp), mir_rorp, self.root_rf.mirror_rp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 371, in get_rf
if not self.add_rfs(index, mir_rorp):
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 417, in add_rfs
new_rfs = list(temp_rf.yield_sub_rfs())
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 589, in yield_sub_rfs
yield self.__class__(mirror_rp, inc_rp, inc_list)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 442, in __init__
self.set_relevant_incs()
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 468, in set_relevant_incs
newer_incs = self.get_newer_incs()
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 494, in get_newer_incs
incpairs.sort()
Traceback (most recent call last):
File "/usr/local/bin/rdiff-backup", line 32, in <module>
rdiff_backup.Main.error_check_Main(sys.argv[1:])
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 390, in error_check_Main
Main(arglist)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 412, in Main
take_action(rps)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 368, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 713, in Restore
restore_root.new_index(restore_index), inc_rpath, dest_rp, time)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 39, in Restore
TargetS.patch(target, diff_iter)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 331, in patch
for diff in rorpiter.FillInIter(diff_iter, target):
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/rorpiter.py", line 191, in FillInIter
first_rp = next(rpiter)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 279, in get_diffs_from_collated
diff = cls.get_diff(mir_rorp, target_rorp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 296, in get_diff
file_fp = cls.rf_cache.get_fp(expanded_index, mir_rorp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 391, in get_fp
self.get_rf(index, mir_rorp), mir_rorp, self.root_rf.mirror_rp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 371, in get_rf
if not self.add_rfs(index, mir_rorp):
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 417, in add_rfs
new_rfs = list(temp_rf.yield_sub_rfs())
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 589, in yield_sub_rfs
yield self.__class__(mirror_rp, inc_rp, inc_list)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 442, in __init__
self.set_relevant_incs()
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 468, in set_relevant_incs
newer_incs = self.get_newer_incs()
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 494, in get_newer_incs
incpairs.sort()
TypeError: '<' not supported between instances of 'QuotedRPath' and 'QuotedRPath'
|
TypeError
|
def Restore(mirror_rp, inc_rpath, target, restore_to_time):
"""Recursively restore mirror and inc_rpath to target at restore_to_time
in epoch format"""
# Store references to classes over the connection
MirrorS = mirror_rp.conn.restore.MirrorStruct
TargetS = target.conn.restore.TargetStruct
MirrorS.set_mirror_and_rest_times(restore_to_time)
MirrorS.initialize_rf_cache(mirror_rp, inc_rpath)
target_iter = TargetS.get_initial_iter(target)
diff_iter = MirrorS.get_diffs(target_iter)
TargetS.patch(target, diff_iter)
MirrorS.close_rf_cache()
|
def Restore(mirror_rp, inc_rpath, target, restore_to_time):
"""Recursively restore mirror and inc_rpath to target at rest_time"""
MirrorS = mirror_rp.conn.restore.MirrorStruct
TargetS = target.conn.restore.TargetStruct
MirrorS.set_mirror_and_rest_times(restore_to_time)
MirrorS.initialize_rf_cache(mirror_rp, inc_rpath)
target_iter = TargetS.get_initial_iter(target)
diff_iter = MirrorS.get_diffs(target_iter)
TargetS.patch(target, diff_iter)
MirrorS.close_rf_cache()
|
https://github.com/rdiff-backup/rdiff-backup/issues/266
|
$ wget https://git.patrikdufresne.com/pdsl/rdiffweb/raw/master/rdiffweb/tests/testcases.tar.gz
$ tar -zxvf testcases.tar.gz
$ rdiff-backup --version
rdiff-backup 1.9.0b0
$ rdiff-backup --restore-as-of=1414921853 '/testcases/Fichier @ <root>'
Fatal Error: Wrong number of arguments given.
See the rdiff-backup manual page for more information.
root@b94a5e7b3814:/# rdiff-backup --restore-as-of=1414921853 '/testcases/Fichier @ <root>' /tmp
Warning: special_escapes file not found,
will assume need to escape DOS devices and trailing spaces based on file systems.
Exception ''<' not supported between instances of 'QuotedRPath' and 'QuotedRPath'' raised of class '<class 'TypeError'>':
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 390, in error_check_Main
Main(arglist)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 412, in Main
take_action(rps)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 368, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 713, in Restore
restore_root.new_index(restore_index), inc_rpath, dest_rp, time)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 39, in Restore
TargetS.patch(target, diff_iter)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 331, in patch
for diff in rorpiter.FillInIter(diff_iter, target):
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/rorpiter.py", line 191, in FillInIter
first_rp = next(rpiter)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 279, in get_diffs_from_collated
diff = cls.get_diff(mir_rorp, target_rorp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 296, in get_diff
file_fp = cls.rf_cache.get_fp(expanded_index, mir_rorp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 391, in get_fp
self.get_rf(index, mir_rorp), mir_rorp, self.root_rf.mirror_rp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 371, in get_rf
if not self.add_rfs(index, mir_rorp):
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 417, in add_rfs
new_rfs = list(temp_rf.yield_sub_rfs())
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 589, in yield_sub_rfs
yield self.__class__(mirror_rp, inc_rp, inc_list)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 442, in __init__
self.set_relevant_incs()
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 468, in set_relevant_incs
newer_incs = self.get_newer_incs()
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 494, in get_newer_incs
incpairs.sort()
Traceback (most recent call last):
File "/usr/local/bin/rdiff-backup", line 32, in <module>
rdiff_backup.Main.error_check_Main(sys.argv[1:])
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 390, in error_check_Main
Main(arglist)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 412, in Main
take_action(rps)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 368, in take_action
Restore(rps[0], rps[1], 1)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/Main.py", line 713, in Restore
restore_root.new_index(restore_index), inc_rpath, dest_rp, time)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 39, in Restore
TargetS.patch(target, diff_iter)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 331, in patch
for diff in rorpiter.FillInIter(diff_iter, target):
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/rorpiter.py", line 191, in FillInIter
first_rp = next(rpiter)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 279, in get_diffs_from_collated
diff = cls.get_diff(mir_rorp, target_rorp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 296, in get_diff
file_fp = cls.rf_cache.get_fp(expanded_index, mir_rorp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 391, in get_fp
self.get_rf(index, mir_rorp), mir_rorp, self.root_rf.mirror_rp)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 371, in get_rf
if not self.add_rfs(index, mir_rorp):
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 417, in add_rfs
new_rfs = list(temp_rf.yield_sub_rfs())
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 589, in yield_sub_rfs
yield self.__class__(mirror_rp, inc_rp, inc_list)
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 442, in __init__
self.set_relevant_incs()
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 468, in set_relevant_incs
newer_incs = self.get_newer_incs()
File "/usr/local/lib/python3.7/dist-packages/rdiff_backup/restore.py", line 494, in get_newer_incs
incpairs.sort()
TypeError: '<' not supported between instances of 'QuotedRPath' and 'QuotedRPath'
|
TypeError
|
def rorp_eq(src_rorp, dest_rorp):
"""Compare hardlinked for equality
Return false if dest_rorp is linked differently, which can happen
if dest is linked more than source, or if it is represented by a
different inode.
"""
if (
not src_rorp.isreg()
or not dest_rorp.isreg()
or src_rorp.getnumlinks() == dest_rorp.getnumlinks() == 1
):
return 1 # Hard links don't apply
"""The sha1 of linked files is only stored in the metadata of the first
linked file on the dest side. If the first linked file on the src side is
deleted, then the sha1 will also be deleted on the dest side, so we test for this
& report not equal so that another sha1 will be stored with the next linked
file on the dest side"""
if not islinked(src_rorp) and not dest_rorp.has_sha1():
return 0
if src_rorp.getnumlinks() != dest_rorp.getnumlinks():
return 0
src_key = get_inode_key(src_rorp)
index, remaining, dest_key, digest = _inode_index[src_key]
if dest_key == "NA":
# Allow this to be ok for first comparison, but not any
# subsequent ones
_inode_index[src_key] = (index, remaining, None, None)
return 1
try:
return dest_key == get_inode_key(dest_rorp)
except KeyError:
return 0 # Inode key might be missing if the metadata file is corrupt
|
def rorp_eq(src_rorp, dest_rorp):
"""Compare hardlinked for equality
Return false if dest_rorp is linked differently, which can happen
if dest is linked more than source, or if it is represented by a
different inode.
"""
if (
not src_rorp.isreg()
or not dest_rorp.isreg()
or src_rorp.getnumlinks() == dest_rorp.getnumlinks() == 1
):
return 1 # Hard links don't apply
if src_rorp.getnumlinks() < dest_rorp.getnumlinks():
return 0
src_key = get_inode_key(src_rorp)
index, remaining, dest_key, digest = _inode_index[src_key]
if dest_key == "NA":
# Allow this to be ok for first comparison, but not any
# subsequent ones
_inode_index[src_key] = (index, remaining, None, None)
return 1
try:
return dest_key == get_inode_key(dest_rorp)
except KeyError:
return 0 # Inode key might be missing if the metadata file is corrupt
|
https://github.com/rdiff-backup/rdiff-backup/issues/78
|
======================================================================
FAIL: test_session (__main__.HashTest)
Run actual sessions and make sure proper hashes recorded
----------------------------------------------------------------------
Traceback (most recent call last):
File "./testing/hashtest.py", line 95, in test_session
assert hashlist == hashlist1, (hashlist1, hashlist)
AssertionError: ([None, '943a702d06f34599aee1f8da8ef9f7296031d699', '943a702d06f34599aee1f8da8ef9f7296031d699', 'eab21fb1a18b408909bae552b847f6b13f370f62', 'eab21fb1a18b408909bae552b847f6b13f370f62'], [None, '943a702d06f34599aee1f8da8ef9f7296031d699', None, 'eab21fb1a18b408909bae552b847f6b13f370f62', None])
|
AssertionError
|
def Verify(mirror_rp, inc_rp, verify_time):
"""Compute SHA1 sums of repository files and check against metadata"""
assert mirror_rp.conn is Globals.local_connection
repo_iter = RepoSide.init_and_get_iter(mirror_rp, inc_rp, verify_time)
base_index = RepoSide.mirror_base.index
bad_files = 0
for repo_rorp in repo_iter:
if not repo_rorp.isreg():
continue
verify_sha1 = get_hash(repo_rorp)
if not verify_sha1:
log.Log(
"Warning: Cannot find SHA1 digest for file %s,\n"
"perhaps because this feature was added in v1.1.1"
% (repo_rorp.get_safeindexpath(),),
2,
)
continue
fp = RepoSide.rf_cache.get_fp(base_index + repo_rorp.index, repo_rorp)
computed_hash = hash.compute_sha1_fp(fp)
if computed_hash == verify_sha1:
log.Log("Verified SHA1 digest of %s" % repo_rorp.get_safeindexpath(), 5)
else:
bad_files += 1
log.Log(
"Warning: Computed SHA1 digest of %s\n %s\n"
"doesn't match recorded digest of\n %s\n"
"Your backup repository may be corrupted!"
% (repo_rorp.get_safeindexpath(), computed_hash, verify_sha1),
2,
)
RepoSide.close_rf_cache()
if not bad_files:
log.Log("Every file verified successfully.", 3)
return bad_files
|
def Verify(mirror_rp, inc_rp, verify_time):
"""Compute SHA1 sums of repository files and check against metadata"""
assert mirror_rp.conn is Globals.local_connection
repo_iter = RepoSide.init_and_get_iter(mirror_rp, inc_rp, verify_time)
base_index = RepoSide.mirror_base.index
bad_files = 0
for repo_rorp in repo_iter:
if not repo_rorp.isreg():
continue
if not repo_rorp.has_sha1():
log.Log(
"Warning: Cannot find SHA1 digest for file %s,\n"
"perhaps because this feature was added in v1.1.1"
% (repo_rorp.get_safeindexpath(),),
2,
)
continue
fp = RepoSide.rf_cache.get_fp(base_index + repo_rorp.index, repo_rorp)
computed_hash = hash.compute_sha1_fp(fp)
if computed_hash == repo_rorp.get_sha1():
log.Log("Verified SHA1 digest of %s" % repo_rorp.get_safeindexpath(), 5)
else:
bad_files += 1
log.Log(
"Warning: Computed SHA1 digest of %s\n %s\n"
"doesn't match recorded digest of\n %s\n"
"Your backup repository may be corrupted!"
% (repo_rorp.get_safeindexpath(), computed_hash, repo_rorp.get_sha1()),
2,
)
RepoSide.close_rf_cache()
if not bad_files:
log.Log("Every file verified successfully.", 3)
return bad_files
|
https://github.com/rdiff-backup/rdiff-backup/issues/78
|
======================================================================
FAIL: test_session (__main__.HashTest)
Run actual sessions and make sure proper hashes recorded
----------------------------------------------------------------------
Traceback (most recent call last):
File "./testing/hashtest.py", line 95, in test_session
assert hashlist == hashlist1, (hashlist1, hashlist)
AssertionError: ([None, '943a702d06f34599aee1f8da8ef9f7296031d699', '943a702d06f34599aee1f8da8ef9f7296031d699', 'eab21fb1a18b408909bae552b847f6b13f370f62', 'eab21fb1a18b408909bae552b847f6b13f370f62'], [None, '943a702d06f34599aee1f8da8ef9f7296031d699', None, 'eab21fb1a18b408909bae552b847f6b13f370f62', None])
|
AssertionError
|
def compare_hash(cls, repo_iter):
"""Like above, but also compare sha1 sums of any regular files"""
def hashes_changed(src_rp, mir_rorp):
"""Return 0 if their data hashes same, 1 otherwise"""
verify_sha1 = get_hash(mir_rorp)
if not verify_sha1:
log.Log(
"Warning: Metadata file has no digest for %s, "
"unable to compare." % (mir_rorp.get_safeindexpath(),),
2,
)
return 0
elif (
src_rp.getsize() == mir_rorp.getsize()
and hash.compute_sha1(src_rp) == verify_sha1
):
return 0
return 1
src_iter = cls.get_source_select()
for src_rp, mir_rorp in rorpiter.Collate2Iters(src_iter, repo_iter):
report = get_basic_report(src_rp, mir_rorp, hashes_changed)
if report:
yield report
else:
log_success(src_rp, mir_rorp)
|
def compare_hash(cls, repo_iter):
"""Like above, but also compare sha1 sums of any regular files"""
def hashes_changed(src_rp, mir_rorp):
"""Return 0 if their data hashes same, 1 otherwise"""
if not mir_rorp.has_sha1():
log.Log(
"Warning: Metadata file has no digest for %s, "
"unable to compare." % (mir_rorp.get_safeindexpath(),),
2,
)
return 0
elif (
src_rp.getsize() == mir_rorp.getsize()
and hash.compute_sha1(src_rp) == mir_rorp.get_sha1()
):
return 0
return 1
src_iter = cls.get_source_select()
for src_rp, mir_rorp in rorpiter.Collate2Iters(src_iter, repo_iter):
report = get_basic_report(src_rp, mir_rorp, hashes_changed)
if report:
yield report
else:
log_success(src_rp, mir_rorp)
|
https://github.com/rdiff-backup/rdiff-backup/issues/78
|
======================================================================
FAIL: test_session (__main__.HashTest)
Run actual sessions and make sure proper hashes recorded
----------------------------------------------------------------------
Traceback (most recent call last):
File "./testing/hashtest.py", line 95, in test_session
assert hashlist == hashlist1, (hashlist1, hashlist)
AssertionError: ([None, '943a702d06f34599aee1f8da8ef9f7296031d699', '943a702d06f34599aee1f8da8ef9f7296031d699', 'eab21fb1a18b408909bae552b847f6b13f370f62', 'eab21fb1a18b408909bae552b847f6b13f370f62'], [None, '943a702d06f34599aee1f8da8ef9f7296031d699', None, 'eab21fb1a18b408909bae552b847f6b13f370f62', None])
|
AssertionError
|
def hashes_changed(src_rp, mir_rorp):
"""Return 0 if their data hashes same, 1 otherwise"""
verify_sha1 = get_hash(mir_rorp)
if not verify_sha1:
log.Log(
"Warning: Metadata file has no digest for %s, "
"unable to compare." % (mir_rorp.get_safeindexpath(),),
2,
)
return 0
elif (
src_rp.getsize() == mir_rorp.getsize()
and hash.compute_sha1(src_rp) == verify_sha1
):
return 0
return 1
|
def hashes_changed(src_rp, mir_rorp):
"""Return 0 if their data hashes same, 1 otherwise"""
if not mir_rorp.has_sha1():
log.Log(
"Warning: Metadata file has no digest for %s, "
"unable to compare." % (mir_rorp.get_safeindexpath(),),
2,
)
return 0
elif (
src_rp.getsize() == mir_rorp.getsize()
and hash.compute_sha1(src_rp) == mir_rorp.get_sha1()
):
return 0
return 1
|
https://github.com/rdiff-backup/rdiff-backup/issues/78
|
======================================================================
FAIL: test_session (__main__.HashTest)
Run actual sessions and make sure proper hashes recorded
----------------------------------------------------------------------
Traceback (most recent call last):
File "./testing/hashtest.py", line 95, in test_session
assert hashlist == hashlist1, (hashlist1, hashlist)
AssertionError: ([None, '943a702d06f34599aee1f8da8ef9f7296031d699', '943a702d06f34599aee1f8da8ef9f7296031d699', 'eab21fb1a18b408909bae552b847f6b13f370f62', 'eab21fb1a18b408909bae552b847f6b13f370f62'], [None, '943a702d06f34599aee1f8da8ef9f7296031d699', None, 'eab21fb1a18b408909bae552b847f6b13f370f62', None])
|
AssertionError
|
def __init__(self, connection, base, index=(), data=None):
"""Make new QuotedRPath"""
# we avoid handing over "None" as data so that the parent
# class doesn't try to gather data of an unquoted filename
# Caution: this works only because RPath checks on "None"
# and its parent RORPath on True/False.
super().__init__(connection, base, index, data or False)
self.quoted_index = tuple(map(quote, self.index))
# we need to recalculate path and data on the basis of
# quoted_index (parent class does it on the basis of index)
if base is not None:
self.path = self.path_join(self.base, *self.quoted_index)
if data is None:
self.setdata()
|
def __init__(self, connection, base, index=(), data=None):
"""Make new QuotedRPath"""
super().__init__(connection, base, index, data)
self.quoted_index = tuple(map(quote, self.index))
# we need to recalculate path and data on the basis of
# quoted_index (parent class does it on the basis of index)
if base is not None:
self.path = self.path_join(self.base, *self.quoted_index)
if data is None:
self.setdata()
|
https://github.com/rdiff-backup/rdiff-backup/issues/216
|
C:\Users\vmtest\Desktop>C:\Users\vmtest\Downloads\rdiff-backup-1.4.0b0-win64.exe
test_base test_destination
Exception '[WinError 123] La syntaxe du nom de fichier, de répertoire ou de volu
me est incorrecte: b'test_destination/rdiff-backup-data/current_mirror.2019-12-1
5T08:36:05-05:00.data'' raised of class '<class 'OSError'>':
File "rdiff_backup\Main.py", line 384, in error_check_Main
File "rdiff_backup\Main.py", line 406, in Main
File "rdiff_backup\Main.py", line 342, in take_action
File "rdiff_backup\Main.py", line 422, in Backup
File "rdiff_backup\Main.py", line 609, in backup_final_init
File "rdiff_backup\Main.py", line 1112, in checkdest_if_necessary
File "rdiff_backup\Main.py", line 1076, in checkdest_need_check
File "rdiff_backup\restore.py", line 56, in get_inclist
File "rdiff_backup\rpath.py", line 1375, in append
File "rdiff_backup\FilenameMapping.py", line 146, in __init__
File "rdiff_backup\rpath.py", line 1047, in __init__
File "rdiff_backup\rpath.py", line 1073, in setdata
File "rdiff_backup\rpath.py", line 352, in make_file_dict
Traceback (most recent call last):
File "rdiff-backup", line 32, in <module>
File "rdiff_backup\Main.py", line 384, in error_check_Main
File "rdiff_backup\Main.py", line 406, in Main
File "rdiff_backup\Main.py", line 342, in take_action
File "rdiff_backup\Main.py", line 422, in Backup
File "rdiff_backup\Main.py", line 609, in backup_final_init
File "rdiff_backup\Main.py", line 1112, in checkdest_if_necessary
File "rdiff_backup\Main.py", line 1076, in checkdest_need_check
File "rdiff_backup\restore.py", line 56, in get_inclist
File "rdiff_backup\rpath.py", line 1375, in append
File "rdiff_backup\FilenameMapping.py", line 146, in __init__
File "rdiff_backup\rpath.py", line 1047, in __init__
File "rdiff_backup\rpath.py", line 1073, in setdata
File "rdiff_backup\rpath.py", line 352, in make_file_dict
OSError: [WinError 123] La syntaxe du nom de fichier, de répertoire ou de volume
est incorrecte: b'test_destination/rdiff-backup-data/current_mirror.2019-12-15T
08:36:05-05:00.data'
[3504] Failed to execute script rdiff-backup
|
OSError
|
def start_process(self, index, diff_rorp):
"""Start processing directory"""
self.base_rp, inc_prefix = longname.get_mirror_inc_rps(
self.CCPP.get_rorps(index), self.basis_root_rp, self.inc_root_rp
)
self.base_rp.setdata()
assert diff_rorp.isdir() or self.base_rp.isdir(), (
"Either %s or %s must be a directory"
% (repr(diff_rorp.get_safeindexpath()), repr(self.base_rp.get_safepath()))
)
if diff_rorp.isdir():
inc = increment.Increment(diff_rorp, self.base_rp, inc_prefix)
if inc and inc.isreg():
inc.fsync_with_dir() # must write inc before rp changed
self.base_rp.setdata() # in case written by increment above
self.prepare_dir(diff_rorp, self.base_rp)
elif self.set_dir_replacement(diff_rorp, self.base_rp):
inc = increment.Increment(self.dir_replacement, self.base_rp, inc_prefix)
if inc:
self.CCPP.set_inc(index, inc)
self.CCPP.flag_success(index)
|
def start_process(self, index, diff_rorp):
"""Start processing directory"""
self.base_rp, inc_prefix = longname.get_mirror_inc_rps(
self.CCPP.get_rorps(index), self.basis_root_rp, self.inc_root_rp
)
self.base_rp.setdata()
assert diff_rorp.isdir() or self.base_rp.isdir(), (
"Either %s or %s must be a directory"
% (repr(diff_rorp.path), repr(self.base_rp.path))
)
if diff_rorp.isdir():
inc = increment.Increment(diff_rorp, self.base_rp, inc_prefix)
if inc and inc.isreg():
inc.fsync_with_dir() # must write inc before rp changed
self.base_rp.setdata() # in case written by increment above
self.prepare_dir(diff_rorp, self.base_rp)
elif self.set_dir_replacement(diff_rorp, self.base_rp):
inc = increment.Increment(self.dir_replacement, self.base_rp, inc_prefix)
if inc:
self.CCPP.set_inc(index, inc)
self.CCPP.flag_success(index)
|
https://github.com/rdiff-backup/rdiff-backup/issues/14
|
Exception 'RORPath instance has no attribute 'path'' raised of class '<type 'exceptions.AttributeError'>':
File "rdiff_backup\Main.pyc", line 304, in error_check_Main
File "rdiff_backup\Main.pyc", line 324, in Main
File "rdiff_backup\Main.pyc", line 280, in take_action
File "rdiff_backup\Main.pyc", line 343, in Backup
File "rdiff_backup\backup.pyc", line 51, in Mirror_and_increment
File "rdiff_backup\backup.pyc", line 243, in patch_and_increment
File "rdiff_backup\rorpiter.pyc", line 284, in __call__
File "rdiff_backup\backup.pyc", line 718, in start_process
Traceback (most recent call last):
File "rdiff-backup", line 30, in <module>
File "rdiff_backup\Main.pyc", line 304, in error_check_Main
File "rdiff_backup\Main.pyc", line 324, in Main
File "rdiff_backup\Main.pyc", line 280, in take_action
File "rdiff_backup\Main.pyc", line 343, in Backup
File "rdiff_backup\backup.pyc", line 51, in Mirror_and_increment
File "rdiff_backup\backup.pyc", line 243, in patch_and_increment
File "rdiff_backup\rorpiter.pyc", line 284, in __call__
File "rdiff_backup\backup.pyc", line 718, in start_process
AttributeError: RORPath instance has no attribute 'path'
|
AttributeError
|
def check_target_type(y, indicate_one_vs_all=False):
"""Check the target types to be conform to the current samplers.
The current samplers should be compatible with ``'binary'``,
``'multilabel-indicator'`` and ``'multiclass'`` targets only.
Parameters
----------
y : ndarray,
The array containing the target.
indicate_one_vs_all : bool, optional
Either to indicate if the targets are encoded in a one-vs-all fashion.
Returns
-------
y : ndarray,
The returned target.
is_one_vs_all : bool, optional
Indicate if the target was originally encoded in a one-vs-all fashion.
Only returned if ``indicate_multilabel=True``.
"""
type_y = type_of_target(y)
if type_y == "multilabel-indicator":
if np.any(y.sum(axis=1) > 1):
raise ValueError(
"Imbalanced-learn currently supports binary, multiclass and "
"binarized encoded multiclasss targets. Multilabel and "
"multioutput targets are not supported."
)
y = y.argmax(axis=1)
else:
y = column_or_1d(y)
return (y, type_y == "multilabel-indicator") if indicate_one_vs_all else y
|
def check_target_type(y, indicate_one_vs_all=False):
"""Check the target types to be conform to the current samplers.
The current samplers should be compatible with ``'binary'``,
``'multilabel-indicator'`` and ``'multiclass'`` targets only.
Parameters
----------
y : ndarray,
The array containing the target.
indicate_one_vs_all : bool, optional
Either to indicate if the targets are encoded in a one-vs-all fashion.
Returns
-------
y : ndarray,
The returned target.
is_one_vs_all : bool, optional
Indicate if the target was originally encoded in a one-vs-all fashion.
Only returned if ``indicate_multilabel=True``.
"""
type_y = type_of_target(y)
if type_y == "multilabel-indicator":
if np.any(y.sum(axis=1) > 1):
raise ValueError(
"Imbalanced-learn currently supports binary, multiclass and "
"binarized encoded multiclasss targets. Multilabel and "
"multioutput targets are not supported."
)
y = y.argmax(axis=1)
return (y, type_y == "multilabel-indicator") if indicate_one_vs_all else y
|
https://github.com/scikit-learn-contrib/imbalanced-learn/issues/671
|
TypeError Traceback (most recent call last)
<ipython-input-51-da2a32d7cb52> in <module>()
----> pipe.fit(X_train, y_train)
10 frames
/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_search.py in fit(self, X, y, groups, **fit_params)
737 refit_start_time = time.time()
738 if y is not None:
--> 739 self.best_estimator_.fit(X, y, **fit_params)
740 else:
741 self.best_estimator_.fit(X, **fit_params)
/usr/local/lib/python3.6/dist-packages/imblearn/pipeline.py in fit(self, X, y, **fit_params)
285
286 """
--> 287 Xt, yt, fit_params = self._fit(X, y, **fit_params)
288 with _print_elapsed_time('Pipeline',
289 self._log_message(len(self.steps) - 1)):
/usr/local/lib/python3.6/dist-packages/imblearn/pipeline.py in _fit(self, X, y, **fit_params)
247 message_clsname='Pipeline',
248 message=self._log_message(step_idx),
--> 249 **fit_params_steps[name]
250 )
251 # Replace the transformer of the step with the fitted
/usr/local/lib/python3.6/dist-packages/joblib/memory.py in __call__(self, *args, **kwargs)
566
567 def __call__(self, *args, **kwargs):
--> 568 return self._cached_call(args, kwargs)[0]
569
570 def __getstate__(self):
/usr/local/lib/python3.6/dist-packages/joblib/memory.py in _cached_call(self, args, kwargs, shelving)
532
533 if must_call:
--> 534 out, metadata = self.call(*args, **kwargs)
535 if self.mmap_mode is not None:
536 # Memmap the output at the first call to be consistent with
/usr/local/lib/python3.6/dist-packages/joblib/memory.py in call(self, *args, **kwargs)
732 if self._verbose > 0:
733 print(format_call(self.func, args, kwargs))
--> 734 output = self.func(*args, **kwargs)
735 self.store_backend.dump_item(
736 [func_id, args_id], output, verbose=self._verbose)
/usr/local/lib/python3.6/dist-packages/imblearn/pipeline.py in _fit_resample_one(sampler, X, y, message_clsname, message, **fit_params)
412 **fit_params):
413 with _print_elapsed_time(message_clsname, message):
--> 414 X_res, y_res = sampler.fit_resample(X, y, **fit_params)
415
416 return X_res, y_res, sampler
/usr/local/lib/python3.6/dist-packages/imblearn/base.py in fit_resample(self, X, y)
79 )
80
---> 81 output = self._fit_resample(X, y)
82
83 if self._X_columns is not None or self._y_name is not None:
/usr/local/lib/python3.6/dist-packages/imblearn/over_sampling/_random_over_sampler.py in _fit_resample(self, X, y)
102 def _fit_resample(self, X, y):
103 random_state = check_random_state(self.random_state)
--> 104 target_stats = Counter(y)
105
106 sample_indices = range(X.shape[0])
/usr/lib/python3.6/collections/__init__.py in __init__(*args, **kwds)
533 raise TypeError('expected at most 1 arguments, got %d' % len(args))
534 super(Counter, self).__init__()
--> 535 self.update(*args, **kwds)
536
537 def __missing__(self, key):
/usr/lib/python3.6/collections/__init__.py in update(*args, **kwds)
620 super(Counter, self).update(iterable) # fast path when counter is empty
621 else:
--> 622 _count_elements(self, iterable)
623 if kwds:
624 self.update(kwds)
TypeError: unhashable type: 'numpy.ndarray'
|
TypeError
|
def _yield_sampler_checks(name, Estimator):
yield check_target_type
yield check_samplers_one_label
yield check_samplers_fit
yield check_samplers_fit_resample
yield check_samplers_sampling_strategy_fit_resample
yield check_samplers_sparse
yield check_samplers_pandas
yield check_samplers_multiclass_ova
yield check_samplers_preserve_dtype
yield check_samplers_sample_indices
yield check_samplers_2d_target
|
def _yield_sampler_checks(name, Estimator):
yield check_target_type
yield check_samplers_one_label
yield check_samplers_fit
yield check_samplers_fit_resample
yield check_samplers_sampling_strategy_fit_resample
yield check_samplers_sparse
yield check_samplers_pandas
yield check_samplers_multiclass_ova
yield check_samplers_preserve_dtype
yield check_samplers_sample_indices
|
https://github.com/scikit-learn-contrib/imbalanced-learn/issues/671
|
TypeError Traceback (most recent call last)
<ipython-input-51-da2a32d7cb52> in <module>()
----> pipe.fit(X_train, y_train)
10 frames
/usr/local/lib/python3.6/dist-packages/sklearn/model_selection/_search.py in fit(self, X, y, groups, **fit_params)
737 refit_start_time = time.time()
738 if y is not None:
--> 739 self.best_estimator_.fit(X, y, **fit_params)
740 else:
741 self.best_estimator_.fit(X, **fit_params)
/usr/local/lib/python3.6/dist-packages/imblearn/pipeline.py in fit(self, X, y, **fit_params)
285
286 """
--> 287 Xt, yt, fit_params = self._fit(X, y, **fit_params)
288 with _print_elapsed_time('Pipeline',
289 self._log_message(len(self.steps) - 1)):
/usr/local/lib/python3.6/dist-packages/imblearn/pipeline.py in _fit(self, X, y, **fit_params)
247 message_clsname='Pipeline',
248 message=self._log_message(step_idx),
--> 249 **fit_params_steps[name]
250 )
251 # Replace the transformer of the step with the fitted
/usr/local/lib/python3.6/dist-packages/joblib/memory.py in __call__(self, *args, **kwargs)
566
567 def __call__(self, *args, **kwargs):
--> 568 return self._cached_call(args, kwargs)[0]
569
570 def __getstate__(self):
/usr/local/lib/python3.6/dist-packages/joblib/memory.py in _cached_call(self, args, kwargs, shelving)
532
533 if must_call:
--> 534 out, metadata = self.call(*args, **kwargs)
535 if self.mmap_mode is not None:
536 # Memmap the output at the first call to be consistent with
/usr/local/lib/python3.6/dist-packages/joblib/memory.py in call(self, *args, **kwargs)
732 if self._verbose > 0:
733 print(format_call(self.func, args, kwargs))
--> 734 output = self.func(*args, **kwargs)
735 self.store_backend.dump_item(
736 [func_id, args_id], output, verbose=self._verbose)
/usr/local/lib/python3.6/dist-packages/imblearn/pipeline.py in _fit_resample_one(sampler, X, y, message_clsname, message, **fit_params)
412 **fit_params):
413 with _print_elapsed_time(message_clsname, message):
--> 414 X_res, y_res = sampler.fit_resample(X, y, **fit_params)
415
416 return X_res, y_res, sampler
/usr/local/lib/python3.6/dist-packages/imblearn/base.py in fit_resample(self, X, y)
79 )
80
---> 81 output = self._fit_resample(X, y)
82
83 if self._X_columns is not None or self._y_name is not None:
/usr/local/lib/python3.6/dist-packages/imblearn/over_sampling/_random_over_sampler.py in _fit_resample(self, X, y)
102 def _fit_resample(self, X, y):
103 random_state = check_random_state(self.random_state)
--> 104 target_stats = Counter(y)
105
106 sample_indices = range(X.shape[0])
/usr/lib/python3.6/collections/__init__.py in __init__(*args, **kwds)
533 raise TypeError('expected at most 1 arguments, got %d' % len(args))
534 super(Counter, self).__init__()
--> 535 self.update(*args, **kwds)
536
537 def __missing__(self, key):
/usr/lib/python3.6/collections/__init__.py in update(*args, **kwds)
620 super(Counter, self).update(iterable) # fast path when counter is empty
621 else:
--> 622 _count_elements(self, iterable)
623 if kwds:
624 self.update(kwds)
TypeError: unhashable type: 'numpy.ndarray'
|
TypeError
|
def save_readability(
link: Link, out_dir: Optional[str] = None, timeout: int = TIMEOUT
) -> ArchiveResult:
"""download reader friendly version using @mozilla/readability"""
out_dir = Path(out_dir or link.link_dir)
output_folder = out_dir.absolute() / "readability"
output = str(output_folder)
# Readability Docs: https://github.com/mozilla/readability
status = "succeeded"
# fake command to show the user so they have something to try debugging if get_html fails
cmd = [CURL_BINARY, link.url]
timer = TimedProgress(timeout, prefix=" ")
try:
document = get_html(link, out_dir)
temp_doc = NamedTemporaryFile(delete=False)
temp_doc.write(document.encode("utf-8"))
temp_doc.close()
cmd = [DEPENDENCIES["READABILITY_BINARY"]["path"], temp_doc.name]
result = run(cmd, cwd=out_dir, timeout=timeout)
result_json = json.loads(result.stdout)
output_folder.mkdir(exist_ok=True)
atomic_write(str(output_folder / "content.html"), result_json.pop("content"))
atomic_write(str(output_folder / "content.txt"), result_json.pop("textContent"))
atomic_write(str(output_folder / "article.json"), result_json)
# parse out number of files downloaded from last line of stderr:
# "Downloaded: 76 files, 4.0M in 1.6s (2.52 MB/s)"
output_tail = [
line.strip()
for line in (result.stdout + result.stderr).decode().rsplit("\n", 3)[-3:]
if line.strip()
]
hints = (
"Got readability response code: {}.".format(result.returncode),
*output_tail,
)
# Check for common failure cases
if result.returncode > 0:
raise ArchiveError("Readability was not able to archive the page", hints)
except (Exception, OSError) as err:
status = "failed"
output = err
finally:
timer.end()
return ArchiveResult(
cmd=cmd,
pwd=str(out_dir),
cmd_version=READABILITY_VERSION,
output=output,
status=status,
**timer.stats,
)
|
def save_readability(
link: Link, out_dir: Optional[str] = None, timeout: int = TIMEOUT
) -> ArchiveResult:
"""download reader friendly version using @mozilla/readability"""
out_dir = Path(out_dir or link.link_dir)
output_folder = out_dir.absolute() / "readability"
output = str(output_folder)
# Readability Docs: https://github.com/mozilla/readability
status = "succeeded"
timer = TimedProgress(timeout, prefix=" ")
try:
document = get_html(link, out_dir)
temp_doc = NamedTemporaryFile(delete=False)
temp_doc.write(document.encode("utf-8"))
temp_doc.close()
cmd = [DEPENDENCIES["READABILITY_BINARY"]["path"], temp_doc.name]
result = run(cmd, cwd=out_dir, timeout=timeout)
result_json = json.loads(result.stdout)
output_folder.mkdir(exist_ok=True)
atomic_write(str(output_folder / "content.html"), result_json.pop("content"))
atomic_write(str(output_folder / "content.txt"), result_json.pop("textContent"))
atomic_write(str(output_folder / "article.json"), result_json)
# parse out number of files downloaded from last line of stderr:
# "Downloaded: 76 files, 4.0M in 1.6s (2.52 MB/s)"
output_tail = [
line.strip()
for line in (result.stdout + result.stderr).decode().rsplit("\n", 3)[-3:]
if line.strip()
]
hints = (
"Got readability response code: {}.".format(result.returncode),
*output_tail,
)
# Check for common failure cases
if result.returncode > 0:
raise ArchiveError("Readability was not able to archive the page", hints)
except (Exception, OSError) as err:
status = "failed"
output = err
finally:
timer.end()
return ArchiveResult(
cmd=cmd,
pwd=str(out_dir),
cmd_version=READABILITY_VERSION,
output=output,
status=status,
**timer.stats,
)
|
https://github.com/ArchiveBox/ArchiveBox/issues/458
|
[√] [2020-08-26 21:10:43] "Big Brother is Watching - '17 WRX Limited and all '17 STI - Page 3 - NASIOC"
https://forums.nasioc.com/forums/showthread.php?t=2803387&amp;highlight=big+brother&amp;page=3
√ ./archive/1576899114
> readability
! Failed to archive link: Exception: Exception in archive_methods.save_readability(Link(url=https://forums.nasioc.com/forums/showthread.php?t=2803387&amp;highlight=big+brother&amp;page=3))
Traceback (most recent call last):
File "/app/archivebox/extractors/__init__.py", line 91, in archive_link
result = method_function(link=link, out_dir=out_dir)
File "/app/archivebox/util.py", line 111, in typechecked_function
return func(*args, **kwargs)
File "/app/archivebox/extractors/readability.py", line 109, in save_readability
cmd=cmd,
UnboundLocalError: local variable 'cmd' referenced before assignment
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/bin/archivebox", line 33, in <module>
sys.exit(load_entry_point('archivebox', 'console_scripts', 'archivebox')())
File "/app/archivebox/cli/__init__.py", line 122, in main
run_subcommand(
File "/app/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/app/archivebox/cli/archivebox_add.py", line 78, in main
add(
File "/app/archivebox/util.py", line 111, in typechecked_function
return func(*args, **kwargs)
File "/app/archivebox/main.py", line 572, in add
archive_links(all_links, overwrite=overwrite, out_dir=out_dir)
File "/app/archivebox/util.py", line 111, in typechecked_function
return func(*args, **kwargs)
File "/app/archivebox/extractors/__init__.py", line 150, in archive_links
archive_link(link, overwrite=overwrite, methods=methods, out_dir=link.link_dir)
File "/app/archivebox/util.py", line 111, in typechecked_function
return func(*args, **kwargs)
File "/app/archivebox/extractors/__init__.py", line 101, in archive_link
raise Exception('Exception in archive_methods.save_{}(Link(url={}))'.format(
Exception: Exception in archive_methods.save_readability(Link(url=https://forums.nasioc.com/forums/showthread.php?t=2803387&amp;highlight=big+brother&amp;page=3))
|
UnboundLocalError
|
def parse_json_main_index(out_dir: str = OUTPUT_DIR) -> Iterator[Link]:
"""parse an archive index json file and return the list of links"""
index_path = os.path.join(out_dir, JSON_INDEX_FILENAME)
if os.path.exists(index_path):
with open(index_path, "r", encoding="utf-8") as f:
links = pyjson.load(f)["links"]
for link_json in links:
try:
yield Link.from_json(link_json)
except KeyError:
detail_index_path = (
Path(OUTPUT_DIR) / ARCHIVE_DIR_NAME / link_json["timestamp"]
)
yield parse_json_link_details(str(detail_index_path))
return ()
|
def parse_json_main_index(out_dir: str = OUTPUT_DIR) -> Iterator[Link]:
"""parse an archive index json file and return the list of links"""
index_path = os.path.join(out_dir, JSON_INDEX_FILENAME)
if os.path.exists(index_path):
with open(index_path, "r", encoding="utf-8") as f:
links = pyjson.load(f)["links"]
for link_json in links:
yield Link.from_json(link_json)
return ()
|
https://github.com/ArchiveBox/ArchiveBox/issues/374
|
Traceback (most recent call last):
File "/home/USERNAME/.local/bin/archivebox", line 8, in <module>
sys.exit(main())
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 126, in main
pwd=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/archivebox_init.py", line 34, in main
out_dir=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/main.py", line 316, in init
for link in load_main_index(out_dir=out_dir, warn=False)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/__init__.py", line 250, in load_main_index
all_links = list(parse_json_main_index(out_dir))
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/json.py", line 52, in parse_json_main_index
yield Link.from_json(link_json)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 203, in from_json
cast_result = ArchiveResult.from_json(json_result)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 62, in from_json
info['start_ts'] = parse_date(info['start_ts'])
KeyError: 'start_ts'
|
KeyError
|
def fix_invalid_folder_locations(
out_dir: str = OUTPUT_DIR,
) -> Tuple[List[str], List[str]]:
fixed = []
cant_fix = []
for entry in os.scandir(os.path.join(out_dir, ARCHIVE_DIR_NAME)):
if entry.is_dir(follow_symlinks=True):
if os.path.exists(os.path.join(entry.path, "index.json")):
try:
link = parse_json_link_details(entry.path)
except KeyError:
link = None
if not link:
continue
if not entry.path.endswith(f"/{link.timestamp}"):
dest = os.path.join(out_dir, ARCHIVE_DIR_NAME, link.timestamp)
if os.path.exists(dest):
cant_fix.append(entry.path)
else:
shutil.move(entry.path, dest)
fixed.append(dest)
timestamp = entry.path.rsplit("/", 1)[-1]
assert link.link_dir == entry.path
assert link.timestamp == timestamp
write_json_link_details(link, out_dir=entry.path)
return fixed, cant_fix
|
def fix_invalid_folder_locations(
out_dir: str = OUTPUT_DIR,
) -> Tuple[List[str], List[str]]:
fixed = []
cant_fix = []
for entry in os.scandir(os.path.join(out_dir, ARCHIVE_DIR_NAME)):
if entry.is_dir(follow_symlinks=True):
if os.path.exists(os.path.join(entry.path, "index.json")):
link = parse_json_link_details(entry.path)
if not link:
continue
if not entry.path.endswith(f"/{link.timestamp}"):
dest = os.path.join(out_dir, ARCHIVE_DIR_NAME, link.timestamp)
if os.path.exists(dest):
cant_fix.append(entry.path)
else:
shutil.move(entry.path, dest)
fixed.append(dest)
timestamp = entry.path.rsplit("/", 1)[-1]
assert link.link_dir == entry.path
assert link.timestamp == timestamp
write_json_link_details(link, out_dir=entry.path)
return fixed, cant_fix
|
https://github.com/ArchiveBox/ArchiveBox/issues/374
|
Traceback (most recent call last):
File "/home/USERNAME/.local/bin/archivebox", line 8, in <module>
sys.exit(main())
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 126, in main
pwd=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/archivebox_init.py", line 34, in main
out_dir=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/main.py", line 316, in init
for link in load_main_index(out_dir=out_dir, warn=False)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/__init__.py", line 250, in load_main_index
all_links = list(parse_json_main_index(out_dir))
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/json.py", line 52, in parse_json_main_index
yield Link.from_json(link_json)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 203, in from_json
cast_result = ArchiveResult.from_json(json_result)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 62, in from_json
info['start_ts'] = parse_date(info['start_ts'])
KeyError: 'start_ts'
|
KeyError
|
def parse_json_main_index(out_dir: str = OUTPUT_DIR) -> Iterator[Link]:
"""parse an archive index json file and return the list of links"""
index_path = os.path.join(out_dir, JSON_INDEX_FILENAME)
if os.path.exists(index_path):
with open(index_path, "r", encoding="utf-8") as f:
links = pyjson.load(f)["links"]
for link_json in links:
try:
yield Link.from_json(link_json)
except KeyError:
try:
detail_index_path = (
Path(OUTPUT_DIR) / ARCHIVE_DIR_NAME / link_json["timestamp"]
)
yield parse_json_link_details(str(detail_index_path))
except KeyError:
print(
" {lightyellow}! Failed to load the index.json from {}".format(
detail_index_path, **ANSI
)
)
continue
return ()
|
def parse_json_main_index(out_dir: str = OUTPUT_DIR) -> Iterator[Link]:
"""parse an archive index json file and return the list of links"""
index_path = os.path.join(out_dir, JSON_INDEX_FILENAME)
if os.path.exists(index_path):
with open(index_path, "r", encoding="utf-8") as f:
links = pyjson.load(f)["links"]
for link_json in links:
try:
yield Link.from_json(link_json)
except KeyError:
detail_index_path = (
Path(OUTPUT_DIR) / ARCHIVE_DIR_NAME / link_json["timestamp"]
)
yield parse_json_link_details(str(detail_index_path))
return ()
|
https://github.com/ArchiveBox/ArchiveBox/issues/374
|
Traceback (most recent call last):
File "/home/USERNAME/.local/bin/archivebox", line 8, in <module>
sys.exit(main())
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 126, in main
pwd=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/archivebox_init.py", line 34, in main
out_dir=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/main.py", line 316, in init
for link in load_main_index(out_dir=out_dir, warn=False)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/__init__.py", line 250, in load_main_index
all_links = list(parse_json_main_index(out_dir))
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/json.py", line 52, in parse_json_main_index
yield Link.from_json(link_json)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 203, in from_json
cast_result = ArchiveResult.from_json(json_result)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 62, in from_json
info['start_ts'] = parse_date(info['start_ts'])
KeyError: 'start_ts'
|
KeyError
|
def parse_json_links_details(out_dir: str) -> Iterator[Link]:
"""read through all the archive data folders and return the parsed links"""
for entry in os.scandir(os.path.join(out_dir, ARCHIVE_DIR_NAME)):
if entry.is_dir(follow_symlinks=True):
if os.path.exists(os.path.join(entry.path, "index.json")):
try:
link = parse_json_link_details(entry.path)
except KeyError:
link = None
if link:
yield link
|
def parse_json_links_details(out_dir: str) -> Iterator[Link]:
"""read through all the archive data folders and return the parsed links"""
for entry in os.scandir(os.path.join(out_dir, ARCHIVE_DIR_NAME)):
if entry.is_dir(follow_symlinks=True):
if os.path.exists(os.path.join(entry.path, "index.json")):
link = parse_json_link_details(entry.path)
if link:
yield link
|
https://github.com/ArchiveBox/ArchiveBox/issues/374
|
Traceback (most recent call last):
File "/home/USERNAME/.local/bin/archivebox", line 8, in <module>
sys.exit(main())
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 126, in main
pwd=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/archivebox_init.py", line 34, in main
out_dir=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/main.py", line 316, in init
for link in load_main_index(out_dir=out_dir, warn=False)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/__init__.py", line 250, in load_main_index
all_links = list(parse_json_main_index(out_dir))
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/json.py", line 52, in parse_json_main_index
yield Link.from_json(link_json)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 203, in from_json
cast_result = ArchiveResult.from_json(json_result)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 62, in from_json
info['start_ts'] = parse_date(info['start_ts'])
KeyError: 'start_ts'
|
KeyError
|
def get_unrecognized_folders(
links, out_dir: str = OUTPUT_DIR
) -> Dict[str, Optional[Link]]:
"""dirs that don't contain recognizable archive data and aren't listed in the main index"""
by_timestamp = {link.timestamp: 0 for link in links}
unrecognized_folders: Dict[str, Optional[Link]] = {}
for entry in os.scandir(os.path.join(out_dir, ARCHIVE_DIR_NAME)):
if entry.is_dir(follow_symlinks=True):
index_exists = os.path.exists(os.path.join(entry.path, "index.json"))
link = None
try:
link = parse_json_link_details(entry.path)
except KeyError:
# Try to fix index
if index_exists:
try:
# Last attempt to repair the detail index
link_guessed = parse_json_link_details(entry.path, guess=True)
write_json_link_details(link_guessed, out_dir=entry.path)
link = parse_json_link_details(entry.path)
except Exception as e:
pass
if index_exists and link is None:
# index exists but it's corrupted or unparseable
unrecognized_folders[entry.path] = link
elif not index_exists:
# link details index doesn't exist and the folder isn't in the main index
timestamp = entry.path.rsplit("/", 1)[-1]
if timestamp not in by_timestamp:
unrecognized_folders[entry.path] = link
return unrecognized_folders
|
def get_unrecognized_folders(
links, out_dir: str = OUTPUT_DIR
) -> Dict[str, Optional[Link]]:
"""dirs that don't contain recognizable archive data and aren't listed in the main index"""
by_timestamp = {link.timestamp: 0 for link in links}
unrecognized_folders: Dict[str, Optional[Link]] = {}
for entry in os.scandir(os.path.join(out_dir, ARCHIVE_DIR_NAME)):
if entry.is_dir(follow_symlinks=True):
index_exists = os.path.exists(os.path.join(entry.path, "index.json"))
link = None
try:
link = parse_json_link_details(entry.path)
except Exception:
pass
if index_exists and link is None:
# index exists but it's corrupted or unparseable
unrecognized_folders[entry.path] = link
elif not index_exists:
# link details index doesn't exist and the folder isn't in the main index
timestamp = entry.path.rsplit("/", 1)[-1]
if timestamp not in by_timestamp:
unrecognized_folders[entry.path] = link
return unrecognized_folders
|
https://github.com/ArchiveBox/ArchiveBox/issues/374
|
Traceback (most recent call last):
File "/home/USERNAME/.local/bin/archivebox", line 8, in <module>
sys.exit(main())
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 126, in main
pwd=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/archivebox_init.py", line 34, in main
out_dir=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/main.py", line 316, in init
for link in load_main_index(out_dir=out_dir, warn=False)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/__init__.py", line 250, in load_main_index
all_links = list(parse_json_main_index(out_dir))
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/json.py", line 52, in parse_json_main_index
yield Link.from_json(link_json)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 203, in from_json
cast_result = ArchiveResult.from_json(json_result)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 62, in from_json
info['start_ts'] = parse_date(info['start_ts'])
KeyError: 'start_ts'
|
KeyError
|
def is_valid(link: Link) -> bool:
dir_exists = os.path.exists(link.link_dir)
index_exists = os.path.exists(os.path.join(link.link_dir, "index.json"))
if not dir_exists:
# unarchived links are not included in the valid list
return False
if dir_exists and not index_exists:
return False
if dir_exists and index_exists:
try:
parsed_link = parse_json_link_details(link.link_dir, guess=True)
return link.url == parsed_link.url
except Exception as e:
pass
return False
|
def is_valid(link: Link) -> bool:
dir_exists = os.path.exists(link.link_dir)
index_exists = os.path.exists(os.path.join(link.link_dir, "index.json"))
if not dir_exists:
# unarchived links are not included in the valid list
return False
if dir_exists and not index_exists:
return False
if dir_exists and index_exists:
try:
parsed_link = parse_json_link_details(link.link_dir)
return link.url == parsed_link.url
except Exception:
pass
return False
|
https://github.com/ArchiveBox/ArchiveBox/issues/374
|
Traceback (most recent call last):
File "/home/USERNAME/.local/bin/archivebox", line 8, in <module>
sys.exit(main())
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 126, in main
pwd=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/archivebox_init.py", line 34, in main
out_dir=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/main.py", line 316, in init
for link in load_main_index(out_dir=out_dir, warn=False)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/__init__.py", line 250, in load_main_index
all_links = list(parse_json_main_index(out_dir))
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/json.py", line 52, in parse_json_main_index
yield Link.from_json(link_json)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 203, in from_json
cast_result = ArchiveResult.from_json(json_result)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 62, in from_json
info['start_ts'] = parse_date(info['start_ts'])
KeyError: 'start_ts'
|
KeyError
|
def parse_json_main_index(out_dir: str = OUTPUT_DIR) -> Iterator[Link]:
"""parse an archive index json file and return the list of links"""
index_path = os.path.join(out_dir, JSON_INDEX_FILENAME)
if os.path.exists(index_path):
with open(index_path, "r", encoding="utf-8") as f:
links = pyjson.load(f)["links"]
for link_json in links:
try:
yield Link.from_json(link_json)
except KeyError:
try:
detail_index_path = (
Path(OUTPUT_DIR) / ARCHIVE_DIR_NAME / link_json["timestamp"]
)
yield parse_json_link_details(str(detail_index_path))
except KeyError:
# as a last effort, try to guess the missing values out of existing ones
try:
yield Link.from_json(link_json, guess=True)
except KeyError:
print(
" {lightyellow}! Failed to load the index.json from {}".format(
detail_index_path, **ANSI
)
)
continue
return ()
|
def parse_json_main_index(out_dir: str = OUTPUT_DIR) -> Iterator[Link]:
"""parse an archive index json file and return the list of links"""
index_path = os.path.join(out_dir, JSON_INDEX_FILENAME)
if os.path.exists(index_path):
with open(index_path, "r", encoding="utf-8") as f:
links = pyjson.load(f)["links"]
for link_json in links:
try:
yield Link.from_json(link_json)
except KeyError:
try:
detail_index_path = (
Path(OUTPUT_DIR) / ARCHIVE_DIR_NAME / link_json["timestamp"]
)
yield parse_json_link_details(str(detail_index_path))
except KeyError:
print(
" {lightyellow}! Failed to load the index.json from {}".format(
detail_index_path, **ANSI
)
)
continue
return ()
|
https://github.com/ArchiveBox/ArchiveBox/issues/374
|
Traceback (most recent call last):
File "/home/USERNAME/.local/bin/archivebox", line 8, in <module>
sys.exit(main())
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 126, in main
pwd=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/archivebox_init.py", line 34, in main
out_dir=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/main.py", line 316, in init
for link in load_main_index(out_dir=out_dir, warn=False)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/__init__.py", line 250, in load_main_index
all_links = list(parse_json_main_index(out_dir))
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/json.py", line 52, in parse_json_main_index
yield Link.from_json(link_json)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 203, in from_json
cast_result = ArchiveResult.from_json(json_result)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 62, in from_json
info['start_ts'] = parse_date(info['start_ts'])
KeyError: 'start_ts'
|
KeyError
|
def parse_json_link_details(
out_dir: str, guess: Optional[bool] = False
) -> Optional[Link]:
"""load the json link index from a given directory"""
existing_index = os.path.join(out_dir, JSON_INDEX_FILENAME)
if os.path.exists(existing_index):
with open(existing_index, "r", encoding="utf-8") as f:
try:
link_json = pyjson.load(f)
return Link.from_json(link_json, guess)
except pyjson.JSONDecodeError:
pass
return None
|
def parse_json_link_details(out_dir: str) -> Optional[Link]:
"""load the json link index from a given directory"""
existing_index = os.path.join(out_dir, JSON_INDEX_FILENAME)
if os.path.exists(existing_index):
with open(existing_index, "r", encoding="utf-8") as f:
try:
link_json = pyjson.load(f)
return Link.from_json(link_json)
except pyjson.JSONDecodeError:
pass
return None
|
https://github.com/ArchiveBox/ArchiveBox/issues/374
|
Traceback (most recent call last):
File "/home/USERNAME/.local/bin/archivebox", line 8, in <module>
sys.exit(main())
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 126, in main
pwd=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/archivebox_init.py", line 34, in main
out_dir=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/main.py", line 316, in init
for link in load_main_index(out_dir=out_dir, warn=False)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/__init__.py", line 250, in load_main_index
all_links = list(parse_json_main_index(out_dir))
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/json.py", line 52, in parse_json_main_index
yield Link.from_json(link_json)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 203, in from_json
cast_result = ArchiveResult.from_json(json_result)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 62, in from_json
info['start_ts'] = parse_date(info['start_ts'])
KeyError: 'start_ts'
|
KeyError
|
def from_json(cls, json_info, guess=False):
from ..util import parse_date
info = {key: val for key, val in json_info.items() if key in cls.field_names()}
if guess:
keys = info.keys()
if "start_ts" not in keys:
info["start_ts"], info["end_ts"] = cls.guess_ts(json_info)
else:
info["start_ts"] = parse_date(info["start_ts"])
info["end_ts"] = parse_date(info["end_ts"])
if "pwd" not in keys:
info["pwd"] = str(
Path(OUTPUT_DIR) / ARCHIVE_DIR_NAME / json_info["timestamp"]
)
if "cmd_version" not in keys:
info["cmd_version"] = "Undefined"
if "cmd" not in keys:
info["cmd"] = []
else:
info["start_ts"] = parse_date(info["start_ts"])
info["end_ts"] = parse_date(info["end_ts"])
info["cmd_version"] = info.get("cmd_version")
return cls(**info)
|
def from_json(cls, json_info):
from ..util import parse_date
info = {key: val for key, val in json_info.items() if key in cls.field_names()}
info["start_ts"] = parse_date(info["start_ts"])
info["end_ts"] = parse_date(info["end_ts"])
info["cmd_version"] = info.get("cmd_version")
return cls(**info)
|
https://github.com/ArchiveBox/ArchiveBox/issues/374
|
Traceback (most recent call last):
File "/home/USERNAME/.local/bin/archivebox", line 8, in <module>
sys.exit(main())
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 126, in main
pwd=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/archivebox_init.py", line 34, in main
out_dir=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/main.py", line 316, in init
for link in load_main_index(out_dir=out_dir, warn=False)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/__init__.py", line 250, in load_main_index
all_links = list(parse_json_main_index(out_dir))
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/json.py", line 52, in parse_json_main_index
yield Link.from_json(link_json)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 203, in from_json
cast_result = ArchiveResult.from_json(json_result)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 62, in from_json
info['start_ts'] = parse_date(info['start_ts'])
KeyError: 'start_ts'
|
KeyError
|
def from_json(cls, json_info, guess=False):
from ..util import parse_date
info = {key: val for key, val in json_info.items() if key in cls.field_names()}
info["updated"] = parse_date(info.get("updated"))
info["sources"] = info.get("sources") or []
json_history = info.get("history") or {}
cast_history = {}
for method, method_history in json_history.items():
cast_history[method] = []
for json_result in method_history:
assert isinstance(json_result, dict), (
'Items in Link["history"][method] must be dicts'
)
cast_result = ArchiveResult.from_json(json_result, guess)
cast_history[method].append(cast_result)
info["history"] = cast_history
return cls(**info)
|
def from_json(cls, json_info):
from ..util import parse_date
info = {key: val for key, val in json_info.items() if key in cls.field_names()}
info["updated"] = parse_date(info.get("updated"))
info["sources"] = info.get("sources") or []
json_history = info.get("history") or {}
cast_history = {}
for method, method_history in json_history.items():
cast_history[method] = []
for json_result in method_history:
assert isinstance(json_result, dict), (
'Items in Link["history"][method] must be dicts'
)
cast_result = ArchiveResult.from_json(json_result)
cast_history[method].append(cast_result)
info["history"] = cast_history
return cls(**info)
|
https://github.com/ArchiveBox/ArchiveBox/issues/374
|
Traceback (most recent call last):
File "/home/USERNAME/.local/bin/archivebox", line 8, in <module>
sys.exit(main())
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 126, in main
pwd=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/archivebox_init.py", line 34, in main
out_dir=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/main.py", line 316, in init
for link in load_main_index(out_dir=out_dir, warn=False)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/__init__.py", line 250, in load_main_index
all_links = list(parse_json_main_index(out_dir))
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/json.py", line 52, in parse_json_main_index
yield Link.from_json(link_json)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 203, in from_json
cast_result = ArchiveResult.from_json(json_result)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 62, in from_json
info['start_ts'] = parse_date(info['start_ts'])
KeyError: 'start_ts'
|
KeyError
|
def from_json(cls, json_info, guess=False):
from ..util import parse_date
info = {key: val for key, val in json_info.items() if key in cls.field_names()}
if guess:
keys = info.keys()
if "start_ts" not in keys:
info["start_ts"], info["end_ts"] = cls.guess_ts(json_info)
else:
info["start_ts"] = parse_date(info["start_ts"])
info["end_ts"] = parse_date(info["end_ts"])
if "pwd" not in keys:
info["pwd"] = str(
Path(OUTPUT_DIR) / ARCHIVE_DIR_NAME / json_info["timestamp"]
)
if "cmd_version" not in keys:
info["cmd_version"] = "Undefined"
if "cmd" not in keys:
info["cmd"] = []
else:
info["start_ts"] = parse_date(info["start_ts"])
info["end_ts"] = parse_date(info["end_ts"])
info["cmd_version"] = info.get("cmd_version")
if type(info["cmd"]) is str:
info["cmd"] = [info["cmd"]]
return cls(**info)
|
def from_json(cls, json_info, guess=False):
from ..util import parse_date
info = {key: val for key, val in json_info.items() if key in cls.field_names()}
if guess:
keys = info.keys()
if "start_ts" not in keys:
info["start_ts"], info["end_ts"] = cls.guess_ts(json_info)
else:
info["start_ts"] = parse_date(info["start_ts"])
info["end_ts"] = parse_date(info["end_ts"])
if "pwd" not in keys:
info["pwd"] = str(
Path(OUTPUT_DIR) / ARCHIVE_DIR_NAME / json_info["timestamp"]
)
if "cmd_version" not in keys:
info["cmd_version"] = "Undefined"
if "cmd" not in keys:
info["cmd"] = []
else:
info["start_ts"] = parse_date(info["start_ts"])
info["end_ts"] = parse_date(info["end_ts"])
info["cmd_version"] = info.get("cmd_version")
return cls(**info)
|
https://github.com/ArchiveBox/ArchiveBox/issues/374
|
Traceback (most recent call last):
File "/home/USERNAME/.local/bin/archivebox", line 8, in <module>
sys.exit(main())
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 126, in main
pwd=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/__init__.py", line 62, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/cli/archivebox_init.py", line 34, in main
out_dir=pwd or OUTPUT_DIR,
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/main.py", line 316, in init
for link in load_main_index(out_dir=out_dir, warn=False)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/util.py", line 108, in typechecked_function
return func(*args, **kwargs)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/__init__.py", line 250, in load_main_index
all_links = list(parse_json_main_index(out_dir))
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/json.py", line 52, in parse_json_main_index
yield Link.from_json(link_json)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 203, in from_json
cast_result = ArchiveResult.from_json(json_result)
File "/home/USERNAME/.local/lib/python3.7/site-packages/archivebox/index/schema.py", line 62, in from_json
info['start_ts'] = parse_date(info['start_ts'])
KeyError: 'start_ts'
|
KeyError
|
def log_link_archiving_started(link: "Link", link_dir: str, is_new: bool):
# [*] [2019-03-22 13:46:45] "Log Structured Merge Trees - ben stopford"
# http://www.benstopford.com/2015/02/14/log-structured-merge-trees/
# > output/archive/1478739709
print(
'\n[{symbol_color}{symbol}{reset}] [{symbol_color}{now}{reset}] "{title}"'.format(
symbol_color=ANSI["green" if is_new else "black"],
symbol="+" if is_new else "√",
now=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
title=link.title or link.base_url,
**ANSI,
)
)
print(" {blue}{url}{reset}".format(url=link.url, **ANSI))
print(
" {} {}".format(
">" if is_new else "√",
pretty_path(link_dir),
)
)
|
def log_link_archiving_started(link: Link, link_dir: str, is_new: bool):
# [*] [2019-03-22 13:46:45] "Log Structured Merge Trees - ben stopford"
# http://www.benstopford.com/2015/02/14/log-structured-merge-trees/
# > output/archive/1478739709
print(
'\n[{symbol_color}{symbol}{reset}] [{symbol_color}{now}{reset}] "{title}"'.format(
symbol_color=ANSI["green" if is_new else "black"],
symbol="+" if is_new else "√",
now=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
title=link.title or link.base_url,
**ANSI,
)
)
print(" {blue}{url}{reset}".format(url=link.url, **ANSI))
print(
" {} {}".format(
">" if is_new else "√",
pretty_path(link_dir),
)
)
|
https://github.com/ArchiveBox/ArchiveBox/issues/372
|
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
Error in sys.excepthook:
Traceback (most recent call last): [0/6631]
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 32, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 12, in <module>
import os, glob, subprocess, os.path, time, pwd, sys, requests_unixsocket
File "/usr/lib/python3/dist-packages/requests_unixsocket/__init__.py", line 1, in <module>
import requests
File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 154, in <module>
LOG = logging.getLogger('crontab')
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)
Original exception was:
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
|
ImportError
|
def log_link_archiving_finished(link: "Link", link_dir: str, is_new: bool, stats: dict):
total = sum(stats.values())
if stats["failed"] > 0:
_LAST_RUN_STATS.failed += 1
elif stats["skipped"] == total:
_LAST_RUN_STATS.skipped += 1
else:
_LAST_RUN_STATS.succeeded += 1
|
def log_link_archiving_finished(link: Link, link_dir: str, is_new: bool, stats: dict):
total = sum(stats.values())
if stats["failed"] > 0:
_LAST_RUN_STATS.failed += 1
elif stats["skipped"] == total:
_LAST_RUN_STATS.skipped += 1
else:
_LAST_RUN_STATS.succeeded += 1
|
https://github.com/ArchiveBox/ArchiveBox/issues/372
|
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
Error in sys.excepthook:
Traceback (most recent call last): [0/6631]
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 32, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 12, in <module>
import os, glob, subprocess, os.path, time, pwd, sys, requests_unixsocket
File "/usr/lib/python3/dist-packages/requests_unixsocket/__init__.py", line 1, in <module>
import requests
File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 154, in <module>
LOG = logging.getLogger('crontab')
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)
Original exception was:
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
|
ImportError
|
def log_archive_method_finished(result: "ArchiveResult"):
"""quote the argument with whitespace in a command so the user can
copy-paste the outputted string directly to run the cmd
"""
# Prettify CMD string and make it safe to copy-paste by quoting arguments
quoted_cmd = " ".join(
'"{}"'.format(arg) if " " in arg else arg for arg in result.cmd
)
if result.status == "failed":
# Prettify error output hints string and limit to five lines
hints = getattr(result.output, "hints", None) or ()
if hints:
hints = hints if isinstance(hints, (list, tuple)) else hints.split("\n")
hints = (
" {}{}{}".format(ANSI["lightyellow"], line.strip(), ANSI["reset"])
for line in hints[:5]
if line.strip()
)
# Collect and prefix output lines with indentation
output_lines = [
"{lightred}Failed:{reset}".format(**ANSI),
" {reset}{} {red}{}{reset}".format(
result.output.__class__.__name__.replace("ArchiveError", ""),
result.output,
**ANSI,
),
*hints,
"{}Run to see full output:{}".format(ANSI["lightred"], ANSI["reset"]),
*([" cd {};".format(result.pwd)] if result.pwd else []),
" {}".format(quoted_cmd),
]
print("\n".join(" {}".format(line) for line in output_lines if line))
print()
|
def log_archive_method_finished(result: ArchiveResult):
"""quote the argument with whitespace in a command so the user can
copy-paste the outputted string directly to run the cmd
"""
# Prettify CMD string and make it safe to copy-paste by quoting arguments
quoted_cmd = " ".join(
'"{}"'.format(arg) if " " in arg else arg for arg in result.cmd
)
if result.status == "failed":
# Prettify error output hints string and limit to five lines
hints = getattr(result.output, "hints", None) or ()
if hints:
hints = hints if isinstance(hints, (list, tuple)) else hints.split("\n")
hints = (
" {}{}{}".format(ANSI["lightyellow"], line.strip(), ANSI["reset"])
for line in hints[:5]
if line.strip()
)
# Collect and prefix output lines with indentation
output_lines = [
"{lightred}Failed:{reset}".format(**ANSI),
" {reset}{} {red}{}{reset}".format(
result.output.__class__.__name__.replace("ArchiveError", ""),
result.output,
**ANSI,
),
*hints,
"{}Run to see full output:{}".format(ANSI["lightred"], ANSI["reset"]),
*([" cd {};".format(result.pwd)] if result.pwd else []),
" {}".format(quoted_cmd),
]
print("\n".join(" {}".format(line) for line in output_lines if line))
print()
|
https://github.com/ArchiveBox/ArchiveBox/issues/372
|
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
Error in sys.excepthook:
Traceback (most recent call last): [0/6631]
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 32, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 12, in <module>
import os, glob, subprocess, os.path, time, pwd, sys, requests_unixsocket
File "/usr/lib/python3/dist-packages/requests_unixsocket/__init__.py", line 1, in <module>
import requests
File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 154, in <module>
LOG = logging.getLogger('crontab')
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)
Original exception was:
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
|
ImportError
|
def log_list_finished(links):
from .index.csv import links_to_csv
print()
print(
"---------------------------------------------------------------------------------------------------"
)
print(
links_to_csv(
links,
cols=["timestamp", "is_archived", "num_outputs", "url"],
header=True,
ljust=16,
separator=" | ",
)
)
print(
"---------------------------------------------------------------------------------------------------"
)
print()
|
def log_list_finished(links):
print()
print(
"---------------------------------------------------------------------------------------------------"
)
print(
links_to_csv(
links,
cols=["timestamp", "is_archived", "num_outputs", "url"],
header=True,
ljust=16,
separator=" | ",
)
)
print(
"---------------------------------------------------------------------------------------------------"
)
print()
|
https://github.com/ArchiveBox/ArchiveBox/issues/372
|
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
Error in sys.excepthook:
Traceback (most recent call last): [0/6631]
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 32, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 12, in <module>
import os, glob, subprocess, os.path, time, pwd, sys, requests_unixsocket
File "/usr/lib/python3/dist-packages/requests_unixsocket/__init__.py", line 1, in <module>
import requests
File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 154, in <module>
LOG = logging.getLogger('crontab')
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)
Original exception was:
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
|
ImportError
|
def log_removal_started(links: List["Link"], yes: bool, delete: bool):
print(
"{lightyellow}[i] Found {} matching URLs to remove.{reset}".format(
len(links), **ANSI
)
)
if delete:
file_counts = [
link.num_outputs for link in links if os.path.exists(link.link_dir)
]
print(
f" {len(links)} Links will be de-listed from the main index, and their archived content folders will be deleted from disk.\n"
f" ({len(file_counts)} data folders with {sum(file_counts)} archived files will be deleted!)"
)
else:
print(
f" Matching links will be de-listed from the main index, but their archived content folders will remain in place on disk.\n"
f" (Pass --delete if you also want to permanently delete the data folders)"
)
if not yes:
print()
print(
"{lightyellow}[?] Do you want to proceed with removing these {} links?{reset}".format(
len(links), **ANSI
)
)
try:
assert input(" y/[n]: ").lower() == "y"
except (KeyboardInterrupt, EOFError, AssertionError):
raise SystemExit(0)
|
def log_removal_started(links: List[Link], yes: bool, delete: bool):
print(
"{lightyellow}[i] Found {} matching URLs to remove.{reset}".format(
len(links), **ANSI
)
)
if delete:
file_counts = [
link.num_outputs for link in links if os.path.exists(link.link_dir)
]
print(
f" {len(links)} Links will be de-listed from the main index, and their archived content folders will be deleted from disk.\n"
f" ({len(file_counts)} data folders with {sum(file_counts)} archived files will be deleted!)"
)
else:
print(
f" Matching links will be de-listed from the main index, but their archived content folders will remain in place on disk.\n"
f" (Pass --delete if you also want to permanently delete the data folders)"
)
if not yes:
print()
print(
"{lightyellow}[?] Do you want to proceed with removing these {} links?{reset}".format(
len(links), **ANSI
)
)
try:
assert input(" y/[n]: ").lower() == "y"
except (KeyboardInterrupt, EOFError, AssertionError):
raise SystemExit(0)
|
https://github.com/ArchiveBox/ArchiveBox/issues/372
|
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
Error in sys.excepthook:
Traceback (most recent call last): [0/6631]
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 32, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 12, in <module>
import os, glob, subprocess, os.path, time, pwd, sys, requests_unixsocket
File "/usr/lib/python3/dist-packages/requests_unixsocket/__init__.py", line 1, in <module>
import requests
File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 154, in <module>
LOG = logging.getLogger('crontab')
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)
Original exception was:
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
|
ImportError
|
def log_shell_welcome_msg():
from .cli import list_subcommands
print("{green}# ArchiveBox Imports{reset}".format(**ANSI))
print(
"{green}from archivebox.core.models import Snapshot, User{reset}".format(**ANSI)
)
print(
"{green}from archivebox import *\n {}{reset}".format(
"\n ".join(list_subcommands().keys()), **ANSI
)
)
print()
print("[i] Welcome to the ArchiveBox Shell!")
print(" https://github.com/pirate/ArchiveBox/wiki/Usage#Shell-Usage")
print()
print(" {lightred}Hint:{reset} Example use:".format(**ANSI))
print(" print(Snapshot.objects.filter(is_archived=True).count())")
print(' Snapshot.objects.get(url="https://example.com").as_json()')
print(' add("https://example.com/some/new/url")')
|
def log_shell_welcome_msg():
from . import list_subcommands
print("{green}# ArchiveBox Imports{reset}".format(**ANSI))
print(
"{green}from archivebox.core.models import Snapshot, User{reset}".format(**ANSI)
)
print(
"{green}from archivebox import *\n {}{reset}".format(
"\n ".join(list_subcommands().keys()), **ANSI
)
)
print()
print("[i] Welcome to the ArchiveBox Shell!")
print(" https://github.com/pirate/ArchiveBox/wiki/Usage#Shell-Usage")
print()
print(" {lightred}Hint:{reset} Example use:".format(**ANSI))
print(" print(Snapshot.objects.filter(is_archived=True).count())")
print(' Snapshot.objects.get(url="https://example.com").as_json()')
print(' add("https://example.com/some/new/url")')
|
https://github.com/ArchiveBox/ArchiveBox/issues/372
|
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
Error in sys.excepthook:
Traceback (most recent call last): [0/6631]
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 32, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 12, in <module>
import os, glob, subprocess, os.path, time, pwd, sys, requests_unixsocket
File "/usr/lib/python3/dist-packages/requests_unixsocket/__init__.py", line 1, in <module>
import requests
File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 154, in <module>
LOG = logging.getLogger('crontab')
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)
Original exception was:
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
|
ImportError
|
def printable_folders(
folders: Dict[str, Optional["Link"]], json: bool = False, csv: Optional[str] = None
) -> str:
if json:
from .index.json import to_json
return to_json(folders.values(), indent=4, sort_keys=True)
elif csv:
from .index.csv import links_to_csv
return links_to_csv(folders.values(), cols=csv.split(","), header=True)
return "\n".join(f"{folder} {link}" for folder, link in folders.items())
|
def printable_folders(
folders: Dict[str, Optional[Link]], json: bool = False, csv: Optional[str] = None
) -> str:
if json:
return to_json(folders.values(), indent=4, sort_keys=True)
elif csv:
return links_to_csv(folders.values(), cols=csv.split(","), header=True)
return "\n".join(f"{folder} {link}" for folder, link in folders.items())
|
https://github.com/ArchiveBox/ArchiveBox/issues/372
|
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
Error in sys.excepthook:
Traceback (most recent call last): [0/6631]
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 32, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 12, in <module>
import os, glob, subprocess, os.path, time, pwd, sys, requests_unixsocket
File "/usr/lib/python3/dist-packages/requests_unixsocket/__init__.py", line 1, in <module>
import requests
File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 154, in <module>
LOG = logging.getLogger('crontab')
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)
Original exception was:
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
|
ImportError
|
def main(
args: Optional[List[str]] = NotProvided,
stdin: Optional[IO] = NotProvided,
pwd: Optional[str] = None,
) -> None:
args = sys.argv[1:] if args is NotProvided else args
stdin = sys.stdin if stdin is NotProvided else stdin
subcommands = list_subcommands()
parser = argparse.ArgumentParser(
prog=__command__,
description="ArchiveBox: The self-hosted internet archive",
add_help=False,
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--help",
"-h",
action="store_true",
help=subcommands["help"],
)
group.add_argument(
"--version",
action="store_true",
help=subcommands["version"],
)
group.add_argument(
"subcommand",
type=str,
help="The name of the subcommand to run",
nargs="?",
choices=subcommands.keys(),
default=None,
)
parser.add_argument(
"subcommand_args",
help="Arguments for the subcommand",
nargs=argparse.REMAINDER,
)
command = parser.parse_args(args or ())
if command.help or command.subcommand is None:
command.subcommand = "help"
elif command.version:
command.subcommand = "version"
if command.subcommand not in ("help", "version", "status"):
from ..logging_util import log_cli_command
log_cli_command(
subcommand=command.subcommand,
subcommand_args=command.subcommand_args,
stdin=stdin,
pwd=pwd or OUTPUT_DIR,
)
run_subcommand(
subcommand=command.subcommand,
subcommand_args=command.subcommand_args,
stdin=stdin,
pwd=pwd or OUTPUT_DIR,
)
|
def main(
args: Optional[List[str]] = NotProvided,
stdin: Optional[IO] = NotProvided,
pwd: Optional[str] = None,
) -> None:
args = sys.argv[1:] if args is NotProvided else args
stdin = sys.stdin if stdin is NotProvided else stdin
subcommands = list_subcommands()
parser = argparse.ArgumentParser(
prog=__command__,
description="ArchiveBox: The self-hosted internet archive",
add_help=False,
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--help",
"-h",
action="store_true",
help=subcommands["help"],
)
group.add_argument(
"--version",
action="store_true",
help=subcommands["version"],
)
group.add_argument(
"subcommand",
type=str,
help="The name of the subcommand to run",
nargs="?",
choices=subcommands.keys(),
default=None,
)
parser.add_argument(
"subcommand_args",
help="Arguments for the subcommand",
nargs=argparse.REMAINDER,
)
command = parser.parse_args(args or ())
if command.help or command.subcommand is None:
command.subcommand = "help"
elif command.version:
command.subcommand = "version"
if command.subcommand not in ("help", "version", "status"):
from ..logging import log_cli_command
log_cli_command(
subcommand=command.subcommand,
subcommand_args=command.subcommand_args,
stdin=stdin,
pwd=pwd or OUTPUT_DIR,
)
run_subcommand(
subcommand=command.subcommand,
subcommand_args=command.subcommand_args,
stdin=stdin,
pwd=pwd or OUTPUT_DIR,
)
|
https://github.com/ArchiveBox/ArchiveBox/issues/372
|
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
Error in sys.excepthook:
Traceback (most recent call last): [0/6631]
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 32, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 12, in <module>
import os, glob, subprocess, os.path, time, pwd, sys, requests_unixsocket
File "/usr/lib/python3/dist-packages/requests_unixsocket/__init__.py", line 1, in <module>
import requests
File "/usr/lib/python3/dist-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/usr/lib/python3/dist-packages/urllib3/__init__.py", line 7, in <module>
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool, connection_from_url
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 154, in <module>
LOG = logging.getLogger('crontab')
AttributeError: partially initialized module 'logging' has no attribute 'getLogger' (most likely due to a circular import)
Original exception was:
Traceback (most recent call last):
File "/home/kangus/src/ArchiveBox/bin/archivebox", line 7, in <module>
from .cli import main
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 65, in <module>
SUBCOMMANDS = list_subcommands()
File "/home/kangus/src/ArchiveBox/archivebox/cli/__init__.py", line 41, in list_subcommands
module = import_module('.archivebox_{}'.format(subcommand), __package__)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/kangus/src/ArchiveBox/archivebox/cli/archivebox_init.py", line 11, in <module>
from ..main import init, docstring
File "/home/kangus/src/ArchiveBox/archivebox/main.py", line 8, in <module>
from crontab import CronTab, CronSlices
File "/usr/local/lib/python3.8/dist-packages/crontab.py", line 91, in <module>
import logging
File "/home/kangus/src/ArchiveBox/archivebox/logging.py", line 16, in <module>
from .index.schema import Link, ArchiveResult
File "/home/kangus/src/ArchiveBox/archivebox/index/__init__.py", line 13, in <module>
from ..system import atomic_write
File "/home/kangus/src/ArchiveBox/archivebox/system.py", line 12, in <module>
from crontab import CronTab
ImportError: cannot import name 'CronTab' from partially initialized module 'crontab' (most likely due to a circular import) (/usr/local/lib/python3.8/dist-packages/crontab.py)
|
ImportError
|
def from_json(cls, json_info):
from ..util import parse_date
info = {key: val for key, val in json_info.items() if key in cls.field_names()}
info["updated"] = parse_date(info.get("updated"))
info["sources"] = info.get("sources") or []
json_history = info.get("history") or {}
cast_history = {}
for method, method_history in json_history.items():
cast_history[method] = []
for json_result in method_history:
assert isinstance(json_result, dict), (
'Items in Link["history"][method] must be dicts'
)
cast_result = ArchiveResult.from_json(json_result)
cast_history[method].append(cast_result)
info["history"] = cast_history
return cls(**info)
|
def from_json(cls, json_info):
from ..util import parse_date
info = {key: val for key, val in json_info.items() if key in cls.field_names()}
try:
info["updated"] = int(
parse_date(info.get("updated"))
) # Cast to int which comes with rounding down
except (ValueError, TypeError):
info["updated"] = None
info["sources"] = info.get("sources") or []
json_history = info.get("history") or {}
cast_history = {}
for method, method_history in json_history.items():
cast_history[method] = []
for json_result in method_history:
assert isinstance(json_result, dict), (
'Items in Link["history"][method] must be dicts'
)
cast_result = ArchiveResult.from_json(json_result)
cast_history[method].append(cast_result)
info["history"] = cast_history
return cls(**info)
|
https://github.com/ArchiveBox/ArchiveBox/issues/336
|
/home/kangus/src/archivebox0.4/ArchiveBox/bin/archivebox init
[*] Updating existing ArchiveBox collection in this folder...
/data/Zalohy/archivebox
------------------------------------------------------------------
[*] Verifying archive folder structure...
√ /data/Zalohy/archivebox/sources
√ /data/Zalohy/archivebox/archive
√ /data/Zalohy/archivebox/logs
√ /data/Zalohy/archivebox/ArchiveBox.conf
[*] Verifying main SQL index and running migrations...
√ /data/Zalohy/archivebox/index.sqlite3
Operations to perform:
Apply all migrations: admin, auth, contenttypes, core, sessions
Running migrations:
No migrations to apply.
[*] Collecting links from any existing indexes and archive folders...
√ Loaded 28875 links from existing main index.
Traceback (most recent call last):
File "/home/kangus/.local/share/virtualenvs/archivebox0.4/lib/python3.8/site-packages/dateutil/parser/_parser.py", line 655, in parse
ret = self._build_naive(res, default)
File "/home/kangus/.local/share/virtualenvs/archivebox0.4/lib/python3.8/site-packages/dateutil/parser/_parser.py", line 1241, in _build_naive
naive = default.replace(**repl)
ValueError: year 1586476777 is out of range
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/kangus/src/archivebox0.4/ArchiveBox/bin/archivebox", line 14, in <module>
archivebox.main(args=sys.argv[1:], stdin=sys.stdin)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/cli/archivebox.py", line 54, in main
run_subcommand(
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/cli/__init__.py", line 55, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/cli/archivebox_init.py", line 32, in main
init(
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/util.py", line 105, in typechecked_function
return func(*args, **kwargs)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/main.py", line 321, in init
fixed, cant_fix = fix_invalid_folder_locations(out_dir=out_dir)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/index/__init__.py", line 572, in fix_invalid_folder_locations
link = parse_json_link_details(entry.path)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/util.py", line 105, in typechecked_function
return func(*args, **kwargs)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/index/json.py", line 100, in parse_json_link_details
return Link.from_json(link_json)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/index/schema.py", line 190, in from_json
info['updated'] = parse_date(info.get('updated'))
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/util.py", line 105, in typechecked_function
return func(*args, **kwargs)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/util.py", line 144, in parse_date
return dateparser.parse(date)
File "/home/kangus/.local/share/virtualenvs/archivebox0.4/lib/python3.8/site-packages/dateutil/parser/_parser.py", line 1374, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/home/kangus/.local/share/virtualenvs/archivebox0.4/lib/python3.8/site-packages/dateutil/parser/_parser.py", line 657, in parse
six.raise_from(ParserError(e.args[0] + ": %s", timestr), e)
File "<string>", line 3, in raise_from
dateutil.parser._parser.ParserError: year 1586476777 is out of range: 1586476777.093312
|
ValueError
|
def parse_date(date: Any) -> Optional[datetime]:
"""Parse unix timestamps, iso format, and human-readable strings"""
if date is None:
return None
if isinstance(date, datetime):
return date
if isinstance(date, (float, int)):
date = str(date)
if isinstance(date, str):
return dateparser(date)
raise ValueError("Tried to parse invalid date! {}".format(date))
|
def parse_date(date: Any) -> Optional[datetime]:
"""Parse unix timestamps, iso format, and human-readable strings"""
if date is None:
return None
if isinstance(date, datetime):
return date
if isinstance(date, (float, int)):
date = str(date)
if isinstance(date, str):
return dateparser.parse(date)
raise ValueError("Tried to parse invalid date! {}".format(date))
|
https://github.com/ArchiveBox/ArchiveBox/issues/336
|
/home/kangus/src/archivebox0.4/ArchiveBox/bin/archivebox init
[*] Updating existing ArchiveBox collection in this folder...
/data/Zalohy/archivebox
------------------------------------------------------------------
[*] Verifying archive folder structure...
√ /data/Zalohy/archivebox/sources
√ /data/Zalohy/archivebox/archive
√ /data/Zalohy/archivebox/logs
√ /data/Zalohy/archivebox/ArchiveBox.conf
[*] Verifying main SQL index and running migrations...
√ /data/Zalohy/archivebox/index.sqlite3
Operations to perform:
Apply all migrations: admin, auth, contenttypes, core, sessions
Running migrations:
No migrations to apply.
[*] Collecting links from any existing indexes and archive folders...
√ Loaded 28875 links from existing main index.
Traceback (most recent call last):
File "/home/kangus/.local/share/virtualenvs/archivebox0.4/lib/python3.8/site-packages/dateutil/parser/_parser.py", line 655, in parse
ret = self._build_naive(res, default)
File "/home/kangus/.local/share/virtualenvs/archivebox0.4/lib/python3.8/site-packages/dateutil/parser/_parser.py", line 1241, in _build_naive
naive = default.replace(**repl)
ValueError: year 1586476777 is out of range
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/kangus/src/archivebox0.4/ArchiveBox/bin/archivebox", line 14, in <module>
archivebox.main(args=sys.argv[1:], stdin=sys.stdin)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/cli/archivebox.py", line 54, in main
run_subcommand(
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/cli/__init__.py", line 55, in run_subcommand
module.main(args=subcommand_args, stdin=stdin, pwd=pwd) # type: ignore
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/cli/archivebox_init.py", line 32, in main
init(
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/util.py", line 105, in typechecked_function
return func(*args, **kwargs)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/main.py", line 321, in init
fixed, cant_fix = fix_invalid_folder_locations(out_dir=out_dir)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/index/__init__.py", line 572, in fix_invalid_folder_locations
link = parse_json_link_details(entry.path)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/util.py", line 105, in typechecked_function
return func(*args, **kwargs)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/index/json.py", line 100, in parse_json_link_details
return Link.from_json(link_json)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/index/schema.py", line 190, in from_json
info['updated'] = parse_date(info.get('updated'))
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/util.py", line 105, in typechecked_function
return func(*args, **kwargs)
File "/home/kangus/src/archivebox0.4/ArchiveBox/archivebox/util.py", line 144, in parse_date
return dateparser.parse(date)
File "/home/kangus/.local/share/virtualenvs/archivebox0.4/lib/python3.8/site-packages/dateutil/parser/_parser.py", line 1374, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/home/kangus/.local/share/virtualenvs/archivebox0.4/lib/python3.8/site-packages/dateutil/parser/_parser.py", line 657, in parse
six.raise_from(ParserError(e.args[0] + ": %s", timestr), e)
File "<string>", line 3, in raise_from
dateutil.parser._parser.ParserError: year 1586476777 is out of range: 1586476777.093312
|
ValueError
|
def clip(
ctx,
files,
output,
bounds,
like,
driver,
projection,
overwrite,
creation_options,
with_complement,
):
"""Clips a raster using projected or geographic bounds.
\b
$ rio clip input.tif output.tif --bounds xmin ymin xmax ymax
$ rio clip input.tif output.tif --like template.tif
The values of --bounds are presumed to be from the coordinate
reference system of the input dataset unless the --geographic option
is used, in which case the values may be longitude and latitude
bounds. Either JSON, for example "[west, south, east, north]", or
plain text "west south east north" representations of a bounding box
are acceptable.
If using --like, bounds will automatically be transformed to match the
coordinate reference system of the input.
It can also be combined to read bounds of a feature dataset using Fiona:
\b
$ rio clip input.tif output.tif --bounds $(fio info features.shp --bounds)
"""
from rasterio.warp import transform_bounds
with ctx.obj["env"]:
output, files = resolve_inout(files=files, output=output, overwrite=overwrite)
input = files[0]
with rasterio.open(input) as src:
rotated = src.transform.b != 0 or src.transform.d != 0
if rotated:
raise click.BadParameter("rotated raster cannot be clipped")
if bounds:
if projection == "geographic":
bounds = transform_bounds(CRS.from_epsg(4326), src.crs, *bounds)
if disjoint_bounds(bounds, src.bounds):
raise click.BadParameter(
"must overlap the extent of the input raster",
param="--bounds",
param_hint="--bounds",
)
elif like:
with rasterio.open(like) as template_ds:
bounds = template_ds.bounds
if template_ds.crs != src.crs:
bounds = transform_bounds(template_ds.crs, src.crs, *bounds)
if disjoint_bounds(bounds, src.bounds):
raise click.BadParameter(
"must overlap the extent of the input raster",
param="--like",
param_hint="--like",
)
else:
raise click.UsageError("--bounds or --like required")
bounds_window = src.window(*bounds)
if not with_complement:
bounds_window = bounds_window.intersection(
Window(0, 0, src.width, src.height)
)
# Get the window with integer height
# and width that contains the bounds window.
out_window = bounds_window.round_lengths(op="ceil")
height = int(out_window.height)
width = int(out_window.width)
out_kwargs = src.profile
out_kwargs.pop("driver", None)
if driver:
out_kwargs["driver"] = driver
out_kwargs.update(
{
"height": height,
"width": width,
"transform": src.window_transform(out_window),
}
)
out_kwargs.update(**creation_options)
if "blockxsize" in out_kwargs and int(out_kwargs["blockxsize"]) > width:
del out_kwargs["blockxsize"]
logger.warning(
"Blockxsize removed from creation options to accomodate small output width"
)
if "blockysize" in out_kwargs and int(out_kwargs["blockysize"]) > height:
del out_kwargs["blockysize"]
logger.warning(
"Blockysize removed from creation options to accomodate small output height"
)
with rasterio.open(output, "w", **out_kwargs) as out:
out.write(
src.read(
window=out_window,
out_shape=(src.count, height, width),
boundless=True,
)
)
|
def clip(
ctx,
files,
output,
bounds,
like,
driver,
projection,
overwrite,
creation_options,
with_complement,
):
"""Clips a raster using projected or geographic bounds.
\b
$ rio clip input.tif output.tif --bounds xmin ymin xmax ymax
$ rio clip input.tif output.tif --like template.tif
The values of --bounds are presumed to be from the coordinate
reference system of the input dataset unless the --geographic option
is used, in which case the values may be longitude and latitude
bounds. Either JSON, for example "[west, south, east, north]", or
plain text "west south east north" representations of a bounding box
are acceptable.
If using --like, bounds will automatically be transformed to match the
coordinate reference system of the input.
It can also be combined to read bounds of a feature dataset using Fiona:
\b
$ rio clip input.tif output.tif --bounds $(fio info features.shp --bounds)
"""
from rasterio.warp import transform_bounds
with ctx.obj["env"]:
output, files = resolve_inout(files=files, output=output, overwrite=overwrite)
input = files[0]
with rasterio.open(input) as src:
if bounds:
if projection == "geographic":
bounds = transform_bounds(CRS.from_epsg(4326), src.crs, *bounds)
if disjoint_bounds(bounds, src.bounds):
raise click.BadParameter(
"must overlap the extent of the input raster",
param="--bounds",
param_hint="--bounds",
)
elif like:
with rasterio.open(like) as template_ds:
bounds = template_ds.bounds
if template_ds.crs != src.crs:
bounds = transform_bounds(template_ds.crs, src.crs, *bounds)
if disjoint_bounds(bounds, src.bounds):
raise click.BadParameter(
"must overlap the extent of the input raster",
param="--like",
param_hint="--like",
)
else:
raise click.UsageError("--bounds or --like required")
bounds_window = src.window(*bounds)
if not with_complement:
bounds_window = bounds_window.intersection(
Window(0, 0, src.width, src.height)
)
# Get the window with integer height
# and width that contains the bounds window.
out_window = bounds_window.round_lengths(op="ceil")
height = int(out_window.height)
width = int(out_window.width)
out_kwargs = src.profile
out_kwargs.pop("driver", None)
if driver:
out_kwargs["driver"] = driver
out_kwargs.update(
{
"height": height,
"width": width,
"transform": src.window_transform(out_window),
}
)
out_kwargs.update(**creation_options)
if "blockxsize" in out_kwargs and int(out_kwargs["blockxsize"]) > width:
del out_kwargs["blockxsize"]
logger.warning(
"Blockxsize removed from creation options to accomodate small output width"
)
if "blockysize" in out_kwargs and int(out_kwargs["blockysize"]) > height:
del out_kwargs["blockysize"]
logger.warning(
"Blockysize removed from creation options to accomodate small output height"
)
with rasterio.open(output, "w", **out_kwargs) as out:
out.write(
src.read(
window=out_window,
out_shape=(src.count, height, width),
boundless=True,
)
)
|
https://github.com/mapbox/rasterio/issues/2112
|
$ rio clip rotated.tif output.tif --like bounds.tif
Traceback (most recent call last):
File "/home/denis/test/env/bin/rio", line 8, in <module>
sys.exit(main_group())
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/rio/clip.py", line 126, in clip
Window(0, 0, src.width, src.height)
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 730, in intersection
return intersection([self, other])
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 118, in wrapper
return function(*args[0])
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 216, in intersection
raise WindowError("windows do not intersect")
rasterio.errors.WindowError: windows do not intersect
|
rasterio.errors.WindowError
|
def geometry_window(
dataset,
shapes,
pad_x=0,
pad_y=0,
north_up=None,
rotated=None,
pixel_precision=None,
boundless=False,
):
"""Calculate the window within the raster that fits the bounds of
the geometry plus optional padding. The window is the outermost
pixel indices that contain the geometry (floor of offsets, ceiling
of width and height).
If shapes do not overlap raster, a WindowError is raised.
Parameters
----------
dataset : dataset object opened in 'r' mode
Raster for which the mask will be created.
shapes : iterable over geometries.
A geometry is a GeoJSON-like object or implements the geo
interface. Must be in same coordinate system as dataset.
pad_x : float
Amount of padding (as fraction of raster's x pixel size) to add
to left and right side of bounds.
pad_y : float
Amount of padding (as fraction of raster's y pixel size) to add
to top and bottom of bounds.
north_up : optional
This parameter is ignored since version 1.2.1. A deprecation
warning will be emitted in 1.3.0.
rotated : optional
This parameter is ignored since version 1.2.1. A deprecation
warning will be emitted in 1.3.0.
pixel_precision : int or float, optional
Number of places of rounding precision or absolute precision for
evaluating bounds of shapes.
boundless : bool, optional
Whether to allow a boundless window or not.
Returns
-------
rasterio.windows.Window
"""
if pad_x:
pad_x = abs(pad_x * dataset.res[0])
if pad_y:
pad_y = abs(pad_y * dataset.res[1])
all_bounds = [bounds(shape) for shape in shapes]
xs = [
x
for (left, bottom, right, top) in all_bounds
for x in (left - pad_x, right + pad_x, right + pad_x, left - pad_x)
]
ys = [
y
for (left, bottom, right, top) in all_bounds
for y in (top + pad_y, top + pad_y, bottom - pad_x, bottom - pad_x)
]
rows1, cols1 = rowcol(
dataset.transform, xs, ys, op=math.floor, precision=pixel_precision
)
if isinstance(rows1, (int, float)):
rows1 = [rows1]
if isinstance(cols1, (int, float)):
cols1 = [cols1]
rows2, cols2 = rowcol(
dataset.transform, xs, ys, op=math.ceil, precision=pixel_precision
)
if isinstance(rows2, (int, float)):
rows2 = [rows2]
if isinstance(cols2, (int, float)):
cols2 = [cols2]
rows = rows1 + rows2
cols = cols1 + cols2
row_start, row_stop = min(rows), max(rows)
col_start, col_stop = min(cols), max(cols)
window = Window(
col_off=col_start,
row_off=row_start,
width=max(col_stop - col_start, 0.0),
height=max(row_stop - row_start, 0.0),
)
# Make sure that window overlaps raster
raster_window = Window(0, 0, dataset.width, dataset.height)
if not boundless:
window = window.intersection(raster_window)
return window
|
def geometry_window(
dataset, shapes, pad_x=0, pad_y=0, north_up=True, rotated=False, pixel_precision=3
):
"""Calculate the window within the raster that fits the bounds of the
geometry plus optional padding. The window is the outermost pixel indices
that contain the geometry (floor of offsets, ceiling of width and height).
If shapes do not overlap raster, a WindowError is raised.
Parameters
----------
dataset: dataset object opened in 'r' mode
Raster for which the mask will be created.
shapes: iterable over geometries.
A geometry is a GeoJSON-like object or implements the geo interface.
Must be in same coordinate system as dataset.
pad_x: float
Amount of padding (as fraction of raster's x pixel size) to add to left
and right side of bounds.
pad_y: float
Amount of padding (as fraction of raster's y pixel size) to add to top
and bottom of bounds.
north_up: bool
If True (default), the origin point of the raster's transform is the
northernmost point and y pixel values are negative.
rotated: bool
If true, some rotation terms exist in the dataset transform (this
requires special attention.)
pixel_precision: int
Number of places of rounding precision for evaluating bounds of shapes.
Returns
-------
window: rasterio.windows.Window instance
"""
if pad_x:
pad_x = abs(pad_x * dataset.res[0])
if pad_y:
pad_y = abs(pad_y * dataset.res[1])
if not rotated:
all_bounds = [bounds(shape, north_up=north_up) for shape in shapes]
lefts, bottoms, rights, tops = zip(*all_bounds)
left = min(lefts) - pad_x
right = max(rights) + pad_x
if north_up:
bottom = min(bottoms) - pad_y
top = max(tops) + pad_y
else:
bottom = max(bottoms) + pad_y
top = min(tops) - pad_y
else:
# get the bounds in the pixel domain by specifying a transform to the bounds function
all_bounds_px = [
bounds(shape, transform=~dataset.transform) for shape in shapes
]
# get left, right, top, and bottom as above
lefts, bottoms, rights, tops = zip(*all_bounds_px)
left = min(lefts) - pad_x
right = max(rights) + pad_x
top = min(tops) - pad_y
bottom = max(bottoms) + pad_y
# do some clamping if there are any values less than zero or greater than dataset shape
left = max(0, left)
top = max(0, top)
right = min(dataset.shape[1], right)
bottom = min(dataset.shape[0], bottom)
# convert the bounds back to the CRS domain
left, top = dataset.transform * (left, top)
right, bottom = dataset.transform * (right, bottom)
window = dataset.window(left, bottom, right, top)
window_floored = window.round_offsets(op="floor", pixel_precision=pixel_precision)
w = math.ceil(window.width + window.col_off - window_floored.col_off)
h = math.ceil(window.height + window.row_off - window_floored.row_off)
window = Window(window_floored.col_off, window_floored.row_off, w, h)
# Make sure that window overlaps raster
raster_window = Window(0, 0, dataset.width, dataset.height)
# This will raise a WindowError if windows do not overlap
window = window.intersection(raster_window)
return window
|
https://github.com/mapbox/rasterio/issues/2112
|
$ rio clip rotated.tif output.tif --like bounds.tif
Traceback (most recent call last):
File "/home/denis/test/env/bin/rio", line 8, in <module>
sys.exit(main_group())
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/rio/clip.py", line 126, in clip
Window(0, 0, src.width, src.height)
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 730, in intersection
return intersection([self, other])
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 118, in wrapper
return function(*args[0])
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 216, in intersection
raise WindowError("windows do not intersect")
rasterio.errors.WindowError: windows do not intersect
|
rasterio.errors.WindowError
|
def raster_geometry_mask(
dataset,
shapes,
all_touched=False,
invert=False,
crop=False,
pad=False,
pad_width=0.5,
):
"""Create a mask from shapes, transform, and optional window within original
raster.
By default, mask is intended for use as a numpy mask, where pixels that
overlap shapes are False.
If shapes do not overlap the raster and crop=True, a ValueError is
raised. Otherwise, a warning is raised, and a completely True mask
is returned (if invert is False).
Parameters
----------
dataset : a dataset object opened in 'r' mode
Raster for which the mask will be created.
shapes : iterable object
The values must be a GeoJSON-like dict or an object that implements
the Python geo interface protocol (such as a Shapely Polygon).
all_touched : bool (opt)
Include a pixel in the mask if it touches any of the shapes.
If False (default), include a pixel only if its center is within one of
the shapes, or if it is selected by Bresenham's line algorithm.
invert : bool (opt)
If False (default), mask will be `False` inside shapes and `True`
outside. If True, mask will be `True` inside shapes and `False`
outside.
crop : bool (opt)
Whether to crop the dataset to the extent of the shapes. Defaults to
False.
pad : bool (opt)
If True, the features will be padded in each direction by
one half of a pixel prior to cropping dataset. Defaults to False.
pad_width : float (opt)
If pad is set (to maintain back-compatibility), then this will be the
pixel-size width of the padding around the mask.
Returns
-------
tuple
Three elements:
mask : numpy ndarray of type 'bool'
Mask that is `True` outside shapes, and `False` within shapes.
out_transform : affine.Affine()
Information for mapping pixel coordinates in `masked` to another
coordinate system.
window: rasterio.windows.Window instance
Window within original raster covered by shapes. None if crop
is False.
"""
if crop and invert:
raise ValueError("crop and invert cannot both be True.")
if crop and pad:
pad_x = pad_width
pad_y = pad_width
else:
pad_x = 0
pad_y = 0
try:
window = geometry_window(dataset, shapes, pad_x=pad_x, pad_y=pad_y)
except WindowError:
# If shapes do not overlap raster, raise Exception or UserWarning
# depending on value of crop
if crop:
raise ValueError("Input shapes do not overlap raster.")
else:
warnings.warn(
"shapes are outside bounds of raster. "
"Are they in different coordinate reference systems?"
)
# Return an entirely True mask (if invert is False)
mask = numpy.ones(shape=dataset.shape[-2:], dtype="bool") * (not invert)
return mask, dataset.transform, None
if crop:
transform = dataset.window_transform(window)
out_shape = (int(window.height), int(window.width))
else:
window = None
transform = dataset.transform
out_shape = (int(dataset.height), int(dataset.width))
mask = geometry_mask(
shapes,
transform=transform,
invert=invert,
out_shape=out_shape,
all_touched=all_touched,
)
return mask, transform, window
|
def raster_geometry_mask(
dataset,
shapes,
all_touched=False,
invert=False,
crop=False,
pad=False,
pad_width=0.5,
):
"""Create a mask from shapes, transform, and optional window within original
raster.
By default, mask is intended for use as a numpy mask, where pixels that
overlap shapes are False.
If shapes do not overlap the raster and crop=True, a ValueError is
raised. Otherwise, a warning is raised, and a completely True mask
is returned (if invert is False).
Parameters
----------
dataset : a dataset object opened in 'r' mode
Raster for which the mask will be created.
shapes : iterable object
The values must be a GeoJSON-like dict or an object that implements
the Python geo interface protocol (such as a Shapely Polygon).
all_touched : bool (opt)
Include a pixel in the mask if it touches any of the shapes.
If False (default), include a pixel only if its center is within one of
the shapes, or if it is selected by Bresenham's line algorithm.
invert : bool (opt)
If False (default), mask will be `False` inside shapes and `True`
outside. If True, mask will be `True` inside shapes and `False`
outside.
crop : bool (opt)
Whether to crop the dataset to the extent of the shapes. Defaults to
False.
pad : bool (opt)
If True, the features will be padded in each direction by
one half of a pixel prior to cropping dataset. Defaults to False.
pad_width : float (opt)
If pad is set (to maintain back-compatibility), then this will be the
pixel-size width of the padding around the mask.
Returns
-------
tuple
Three elements:
mask : numpy ndarray of type 'bool'
Mask that is `True` outside shapes, and `False` within shapes.
out_transform : affine.Affine()
Information for mapping pixel coordinates in `masked` to another
coordinate system.
window: rasterio.windows.Window instance
Window within original raster covered by shapes. None if crop
is False.
"""
if crop and invert:
raise ValueError("crop and invert cannot both be True.")
if crop and pad:
pad_x = pad_width
pad_y = pad_width
else:
pad_x = 0
pad_y = 0
north_up = dataset.transform.e <= 0
rotated = dataset.transform.b != 0 or dataset.transform.d != 0
try:
window = geometry_window(
dataset,
shapes,
north_up=north_up,
rotated=rotated,
pad_x=pad_x,
pad_y=pad_y,
)
except WindowError:
# If shapes do not overlap raster, raise Exception or UserWarning
# depending on value of crop
if crop:
raise ValueError("Input shapes do not overlap raster.")
else:
warnings.warn(
"shapes are outside bounds of raster. "
"Are they in different coordinate reference systems?"
)
# Return an entirely True mask (if invert is False)
mask = np.ones(shape=dataset.shape[-2:], dtype="bool") * (not invert)
return mask, dataset.transform, None
if crop:
transform = dataset.window_transform(window)
out_shape = (int(window.height), int(window.width))
else:
window = None
transform = dataset.transform
out_shape = (int(dataset.height), int(dataset.width))
mask = geometry_mask(
shapes,
transform=transform,
invert=invert,
out_shape=out_shape,
all_touched=all_touched,
)
return mask, transform, window
|
https://github.com/mapbox/rasterio/issues/2112
|
$ rio clip rotated.tif output.tif --like bounds.tif
Traceback (most recent call last):
File "/home/denis/test/env/bin/rio", line 8, in <module>
sys.exit(main_group())
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/rio/clip.py", line 126, in clip
Window(0, 0, src.width, src.height)
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 730, in intersection
return intersection([self, other])
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 118, in wrapper
return function(*args[0])
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 216, in intersection
raise WindowError("windows do not intersect")
rasterio.errors.WindowError: windows do not intersect
|
rasterio.errors.WindowError
|
def rowcol(transform, xs, ys, op=math.floor, precision=None):
"""
Returns the rows and cols of the pixels containing (x, y) given a
coordinate reference system.
Use an epsilon, magnitude determined by the precision parameter
and sign determined by the op function:
positive for floor, negative for ceil.
Parameters
----------
transform : Affine
Coefficients mapping pixel coordinates to coordinate reference system.
xs : list or float
x values in coordinate reference system
ys : list or float
y values in coordinate reference system
op : function
Function to convert fractional pixels to whole numbers (floor, ceiling,
round)
precision : int or float, optional
An integer number of decimal points of precision when computing
inverse transform, or an absolute float precision.
Returns
-------
rows : list of ints
list of row indices
cols : list of ints
list of column indices
"""
if not isinstance(xs, Iterable):
xs = [xs]
if not isinstance(ys, Iterable):
ys = [ys]
if precision is None:
eps = sys.float_info.epsilon
elif isinstance(precision, int):
eps = 10.0**-precision
else:
eps = precision
# If op rounds up, switch the sign of eps.
if op(0.1) >= 1:
eps = -eps
invtransform = ~transform
rows = []
cols = []
for x, y in zip(xs, ys):
fcol, frow = invtransform * (x + eps, y + eps)
cols.append(op(fcol))
rows.append(op(frow))
if len(cols) == 1:
# rows and cols will always have the same length
return rows[0], cols[0]
return rows, cols
|
def rowcol(transform, xs, ys, op=math.floor, precision=None):
"""
Returns the rows and cols of the pixels containing (x, y) given a
coordinate reference system.
Use an epsilon, magnitude determined by the precision parameter
and sign determined by the op function:
positive for floor, negative for ceil.
Parameters
----------
transform : Affine
Coefficients mapping pixel coordinates to coordinate reference system.
xs : list or float
x values in coordinate reference system
ys : list or float
y values in coordinate reference system
op : function
Function to convert fractional pixels to whole numbers (floor, ceiling,
round)
precision : int or float, optional
An integer number of decimal points of precision when computing
inverse transform, or an absolute float precision.
Returns
-------
rows : list of ints
list of row indices
cols : list of ints
list of column indices
"""
if not isinstance(xs, Iterable):
xs = [xs]
if not isinstance(ys, Iterable):
ys = [ys]
if precision is None:
eps = sys.float_info.epsilon
elif isinstance(precision, int):
eps = 10.0**-precision
else:
eps = precision
# If op rounds up, switch the sign of eps.
if op(0.1) >= 1:
eps = -eps
invtransform = ~transform
rows = []
cols = []
for x, y in zip(xs, ys):
fcol, frow = invtransform * (x + eps, y - eps)
cols.append(op(fcol))
rows.append(op(frow))
if len(cols) == 1:
# rows and cols will always have the same length
return rows[0], cols[0]
return rows, cols
|
https://github.com/mapbox/rasterio/issues/2112
|
$ rio clip rotated.tif output.tif --like bounds.tif
Traceback (most recent call last):
File "/home/denis/test/env/bin/rio", line 8, in <module>
sys.exit(main_group())
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/rio/clip.py", line 126, in clip
Window(0, 0, src.width, src.height)
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 730, in intersection
return intersection([self, other])
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 118, in wrapper
return function(*args[0])
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 216, in intersection
raise WindowError("windows do not intersect")
rasterio.errors.WindowError: windows do not intersect
|
rasterio.errors.WindowError
|
def from_bounds(
left, bottom, right, top, transform=None, height=None, width=None, precision=None
):
"""Get the window corresponding to the bounding coordinates.
Parameters
----------
left: float, required
Left (west) bounding coordinates
bottom: float, required
Bottom (south) bounding coordinates
right: float, required
Right (east) bounding coordinates
top: float, required
Top (north) bounding coordinates
transform: Affine, required
Affine transform matrix.
height: int, required
Number of rows of the window.
width: int, required
Number of columns of the window.
precision: int or float, optional
An integer number of decimal points of precision when computing
inverse transform, or an absolute float precision.
Returns
-------
Window
A new Window.
Raises
------
WindowError
If a window can't be calculated.
"""
if not isinstance(transform, Affine): # TODO: RPCs?
raise WindowError("A transform object is required to calculate the window")
rows, cols = rowcol(
transform,
[left, right, right, left],
[top, top, bottom, bottom],
op=float,
precision=precision,
)
row_start, row_stop = min(rows), max(rows)
col_start, col_stop = min(cols), max(cols)
return Window(
col_off=col_start,
row_off=row_start,
width=max(col_stop - col_start, 0.0),
height=max(row_stop - row_start, 0.0),
)
|
def from_bounds(
left, bottom, right, top, transform=None, height=None, width=None, precision=None
):
"""Get the window corresponding to the bounding coordinates.
Parameters
----------
left: float, required
Left (west) bounding coordinates
bottom: float, required
Bottom (south) bounding coordinates
right: float, required
Right (east) bounding coordinates
top: float, required
Top (north) bounding coordinates
transform: Affine, required
Affine transform matrix.
height: int, required
Number of rows of the window.
width: int, required
Number of columns of the window.
precision: int or float, optional
An integer number of decimal points of precision when computing
inverse transform, or an absolute float precision.
Returns
-------
Window
A new Window.
Raises
------
WindowError
If a window can't be calculated.
"""
if not isinstance(transform, Affine): # TODO: RPCs?
raise WindowError("A transform object is required to calculate the window")
row_start, col_start = rowcol(transform, left, top, op=float, precision=precision)
row_stop, col_stop = rowcol(transform, right, bottom, op=float, precision=precision)
return Window.from_slices(
(row_start, row_stop),
(col_start, col_stop),
height=height,
width=width,
boundless=True,
)
|
https://github.com/mapbox/rasterio/issues/2112
|
$ rio clip rotated.tif output.tif --like bounds.tif
Traceback (most recent call last):
File "/home/denis/test/env/bin/rio", line 8, in <module>
sys.exit(main_group())
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/denis/test/env/lib/python3.6/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/rio/clip.py", line 126, in clip
Window(0, 0, src.width, src.height)
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 730, in intersection
return intersection([self, other])
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 118, in wrapper
return function(*args[0])
File "/home/denis/test/env/lib/python3.6/site-packages/rasterio/windows.py", line 216, in intersection
raise WindowError("windows do not intersect")
rasterio.errors.WindowError: windows do not intersect
|
rasterio.errors.WindowError
|
def main_group(
ctx,
verbose,
quiet,
aws_profile,
aws_no_sign_requests,
aws_requester_pays,
gdal_version,
):
"""Rasterio command line interface."""
verbosity = verbose - quiet
configure_logging(verbosity)
ctx.obj = {}
ctx.obj["verbosity"] = verbosity
ctx.obj["aws_profile"] = aws_profile
envopts = {"CPL_DEBUG": (verbosity > 2)}
if aws_profile or aws_no_sign_requests or aws_requester_pays:
ctx.obj["env"] = rasterio.Env(
session=AWSSession(
profile_name=aws_profile,
aws_unsigned=aws_no_sign_requests,
requester_pays=aws_requester_pays,
),
**envopts,
)
else:
ctx.obj["env"] = rasterio.Env(**envopts)
|
def main_group(
ctx,
verbose,
quiet,
aws_profile,
aws_no_sign_requests,
aws_requester_pays,
gdal_version,
):
"""Rasterio command line interface."""
verbosity = verbose - quiet
configure_logging(verbosity)
ctx.obj = {}
ctx.obj["verbosity"] = verbosity
ctx.obj["aws_profile"] = aws_profile
envopts = {"CPL_DEBUG": (verbosity > 2)}
if aws_profile or aws_no_sign_requests:
ctx.obj["env"] = rasterio.Env(
session=AWSSession(
profile_name=aws_profile,
aws_unsigned=aws_no_sign_requests,
requester_pays=aws_requester_pays,
),
**envopts,
)
else:
ctx.obj["env"] = rasterio.Env(**envopts)
|
https://github.com/mapbox/rasterio/issues/2061
|
Traceback (most recent call last):
File "rasterio/_base.pyx", line 216, in rasterio._base.DatasetBase.__init__
File "rasterio/_shim.pyx", line 78, in rasterio._shim.open_dataset
File "rasterio/_err.pyx", line 213, in rasterio._err.exc_wrap_pointer
rasterio._err.CPLE_AWSAccessDeniedError: Access Denied
|
rasterio._err.CPLE_AWSAccessDeniedError
|
def convert(
ctx,
files,
output,
driver,
dtype,
scale_ratio,
scale_offset,
photometric,
overwrite,
creation_options,
):
"""Copy and convert raster datasets to other data types and formats.
Data values may be linearly scaled when copying by using the
--scale-ratio and --scale-offset options. Destination raster values
are calculated as
dst = scale_ratio * src + scale_offset
For example, to scale uint16 data with an actual range of 0-4095 to
0-255 as uint8:
$ rio convert in16.tif out8.tif --dtype uint8 --scale-ratio 0.0625
Format specific creation options may also be passed using --co. To
tile a new GeoTIFF output file, do the following.
--co tiled=true --co blockxsize=256 --co blockysize=256
To compress it using the LZW method, add
--co compress=LZW
"""
with ctx.obj["env"]:
outputfile, files = resolve_inout(
files=files, output=output, overwrite=overwrite, num_inputs=1
)
inputfile = files[0]
with rasterio.open(inputfile) as src:
# Use the input file's profile, updated by CLI
# options, as the profile for the output file.
profile = src.profile
if driver:
profile["driver"] = driver
if dtype:
profile["dtype"] = dtype
dst_dtype = profile["dtype"]
if photometric:
creation_options["photometric"] = photometric
profile.update(**creation_options)
with rasterio.open(outputfile, "w", **profile) as dst:
data = src.read()
if scale_ratio:
# Cast to float64 before multiplying.
data = data.astype("float64", casting="unsafe", copy=False)
np.multiply(data, scale_ratio, out=data, casting="unsafe")
if scale_offset:
# My understanding of copy=False is that this is a
# no-op if the array was cast for multiplication.
data = data.astype("float64", casting="unsafe", copy=False)
np.add(data, scale_offset, out=data, casting="unsafe")
# Cast to the output dtype and write.
result = data.astype(dst_dtype, casting="unsafe", copy=False)
dst.write(result)
|
def convert(
ctx,
files,
output,
driver,
dtype,
scale_ratio,
scale_offset,
photometric,
overwrite,
creation_options,
):
"""Copy and convert raster datasets to other data types and formats.
Data values may be linearly scaled when copying by using the
--scale-ratio and --scale-offset options. Destination raster values
are calculated as
dst = scale_ratio * src + scale_offset
For example, to scale uint16 data with an actual range of 0-4095 to
0-255 as uint8:
$ rio convert in16.tif out8.tif --dtype uint8 --scale-ratio 0.0625
Format specific creation options may also be passed using --co. To
tile a new GeoTIFF output file, do the following.
--co tiled=true --co blockxsize=256 --co blockysize=256
To compress it using the LZW method, add
--co compress=LZW
"""
with ctx.obj["env"]:
outputfile, files = resolve_inout(
files=files, output=output, overwrite=overwrite
)
inputfile = files[0]
with rasterio.open(inputfile) as src:
# Use the input file's profile, updated by CLI
# options, as the profile for the output file.
profile = src.profile
if driver:
profile["driver"] = driver
if dtype:
profile["dtype"] = dtype
dst_dtype = profile["dtype"]
if photometric:
creation_options["photometric"] = photometric
profile.update(**creation_options)
with rasterio.open(outputfile, "w", **profile) as dst:
data = src.read()
if scale_ratio:
# Cast to float64 before multiplying.
data = data.astype("float64", casting="unsafe", copy=False)
np.multiply(data, scale_ratio, out=data, casting="unsafe")
if scale_offset:
# My understanding of copy=False is that this is a
# no-op if the array was cast for multiplication.
data = data.astype("float64", casting="unsafe", copy=False)
np.add(data, scale_offset, out=data, casting="unsafe")
# Cast to the output dtype and write.
result = data.astype(dst_dtype, casting="unsafe", copy=False)
dst.write(result)
|
https://github.com/mapbox/rasterio/issues/1985
|
$ rio convert --overwrite 20200829.tif
Traceback (most recent call last):
File "/usr/local/bin/rio", line 8, in <module>
sys.exit(main_group())
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/rasterio/rio/convert.py", line 55, in convert
inputfile = files[0]
IndexError: list index out of range
|
IndexError
|
def resolve_inout(
input=None, output=None, files=None, overwrite=False, num_inputs=None
):
"""Resolves inputs and outputs from standard args and options.
Parameters
----------
input : str
A single input filename, optional.
output : str
A single output filename, optional.
files : str
A sequence of filenames in which the last is the output filename.
overwrite : bool
Whether to force overwriting the output file.
num_inputs : int
Raise exceptions if the number of resolved input files is higher
or lower than this number.
Returns
-------
tuple (str, list of str)
The resolved output filename and input filenames as a tuple of
length 2.
If provided, the output file may be overwritten. An output
file extracted from files will not be overwritten unless
overwrite is True.
Raises
------
click.BadParameter
"""
resolved_output = output or (files[-1] if files else None)
if not overwrite and resolved_output and os.path.exists(resolved_output):
raise FileOverwriteError(
"file exists and won't be overwritten without use of the `--overwrite` option."
)
resolved_inputs = (
[input]
if input
else [] + list(files[: -1 if not output else None])
if files
else []
)
if num_inputs is not None:
if len(resolved_inputs) < num_inputs:
raise click.BadParameter("Insufficient inputs")
elif len(resolved_inputs) > num_inputs:
raise click.BadParameter("Too many inputs")
return resolved_output, resolved_inputs
|
def resolve_inout(input=None, output=None, files=None, overwrite=False):
"""Resolves inputs and outputs from standard args and options.
:param input: a single input filename, optional.
:param output: a single output filename, optional.
:param files: a sequence of filenames in which the last is the
output filename.
:param overwrite: whether to force overwriting the output
file, bool.
:return: the resolved output filename and input filenames as a
tuple of length 2.
If provided, the :param:`output` file may be overwritten. An output
file extracted from :param:`files` will not be overwritten unless
:param:`overwrite` is `True`.
"""
resolved_output = output or (files[-1] if files else None)
if not overwrite and resolved_output and os.path.exists(resolved_output):
raise FileOverwriteError(
"file exists and won't be overwritten without use of the "
"`--overwrite` option."
)
resolved_inputs = (
[input]
if input
else [] + list(files[: -1 if not output else None])
if files
else []
)
return resolved_output, resolved_inputs
|
https://github.com/mapbox/rasterio/issues/1985
|
$ rio convert --overwrite 20200829.tif
Traceback (most recent call last):
File "/usr/local/bin/rio", line 8, in <module>
sys.exit(main_group())
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/rasterio/rio/convert.py", line 55, in convert
inputfile = files[0]
IndexError: list index out of range
|
IndexError
|
def clip(
ctx,
files,
output,
bounds,
like,
driver,
projection,
overwrite,
creation_options,
with_complement,
):
"""Clips a raster using projected or geographic bounds.
\b
$ rio clip input.tif output.tif --bounds xmin ymin xmax ymax
$ rio clip input.tif output.tif --like template.tif
The values of --bounds are presumed to be from the coordinate
reference system of the input dataset unless the --geographic option
is used, in which case the values may be longitude and latitude
bounds. Either JSON, for example "[west, south, east, north]", or
plain text "west south east north" representations of a bounding box
are acceptable.
If using --like, bounds will automatically be transformed to match the
coordinate reference system of the input.
It can also be combined to read bounds of a feature dataset using Fiona:
\b
$ rio clip input.tif output.tif --bounds $(fio info features.shp --bounds)
"""
from rasterio.warp import transform_bounds
with ctx.obj["env"]:
output, files = resolve_inout(files=files, output=output, overwrite=overwrite)
input = files[0]
with rasterio.open(input) as src:
if bounds:
if projection == "geographic":
bounds = transform_bounds(CRS.from_epsg(4326), src.crs, *bounds)
if disjoint_bounds(bounds, src.bounds):
raise click.BadParameter(
"must overlap the extent of the input raster",
param="--bounds",
param_hint="--bounds",
)
elif like:
with rasterio.open(like) as template_ds:
bounds = template_ds.bounds
if template_ds.crs != src.crs:
bounds = transform_bounds(template_ds.crs, src.crs, *bounds)
if disjoint_bounds(bounds, src.bounds):
raise click.BadParameter(
"must overlap the extent of the input raster",
param="--like",
param_hint="--like",
)
else:
raise click.UsageError("--bounds or --like required")
bounds_window = src.window(*bounds)
if not with_complement:
bounds_window = bounds_window.intersection(
Window(0, 0, src.width, src.height)
)
# Get the window with integer height
# and width that contains the bounds window.
out_window = bounds_window.round_lengths(op="ceil")
height = int(out_window.height)
width = int(out_window.width)
out_kwargs = src.profile
out_kwargs.update(
{
"driver": driver,
"height": height,
"width": width,
"transform": src.window_transform(out_window),
}
)
out_kwargs.update(**creation_options)
if "blockxsize" in out_kwargs and int(out_kwargs["blockxsize"]) > width:
del out_kwargs["blockxsize"]
logger.warning(
"Blockxsize removed from creation options to accomodate small output width"
)
if "blockysize" in out_kwargs and int(out_kwargs["blockysize"]) > height:
del out_kwargs["blockysize"]
logger.warning(
"Blockysize removed from creation options to accomodate small output height"
)
with rasterio.open(output, "w", **out_kwargs) as out:
out.write(
src.read(
window=out_window,
out_shape=(src.count, height, width),
boundless=True,
)
)
|
def clip(
ctx,
files,
output,
bounds,
like,
driver,
projection,
overwrite,
creation_options,
with_complement,
):
"""Clips a raster using projected or geographic bounds.
\b
$ rio clip input.tif output.tif --bounds xmin ymin xmax ymax
$ rio clip input.tif output.tif --like template.tif
The values of --bounds are presumed to be from the coordinate
reference system of the input dataset unless the --geographic option
is used, in which case the values may be longitude and latitude
bounds. Either JSON, for example "[west, south, east, north]", or
plain text "west south east north" representations of a bounding box
are acceptable.
If using --like, bounds will automatically be transformed to match the
coordinate reference system of the input.
It can also be combined to read bounds of a feature dataset using Fiona:
\b
$ rio clip input.tif output.tif --bounds $(fio info features.shp --bounds)
"""
from rasterio.warp import transform_bounds
with ctx.obj["env"]:
output, files = resolve_inout(files=files, output=output, overwrite=overwrite)
input = files[0]
with rasterio.open(input) as src:
if bounds:
if projection == "geographic":
bounds = transform_bounds(CRS.from_epsg(4326), src.crs, *bounds)
if disjoint_bounds(bounds, src.bounds):
raise click.BadParameter(
"must overlap the extent of the input raster",
param="--bounds",
param_hint="--bounds",
)
elif like:
with rasterio.open(like) as template_ds:
bounds = template_ds.bounds
if template_ds.crs != src.crs:
bounds = transform_bounds(template_ds.crs, src.crs, *bounds)
if disjoint_bounds(bounds, src.bounds):
raise click.BadParameter(
"must overlap the extent of the input raster",
param="--like",
param_hint="--like",
)
else:
raise click.UsageError("--bounds or --like required")
bounds_window = src.window(*bounds)
if not with_complement:
bounds_window = bounds_window.intersection(
Window(0, 0, src.width, src.height)
)
# Get the window with integer height
# and width that contains the bounds window.
out_window = bounds_window.round_lengths(op="ceil")
height = int(out_window.height)
width = int(out_window.width)
out_kwargs = src.profile
out_kwargs.update(
{
"driver": driver,
"height": height,
"width": width,
"transform": src.window_transform(out_window),
}
)
out_kwargs.update(**creation_options)
if "blockxsize" in out_kwargs and out_kwargs["blockxsize"] > width:
del out_kwargs["blockxsize"]
logger.warning(
"Blockxsize removed from creation options to accomodate small output width"
)
if "blockysize" in out_kwargs and out_kwargs["blockysize"] > height:
del out_kwargs["blockysize"]
logger.warning(
"Blockysize removed from creation options to accomodate small output height"
)
with rasterio.open(output, "w", **out_kwargs) as out:
out.write(
src.read(
window=out_window,
out_shape=(src.count, height, width),
boundless=True,
)
)
|
https://github.com/mapbox/rasterio/issues/1989
|
Traceback (most recent call last):
File "/home/sentinel/.local/bin/rio", line 8, in <module>
sys.exit(main_group())
File "/home/sentinel/.local/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/sentinel/.local/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/sentinel/.local/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/sentinel/.local/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/sentinel/.local/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/sentinel/.local/lib/python3.8/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/sentinel/.local/lib/python3.8/site-packages/rasterio/rio/clip.py", line 147, in clip
if 'blockxsize' in out_kwargs and out_kwargs['blockxsize'] > width:
TypeError: '>' not supported between instances of 'str' and 'int'
|
TypeError
|
def warp(
ctx,
files,
output,
driver,
like,
dst_crs,
dimensions,
src_bounds,
dst_bounds,
res,
resampling,
src_nodata,
dst_nodata,
threads,
check_invert_proj,
overwrite,
creation_options,
target_aligned_pixels,
):
"""
Warp a raster dataset.
If a template raster is provided using the --like option, the
coordinate reference system, affine transform, and dimensions of
that raster will be used for the output. In this case --dst-crs,
--bounds, --res, and --dimensions options are not applicable and
an exception will be raised.
\b
$ rio warp input.tif output.tif --like template.tif
The output coordinate reference system may be either a PROJ.4 or
EPSG:nnnn string,
\b
--dst-crs EPSG:4326
--dst-crs '+proj=longlat +ellps=WGS84 +datum=WGS84'
or a JSON text-encoded PROJ.4 object.
\b
--dst-crs '{"proj": "utm", "zone": 18, ...}'
If --dimensions are provided, --res and --bounds are not applicable and an
exception will be raised.
Resolution is calculated based on the relationship between the
raster bounds in the target coordinate system and the dimensions,
and may produce rectangular rather than square pixels.
\b
$ rio warp input.tif output.tif --dimensions 100 200 \\
> --dst-crs EPSG:4326
If --bounds are provided, --res is required if --dst-crs is provided
(defaults to source raster resolution otherwise).
\b
$ rio warp input.tif output.tif \\
> --bounds -78 22 -76 24 --res 0.1 --dst-crs EPSG:4326
"""
output, files = resolve_inout(files=files, output=output, overwrite=overwrite)
resampling = Resampling[resampling] # get integer code for method
if not len(res):
# Click sets this as an empty tuple if not provided
res = None
else:
# Expand one value to two if needed
res = (res[0], res[0]) if len(res) == 1 else res
if target_aligned_pixels:
if not res:
raise click.BadParameter(
"--target-aligned-pixels requires a specified resolution"
)
if src_bounds or dst_bounds:
raise click.BadParameter(
"--target-aligned-pixels cannot be used with "
"--src-bounds or --dst-bounds"
)
# Check invalid parameter combinations
if like:
invalid_combos = (dimensions, dst_bounds, dst_crs, res)
if any(p for p in invalid_combos if p is not None):
raise click.BadParameter(
"--like cannot be used with any of --dimensions, --bounds, "
"--dst-crs, or --res"
)
elif dimensions:
invalid_combos = (dst_bounds, res)
if any(p for p in invalid_combos if p is not None):
raise click.BadParameter(
"--dimensions cannot be used with --bounds or --res"
)
with ctx.obj["env"]:
setenv(CHECK_WITH_INVERT_PROJ=check_invert_proj)
with rasterio.open(files[0]) as src:
l, b, r, t = src.bounds
out_kwargs = src.profile
out_kwargs.update(driver=driver)
# Sort out the bounds options.
if src_bounds and dst_bounds:
raise click.BadParameter(
"--src-bounds and destination --bounds may not be "
"specified simultaneously."
)
if like:
with rasterio.open(like) as template_ds:
dst_crs = template_ds.crs
dst_transform = template_ds.transform
dst_height = template_ds.height
dst_width = template_ds.width
elif dst_crs is not None:
try:
dst_crs = CRS.from_string(dst_crs)
except ValueError as err:
raise click.BadParameter(
str(err), param="dst_crs", param_hint="dst_crs"
)
if dimensions:
# Calculate resolution appropriate for dimensions
# in target.
dst_width, dst_height = dimensions
bounds = src_bounds or src.bounds
try:
xmin, ymin, xmax, ymax = transform_bounds(
src.crs, dst_crs, *bounds
)
except CRSError as err:
raise click.BadParameter(
str(err), param="dst_crs", param_hint="dst_crs"
)
dst_transform = Affine(
(xmax - xmin) / float(dst_width),
0,
xmin,
0,
(ymin - ymax) / float(dst_height),
ymax,
)
elif src_bounds or dst_bounds:
if not res:
raise click.BadParameter(
"Required when using --bounds.",
param="res",
param_hint="res",
)
if src_bounds:
try:
xmin, ymin, xmax, ymax = transform_bounds(
src.crs, dst_crs, *src_bounds
)
except CRSError as err:
raise click.BadParameter(
str(err), param="dst_crs", param_hint="dst_crs"
)
else:
xmin, ymin, xmax, ymax = dst_bounds
dst_transform = Affine(res[0], 0, xmin, 0, -res[1], ymax)
dst_width = max(int(ceil((xmax - xmin) / res[0])), 1)
dst_height = max(int(ceil((ymax - ymin) / res[1])), 1)
else:
try:
if src.transform.is_identity and src.gcps:
src_crs = src.gcps[1]
kwargs = {"gcps": src.gcps[0]}
else:
src_crs = src.crs
kwargs = src.bounds._asdict()
dst_transform, dst_width, dst_height = calcdt(
src_crs,
dst_crs,
src.width,
src.height,
resolution=res,
**kwargs,
)
except CRSError as err:
raise click.BadParameter(
str(err), param="dst_crs", param_hint="dst_crs"
)
elif dimensions:
# Same projection, different dimensions, calculate resolution.
dst_crs = src.crs
dst_width, dst_height = dimensions
l, b, r, t = src_bounds or (l, b, r, t)
dst_transform = Affine(
(r - l) / float(dst_width), 0, l, 0, (b - t) / float(dst_height), t
)
elif src_bounds or dst_bounds:
# Same projection, different dimensions and possibly
# different resolution.
if not res:
res = (src.transform.a, -src.transform.e)
dst_crs = src.crs
xmin, ymin, xmax, ymax = src_bounds or dst_bounds
dst_transform = Affine(res[0], 0, xmin, 0, -res[1], ymax)
dst_width = max(int(ceil((xmax - xmin) / res[0])), 1)
dst_height = max(int(ceil((ymax - ymin) / res[1])), 1)
elif res:
# Same projection, different resolution.
dst_crs = src.crs
dst_transform = Affine(res[0], 0, l, 0, -res[1], t)
dst_width = max(int(ceil((r - l) / res[0])), 1)
dst_height = max(int(ceil((t - b) / res[1])), 1)
else:
dst_crs = src.crs
dst_transform = src.transform
dst_width = src.width
dst_height = src.height
if target_aligned_pixels:
dst_transform, dst_width, dst_height = aligned_target(
dst_transform, dst_width, dst_height, res
)
# If src_nodata is not None, update the dst metadata NODATA
# value to src_nodata (will be overridden by dst_nodata if it is not None
if src_nodata is not None:
# Update the dst nodata value
out_kwargs.update(nodata=src_nodata)
# Validate a manually set destination NODATA value
# against the input datatype.
if dst_nodata is not None:
if src_nodata is None and src.meta["nodata"] is None:
raise click.BadParameter(
"--src-nodata must be provided because dst-nodata is not None"
)
else:
# Update the dst nodata value
out_kwargs.update(nodata=dst_nodata)
# When the bounds option is misused, extreme values of
# destination width and height may result.
if (
dst_width < 0
or dst_height < 0
or dst_width > MAX_OUTPUT_WIDTH
or dst_height > MAX_OUTPUT_HEIGHT
):
raise click.BadParameter(
"Invalid output dimensions: {0}.".format((dst_width, dst_height))
)
out_kwargs.update(
crs=dst_crs, transform=dst_transform, width=dst_width, height=dst_height
)
# Adjust block size if necessary.
if "blockxsize" in out_kwargs and dst_width < int(out_kwargs["blockxsize"]):
del out_kwargs["blockxsize"]
logger.warning(
"Blockxsize removed from creation options to accomodate small output width"
)
if "blockysize" in out_kwargs and dst_height < int(
out_kwargs["blockysize"]
):
del out_kwargs["blockysize"]
logger.warning(
"Blockxsize removed from creation options to accomodate small output height"
)
out_kwargs.update(**creation_options)
with rasterio.open(output, "w", **out_kwargs) as dst:
reproject(
source=rasterio.band(src, list(range(1, src.count + 1))),
destination=rasterio.band(dst, list(range(1, src.count + 1))),
src_transform=src.transform,
src_crs=src.crs,
src_nodata=src_nodata,
dst_transform=out_kwargs["transform"],
dst_crs=out_kwargs["crs"],
dst_nodata=dst_nodata,
resampling=resampling,
num_threads=threads,
)
|
def warp(
ctx,
files,
output,
driver,
like,
dst_crs,
dimensions,
src_bounds,
dst_bounds,
res,
resampling,
src_nodata,
dst_nodata,
threads,
check_invert_proj,
overwrite,
creation_options,
target_aligned_pixels,
):
"""
Warp a raster dataset.
If a template raster is provided using the --like option, the
coordinate reference system, affine transform, and dimensions of
that raster will be used for the output. In this case --dst-crs,
--bounds, --res, and --dimensions options are not applicable and
an exception will be raised.
\b
$ rio warp input.tif output.tif --like template.tif
The output coordinate reference system may be either a PROJ.4 or
EPSG:nnnn string,
\b
--dst-crs EPSG:4326
--dst-crs '+proj=longlat +ellps=WGS84 +datum=WGS84'
or a JSON text-encoded PROJ.4 object.
\b
--dst-crs '{"proj": "utm", "zone": 18, ...}'
If --dimensions are provided, --res and --bounds are not applicable and an
exception will be raised.
Resolution is calculated based on the relationship between the
raster bounds in the target coordinate system and the dimensions,
and may produce rectangular rather than square pixels.
\b
$ rio warp input.tif output.tif --dimensions 100 200 \\
> --dst-crs EPSG:4326
If --bounds are provided, --res is required if --dst-crs is provided
(defaults to source raster resolution otherwise).
\b
$ rio warp input.tif output.tif \\
> --bounds -78 22 -76 24 --res 0.1 --dst-crs EPSG:4326
"""
output, files = resolve_inout(files=files, output=output, overwrite=overwrite)
resampling = Resampling[resampling] # get integer code for method
if not len(res):
# Click sets this as an empty tuple if not provided
res = None
else:
# Expand one value to two if needed
res = (res[0], res[0]) if len(res) == 1 else res
if target_aligned_pixels:
if not res:
raise click.BadParameter(
"--target-aligned-pixels requires a specified resolution"
)
if src_bounds or dst_bounds:
raise click.BadParameter(
"--target-aligned-pixels cannot be used with "
"--src-bounds or --dst-bounds"
)
# Check invalid parameter combinations
if like:
invalid_combos = (dimensions, dst_bounds, dst_crs, res)
if any(p for p in invalid_combos if p is not None):
raise click.BadParameter(
"--like cannot be used with any of --dimensions, --bounds, "
"--dst-crs, or --res"
)
elif dimensions:
invalid_combos = (dst_bounds, res)
if any(p for p in invalid_combos if p is not None):
raise click.BadParameter(
"--dimensions cannot be used with --bounds or --res"
)
with ctx.obj["env"]:
setenv(CHECK_WITH_INVERT_PROJ=check_invert_proj)
with rasterio.open(files[0]) as src:
l, b, r, t = src.bounds
out_kwargs = src.profile
out_kwargs.update(driver=driver)
# Sort out the bounds options.
if src_bounds and dst_bounds:
raise click.BadParameter(
"--src-bounds and destination --bounds may not be "
"specified simultaneously."
)
if like:
with rasterio.open(like) as template_ds:
dst_crs = template_ds.crs
dst_transform = template_ds.transform
dst_height = template_ds.height
dst_width = template_ds.width
elif dst_crs is not None:
try:
dst_crs = CRS.from_string(dst_crs)
except ValueError as err:
raise click.BadParameter(
str(err), param="dst_crs", param_hint="dst_crs"
)
if dimensions:
# Calculate resolution appropriate for dimensions
# in target.
dst_width, dst_height = dimensions
bounds = src_bounds or src.bounds
try:
xmin, ymin, xmax, ymax = transform_bounds(
src.crs, dst_crs, *bounds
)
except CRSError as err:
raise click.BadParameter(
str(err), param="dst_crs", param_hint="dst_crs"
)
dst_transform = Affine(
(xmax - xmin) / float(dst_width),
0,
xmin,
0,
(ymin - ymax) / float(dst_height),
ymax,
)
elif src_bounds or dst_bounds:
if not res:
raise click.BadParameter(
"Required when using --bounds.",
param="res",
param_hint="res",
)
if src_bounds:
try:
xmin, ymin, xmax, ymax = transform_bounds(
src.crs, dst_crs, *src_bounds
)
except CRSError as err:
raise click.BadParameter(
str(err), param="dst_crs", param_hint="dst_crs"
)
else:
xmin, ymin, xmax, ymax = dst_bounds
dst_transform = Affine(res[0], 0, xmin, 0, -res[1], ymax)
dst_width = max(int(ceil((xmax - xmin) / res[0])), 1)
dst_height = max(int(ceil((ymax - ymin) / res[1])), 1)
else:
try:
if src.transform.is_identity and src.gcps:
src_crs = src.gcps[1]
kwargs = {"gcps": src.gcps[0]}
else:
src_crs = src.crs
kwargs = src.bounds._asdict()
dst_transform, dst_width, dst_height = calcdt(
src_crs,
dst_crs,
src.width,
src.height,
resolution=res,
**kwargs,
)
except CRSError as err:
raise click.BadParameter(
str(err), param="dst_crs", param_hint="dst_crs"
)
elif dimensions:
# Same projection, different dimensions, calculate resolution.
dst_crs = src.crs
dst_width, dst_height = dimensions
l, b, r, t = src_bounds or (l, b, r, t)
dst_transform = Affine(
(r - l) / float(dst_width), 0, l, 0, (b - t) / float(dst_height), t
)
elif src_bounds or dst_bounds:
# Same projection, different dimensions and possibly
# different resolution.
if not res:
res = (src.transform.a, -src.transform.e)
dst_crs = src.crs
xmin, ymin, xmax, ymax = src_bounds or dst_bounds
dst_transform = Affine(res[0], 0, xmin, 0, -res[1], ymax)
dst_width = max(int(ceil((xmax - xmin) / res[0])), 1)
dst_height = max(int(ceil((ymax - ymin) / res[1])), 1)
elif res:
# Same projection, different resolution.
dst_crs = src.crs
dst_transform = Affine(res[0], 0, l, 0, -res[1], t)
dst_width = max(int(ceil((r - l) / res[0])), 1)
dst_height = max(int(ceil((t - b) / res[1])), 1)
else:
dst_crs = src.crs
dst_transform = src.transform
dst_width = src.width
dst_height = src.height
if target_aligned_pixels:
dst_transform, dst_width, dst_height = aligned_target(
dst_transform, dst_width, dst_height, res
)
# If src_nodata is not None, update the dst metadata NODATA
# value to src_nodata (will be overridden by dst_nodata if it is not None
if src_nodata is not None:
# Update the dst nodata value
out_kwargs.update(nodata=src_nodata)
# Validate a manually set destination NODATA value
# against the input datatype.
if dst_nodata is not None:
if src_nodata is None and src.meta["nodata"] is None:
raise click.BadParameter(
"--src-nodata must be provided because dst-nodata is not None"
)
else:
# Update the dst nodata value
out_kwargs.update(nodata=dst_nodata)
# When the bounds option is misused, extreme values of
# destination width and height may result.
if (
dst_width < 0
or dst_height < 0
or dst_width > MAX_OUTPUT_WIDTH
or dst_height > MAX_OUTPUT_HEIGHT
):
raise click.BadParameter(
"Invalid output dimensions: {0}.".format((dst_width, dst_height))
)
out_kwargs.update(
crs=dst_crs, transform=dst_transform, width=dst_width, height=dst_height
)
# Adjust block size if necessary.
if "blockxsize" in out_kwargs and dst_width < out_kwargs["blockxsize"]:
del out_kwargs["blockxsize"]
if "blockysize" in out_kwargs and dst_height < out_kwargs["blockysize"]:
del out_kwargs["blockysize"]
out_kwargs.update(**creation_options)
with rasterio.open(output, "w", **out_kwargs) as dst:
reproject(
source=rasterio.band(src, list(range(1, src.count + 1))),
destination=rasterio.band(dst, list(range(1, src.count + 1))),
src_transform=src.transform,
src_crs=src.crs,
src_nodata=src_nodata,
dst_transform=out_kwargs["transform"],
dst_crs=out_kwargs["crs"],
dst_nodata=dst_nodata,
resampling=resampling,
num_threads=threads,
)
|
https://github.com/mapbox/rasterio/issues/1989
|
Traceback (most recent call last):
File "/home/sentinel/.local/bin/rio", line 8, in <module>
sys.exit(main_group())
File "/home/sentinel/.local/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/sentinel/.local/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/sentinel/.local/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/sentinel/.local/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/sentinel/.local/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/sentinel/.local/lib/python3.8/site-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/home/sentinel/.local/lib/python3.8/site-packages/rasterio/rio/clip.py", line 147, in clip
if 'blockxsize' in out_kwargs and out_kwargs['blockxsize'] > width:
TypeError: '>' not supported between instances of 'str' and 'int'
|
TypeError
|
def __init__(
self,
session=None,
aws_unsigned=False,
profile_name=None,
session_class=AWSSession,
**options,
):
"""Create a new GDAL/AWS environment.
Note: this class is a context manager. GDAL isn't configured
until the context is entered via `with rasterio.Env():`
Parameters
----------
session : optional
A Session object.
aws_unsigned : bool, optional
Do not sign cloud requests.
profile_name : str, optional
A shared credentials profile name, as per boto3.
session_class : Session, optional
A sub-class of Session.
**options : optional
A mapping of GDAL configuration options, e.g.,
`CPL_DEBUG=True, CHECK_WITH_INVERT_PROJ=False`.
Returns
-------
Env
Notes
-----
We raise EnvError if the GDAL config options
AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY are given. AWS
credentials are handled exclusively by boto3.
Examples
--------
>>> with Env(CPL_DEBUG=True, CPL_CURL_VERBOSE=True):
... with rasterio.open("https://example.com/a.tif") as src:
... print(src.profile)
For access to secured cloud resources, a Rasterio Session or a
foreign session object may be passed to the constructor.
>>> import boto3
>>> from rasterio.session import AWSSession
>>> boto3_session = boto3.Session(...)
>>> with Env(AWSSession(boto3_session)):
... with rasterio.open("s3://mybucket/a.tif") as src:
... print(src.profile)
"""
aws_access_key_id = options.pop("aws_access_key_id", None)
# Before 1.0, Rasterio only supported AWS. We will special
# case AWS in 1.0.x. TODO: warn deprecation in 1.1.
if aws_access_key_id:
warnings.warn(
"Passing abstract session keyword arguments is deprecated. "
"Pass a Rasterio AWSSession object instead.",
RasterioDeprecationWarning,
)
aws_secret_access_key = options.pop("aws_secret_access_key", None)
aws_session_token = options.pop("aws_session_token", None)
region_name = options.pop("region_name", None)
if "AWS_ACCESS_KEY_ID" in options or "AWS_SECRET_ACCESS_KEY" in options:
raise EnvError(
"GDAL's AWS config options can not be directly set. "
"AWS credentials are handled exclusively by boto3."
)
if session:
# Passing a session via keyword argument is the canonical
# way to configure access to secured cloud resources.
if not isinstance(session, Session):
warnings.warn(
"Passing a boto3 session is deprecated. Pass a Rasterio "
"AWSSession object instead.",
RasterioDeprecationWarning,
)
session = AWSSession(session=session)
self.session = session
elif aws_access_key_id or profile_name or aws_unsigned:
self.session = AWSSession(
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token,
region_name=region_name,
profile_name=profile_name,
aws_unsigned=aws_unsigned,
)
elif "AWS_ACCESS_KEY_ID" in os.environ and "AWS_SECRET_ACCESS_KEY" in os.environ:
self.session = AWSSession() if boto3 is not None else DummySession()
else:
self.session = DummySession()
self.options = options.copy()
self.context_options = {}
|
def __init__(
self,
session=None,
aws_unsigned=False,
profile_name=None,
session_class=AWSSession,
**options,
):
"""Create a new GDAL/AWS environment.
Note: this class is a context manager. GDAL isn't configured
until the context is entered via `with rasterio.Env():`
Parameters
----------
session : optional
A Session object.
aws_unsigned : bool, optional
Do not sign cloud requests.
profile_name : str, optional
A shared credentials profile name, as per boto3.
session_class : Session, optional
A sub-class of Session.
**options : optional
A mapping of GDAL configuration options, e.g.,
`CPL_DEBUG=True, CHECK_WITH_INVERT_PROJ=False`.
Returns
-------
Env
Notes
-----
We raise EnvError if the GDAL config options
AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY are given. AWS
credentials are handled exclusively by boto3.
Examples
--------
>>> with Env(CPL_DEBUG=True, CPL_CURL_VERBOSE=True):
... with rasterio.open("https://example.com/a.tif") as src:
... print(src.profile)
For access to secured cloud resources, a Rasterio Session or a
foreign session object may be passed to the constructor.
>>> import boto3
>>> from rasterio.session import AWSSession
>>> boto3_session = boto3.Session(...)
>>> with Env(AWSSession(boto3_session)):
... with rasterio.open("s3://mybucket/a.tif") as src:
... print(src.profile)
"""
aws_access_key_id = options.pop("aws_access_key_id", None)
# Before 1.0, Rasterio only supported AWS. We will special
# case AWS in 1.0.x. TODO: warn deprecation in 1.1.
if aws_access_key_id:
warnings.warn(
"Passing abstract session keyword arguments is deprecated. "
"Pass a Rasterio AWSSession object instead.",
RasterioDeprecationWarning,
)
aws_secret_access_key = options.pop("aws_secret_access_key", None)
aws_session_token = options.pop("aws_session_token", None)
region_name = options.pop("region_name", None)
if "AWS_ACCESS_KEY_ID" in options or "AWS_SECRET_ACCESS_KEY" in options:
raise EnvError(
"GDAL's AWS config options can not be directly set. "
"AWS credentials are handled exclusively by boto3."
)
if session:
# Passing a session via keyword argument is the canonical
# way to configure access to secured cloud resources.
if not isinstance(session, Session):
warnings.warn(
"Passing a boto3 session is deprecated. Pass a Rasterio "
"AWSSession object instead.",
RasterioDeprecationWarning,
)
session = AWSSession(session=session)
self.session = session
elif aws_access_key_id or profile_name or aws_unsigned:
self.session = AWSSession(
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token,
region_name=region_name,
profile_name=profile_name,
aws_unsigned=aws_unsigned,
)
elif "AWS_ACCESS_KEY_ID" in os.environ and "AWS_SECRET_ACCESS_KEY" in os.environ:
self.session = AWSSession()
else:
self.session = DummySession()
self.options = options.copy()
self.context_options = {}
|
https://github.com/mapbox/rasterio/issues/1708
|
Traceback (most recent call last):
File "/Users/guillaumelostis/.venv/rio/bin/rio", line 10, in <module>
sys.exit(main_group())
File "/Users/guillaumelostis/.venv/rio/lib/python3.7/site-packages/click/core.py", line 764, in __call__
return self.main(*args, **kwargs)
File "/Users/guillaumelostis/.venv/rio/lib/python3.7/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/Users/guillaumelostis/.venv/rio/lib/python3.7/site-packages/click/core.py", line 1134, in invoke
Command.invoke(self, ctx)
File "/Users/guillaumelostis/.venv/rio/lib/python3.7/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/Users/guillaumelostis/.venv/rio/lib/python3.7/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/Users/guillaumelostis/.venv/rio/lib/python3.7/site-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/Users/guillaumelostis/.venv/rio/lib/python3.7/site-packages/rasterio/rio/main.py", line 103, in main_group
ctx.obj["env"] = rasterio.Env(**envopts)
File "/Users/guillaumelostis/.venv/rio/lib/python3.7/site-packages/rasterio/env.py", line 195, in __init__
self.session = AWSSession()
File "/Users/guillaumelostis/.venv/rio/lib/python3.7/site-packages/rasterio/session.py", line 198, in __init__
import boto3
ModuleNotFoundError: No module named 'boto3'
|
ModuleNotFoundError
|
def __getstate__(self):
return self.to_wkt()
|
def __getstate__(self):
return self.wkt
|
https://github.com/mapbox/rasterio/issues/1643
|
In [1]: from copy import copy
In [2]: from rasterio.crs import CRS
In [3]: some_crs = CRS(init='epsg:32620')
In [4]: crs = copy(some_crs)
In [5]: bool(some_crs)
Out[5]: True
In [6]: bool(crs)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-6-dfa06d6ae714> in <module>
----> 1 bool(crs)
~/satellogic/env/lib/python3.6/site-packages/rasterio/crs.py in __bool__(self)
82
83 def __bool__(self):
---> 84 return bool(self.wkt)
85
86 __nonzero__ = __bool__
~/satellogic/env/lib/python3.6/site-packages/rasterio/crs.py in wkt(self)
130
131 """
--> 132 if not self._wkt:
133 self._wkt = self.to_wkt()
134 return self._wkt
AttributeError: 'CRS' object has no attribute '_wkt'
|
AttributeError
|
def __setstate__(self, state):
self._wkt = None
self._data = None
self._crs = _CRS.from_wkt(state)
|
def __setstate__(self, state):
self._crs = _CRS.from_wkt(state)
|
https://github.com/mapbox/rasterio/issues/1643
|
In [1]: from copy import copy
In [2]: from rasterio.crs import CRS
In [3]: some_crs = CRS(init='epsg:32620')
In [4]: crs = copy(some_crs)
In [5]: bool(some_crs)
Out[5]: True
In [6]: bool(crs)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-6-dfa06d6ae714> in <module>
----> 1 bool(crs)
~/satellogic/env/lib/python3.6/site-packages/rasterio/crs.py in __bool__(self)
82
83 def __bool__(self):
---> 84 return bool(self.wkt)
85
86 __nonzero__ = __bool__
~/satellogic/env/lib/python3.6/site-packages/rasterio/crs.py in wkt(self)
130
131 """
--> 132 if not self._wkt:
133 self._wkt = self.to_wkt()
134 return self._wkt
AttributeError: 'CRS' object has no attribute '_wkt'
|
AttributeError
|
def __init__(self, initialdata=None, **kwargs):
"""Make a CRS from a PROJ dict or mapping
Parameters
----------
initialdata : mapping, optional
A dictionary or other mapping
kwargs : mapping, optional
Another mapping. Will be overlaid on the initialdata.
Returns
-------
CRS
"""
self._wkt = None
self._data = None
self._crs = None
if initialdata or kwargs:
data = dict(initialdata or {})
data.update(**kwargs)
data = {k: v for k, v in data.items() if k in all_proj_keys}
# always use lowercase 'epsg'.
if "init" in data:
data["init"] = data["init"].replace("EPSG:", "epsg:")
proj_parts = []
for key, val in data.items():
if val is False or None:
continue
elif val is True:
proj_parts.append("+{}".format(key))
else:
proj_parts.append("+{}={}".format(key, val))
proj = " ".join(proj_parts)
self._crs = _CRS.from_proj4(proj)
else:
self._crs = _CRS()
|
def __init__(self, initialdata=None, **kwargs):
"""Make a CRS from a PROJ dict or mapping
Parameters
----------
initialdata : mapping, optional
A dictionary or other mapping
kwargs : mapping, optional
Another mapping. Will be overlaid on the initialdata.
Returns
-------
CRS
"""
self._wkt = None
self._data = None
self._crs = None
if initialdata or kwargs:
data = dict(initialdata or {})
data.update(**kwargs)
data = {k: v for k, v in data.items() if k in all_proj_keys}
# always use lowercase 'epsg'.
if "init" in data:
data["init"] = data["init"].replace("EPSG:", "epsg:")
proj = " ".join(["+{}={}".format(key, val) for key, val in data.items()])
self._crs = _CRS.from_proj4(proj)
else:
self._crs = _CRS()
|
https://github.com/mapbox/rasterio/issues/1609
|
src_crs = rcrs(src_proj)
dst_crs = rcrs(dst_proj)
warp.calculate_default_transform(src_crs, dst_crs, src_width, src_height, left, bottom, right, top)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/trst2284/miniconda3/envs/test/lib/python3.6/site-packages/rasterio/env.py", line 373, in wrapper
return f(*args, **kwds)
File "/home/trst2284/miniconda3/envs/test/lib/python3.6/site-packages/rasterio/warp.py", line 418, in calculate_default_transform
src_crs, dst_crs, width, height, left, bottom, right, top, gcps)
File "rasterio/_warp.pyx", line 542, in rasterio._warp._calculate_default_transform
File "rasterio/_base.pyx", line 1331, in rasterio._base._osr_from_crs
rasterio.errors.CRSError: Invalid CRS: CRS({'ellps': 'WGS84', 'h': 9000000.0, 'lat_0': -78.0, 'lon_0': 0.0, 'proj': 'nsper', 'units': 'm', 'x_0': 0, 'y_0': 0, 'wktext': True})
|
rasterio.errors.CRSError
|
def __enter__(self):
return self
|
def __enter__(self):
log.debug("Entering env context: %r", self)
if local._env is None:
log.debug("Starting outermost env")
self._has_parent_env = False
# See note directly above where _discovered_options is globally
# defined. This MUST happen before calling 'defenv()'.
local._discovered_options = {}
# Don't want to reinstate the "RASTERIO_ENV" option.
probe_env = {k for k in self.options.keys() if k != "RASTERIO_ENV"}
for key in probe_env:
val = get_gdal_config(key, normalize=False)
if val is not None:
local._discovered_options[key] = val
defenv(**self.options)
self.context_options = {}
else:
self._has_parent_env = True
self.context_options = getenv()
setenv(**self.options)
self.credentialize()
log.debug("Entered env context: %r", self)
return self
|
https://github.com/mapbox/rasterio/issues/1539
|
from rasterio.crs import CRS
CRS({'init': 'epsg:4326'}).is_geographic
ERROR 4: Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files.
Traceback (most recent call last):
File "rasterio/_base.pyx", line 1310, in rasterio._base._osr_from_crs
File "rasterio/_err.pyx", line 182, in rasterio._err.exc_wrap_int
rasterio._err.CPLE_OpenFailedError: Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "rasterio/_crs.pyx", line 37, in rasterio._crs._CRS.is_geographic
File "rasterio/_base.pyx", line 1316, in rasterio._base._osr_from_crs
rasterio.errors.CRSError: Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files.
|
rasterio._err.CPLE_OpenFailedError
|
def __exit__(self, *args):
pass
|
def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
log.debug("Exiting env context: %r", self)
delenv()
if self._has_parent_env:
defenv()
setenv(**self.context_options)
else:
log.debug("Exiting outermost env")
# See note directly above where _discovered_options is globally
# defined.
while local._discovered_options:
key, val = local._discovered_options.popitem()
set_gdal_config(key, val, normalize=False)
local._discovered_options = None
log.debug("Exited env context: %r", self)
|
https://github.com/mapbox/rasterio/issues/1539
|
from rasterio.crs import CRS
CRS({'init': 'epsg:4326'}).is_geographic
ERROR 4: Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files.
Traceback (most recent call last):
File "rasterio/_base.pyx", line 1310, in rasterio._base._osr_from_crs
File "rasterio/_err.pyx", line 182, in rasterio._err.exc_wrap_int
rasterio._err.CPLE_OpenFailedError: Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "rasterio/_crs.pyx", line 37, in rasterio._crs._CRS.is_geographic
File "rasterio/_base.pyx", line 1316, in rasterio._base._osr_from_crs
rasterio.errors.CRSError: Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA environment variable to point to the directory containing EPSG csv files.
|
rasterio._err.CPLE_OpenFailedError
|
def plotting_extent(source, transform=None):
"""Returns an extent in the format needed
for matplotlib's imshow (left, right, bottom, top)
instead of rasterio's bounds (left, bottom, top, right)
Parameters
----------
source : array or dataset object opened in 'r' mode
If array, data in the order rows, columns and optionally bands. If array
is band order (bands in the first dimension), use arr[0]
transform: Affine, required if source is array
Defines the affine transform if source is an array
Returns
-------
tuple of float
left, right, bottom, top
"""
if hasattr(source, "bounds"):
extent = (
source.bounds.left,
source.bounds.right,
source.bounds.bottom,
source.bounds.top,
)
elif not transform:
raise ValueError("transform is required if source is an array")
else:
transform = guard_transform(transform)
rows, cols = source.shape[0:2]
left, top = transform * (0, 0)
right, bottom = transform * (cols, rows)
extent = (left, right, bottom, top)
return extent
|
def plotting_extent(source, transform=None):
"""Returns an extent in the format needed
for matplotlib's imshow (left, right, bottom, top)
instead of rasterio's bounds (left, bottom, top, right)
Parameters
----------
source : array or dataset object opened in 'r' mode
input data
transform: Affine, required if source is array
Defines the affine transform if source is an array
Returns
-------
tuple of float
left, right, bottom, top
"""
if hasattr(source, "bounds"):
extent = (
source.bounds.left,
source.bounds.right,
source.bounds.bottom,
source.bounds.top,
)
elif not transform:
raise ValueError("transform is required if source is an array")
else:
transform = guard_transform(transform)
rows, cols = source.shape[0:2]
left, top = transform * (0, 0)
right, bottom = transform * (cols, rows)
extent = (left, right, bottom, top)
return extent
|
https://github.com/mapbox/rasterio/issues/1537
|
from rasterio.crs import CRS
CRS.from_string(projection_string)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "rasterio/_crs.pyx", line 205, in rasterio._crs._CRS.from_string
File "rasterio/_crs.pyx", line 230, in rasterio._crs._CRS.from_wkt
rasterio.errors.CRSError: Invalid CRS: 'PROJCS["USA_Contiguous_Albers_Equal_Area_Conic_USGS_version",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Albers"],PARAMETER["false_easting",0.0],PARAMETER["false_northing",0.0],PARAMETER["central_meridian",-96.0],PARAMETER["standard_parallel_1",29.5],PARAMETER["standard_parallel_2",45.5],PARAMETER["latitude_of_origin",23.0],UNIT["Meter",1.0],VERTCS["NAVD_1988",VDATUM["North_American_Vertical_Datum_1988"],PARAMETER["Vertical_Shift",0.0],PARAMETER["Direction",1.0],UNIT["Centimeter",0.01]'
|
rasterio.errors.CRSError
|
def xy(self, row, col, offset="center"):
"""Returns the coordinates ``(x, y)`` of a pixel at `row` and `col`.
The pixel's center is returned by default, but a corner can be returned
by setting `offset` to one of `ul, ur, ll, lr`.
Parameters
----------
row : int
Pixel row.
col : int
Pixel column.
offset : str, optional
Determines if the returned coordinates are for the center of the
pixel or for a corner.
Returns
-------
tuple
``(x, y)``
"""
return xy(self.transform, row, col, offset=offset)
|
def xy(transform, rows, cols, offset="center"):
"""Returns the x and y coordinates of pixels at `rows` and `cols`.
The pixel's center is returned by default, but a corner can be returned
by setting `offset` to one of `ul, ur, ll, lr`.
Parameters
----------
transform : affine.Affine
Transformation from pixel coordinates to coordinate reference system.
rows : list or int
Pixel rows.
cols : list or int
Pixel columns.
offset : str, optional
Determines if the returned coordinates are for the center of the
pixel or for a corner.
Returns
-------
xs : list
x coordinates in coordinate reference system
ys : list
y coordinates in coordinate reference system
"""
single_col = False
single_row = False
if not isinstance(cols, collections.Iterable):
cols = [cols]
single_col = True
if not isinstance(rows, collections.Iterable):
rows = [rows]
single_row = True
if offset == "center":
coff, roff = (0.5, 0.5)
elif offset == "ul":
coff, roff = (0, 0)
elif offset == "ur":
coff, roff = (1, 0)
elif offset == "ll":
coff, roff = (0, 1)
elif offset == "lr":
coff, roff = (1, 1)
else:
raise ValueError("Invalid offset")
xs = []
ys = []
for col, row in zip(cols, rows):
x, y = transform * transform.translation(coff, roff) * (col, row)
xs.append(x)
ys.append(y)
if single_row:
ys = ys[0]
if single_col:
xs = xs[0]
return xs, ys
|
https://github.com/mapbox/rasterio/issues/1174
|
Traceback (most recent call last):
File "buh.py", line 1, in <module>
import rasterio
File "/Users/wursterk/code/rasterio/rasterio/__init__.py", line 25, in <module>
from rasterio.io import (
File "/Users/wursterk/code/rasterio/rasterio/io.py", line 13, in <module>
from rasterio._io import (
File "rasterio/_io.pyx", line 31, in init rasterio._io
from rasterio.vrt import WarpedVRT
File "/Users/wursterk/code/rasterio/rasterio/vrt.py", line 4, in <module>
from rasterio.io import WindowMethodsMixin, TransformMethodsMixin
ImportError: cannot import name 'WindowMethodsMixin'
|
ImportError
|
def reproject(
source,
destination,
src_transform=None,
src_crs=None,
src_nodata=None,
dst_transform=None,
dst_crs=None,
dst_nodata=None,
resampling=Resampling.nearest,
**kwargs,
):
"""
Reproject a source raster to a destination raster.
If the source and destination are ndarrays, coordinate reference
system definitions and affine transformation parameters are required
for reprojection.
If the source and destination are rasterio Bands, shorthand for
bands of datasets on disk, the coordinate reference systems and
transforms will be read from the appropriate datasets.
Parameters
------------
source: ndarray or rasterio Band
Source raster.
destination: ndarray or rasterio Band
Target raster.
src_transform: affine transform object, optional
Source affine transformation. Required if source and destination
are ndarrays. Will be derived from source if it is a rasterio Band.
src_crs: dict, optional
Source coordinate reference system, in rasterio dict format.
Required if source and destination are ndarrays.
Will be derived from source if it is a rasterio Band.
Example: {'init': 'EPSG:4326'}
src_nodata: int or float, optional
The source nodata value. Pixels with this value will not be used
for interpolation. If not set, it will be default to the
nodata value of the source image if a masked ndarray or rasterio band,
if available. Must be provided if dst_nodata is not None.
dst_transform: affine transform object, optional
Target affine transformation. Required if source and destination
are ndarrays. Will be derived from target if it is a rasterio Band.
dst_crs: dict, optional
Target coordinate reference system. Required if source and destination
are ndarrays. Will be derived from target if it is a rasterio Band.
dst_nodata: int or float, optional
The nodata value used to initialize the destination; it will remain
in all areas not covered by the reprojected source. Defaults to the
nodata value of the destination image (if set), the value of
src_nodata, or 0 (GDAL default).
resampling: int
Resampling method to use. One of the following:
Resampling.nearest,
Resampling.bilinear,
Resampling.cubic,
Resampling.cubic_spline,
Resampling.lanczos,
Resampling.average,
Resampling.mode
kwargs: dict, optional
Additional arguments passed to transformation function.
Returns
---------
out: None
Output is written to destination.
"""
# Resampling guard.
try:
Resampling(resampling)
if resampling == 7:
raise ValueError
except ValueError:
raise ValueError(
"resampling must be one of: {0}".format(
", ".join(
[
"Resampling.{0}".format(k)
for k in Resampling.__members__.keys()
if k != "gauss"
]
)
)
)
# If working with identity transform, assume it is crs-less data
# and that translating the matrix very slightly will avoid #674
eps = 1e-100
if src_transform and guard_transform(src_transform).is_identity:
src_transform = src_transform.translation(eps, eps)
if dst_transform and guard_transform(dst_transform).is_identity:
dst_transform = dst_transform.translation(eps, eps)
if src_transform:
src_transform = guard_transform(src_transform).to_gdal()
if dst_transform:
dst_transform = guard_transform(dst_transform).to_gdal()
# Passing None can cause segfault, use empty dict
if src_crs is None:
src_crs = {}
if dst_crs is None:
dst_crs = {}
_reproject(
source,
destination,
src_transform,
src_crs,
src_nodata,
dst_transform,
dst_crs,
dst_nodata,
resampling,
**kwargs,
)
|
def reproject(
source,
destination,
src_transform=None,
src_crs=None,
src_nodata=None,
dst_transform=None,
dst_crs=None,
dst_nodata=None,
resampling=Resampling.nearest,
**kwargs,
):
"""
Reproject a source raster to a destination raster.
If the source and destination are ndarrays, coordinate reference
system definitions and affine transformation parameters are required
for reprojection.
If the source and destination are rasterio Bands, shorthand for
bands of datasets on disk, the coordinate reference systems and
transforms will be read from the appropriate datasets.
Parameters
------------
source: ndarray or rasterio Band
Source raster.
destination: ndarray or rasterio Band
Target raster.
src_transform: affine transform object, optional
Source affine transformation. Required if source and destination
are ndarrays. Will be derived from source if it is a rasterio Band.
src_crs: dict, optional
Source coordinate reference system, in rasterio dict format.
Required if source and destination are ndarrays.
Will be derived from source if it is a rasterio Band.
Example: {'init': 'EPSG:4326'}
src_nodata: int or float, optional
The source nodata value. Pixels with this value will not be used
for interpolation. If not set, it will be default to the
nodata value of the source image if a masked ndarray or rasterio band,
if available. Must be provided if dst_nodata is not None.
dst_transform: affine transform object, optional
Target affine transformation. Required if source and destination
are ndarrays. Will be derived from target if it is a rasterio Band.
dst_crs: dict, optional
Target coordinate reference system. Required if source and destination
are ndarrays. Will be derived from target if it is a rasterio Band.
dst_nodata: int or float, optional
The nodata value used to initialize the destination; it will remain
in all areas not covered by the reprojected source. Defaults to the
nodata value of the destination image (if set), the value of
src_nodata, or 0 (GDAL default).
resampling: int
Resampling method to use. One of the following:
Resampling.nearest,
Resampling.bilinear,
Resampling.cubic,
Resampling.cubic_spline,
Resampling.lanczos,
Resampling.average,
Resampling.mode
kwargs: dict, optional
Additional arguments passed to transformation function.
Returns
---------
out: None
Output is written to destination.
"""
# Resampling guard.
try:
Resampling(resampling)
if resampling == 7:
raise ValueError
except ValueError:
raise ValueError(
"resampling must be one of: {0}".format(
", ".join(
[
"Resampling.{0}".format(k)
for k in Resampling.__members__.keys()
if k != "gauss"
]
)
)
)
if src_transform:
src_transform = guard_transform(src_transform).to_gdal()
if dst_transform:
dst_transform = guard_transform(dst_transform).to_gdal()
_reproject(
source,
destination,
src_transform,
src_crs,
src_nodata,
dst_transform,
dst_crs,
dst_nodata,
resampling,
**kwargs,
)
|
https://github.com/mapbox/rasterio/issues/674
|
Traceback (most recent call last):
File "_test_err.py", line 51, in <module>
test_reproject_identity()
File "_test_err.py", line 45, in test_reproject_identity
resampling=Resampling.nearest)
File "/Users/mperry/work/rasterio/rasterio/warp.py", line 257, in reproject
**kwargs)
File "rasterio/_warp.pyx", line 391, in rasterio._warp._reproject (rasterio/_warp.cpp:8343)
File "rasterio/_warp.pyx", line 380, in rasterio._warp._reproject (rasterio/_warp.cpp:8219)
File "rasterio/_warp.pyx", line 384, in rasterio._warp._reproject (rasterio/_warp.cpp:8133)
File "rasterio/_err.pyx", line 190, in rasterio._err.CPLErrors.check (rasterio/_err.c:1502)
rasterio._err.CPLE_AppDefined: (3, 1, 'The transformation is already "north up" or a transformation
between pixel/line and georeferenced coordinates cannot be computed for Temporary source dataset
for _reproject(). There is no affine transformation and no GCPs. Specify transformation option
SRC_METHOD=NO_GEOTRANSFORM to bypass this check.')
|
rasterio._err.CPLError
|
def reproject(
source,
destination,
src_transform=None,
src_crs=None,
src_nodata=None,
dst_transform=None,
dst_crs=None,
dst_nodata=None,
resampling=Resampling.nearest,
**kwargs,
):
"""
Reproject a source raster to a destination raster.
If the source and destination are ndarrays, coordinate reference
system definitions and affine transformation parameters are required
for reprojection.
If the source and destination are rasterio Bands, shorthand for
bands of datasets on disk, the coordinate reference systems and
transforms will be read from the appropriate datasets.
Parameters
------------
source: ndarray or rasterio Band
Source raster.
destination: ndarray or rasterio Band
Target raster.
src_transform: affine transform object, optional
Source affine transformation. Required if source and destination
are ndarrays. Will be derived from source if it is a rasterio Band.
src_crs: dict, optional
Source coordinate reference system, in rasterio dict format.
Required if source and destination are ndarrays.
Will be derived from source if it is a rasterio Band.
Example: {'init': 'EPSG:4326'}
src_nodata: int or float, optional
The source nodata value. Pixels with this value will not be used
for interpolation. If not set, it will be default to the
nodata value of the source image if a masked ndarray or rasterio band,
if available. Must be provided if dst_nodata is not None.
dst_transform: affine transform object, optional
Target affine transformation. Required if source and destination
are ndarrays. Will be derived from target if it is a rasterio Band.
dst_crs: dict, optional
Target coordinate reference system. Required if source and destination
are ndarrays. Will be derived from target if it is a rasterio Band.
dst_nodata: int or float, optional
The nodata value used to initialize the destination; it will remain
in all areas not covered by the reprojected source. Defaults to the
nodata value of the destination image (if set), the value of
src_nodata, or 0 (GDAL default).
resampling: int
Resampling method to use. One of the following:
Resampling.nearest,
Resampling.bilinear,
Resampling.cubic,
Resampling.cubic_spline,
Resampling.lanczos,
Resampling.average,
Resampling.mode
kwargs: dict, optional
Additional arguments passed to transformation function.
Returns
---------
out: None
Output is written to destination.
"""
# Resampling guard.
try:
Resampling(resampling)
if resampling == 7:
raise ValueError
except ValueError:
raise ValueError(
"resampling must be one of: {0}".format(
", ".join(
[
"Resampling.{0}".format(k)
for k in Resampling.__members__.keys()
if k != "gauss"
]
)
)
)
# If working with identity transform, assume it is crs-less data
# and that translating the matrix very slightly will avoid #674
eps = 1e-100
if src_transform.is_identity:
src_transform = src_transform.translation(eps, eps)
if dst_transform.is_identity:
dst_transform = dst_transform.translation(eps, eps)
if src_transform:
src_transform = guard_transform(src_transform).to_gdal()
if dst_transform:
dst_transform = guard_transform(dst_transform).to_gdal()
# Passing None can cause segfault, use empty dict
if src_crs is None:
src_crs = {}
if dst_crs is None:
dst_crs = {}
_reproject(
source,
destination,
src_transform,
src_crs,
src_nodata,
dst_transform,
dst_crs,
dst_nodata,
resampling,
**kwargs,
)
|
def reproject(
source,
destination,
src_transform=None,
src_crs=None,
src_nodata=None,
dst_transform=None,
dst_crs=None,
dst_nodata=None,
resampling=Resampling.nearest,
**kwargs,
):
"""
Reproject a source raster to a destination raster.
If the source and destination are ndarrays, coordinate reference
system definitions and affine transformation parameters are required
for reprojection.
If the source and destination are rasterio Bands, shorthand for
bands of datasets on disk, the coordinate reference systems and
transforms will be read from the appropriate datasets.
Parameters
------------
source: ndarray or rasterio Band
Source raster.
destination: ndarray or rasterio Band
Target raster.
src_transform: affine transform object, optional
Source affine transformation. Required if source and destination
are ndarrays. Will be derived from source if it is a rasterio Band.
src_crs: dict, optional
Source coordinate reference system, in rasterio dict format.
Required if source and destination are ndarrays.
Will be derived from source if it is a rasterio Band.
Example: {'init': 'EPSG:4326'}
src_nodata: int or float, optional
The source nodata value. Pixels with this value will not be used
for interpolation. If not set, it will be default to the
nodata value of the source image if a masked ndarray or rasterio band,
if available. Must be provided if dst_nodata is not None.
dst_transform: affine transform object, optional
Target affine transformation. Required if source and destination
are ndarrays. Will be derived from target if it is a rasterio Band.
dst_crs: dict, optional
Target coordinate reference system. Required if source and destination
are ndarrays. Will be derived from target if it is a rasterio Band.
dst_nodata: int or float, optional
The nodata value used to initialize the destination; it will remain
in all areas not covered by the reprojected source. Defaults to the
nodata value of the destination image (if set), the value of
src_nodata, or 0 (GDAL default).
resampling: int
Resampling method to use. One of the following:
Resampling.nearest,
Resampling.bilinear,
Resampling.cubic,
Resampling.cubic_spline,
Resampling.lanczos,
Resampling.average,
Resampling.mode
kwargs: dict, optional
Additional arguments passed to transformation function.
Returns
---------
out: None
Output is written to destination.
"""
# Resampling guard.
try:
Resampling(resampling)
if resampling == 7:
raise ValueError
except ValueError:
raise ValueError(
"resampling must be one of: {0}".format(
", ".join(
[
"Resampling.{0}".format(k)
for k in Resampling.__members__.keys()
if k != "gauss"
]
)
)
)
if src_transform:
src_transform = guard_transform(src_transform).to_gdal()
if dst_transform:
dst_transform = guard_transform(dst_transform).to_gdal()
# Passing None can cause segfault, use empty dict
if src_crs is None:
src_crs = {}
if dst_crs is None:
dst_crs = {}
_reproject(
source,
destination,
src_transform,
src_crs,
src_nodata,
dst_transform,
dst_crs,
dst_nodata,
resampling,
**kwargs,
)
|
https://github.com/mapbox/rasterio/issues/674
|
Traceback (most recent call last):
File "_test_err.py", line 51, in <module>
test_reproject_identity()
File "_test_err.py", line 45, in test_reproject_identity
resampling=Resampling.nearest)
File "/Users/mperry/work/rasterio/rasterio/warp.py", line 257, in reproject
**kwargs)
File "rasterio/_warp.pyx", line 391, in rasterio._warp._reproject (rasterio/_warp.cpp:8343)
File "rasterio/_warp.pyx", line 380, in rasterio._warp._reproject (rasterio/_warp.cpp:8219)
File "rasterio/_warp.pyx", line 384, in rasterio._warp._reproject (rasterio/_warp.cpp:8133)
File "rasterio/_err.pyx", line 190, in rasterio._err.CPLErrors.check (rasterio/_err.c:1502)
rasterio._err.CPLE_AppDefined: (3, 1, 'The transformation is already "north up" or a transformation
between pixel/line and georeferenced coordinates cannot be computed for Temporary source dataset
for _reproject(). There is no affine transformation and no GCPs. Specify transformation option
SRC_METHOD=NO_GEOTRANSFORM to bypass this check.')
|
rasterio._err.CPLError
|
def merge(ctx, files, driver):
"""Copy valid pixels from input files to an output file.
All files must have the same shape, number of bands, and data type.
Input files are merged in their listed order using a reverse
painter's algorithm.
"""
import numpy as np
verbosity = (ctx.obj and ctx.obj.get("verbosity")) or 1
logger = logging.getLogger("rio")
try:
with rasterio.drivers(CPL_DEBUG=verbosity > 2):
output = files[-1]
files = files[:-1]
with rasterio.open(files[0]) as first:
kwargs = first.meta
kwargs["transform"] = kwargs.pop("affine")
dest = np.zeros((first.count,) + first.shape, dtype=first.dtypes[0])
nodataval = 0.0
if os.path.exists(output):
dst = rasterio.open(output, "r+")
nodataval = dst.nodatavals[0] or nodataval
else:
kwargs["driver"] == driver
dst = rasterio.open(output, "w", **kwargs)
nodataval = first.nodatavals[0] or nodataval
if nodataval:
dest.fill(nodataval)
for fname in reversed(files):
with rasterio.open(fname) as src:
data = src.read()
try:
where = np.logical_and(dest == nodataval, data.mask == False)
except AttributeError:
where = dest == nodataval
np.copyto(dest, data, where=where)
if dst.mode == "r+":
data = dst.read()
try:
where = np.logical_and(dest == nodataval, data.mask == False)
except AttributeError:
where = dest == nodataval
np.copyto(dest, data, where=where)
dst.write(dest)
dst.close()
sys.exit(0)
except Exception:
logger.exception("Failed. Exception caught")
sys.exit(1)
|
def merge(ctx, files, driver):
"""Copy valid pixels from input files to an output file.
All files must have the same shape, number of bands, and data type.
Input files are merged in their listed order using a reverse
painter's algorithm.
"""
import numpy as np
verbosity = (ctx.obj and ctx.obj.get("verbosity")) or 1
logger = logging.getLogger("rio")
try:
with rasterio.drivers(CPL_DEBUG=verbosity > 2):
output = files[-1]
files = files[:-1]
with rasterio.open(files[0]) as first:
kwargs = first.meta
kwargs["transform"] = kwargs.pop("affine")
dest = np.empty((first.count,) + first.shape, dtype=first.dtypes[0])
if os.path.exists(output):
dst = rasterio.open(output, "r+")
nodataval = dst.nodatavals[0]
else:
kwargs["driver"] == driver
dst = rasterio.open(output, "w", **kwargs)
nodataval = first.nodatavals[0]
dest.fill(nodataval)
for fname in reversed(files):
with rasterio.open(fname) as src:
data = src.read()
np.copyto(
dest,
data,
where=np.logical_and(dest == nodataval, data.mask == False),
)
if dst.mode == "r+":
data = dst.read()
np.copyto(
dest,
data,
where=np.logical_and(dest == nodataval, data.mask == False),
)
dst.write(dest)
dst.close()
sys.exit(0)
except Exception:
logger.exception("Failed. Exception caught")
sys.exit(1)
|
https://github.com/mapbox/rasterio/issues/240
|
(rio-test)$ rio merge warped.tif merged.tif
ERROR:rio:Failed. Exception caught
Traceback (most recent call last):
File "/Users/amit/Mapbox/rasterio/rasterio/rio/merge.py", line 50, in merge
dest.fill(nodataval)
TypeError: long() argument must be a string or a number, not 'NoneType'
|
TypeError
|
def tastes_like_gdal(seq):
"""Return True if `seq` matches the GDAL geotransform pattern."""
return seq[2] == seq[4] == 0.0 and seq[1] > 0 and seq[5] < 0
|
def tastes_like_gdal(t):
return t[2] == t[4] == 0.0 and t[1] > 0 and t[5] < 0
|
https://github.com/mapbox/rasterio/issues/210
|
Traceback (most recent call last):
...
File "copier.py", line 15, in to_png
with rio.open(dst_path, "w", **meta) as dst:
File "xxx/anaconda/lib/python2.7/site-packages/rasterio/__init__.py", line 91, in open
transform = guard_transform(transform)
File "xxx/anaconda/lib/python2.7/site-packages/rasterio/transform.py", line 27, in guard_transform
transform.a, transform.e))
ValueError: Transform has invalid coefficients a, e: (0.000000, 0.000000)
|
ValueError
|
def guard_transform(transform):
"""Return an Affine transformation instance"""
if not isinstance(transform, Affine):
if tastes_like_gdal(transform):
warnings.warn(
"GDAL-style transforms are deprecated and will not "
"be supported in Rasterio 1.0.",
FutureWarning,
stacklevel=2,
)
transform = Affine.from_gdal(*transform)
else:
transform = Affine(*transform)
return transform
|
def guard_transform(transform):
"""Return an Affine transformation instance"""
if not isinstance(transform, Affine):
if tastes_like_gdal(transform):
warnings.warn(
"GDAL-style transforms are deprecated and will not "
"be supported in Rasterio 1.0.",
FutureWarning,
stacklevel=2,
)
transform = Affine.from_gdal(*transform)
else:
transform = Affine(*transform)
a, e = transform.a, transform.e
if a == 0.0 or e == 0.0:
raise ValueError(
"Transform has invalid coefficients a, e: (%f, %f)"
% (transform.a, transform.e)
)
return transform
|
https://github.com/mapbox/rasterio/issues/210
|
Traceback (most recent call last):
...
File "copier.py", line 15, in to_png
with rio.open(dst_path, "w", **meta) as dst:
File "xxx/anaconda/lib/python2.7/site-packages/rasterio/__init__.py", line 91, in open
transform = guard_transform(transform)
File "xxx/anaconda/lib/python2.7/site-packages/rasterio/transform.py", line 27, in guard_transform
transform.a, transform.e))
ValueError: Transform has invalid coefficients a, e: (0.000000, 0.000000)
|
ValueError
|
def init_cgroups():
# Track metrics for the roll-up cgroup and for the agent cgroup
try:
CGroupsTelemetry.track_cgroup(CGroups.for_extension(""))
CGroupsTelemetry.track_agent()
except Exception as e:
# when a hierarchy is not mounted, we raise an exception
# and we should therefore only issue a warning, since this
# is not unexpected
logger.warn("Monitor: cgroups not initialized: {0}", ustr(e))
logger.verbose(traceback.format_exc())
|
def init_cgroups():
# Track metrics for the roll-up cgroup and for the agent cgroup
try:
CGroupsTelemetry.track_cgroup(CGroups.for_extension(""))
CGroupsTelemetry.track_agent()
except Exception as e:
logger.error(
"monitor: Exception tracking wrapper and agent: {0} [{1}]",
e,
traceback.format_exc(),
)
|
https://github.com/Azure/WALinuxAgent/issues/1282
|
2018/07/31 11:41:06.400633 ERROR ExtHandler monitor: Exception tracking wrapper and agent: 'Hierarchy memory is not mounted' [Traceback (most recent call last):
File "bin/WALinuxAgent-2.2.30-py2.7.egg/azurelinuxagent/ga/monitor.py", line 397, in init_cgroups
CGroupsTelemetry.track_cgroup(CGroups.for_extension(""))
File "bin/WALinuxAgent-2.2.30-py2.7.egg/azurelinuxagent/common/cgroups.py", line 360, in for_extension
return CGroups(name, CGroups._construct_custom_path_for_hierarchy)
File "bin/WALinuxAgent-2.2.30-py2.7.egg/azurelinuxagent/common/cgroups.py", line 401, in __init__
raise CGroupsException("Hierarchy {0} is not mounted".format(hierarchy))
azurelinuxagent.common.cgroups.CGroupsException: 'Hierarchy memory is not mounted'
]
|
azurelinuxagent.common.cgroups.CGroupsException
|
def send_cgroup_telemetry(self):
if self.last_cgroup_telemetry is None:
self.last_cgroup_telemetry = datetime.datetime.utcnow()
if datetime.datetime.utcnow() >= (
self.last_telemetry_heartbeat + MonitorHandler.CGROUP_TELEMETRY_PERIOD
):
try:
for cgroup_name, metrics in CGroupsTelemetry.collect_all_tracked().items():
for metric_group, metric_name, value in metrics:
if value > 0:
report_metric(metric_group, metric_name, cgroup_name, value)
except Exception as e:
logger.warn(
"Monitor: failed to collect cgroups performance metrics: {0}", ustr(e)
)
logger.verbose(traceback.format_exc())
# Look for extension cgroups we're not already tracking and track them
try:
CGroupsTelemetry.update_tracked(self.protocol.client.get_current_handlers())
except Exception as e:
logger.warn(
"Monitor: failed to update cgroups tracked extensions: {0}", ustr(e)
)
logger.verbose(traceback.format_exc())
self.last_cgroup_telemetry = datetime.datetime.utcnow()
|
def send_cgroup_telemetry(self):
if self.last_cgroup_telemetry is None:
self.last_cgroup_telemetry = datetime.datetime.utcnow()
if datetime.datetime.utcnow() >= (
self.last_telemetry_heartbeat + MonitorHandler.CGROUP_TELEMETRY_PERIOD
):
try:
for cgroup_name, metrics in CGroupsTelemetry.collect_all_tracked().items():
for metric_group, metric_name, value in metrics:
if value > 0:
report_metric(metric_group, metric_name, cgroup_name, value)
except Exception as e:
logger.warn(
"Failed to collect performance metrics: {0} [{1}]",
e,
traceback.format_exc(),
)
# Look for extension cgroups we're not already tracking and track them
try:
CGroupsTelemetry.update_tracked(self.protocol.client.get_current_handlers())
except Exception as e:
logger.warn(
"Monitor: updating tracked extensions raised {0}: {1}",
e,
traceback.format_exc(),
)
self.last_cgroup_telemetry = datetime.datetime.utcnow()
|
https://github.com/Azure/WALinuxAgent/issues/1282
|
2018/07/31 11:41:06.400633 ERROR ExtHandler monitor: Exception tracking wrapper and agent: 'Hierarchy memory is not mounted' [Traceback (most recent call last):
File "bin/WALinuxAgent-2.2.30-py2.7.egg/azurelinuxagent/ga/monitor.py", line 397, in init_cgroups
CGroupsTelemetry.track_cgroup(CGroups.for_extension(""))
File "bin/WALinuxAgent-2.2.30-py2.7.egg/azurelinuxagent/common/cgroups.py", line 360, in for_extension
return CGroups(name, CGroups._construct_custom_path_for_hierarchy)
File "bin/WALinuxAgent-2.2.30-py2.7.egg/azurelinuxagent/common/cgroups.py", line 401, in __init__
raise CGroupsException("Hierarchy {0} is not mounted".format(hierarchy))
azurelinuxagent.common.cgroups.CGroupsException: 'Hierarchy memory is not mounted'
]
|
azurelinuxagent.common.cgroups.CGroupsException
|
def deploy_ssh_pubkey(self, username, pubkey):
"""
Deploy authorized_key
"""
path, thumbprint, value = pubkey
if path is None:
raise OSUtilError("Public key path is None")
crytputil = CryptUtil(conf.get_openssl_cmd())
path = self._norm_path(path)
dir_path = os.path.dirname(path)
fileutil.mkdir(dir_path, mode=0o700, owner=username)
if value is not None:
if not value.startswith("ssh-"):
raise OSUtilError("Bad public key: {0}".format(value))
fileutil.write_file(path, value)
elif thumbprint is not None:
lib_dir = conf.get_lib_dir()
crt_path = os.path.join(lib_dir, thumbprint + ".crt")
if not os.path.isfile(crt_path):
raise OSUtilError("Can't find {0}.crt".format(thumbprint))
pub_path = os.path.join(lib_dir, thumbprint + ".pub")
pub = crytputil.get_pubkey_from_crt(crt_path)
fileutil.write_file(pub_path, pub)
self.set_selinux_context(pub_path, "unconfined_u:object_r:ssh_home_t:s0")
self.openssl_to_openssh(pub_path, path)
fileutil.chmod(pub_path, 0o600)
else:
raise OSUtilError("SSH public key Fingerprint and Value are None")
self.set_selinux_context(path, "unconfined_u:object_r:ssh_home_t:s0")
fileutil.chowner(path, username)
fileutil.chmod(path, 0o644)
|
def deploy_ssh_pubkey(self, username, pubkey):
"""
Deploy authorized_key
"""
path, thumbprint, value = pubkey
if path is None:
raise OSUtilError("Publich key path is None")
crytputil = CryptUtil(conf.get_openssl_cmd())
path = self._norm_path(path)
dir_path = os.path.dirname(path)
fileutil.mkdir(dir_path, mode=0o700, owner=username)
if value is not None:
if not value.startswith("ssh-"):
raise OSUtilError("Bad public key: {0}".format(value))
fileutil.write_file(path, value)
elif thumbprint is not None:
lib_dir = conf.get_lib_dir()
crt_path = os.path.join(lib_dir, thumbprint + ".crt")
if not os.path.isfile(crt_path):
raise OSUtilError("Can't find {0}.crt".format(thumbprint))
pub_path = os.path.join(lib_dir, thumbprint + ".pub")
pub = crytputil.get_pubkey_from_crt(crt_path)
fileutil.write_file(pub_path, pub)
self.set_selinux_context(pub_path, "unconfined_u:object_r:ssh_home_t:s0")
self.openssl_to_openssh(pub_path, path)
fileutil.chmod(pub_path, 0o600)
else:
raise OSUtilError("SSH public key Fingerprint and Value are None")
self.set_selinux_context(path, "unconfined_u:object_r:ssh_home_t:s0")
fileutil.chowner(path, username)
fileutil.chmod(path, 0o644)
|
https://github.com/Azure/WALinuxAgent/issues/104
|
2015/08/19 12:35:10 Created user account: azureuser
2015/08/19 12:35:10 Deploy public key:28EC0E2C8F27A2A0D081742A97D517CBD1FFFFFF
2015/08/19 12:35:10 ERROR:Failed: 28EC0E2C8F27A2A0D081742A97D517CBD1FFFFFF.pub.crt -> /home/azureuser/.ssh/authorized_keys
2015/08/19 12:35:10 ERROR:CalledProcessError. Error Code is 1
2015/08/19 12:35:10 ERROR:CalledProcessError. Command string was chcon unconfined_u:object_r:ssh_home_t:s0 /home/azureuser/.ssh/authorized_keys
2015/08/19 12:35:10 ERROR:CalledProcessError. Command result was chcon: cannot access /home/azureuser/.ssh/authorized_keys: No such file or directory
2015/08/19 12:35:10 ERROR:Traceback (most recent call last):
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 6048, in main
2015/08/19 12:35:10 ERROR: WaAgent.Run()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 5571, in Run
2015/08/19 12:35:10 ERROR: provisionError = self.Provision()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 5457, in Provision
2015/08/19 12:35:10 ERROR: error = ovfobj.Process()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 4566, in Process
2015/08/19 12:35:10 ERROR: ChangeOwner(path, self.UserName)
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 2242, in ChangeOwner
2015/08/19 12:35:10 ERROR: os.chown(filepath, p[2], p[3])
2015/08/19 12:35:10 ERROR:OSError: [Errno 2] No such file or directory: '/home/azureuser/.ssh/authorized_keys'
2015/08/19 12:35:10 ERROR:
2015/08/19 12:35:10 ERROR:Exception: [Errno 2] No such file or directory: '/home/azureuser/.ssh/authorized_keys'
|
CalledProcessError
|
def set_selinux_context(self, path, con):
"""
Calls shell 'chcon' with 'path' and 'con' context.
Returns exit result.
"""
if self.is_selinux_system():
if not os.path.exists(path):
logger.error("Path does not exist: {0}".format(path))
return 1
return shellutil.run("chcon " + con + " " + path)
|
def set_selinux_context(self, path, con):
"""
Calls shell 'chcon' with 'path' and 'con' context.
Returns exit result.
"""
if self.is_selinux_system():
return shellutil.run("chcon " + con + " " + path)
|
https://github.com/Azure/WALinuxAgent/issues/104
|
2015/08/19 12:35:10 Created user account: azureuser
2015/08/19 12:35:10 Deploy public key:28EC0E2C8F27A2A0D081742A97D517CBD1FFFFFF
2015/08/19 12:35:10 ERROR:Failed: 28EC0E2C8F27A2A0D081742A97D517CBD1FFFFFF.pub.crt -> /home/azureuser/.ssh/authorized_keys
2015/08/19 12:35:10 ERROR:CalledProcessError. Error Code is 1
2015/08/19 12:35:10 ERROR:CalledProcessError. Command string was chcon unconfined_u:object_r:ssh_home_t:s0 /home/azureuser/.ssh/authorized_keys
2015/08/19 12:35:10 ERROR:CalledProcessError. Command result was chcon: cannot access /home/azureuser/.ssh/authorized_keys: No such file or directory
2015/08/19 12:35:10 ERROR:Traceback (most recent call last):
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 6048, in main
2015/08/19 12:35:10 ERROR: WaAgent.Run()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 5571, in Run
2015/08/19 12:35:10 ERROR: provisionError = self.Provision()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 5457, in Provision
2015/08/19 12:35:10 ERROR: error = ovfobj.Process()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 4566, in Process
2015/08/19 12:35:10 ERROR: ChangeOwner(path, self.UserName)
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 2242, in ChangeOwner
2015/08/19 12:35:10 ERROR: os.chown(filepath, p[2], p[3])
2015/08/19 12:35:10 ERROR:OSError: [Errno 2] No such file or directory: '/home/azureuser/.ssh/authorized_keys'
2015/08/19 12:35:10 ERROR:
2015/08/19 12:35:10 ERROR:Exception: [Errno 2] No such file or directory: '/home/azureuser/.ssh/authorized_keys'
|
CalledProcessError
|
def chowner(path, owner):
if not os.path.exists(path):
logger.error("Path does not exist: {0}".format(path))
else:
owner_info = pwd.getpwnam(owner)
os.chown(path, owner_info[2], owner_info[3])
|
def chowner(path, owner):
owner_info = pwd.getpwnam(owner)
os.chown(path, owner_info[2], owner_info[3])
|
https://github.com/Azure/WALinuxAgent/issues/104
|
2015/08/19 12:35:10 Created user account: azureuser
2015/08/19 12:35:10 Deploy public key:28EC0E2C8F27A2A0D081742A97D517CBD1FFFFFF
2015/08/19 12:35:10 ERROR:Failed: 28EC0E2C8F27A2A0D081742A97D517CBD1FFFFFF.pub.crt -> /home/azureuser/.ssh/authorized_keys
2015/08/19 12:35:10 ERROR:CalledProcessError. Error Code is 1
2015/08/19 12:35:10 ERROR:CalledProcessError. Command string was chcon unconfined_u:object_r:ssh_home_t:s0 /home/azureuser/.ssh/authorized_keys
2015/08/19 12:35:10 ERROR:CalledProcessError. Command result was chcon: cannot access /home/azureuser/.ssh/authorized_keys: No such file or directory
2015/08/19 12:35:10 ERROR:Traceback (most recent call last):
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 6048, in main
2015/08/19 12:35:10 ERROR: WaAgent.Run()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 5571, in Run
2015/08/19 12:35:10 ERROR: provisionError = self.Provision()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 5457, in Provision
2015/08/19 12:35:10 ERROR: error = ovfobj.Process()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 4566, in Process
2015/08/19 12:35:10 ERROR: ChangeOwner(path, self.UserName)
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 2242, in ChangeOwner
2015/08/19 12:35:10 ERROR: os.chown(filepath, p[2], p[3])
2015/08/19 12:35:10 ERROR:OSError: [Errno 2] No such file or directory: '/home/azureuser/.ssh/authorized_keys'
2015/08/19 12:35:10 ERROR:
2015/08/19 12:35:10 ERROR:Exception: [Errno 2] No such file or directory: '/home/azureuser/.ssh/authorized_keys'
|
CalledProcessError
|
def chmod(path, mode):
if not os.path.exists(path):
logger.error("Path does not exist: {0}".format(path))
else:
os.chmod(path, mode)
|
def chmod(path, mode):
os.chmod(path, mode)
|
https://github.com/Azure/WALinuxAgent/issues/104
|
2015/08/19 12:35:10 Created user account: azureuser
2015/08/19 12:35:10 Deploy public key:28EC0E2C8F27A2A0D081742A97D517CBD1FFFFFF
2015/08/19 12:35:10 ERROR:Failed: 28EC0E2C8F27A2A0D081742A97D517CBD1FFFFFF.pub.crt -> /home/azureuser/.ssh/authorized_keys
2015/08/19 12:35:10 ERROR:CalledProcessError. Error Code is 1
2015/08/19 12:35:10 ERROR:CalledProcessError. Command string was chcon unconfined_u:object_r:ssh_home_t:s0 /home/azureuser/.ssh/authorized_keys
2015/08/19 12:35:10 ERROR:CalledProcessError. Command result was chcon: cannot access /home/azureuser/.ssh/authorized_keys: No such file or directory
2015/08/19 12:35:10 ERROR:Traceback (most recent call last):
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 6048, in main
2015/08/19 12:35:10 ERROR: WaAgent.Run()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 5571, in Run
2015/08/19 12:35:10 ERROR: provisionError = self.Provision()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 5457, in Provision
2015/08/19 12:35:10 ERROR: error = ovfobj.Process()
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 4566, in Process
2015/08/19 12:35:10 ERROR: ChangeOwner(path, self.UserName)
2015/08/19 12:35:10 ERROR: File "/usr/sbin/waagent", line 2242, in ChangeOwner
2015/08/19 12:35:10 ERROR: os.chown(filepath, p[2], p[3])
2015/08/19 12:35:10 ERROR:OSError: [Errno 2] No such file or directory: '/home/azureuser/.ssh/authorized_keys'
2015/08/19 12:35:10 ERROR:
2015/08/19 12:35:10 ERROR:Exception: [Errno 2] No such file or directory: '/home/azureuser/.ssh/authorized_keys'
|
CalledProcessError
|
def _common_defaults(is_hyperband: bool) -> (Set[str], dict, dict):
mandatory = set()
default_options = {
"random_seed": np.random.randint(10000),
"opt_skip_init_length": 150,
"opt_skip_period": 1,
"profiler": False,
"opt_maxiter": 50,
"opt_nstarts": 2,
"opt_warmstart": False,
"opt_verbose": False,
"opt_debug_writer": False,
"num_fantasy_samples": 20,
"scheduler": "fifo",
"num_init_random": DEFAULT_NUM_INITIAL_RANDOM_EVALUATIONS,
"num_init_candidates": DEFAULT_NUM_INITIAL_CANDIDATES,
"initial_scoring": DEFAULT_INITIAL_SCORING,
"first_is_default": True,
"debug_log": False,
}
if is_hyperband:
default_options["opt_skip_num_max_resource"] = False
default_options["gp_resource_kernel"] = "matern52"
default_options["resource_acq"] = "bohb"
default_options["num_init_random"] = 10
constraints = {
"random_seed": Integer(),
"opt_skip_init_length": Integer(0, None),
"opt_skip_period": Integer(1, None),
"profiler": Boolean(),
"opt_maxiter": Integer(1, None),
"opt_nstarts": Integer(1, None),
"opt_warmstart": Boolean(),
"opt_verbose": Boolean(),
"opt_debug_writer": Boolean(),
"num_fantasy_samples": Integer(1, None),
"num_init_random": Integer(1, None),
"num_init_candidates": Integer(5, None),
"initial_scoring": Categorical(choices=tuple(SUPPORTED_INITIAL_SCORING)),
"first_is_default": Boolean(),
"debug_log": Boolean(),
}
if is_hyperband:
constraints["opt_skip_num_max_resource"] = Boolean()
constraints["gp_resource_kernel"] = Categorical(
choices=(
"exp-decay-sum",
"exp-decay-combined",
"exp-decay-delta1",
"matern52",
"matern52-res-warp",
)
)
constraints["resource_acq"] = Categorical(choices=("bohb", "first"))
return mandatory, default_options, constraints
|
def _common_defaults(is_hyperband: bool) -> (Set[str], dict, dict):
mandatory = set()
default_options = {
"random_seed": 31415927,
"opt_skip_init_length": 150,
"opt_skip_period": 1,
"profiler": False,
"opt_maxiter": 50,
"opt_nstarts": 2,
"opt_warmstart": False,
"opt_verbose": False,
"opt_debug_writer": False,
"num_fantasy_samples": 20,
"num_init_random": DEFAULT_NUM_INITIAL_RANDOM_EVALUATIONS,
"num_init_candidates": DEFAULT_NUM_INITIAL_CANDIDATES,
"initial_scoring": DEFAULT_INITIAL_SCORING,
"first_is_default": True,
"debug_log": False,
}
if is_hyperband:
default_options["opt_skip_num_max_resource"] = False
default_options["gp_resource_kernel"] = "matern52"
default_options["resource_acq"] = "bohb"
default_options["num_init_random"] = 10
constraints = {
"random_seed": Integer(),
"opt_skip_init_length": Integer(0, None),
"opt_skip_period": Integer(1, None),
"profiler": Boolean(),
"opt_maxiter": Integer(1, None),
"opt_nstarts": Integer(1, None),
"opt_warmstart": Boolean(),
"opt_verbose": Boolean(),
"opt_debug_writer": Boolean(),
"num_fantasy_samples": Integer(1, None),
"num_init_random": Integer(1, None),
"num_init_candidates": Integer(5, None),
"initial_scoring": Categorical(choices=tuple(SUPPORTED_INITIAL_SCORING)),
"first_is_default": Boolean(),
"debug_log": Boolean(),
}
if is_hyperband:
constraints["opt_skip_num_max_resource"] = Boolean()
constraints["gp_resource_kernel"] = Categorical(
choices=(
"exp-decay-sum",
"exp-decay-combined",
"exp-decay-delta1",
"matern52",
"matern52-res-warp",
)
)
constraints["resource_acq"] = Categorical(choices=("bohb", "first"))
return mandatory, default_options, constraints
|
https://github.com/awslabs/autogluon/issues/685
|
Traceback (most recent call last):
File "/Users/yixiaxia/Desktop/test/test.py", line 17, in <module>
searcher = GPFIFOSearcher(train_fn.cs)
TypeError: __init__() takes 1 positional argument but 2 were given
|
TypeError
|
def __init__(self, configspace, **kwargs):
_gp_searcher = kwargs.get("_gp_searcher")
if _gp_searcher is None:
kwargs["configspace"] = configspace
_kwargs = check_and_merge_defaults(
kwargs, *gp_fifo_searcher_defaults(), dict_name="search_options"
)
_gp_searcher = gp_fifo_searcher_factory(**_kwargs)
super().__init__(
_gp_searcher.hp_ranges.config_space,
reward_attribute=kwargs.get("reward_attribute"),
)
self.gp_searcher = _gp_searcher
# This lock protects gp_searcher. We are not using self.LOCK, this
# can lead to deadlocks when superclass methods are called
self._gp_lock = mp.Lock()
|
def __init__(self, **kwargs):
_gp_searcher = kwargs.get("_gp_searcher")
if _gp_searcher is None:
_kwargs = check_and_merge_defaults(
kwargs, *gp_fifo_searcher_defaults(), dict_name="search_options"
)
_gp_searcher = gp_fifo_searcher_factory(**_kwargs)
super().__init__(
_gp_searcher.hp_ranges.config_space,
reward_attribute=kwargs.get("reward_attribute"),
)
self.gp_searcher = _gp_searcher
# This lock protects gp_searcher. We are not using self.LOCK, this
# can lead to deadlocks when superclass methods are called
self._gp_lock = mp.Lock()
|
https://github.com/awslabs/autogluon/issues/685
|
Traceback (most recent call last):
File "/Users/yixiaxia/Desktop/test/test.py", line 17, in <module>
searcher = GPFIFOSearcher(train_fn.cs)
TypeError: __init__() takes 1 positional argument but 2 were given
|
TypeError
|
def fit_summary(self, verbosity=3):
"""
Output summary of information about models produced during `fit()`.
May create various generated summary plots and store them in folder: `Predictor.output_directory`.
Parameters
----------
verbosity : int, default = 3
Controls how detailed of a summary to ouput.
Set <= 0 for no output printing, 1 to print just high-level summary,
2 to print summary and create plots, >= 3 to print all information produced during fit().
Returns
-------
Dict containing various detailed information. We do not recommend directly printing this dict as it may be very large.
"""
hpo_used = len(self._trainer.hpo_results) > 0
model_typenames = {
key: self._trainer.model_types[key].__name__
for key in self._trainer.model_types
}
model_innertypenames = {
key: self._trainer.model_types_inner[key].__name__
for key in self._trainer.model_types
if key in self._trainer.model_types_inner
}
MODEL_STR = "Model"
ENSEMBLE_STR = "Ensemble"
for model in model_typenames:
if (
(model in model_innertypenames)
and (ENSEMBLE_STR not in model_innertypenames[model])
and (ENSEMBLE_STR in model_typenames[model])
):
new_model_typename = (
model_typenames[model] + "_" + model_innertypenames[model]
)
if new_model_typename.endswith(MODEL_STR):
new_model_typename = new_model_typename[: -len(MODEL_STR)]
model_typenames[model] = new_model_typename
unique_model_types = set(model_typenames.values()) # no more class info
# all fit() information that is returned:
results = {
"model_types": model_typenames, # dict with key = model-name, value = type of model (class-name)
"model_performance": self._trainer.get_model_attributes_dict(
"val_score"
), # dict with key = model-name, value = validation performance
"model_best": self._trainer.model_best, # the name of the best model (on validation data)
"model_paths": self._trainer.model_paths, # dict with key = model-name, value = path to model file
"model_fit_times": self._trainer.get_model_attributes_dict("fit_time"),
"model_pred_times": self._trainer.get_model_attributes_dict("predict_time"),
"num_bagging_folds": self._trainer.kfolds,
"stack_ensemble_levels": self._trainer.stack_ensemble_levels,
"feature_prune": self._trainer.feature_prune,
"hyperparameter_tune": hpo_used,
"hyperparameters_userspecified": self._trainer.hyperparameters,
}
if self.problem_type != REGRESSION:
results["num_classes"] = self._trainer.num_classes
if hpo_used:
results["hpo_results"] = self._trainer.hpo_results
# get dict mapping model name to final hyperparameter values for each model:
model_hyperparams = {}
for model_name in self._trainer.get_model_names_all():
model_obj = self._trainer.load_model(model_name)
model_hyperparams[model_name] = model_obj.params
results["model_hyperparams"] = model_hyperparams
if verbosity > 0: # print stuff
print("*** Summary of fit() ***")
print("Estimated performance of each model:")
results["leaderboard"] = self._learner.leaderboard(silent=False)
# self._summarize('model_performance', 'Validation performance of individual models', results)
# self._summarize('model_best', 'Best model (based on validation performance)', results)
# self._summarize('hyperparameter_tune', 'Hyperparameter-tuning used', results)
print("Number of models trained: %s" % len(results["model_performance"]))
print("Types of models trained:")
print(unique_model_types)
num_fold_str = ""
bagging_used = results["num_bagging_folds"] > 0
if bagging_used:
num_fold_str = f" (with {results['num_bagging_folds']} folds)"
print("Bagging used: %s %s" % (bagging_used, num_fold_str))
num_stack_str = ""
stacking_used = results["stack_ensemble_levels"] > 0
if stacking_used:
num_stack_str = f" (with {results['stack_ensemble_levels']} levels)"
print("Stack-ensembling used: %s %s" % (stacking_used, num_stack_str))
hpo_str = ""
if hpo_used and verbosity <= 2:
hpo_str = (
" (call fit_summary() with verbosity >= 3 to see detailed HPO info)"
)
print("Hyperparameter-tuning used: %s %s" % (hpo_used, hpo_str))
# TODO: uncomment once feature_prune is functional: self._summarize('feature_prune', 'feature-selection used', results)
print("User-specified hyperparameters:")
print(results["hyperparameters_userspecified"])
print("Feature Metadata (Processed):")
print("(raw dtype, special dtypes):")
print(self.feature_metadata)
if verbosity > 1: # create plots
plot_tabular_models(
results,
output_directory=self.output_directory,
save_file="SummaryOfModels.html",
plot_title="Models produced during fit()",
)
if hpo_used:
for model_type in results["hpo_results"]:
if "trial_info" in results["hpo_results"][model_type]:
plot_summary_of_models(
results["hpo_results"][model_type],
output_directory=self.output_directory,
save_file=model_type + "_HPOmodelsummary.html",
plot_title=f"Models produced during {model_type} HPO",
)
plot_performance_vs_trials(
results["hpo_results"][model_type],
output_directory=self.output_directory,
save_file=model_type + "_HPOperformanceVStrials.png",
plot_title=f"HPO trials for {model_type} models",
)
if verbosity > 2: # print detailed information
if hpo_used:
hpo_results = results["hpo_results"]
print("*** Details of Hyperparameter optimization ***")
for model_type in hpo_results:
hpo_model = hpo_results[model_type]
if "trial_info" in hpo_model:
print(
f"HPO for {model_type} model: Num. configurations tried = {len(hpo_model['trial_info'])}, Time spent = {hpo_model['total_time']}s, Search strategy = {hpo_model['search_strategy']}"
)
print(
f"Best hyperparameter-configuration (validation-performance: {self.eval_metric} = {hpo_model['validation_performance']}):"
)
print(hpo_model["best_config"])
"""
if bagging_used:
pass # TODO: print detailed bagging info
if stacking_used:
pass # TODO: print detailed stacking info, like how much it improves validation performance
if results['feature_prune']:
pass # TODO: print detailed feature-selection info once feature-selection is functional.
"""
if verbosity > 0:
print("*** End of fit() summary ***")
return results
|
def fit_summary(self, verbosity=3):
"""
Output summary of information about models produced during `fit()`.
May create various generated summary plots and store them in folder: `Predictor.output_directory`.
Parameters
----------
verbosity : int, default = 3
Controls how detailed of a summary to ouput.
Set <= 0 for no output printing, 1 to print just high-level summary,
2 to print summary and create plots, >= 3 to print all information produced during fit().
Returns
-------
Dict containing various detailed information. We do not recommend directly printing this dict as it may be very large.
"""
hpo_used = len(self._trainer.hpo_results) > 0
model_typenames = {
key: self._trainer.model_types[key].__name__
for key in self._trainer.model_types
}
model_innertypenames = {
key: self._trainer.model_types_inner[key].__name__
for key in self._trainer.model_types
if key in self._trainer.model_types_inner
}
MODEL_STR = "Model"
ENSEMBLE_STR = "Ensemble"
for model in model_typenames:
if (
(model in model_innertypenames)
and (ENSEMBLE_STR not in model_innertypenames[model])
and (ENSEMBLE_STR in model_typenames[model])
):
new_model_typename = (
model_typenames[model] + "_" + model_innertypenames[model]
)
if new_model_typename.endswith(MODEL_STR):
new_model_typename = new_model_typename[: -len(MODEL_STR)]
model_typenames[model] = new_model_typename
unique_model_types = set(model_typenames.values()) # no more class info
# all fit() information that is returned:
results = {
"model_types": model_typenames, # dict with key = model-name, value = type of model (class-name)
"model_performance": self._trainer.get_model_attributes_dict(
"val_score"
), # dict with key = model-name, value = validation performance
"model_best": self._trainer.model_best, # the name of the best model (on validation data)
"model_paths": self._trainer.model_paths, # dict with key = model-name, value = path to model file
"model_fit_times": self._trainer.get_model_attributes_dict("fit_time"),
"model_pred_times": self._trainer.get_model_attributes_dict("predict_time"),
"num_bagging_folds": self._trainer.kfolds,
"stack_ensemble_levels": self._trainer.stack_ensemble_levels,
"feature_prune": self._trainer.feature_prune,
"hyperparameter_tune": hpo_used,
"hyperparameters_userspecified": self._trainer.hyperparameters,
}
if self.problem_type != REGRESSION:
results["num_classes"] = self._trainer.num_classes
if hpo_used:
results["hpo_results"] = self._trainer.hpo_results
# get dict mapping model name to final hyperparameter values for each model:
model_hyperparams = {}
for model_name in self._trainer.get_model_names_all():
model_obj = self._trainer.load_model(model_name)
model_hyperparams[model_name] = model_obj.params
results["model_hyperparams"] = model_hyperparams
if verbosity > 0: # print stuff
print("*** Summary of fit() ***")
print("Estimated performance of each model:")
results["leaderboard"] = self._learner.leaderboard(silent=False)
# self._summarize('model_performance', 'Validation performance of individual models', results)
# self._summarize('model_best', 'Best model (based on validation performance)', results)
# self._summarize('hyperparameter_tune', 'Hyperparameter-tuning used', results)
print("Number of models trained: %s" % len(results["model_performance"]))
print("Types of models trained:")
print(unique_model_types)
num_fold_str = ""
bagging_used = results["num_bagging_folds"] > 0
if bagging_used:
num_fold_str = f" (with {results['num_bagging_folds']} folds)"
print("Bagging used: %s %s" % (bagging_used, num_fold_str))
num_stack_str = ""
stacking_used = results["stack_ensemble_levels"] > 0
if stacking_used:
num_stack_str = f" (with {results['stack_ensemble_levels']} levels)"
print("Stack-ensembling used: %s %s" % (stacking_used, num_stack_str))
hpo_str = ""
if hpo_used and verbosity <= 2:
hpo_str = (
" (call fit_summary() with verbosity >= 3 to see detailed HPO info)"
)
print("Hyperparameter-tuning used: %s %s" % (hpo_used, hpo_str))
# TODO: uncomment once feature_prune is functional: self._summarize('feature_prune', 'feature-selection used', results)
print("User-specified hyperparameters:")
print(results["hyperparameters_userspecified"])
print("Feature Metadata (Processed):")
print("(raw dtype, special dtypes):")
print(self.feature_metadata)
if verbosity > 1: # create plots
plot_tabular_models(
results,
output_directory=self.output_directory,
save_file="SummaryOfModels.html",
plot_title="Models produced during fit()",
)
if hpo_used:
for model_type in results["hpo_results"]:
if "trial_info" in results["hpo_results"][model_type]:
plot_summary_of_models(
results["hpo_results"][model_type],
output_directory=self.output_directory,
save_file=model_type + "_HPOmodelsummary.html",
plot_title=f"Models produced during {model_type} HPO",
)
plot_performance_vs_trials(
results["hpo_results"][model_type],
output_directory=self.output_directory,
save_file=model_type + "_HPOperformanceVStrials.png",
plot_title=f"HPO trials for {model_type} models",
)
if verbosity > 2: # print detailed information
if hpo_used:
hpo_results = results["hpo_results"]
print("*** Details of Hyperparameter optimization ***")
for model_type in hpo_results:
hpo_model = hpo_results[model_type]
print(
"HPO for %s model: Num. configurations tried = %s, Time spent = %s, Search strategy = %s"
% (
model_type,
len(hpo_model["trial_info"]),
hpo_model["total_time"],
hpo_model["search_strategy"],
)
)
print(
"Best hyperparameter-configuration (validation-performance: %s = %s):"
% (self.eval_metric, hpo_model["validation_performance"])
)
print(hpo_model["best_config"])
"""
if bagging_used:
pass # TODO: print detailed bagging info
if stacking_used:
pass # TODO: print detailed stacking info, like how much it improves validation performance
if results['feature_prune']:
pass # TODO: print detailed feature-selection info once feature-selection is functional.
"""
if verbosity > 0:
print("*** End of fit() summary ***")
return results
|
https://github.com/awslabs/autogluon/issues/675
|
*** Details of Hyperparameter optimization ***
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-2-1b709fc952c0> in <module>
11 predictor = task.fit(train_data=train_data, label=label_column, output_directory=savedir, problem_type='binary', eval_metric='accuracy', hyperparameter_tune=True )
---> 13 results = predictor.fit_summary()
14
15 # Inference time:
/usr/local/lib/python3.6/dist-packages/autogluon/task/tabular_prediction/predictor.py in fit_summary(self, verbosity)
414 hpo_model = hpo_results[model_type]
415 print("HPO for %s model: Num. configurations tried = %s, Time spent = %s, Search strategy = %s"
--> 416 % (model_type, len(hpo_model['trial_info']), hpo_model['total_time'], hpo_model['search_strategy']))
417 print("Best hyperparameter-configuration (validation-performance: %s = %s):"
418 % (self.eval_metric, hpo_model['validation_performance']))
KeyError: 'trial_info'
|
KeyError
|
def __init__(self, learner):
"""Creates TabularPredictor object.
You should not construct a TabularPredictor yourself, it is only intended to be produced during fit().
Parameters
----------
learner : `AbstractLearner` object
Object that implements the `AbstractLearner` APIs.
To access any learner method `func()` from this Predictor, use: `predictor._learner.func()`.
To access any trainer method `func()` from this `Predictor`, use: `predictor._trainer.func()`.
"""
self._learner: Learner = learner # Learner object
self._learner.persist_trainer(low_memory=True)
self._trainer: AbstractTrainer = self._learner.load_trainer() # Trainer object
self.output_directory = self._learner.path
self.problem_type = self._learner.problem_type
self.eval_metric = self._learner.eval_metric
self.label_column = self._learner.label
self.feature_metadata = self._trainer.feature_metadata
self.class_labels = self._learner.class_labels
self.class_labels_internal = (
self._learner.label_cleaner.ordered_class_labels_transformed
)
self.class_labels_internal_map = self._learner.label_cleaner.inv_map
|
def __init__(self, learner):
"""Creates TabularPredictor object.
You should not construct a TabularPredictor yourself, it is only intended to be produced during fit().
Parameters
----------
learner : `AbstractLearner` object
Object that implements the `AbstractLearner` APIs.
To access any learner method `func()` from this Predictor, use: `predictor._learner.func()`.
To access any trainer method `func()` from this `Predictor`, use: `predictor._trainer.func()`.
"""
self._learner: Learner = learner # Learner object
self._learner.persist_trainer(low_memory=True)
self._trainer: AbstractTrainer = self._learner.load_trainer() # Trainer object
self.output_directory = self._learner.path
self.problem_type = self._learner.problem_type
self.eval_metric = self._learner.eval_metric
self.label_column = self._learner.label
self.feature_types = self._trainer.feature_types_metadata
self.class_labels = self._learner.class_labels
self.class_labels_internal = (
self._learner.label_cleaner.ordered_class_labels_transformed
)
self.class_labels_internal_map = self._learner.label_cleaner.inv_map
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def fit_summary(self, verbosity=3):
"""
Output summary of information about models produced during `fit()`.
May create various generated summary plots and store them in folder: `Predictor.output_directory`.
Parameters
----------
verbosity : int, default = 3
Controls how detailed of a summary to ouput.
Set <= 0 for no output printing, 1 to print just high-level summary,
2 to print summary and create plots, >= 3 to print all information produced during fit().
Returns
-------
Dict containing various detailed information. We do not recommend directly printing this dict as it may be very large.
"""
hpo_used = len(self._trainer.hpo_results) > 0
model_typenames = {
key: self._trainer.model_types[key].__name__
for key in self._trainer.model_types
}
model_innertypenames = {
key: self._trainer.model_types_inner[key].__name__
for key in self._trainer.model_types
if key in self._trainer.model_types_inner
}
MODEL_STR = "Model"
ENSEMBLE_STR = "Ensemble"
for model in model_typenames:
if (
(model in model_innertypenames)
and (ENSEMBLE_STR not in model_innertypenames[model])
and (ENSEMBLE_STR in model_typenames[model])
):
new_model_typename = (
model_typenames[model] + "_" + model_innertypenames[model]
)
if new_model_typename.endswith(MODEL_STR):
new_model_typename = new_model_typename[: -len(MODEL_STR)]
model_typenames[model] = new_model_typename
unique_model_types = set(model_typenames.values()) # no more class info
# all fit() information that is returned:
results = {
"model_types": model_typenames, # dict with key = model-name, value = type of model (class-name)
"model_performance": self._trainer.get_model_attributes_dict(
"val_score"
), # dict with key = model-name, value = validation performance
"model_best": self._trainer.model_best, # the name of the best model (on validation data)
"model_paths": self._trainer.model_paths, # dict with key = model-name, value = path to model file
"model_fit_times": self._trainer.get_model_attributes_dict("fit_time"),
"model_pred_times": self._trainer.get_model_attributes_dict("predict_time"),
"num_bagging_folds": self._trainer.kfolds,
"stack_ensemble_levels": self._trainer.stack_ensemble_levels,
"feature_prune": self._trainer.feature_prune,
"hyperparameter_tune": hpo_used,
"hyperparameters_userspecified": self._trainer.hyperparameters,
}
if self.problem_type != REGRESSION:
results["num_classes"] = self._trainer.num_classes
if hpo_used:
results["hpo_results"] = self._trainer.hpo_results
# get dict mapping model name to final hyperparameter values for each model:
model_hyperparams = {}
for model_name in self._trainer.get_model_names_all():
model_obj = self._trainer.load_model(model_name)
model_hyperparams[model_name] = model_obj.params
results["model_hyperparams"] = model_hyperparams
if verbosity > 0: # print stuff
print("*** Summary of fit() ***")
print("Estimated performance of each model:")
results["leaderboard"] = self._learner.leaderboard(silent=False)
# self._summarize('model_performance', 'Validation performance of individual models', results)
# self._summarize('model_best', 'Best model (based on validation performance)', results)
# self._summarize('hyperparameter_tune', 'Hyperparameter-tuning used', results)
print("Number of models trained: %s" % len(results["model_performance"]))
print("Types of models trained:")
print(unique_model_types)
num_fold_str = ""
bagging_used = results["num_bagging_folds"] > 0
if bagging_used:
num_fold_str = f" (with {results['num_bagging_folds']} folds)"
print("Bagging used: %s %s" % (bagging_used, num_fold_str))
num_stack_str = ""
stacking_used = results["stack_ensemble_levels"] > 0
if stacking_used:
num_stack_str = f" (with {results['stack_ensemble_levels']} levels)"
print("Stack-ensembling used: %s %s" % (stacking_used, num_stack_str))
hpo_str = ""
if hpo_used and verbosity <= 2:
hpo_str = (
" (call fit_summary() with verbosity >= 3 to see detailed HPO info)"
)
print("Hyperparameter-tuning used: %s %s" % (hpo_used, hpo_str))
# TODO: uncomment once feature_prune is functional: self._summarize('feature_prune', 'feature-selection used', results)
print("User-specified hyperparameters:")
print(results["hyperparameters_userspecified"])
print("Feature Metadata (Processed):")
print("(raw dtype, special dtypes):")
print(self.feature_metadata)
if verbosity > 1: # create plots
plot_tabular_models(
results,
output_directory=self.output_directory,
save_file="SummaryOfModels.html",
plot_title="Models produced during fit()",
)
if hpo_used:
for model_type in results["hpo_results"]:
if "trial_info" in results["hpo_results"][model_type]:
plot_summary_of_models(
results["hpo_results"][model_type],
output_directory=self.output_directory,
save_file=model_type + "_HPOmodelsummary.html",
plot_title=f"Models produced during {model_type} HPO",
)
plot_performance_vs_trials(
results["hpo_results"][model_type],
output_directory=self.output_directory,
save_file=model_type + "_HPOperformanceVStrials.png",
plot_title=f"HPO trials for {model_type} models",
)
if verbosity > 2: # print detailed information
if hpo_used:
hpo_results = results["hpo_results"]
print("*** Details of Hyperparameter optimization ***")
for model_type in hpo_results:
hpo_model = hpo_results[model_type]
print(
"HPO for %s model: Num. configurations tried = %s, Time spent = %s, Search strategy = %s"
% (
model_type,
len(hpo_model["trial_info"]),
hpo_model["total_time"],
hpo_model["search_strategy"],
)
)
print(
"Best hyperparameter-configuration (validation-performance: %s = %s):"
% (self.eval_metric, hpo_model["validation_performance"])
)
print(hpo_model["best_config"])
"""
if bagging_used:
pass # TODO: print detailed bagging info
if stacking_used:
pass # TODO: print detailed stacking info, like how much it improves validation performance
if results['feature_prune']:
pass # TODO: print detailed feature-selection info once feature-selection is functional.
"""
if verbosity > 0:
print("*** End of fit() summary ***")
return results
|
def fit_summary(self, verbosity=3):
"""
Output summary of information about models produced during `fit()`.
May create various generated summary plots and store them in folder: `Predictor.output_directory`.
Parameters
----------
verbosity : int, default = 3
Controls how detailed of a summary to ouput.
Set <= 0 for no output printing, 1 to print just high-level summary,
2 to print summary and create plots, >= 3 to print all information produced during fit().
Returns
-------
Dict containing various detailed information. We do not recommend directly printing this dict as it may be very large.
"""
hpo_used = len(self._trainer.hpo_results) > 0
model_typenames = {
key: self._trainer.model_types[key].__name__
for key in self._trainer.model_types
}
model_innertypenames = {
key: self._trainer.model_types_inner[key].__name__
for key in self._trainer.model_types
if key in self._trainer.model_types_inner
}
MODEL_STR = "Model"
ENSEMBLE_STR = "Ensemble"
for model in model_typenames:
if (
(model in model_innertypenames)
and (ENSEMBLE_STR not in model_innertypenames[model])
and (ENSEMBLE_STR in model_typenames[model])
):
new_model_typename = (
model_typenames[model] + "_" + model_innertypenames[model]
)
if new_model_typename.endswith(MODEL_STR):
new_model_typename = new_model_typename[: -len(MODEL_STR)]
model_typenames[model] = new_model_typename
unique_model_types = set(model_typenames.values()) # no more class info
# all fit() information that is returned:
results = {
"model_types": model_typenames, # dict with key = model-name, value = type of model (class-name)
"model_performance": self._trainer.get_model_attributes_dict(
"val_score"
), # dict with key = model-name, value = validation performance
"model_best": self._trainer.model_best, # the name of the best model (on validation data)
"model_paths": self._trainer.model_paths, # dict with key = model-name, value = path to model file
"model_fit_times": self._trainer.get_model_attributes_dict("fit_time"),
"model_pred_times": self._trainer.get_model_attributes_dict("predict_time"),
"num_bagging_folds": self._trainer.kfolds,
"stack_ensemble_levels": self._trainer.stack_ensemble_levels,
"feature_prune": self._trainer.feature_prune,
"hyperparameter_tune": hpo_used,
"hyperparameters_userspecified": self._trainer.hyperparameters,
}
if self.problem_type != REGRESSION:
results["num_classes"] = self._trainer.num_classes
if hpo_used:
results["hpo_results"] = self._trainer.hpo_results
# get dict mapping model name to final hyperparameter values for each model:
model_hyperparams = {}
for model_name in self._trainer.get_model_names_all():
model_obj = self._trainer.load_model(model_name)
model_hyperparams[model_name] = model_obj.params
results["model_hyperparams"] = model_hyperparams
if verbosity > 0: # print stuff
print("*** Summary of fit() ***")
print("Estimated performance of each model:")
results["leaderboard"] = self._learner.leaderboard(silent=False)
# self._summarize('model_performance', 'Validation performance of individual models', results)
# self._summarize('model_best', 'Best model (based on validation performance)', results)
# self._summarize('hyperparameter_tune', 'Hyperparameter-tuning used', results)
print("Number of models trained: %s" % len(results["model_performance"]))
print("Types of models trained:")
print(unique_model_types)
num_fold_str = ""
bagging_used = results["num_bagging_folds"] > 0
if bagging_used:
num_fold_str = f" (with {results['num_bagging_folds']} folds)"
print("Bagging used: %s %s" % (bagging_used, num_fold_str))
num_stack_str = ""
stacking_used = results["stack_ensemble_levels"] > 0
if stacking_used:
num_stack_str = f" (with {results['stack_ensemble_levels']} levels)"
print("Stack-ensembling used: %s %s" % (stacking_used, num_stack_str))
hpo_str = ""
if hpo_used and verbosity <= 2:
hpo_str = (
" (call fit_summary() with verbosity >= 3 to see detailed HPO info)"
)
print("Hyperparameter-tuning used: %s %s" % (hpo_used, hpo_str))
# TODO: uncomment once feature_prune is functional: self._summarize('feature_prune', 'feature-selection used', results)
print("User-specified hyperparameters:")
print(results["hyperparameters_userspecified"])
if verbosity > 1: # create plots
plot_tabular_models(
results,
output_directory=self.output_directory,
save_file="SummaryOfModels.html",
plot_title="Models produced during fit()",
)
if hpo_used:
for model_type in results["hpo_results"]:
if "trial_info" in results["hpo_results"][model_type]:
plot_summary_of_models(
results["hpo_results"][model_type],
output_directory=self.output_directory,
save_file=model_type + "_HPOmodelsummary.html",
plot_title=f"Models produced during {model_type} HPO",
)
plot_performance_vs_trials(
results["hpo_results"][model_type],
output_directory=self.output_directory,
save_file=model_type + "_HPOperformanceVStrials.png",
plot_title=f"HPO trials for {model_type} models",
)
if verbosity > 2: # print detailed information
if hpo_used:
hpo_results = results["hpo_results"]
print("*** Details of Hyperparameter optimization ***")
for model_type in hpo_results:
hpo_model = hpo_results[model_type]
print(
"HPO for %s model: Num. configurations tried = %s, Time spent = %s, Search strategy = %s"
% (
model_type,
len(hpo_model["trial_info"]),
hpo_model["total_time"],
hpo_model["search_strategy"],
)
)
print(
"Best hyperparameter-configuration (validation-performance: %s = %s):"
% (self.eval_metric, hpo_model["validation_performance"])
)
print(hpo_model["best_config"])
"""
if bagging_used:
pass # TODO: print detailed bagging info
if stacking_used:
pass # TODO: print detailed stacking info, like how much it improves validation performance
if results['feature_prune']:
pass # TODO: print detailed feature-selection info once feature-selection is functional.
"""
if verbosity > 0:
print("*** End of fit() summary ***")
return results
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def fit(
train_data,
label,
tuning_data=None,
time_limits=None,
output_directory=None,
presets=None,
problem_type=None,
eval_metric=None,
stopping_metric=None,
auto_stack=False,
hyperparameter_tune=False,
feature_prune=False,
holdout_frac=None,
num_bagging_folds=0,
num_bagging_sets=None,
stack_ensemble_levels=0,
hyperparameters=None,
num_trials=None,
scheduler_options=None,
search_strategy="random",
search_options=None,
nthreads_per_trial=None,
ngpus_per_trial=None,
dist_ip_addrs=None,
visualizer="none",
verbosity=2,
**kwargs,
):
"""
Fit models to predict a column of data table based on the other columns.
Parameters
----------
train_data : str or :class:`autogluon.task.tabular_prediction.TabularDataset` or `pandas.DataFrame`
Table of the training data, which is similar to pandas DataFrame.
If str is passed, `train_data` will be loaded using the str value as the file path.
label : str
Name of the column that contains the target variable to predict.
tuning_data : str or :class:`autogluon.task.tabular_prediction.TabularDataset` or `pandas.DataFrame`, default = None
Another dataset containing validation data reserved for hyperparameter tuning (in same format as training data).
If str is passed, `tuning_data` will be loaded using the str value as the file path.
Note: final model returned may be fit on this tuning_data as well as train_data. Do not provide your evaluation test data here!
In particular, when `num_bagging_folds` > 0 or `stack_ensemble_levels` > 0, models will be trained on both `tuning_data` and `train_data`.
If `tuning_data = None`, `fit()` will automatically hold out some random validation examples from `train_data`.
time_limits : int, default = None
Approximately how long `fit()` should run for (wallclock time in seconds).
If not specified, `fit()` will run until all models have completed training, but will not repeatedly bag models unless `num_bagging_sets` or `auto_stack` is specified.
output_directory : str, default = None
Path to directory where models and intermediate outputs should be saved.
If unspecified, a time-stamped folder called "AutogluonModels/ag-[TIMESTAMP]" will be created in the working directory to store all models.
Note: To call `fit()` twice and save all results of each fit, you must specify different `output_directory` locations.
Otherwise files from first `fit()` will be overwritten by second `fit()`.
presets : list or str or dict, default = 'medium_quality_faster_train'
List of preset configurations for various arguments in `fit()`. Can significantly impact predictive accuracy, memory-footprint, and inference latency of trained models, and various other properties of the returned `predictor`.
It is recommended to specify presets and avoid specifying most other `fit()` arguments or model hyperparameters prior to becoming familiar with AutoGluon.
As an example, to get the most accurate overall predictor (regardless of its efficiency), set `presets='best_quality'`.
To get good quality with minimal disk usage, set `presets=['good_quality_faster_inference_only_refit', 'optimize_for_deployment']`
Any user-specified arguments in `fit()` will override the values used by presets.
If specifying a list of presets, later presets will override earlier presets if they alter the same argument.
For precise definitions of the provided presets, see file: `autogluon/tasks/tabular_prediction/presets_configs.py`.
Users can specify custom presets by passing in a dictionary of argument values as an element to the list.
Available Presets: ['best_quality', 'best_quality_with_high_quality_refit', 'high_quality_fast_inference_only_refit', 'good_quality_faster_inference_only_refit', 'medium_quality_faster_train', 'optimize_for_deployment', 'ignore_text']
It is recommended to only use one `quality` based preset in a given call to `fit()` as they alter many of the same arguments and are not compatible with each-other.
In-depth Preset Info:
best_quality={'auto_stack': True}
Best predictive accuracy with little consideration to inference time or disk usage. Achieve even better results by specifying a large time_limits value.
Recommended for applications that benefit from the best possible model accuracy.
best_quality_with_high_quality_refit={'auto_stack': True, 'refit_full': True}
Identical to best_quality but additionally trains refit_full models that have slightly lower predictive accuracy but are over 10x faster during inference and require 10x less disk space.
high_quality_fast_inference_only_refit={'auto_stack': True, 'refit_full': True, 'set_best_to_refit_full': True, 'save_bagged_folds': False}
High predictive accuracy with fast inference. ~10x-200x faster inference and ~10x-200x lower disk usage than `best_quality`.
Recommended for applications that require reasonable inference speed and/or model size.
good_quality_faster_inference_only_refit={'auto_stack': True, 'refit_full': True, 'set_best_to_refit_full': True, 'save_bagged_folds': False, 'hyperparameters': 'light'}
Good predictive accuracy with very fast inference. ~4x faster inference and ~4x lower disk usage than `high_quality_fast_inference_only_refit`.
Recommended for applications that require fast inference speed.
medium_quality_faster_train={'auto_stack': False}
Medium predictive accuracy with very fast inference and very fast training time. ~20x faster training than `good_quality_faster_inference_only_refit`.
This is the default preset in AutoGluon, but should generally only be used for quick prototyping, as `good_quality_faster_inference_only_refit` results in significantly better predictive accuracy and faster inference time.
optimize_for_deployment={'keep_only_best': True, 'save_space': True}
Optimizes result immediately for deployment by deleting unused models and removing training artifacts.
Often can reduce disk usage by ~2-4x with no negatives to model accuracy or inference speed.
This will disable numerous advanced functionality, but has no impact on inference.
This will make certain functionality less informative, such as `predictor.leaderboard()` and `predictor.fit_summary()`.
Because unused models will be deleted under this preset, methods like `predictor.leaderboard()` and `predictor.fit_summary()` will no longer show the full set of models that were trained during `fit()`.
Recommended for applications where the inner details of AutoGluon's training is not important and there is no intention of manually choosing between the final models.
This preset pairs well with the other presets such as `good_quality_faster_inference_only_refit` to make a very compact final model.
Identical to calling `predictor.delete_models(models_to_keep='best', dry_run=False)` and `predictor.save_space()` directly after `fit()`.
ignore_text={'feature_generator_kwargs': {'enable_text_ngram_features': False, 'enable_text_special_features': False}}
Disables automated feature generation when text features are detected.
This is useful to determine how beneficial text features are to the end result, as well as to ensure features are not mistaken for text when they are not.
problem_type : str, default = None
Type of prediction problem, i.e. is this a binary/multiclass classification or regression problem (options: 'binary', 'multiclass', 'regression').
If `problem_type = None`, the prediction problem type is inferred based on the label-values in provided dataset.
eval_metric : function or str, default = None
Metric by which predictions will be ultimately evaluated on test data.
AutoGluon tunes factors such as hyperparameters, early-stopping, ensemble-weights, etc. in order to improve this metric on validation data.
If `eval_metric = None`, it is automatically chosen based on `problem_type`.
Defaults to 'accuracy' for binary and multiclass classification and 'root_mean_squared_error' for regression.
Otherwise, options for classification:
['accuracy', 'balanced_accuracy', 'f1', 'f1_macro', 'f1_micro', 'f1_weighted',
'roc_auc', 'average_precision', 'precision', 'precision_macro', 'precision_micro', 'precision_weighted',
'recall', 'recall_macro', 'recall_micro', 'recall_weighted', 'log_loss', 'pac_score']
Options for regression:
['root_mean_squared_error', 'mean_squared_error', 'mean_absolute_error', 'median_absolute_error', 'r2']
For more information on these options, see `sklearn.metrics`: https://scikit-learn.org/stable/modules/classes.html#sklearn-metrics-metrics
You can also pass your own evaluation function here as long as it follows formatting of the functions defined in `autogluon/utils/tabular/metrics/`.
stopping_metric : function or str, default = None
Metric which iteratively-trained models use to early stop to avoid overfitting.
`stopping_metric` is not used by weighted ensembles, instead weighted ensembles maximize `eval_metric`.
Defaults to `eval_metric` value except when `eval_metric='roc_auc'`, where it defaults to `log_loss`.
Options are identical to options for `eval_metric`.
auto_stack : bool, default = False
Whether AutoGluon should automatically utilize bagging and multi-layer stack ensembling to boost predictive accuracy.
Set this = True if you are willing to tolerate longer training times in order to maximize predictive accuracy!
Note: This overrides `num_bagging_folds` and `stack_ensemble_levels` arguments (selects optimal values for these parameters based on dataset properties).
Note: This can increase training time (and inference time) by up to 20x, but can greatly improve predictive performance.
hyperparameter_tune : bool, default = False
Whether to tune hyperparameters or just use fixed hyperparameter values for each model. Setting as True will increase `fit()` runtimes.
It is currently not recommended to use `hyperparameter_tune` with `auto_stack` due to potential overfitting.
Use `auto_stack` to maximize predictive accuracy; use `hyperparameter_tune` if you prefer to deploy just a single model rather than an ensemble.
feature_prune : bool, default = False
Whether or not to perform feature selection.
hyperparameters : str or dict, default = 'default'
Determines the hyperparameters used by the models.
If `str` is passed, will use a preset hyperparameter configuration.
Valid `str` options: ['default', 'light', 'very_light', 'toy']
'default': Default AutoGluon hyperparameters intended to maximize accuracy without significant regard to inference time or disk usage.
'light': Results in smaller models. Generally will make inference speed much faster and disk usage much lower, but with worse accuracy.
'very_light': Results in much smaller models. Behaves similarly to 'light', but in many cases with over 10x less disk usage and a further reduction in accuracy.
'toy': Results in extremely small models. Only use this when prototyping, as the model quality will be severely reduced.
Reference `autogluon/task/tabular_prediction/hyperparameter_configs.py` for information on the hyperparameters associated with each preset.
Keys are strings that indicate which model types to train.
Options include: 'NN' (neural network), 'GBM' (lightGBM boosted trees), 'CAT' (CatBoost boosted trees), 'RF' (random forest), 'XT' (extremely randomized trees), 'KNN' (k-nearest neighbors), 'LR' (linear regression)
If certain key is missing from hyperparameters, then `fit()` will not train any models of that type. Omitting a model key from hyperparameters is equivalent to including this model key in `excluded_model_types`.
For example, set `hyperparameters = { 'NN':{...} }` if say you only want to train neural networks and no other types of models.
Values = dict of hyperparameter settings for each model type, or list of dicts.
Each hyperparameter can either be a single fixed value or a search space containing many possible values.
Unspecified hyperparameters will be set to default values (or default search spaces if `hyperparameter_tune = True`).
Caution: Any provided search spaces will be overridden by fixed defaults if `hyperparameter_tune = False`.
To train multiple models of a given type, set the value to a list of hyperparameter dictionaries.
For example, `hyperparameters = {'RF': [{'criterion': 'gini'}, {'criterion': 'entropy'}]}` will result in 2 random forest models being trained with separate hyperparameters.
Advanced functionality: Custom models
`hyperparameters` can also take a special key 'custom', which maps to a list of model names (currently supported options = 'GBM').
If `hyperparameter_tune = False`, then these additional models will also be trained using custom pre-specified hyperparameter settings that are known to work well.
Advanced functionality: Custom stack levels
By default, AutoGluon re-uses the same models and model hyperparameters at each level during stack ensembling.
To customize this behaviour, create a hyperparameters dictionary separately for each stack level, and then add them as values to a new dictionary, with keys equal to the stack level.
Example: `hyperparameters = {0: {'RF': rf_params1}, 1: {'CAT': [cat_params1, cat_params2], 'NN': {}}}`
This will result in a stack ensemble that has one custom random forest in level 0 followed by two CatBoost models with custom hyperparameters and a default neural network in level 1, for a total of 4 models.
If a level is not specified in `hyperparameters`, it will default to using the highest specified level to train models. This can also be explicitly controlled by adding a 'default' key.
Default:
hyperparameters = {
'NN': {},
'GBM': {},
'CAT': {},
'RF': [
{'criterion': 'gini', 'AG_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}},
{'criterion': 'entropy', 'AG_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}},
{'criterion': 'mse', 'AG_args': {'name_suffix': 'MSE', 'problem_types': ['regression']}},
],
'XT': [
{'criterion': 'gini', 'AG_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}},
{'criterion': 'entropy', 'AG_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}},
{'criterion': 'mse', 'AG_args': {'name_suffix': 'MSE', 'problem_types': ['regression']}},
],
'KNN': [
{'weights': 'uniform', 'AG_args': {'name_suffix': 'Unif'}},
{'weights': 'distance', 'AG_args': {'name_suffix': 'Dist'}},
],
'custom': ['GBM']
}
Details regarding the hyperparameters you can specify for each model are provided in the following files:
NN: `autogluon/utils/tabular/ml/models/tabular_nn/hyperparameters/parameters.py`
Note: certain hyperparameter settings may cause these neural networks to train much slower.
GBM: `autogluon/utils/tabular/ml/models/lgb/hyperparameters/parameters.py`
See also the lightGBM docs: https://lightgbm.readthedocs.io/en/latest/Parameters.html
CAT: `autogluon/utils/tabular/ml/models/catboost/hyperparameters/parameters.py`
See also the CatBoost docs: https://catboost.ai/docs/concepts/parameter-tuning.html
RF: See sklearn documentation: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html
Note: Hyperparameter tuning is disabled for this model.
XT: See sklearn documentation: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesClassifier.html
Note: Hyperparameter tuning is disabled for this model.
KNN: See sklearn documentation: https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html
Note: Hyperparameter tuning is disabled for this model.
LR: `autogluon/utils/tabular/ml/models/lr/hyperparameters/parameters.py`
Note: Hyperparameter tuning is disabled for this model.
Note: 'penalty' parameter can be used for regression to specify regularization method: 'L1' and 'L2' values are supported.
Advanced functionality: Custom AutoGluon model arguments
These arguments are optional and can be specified in any model's hyperparameters.
Example: `hyperparameters = {'RF': {..., 'AG_args': {'name_suffix': 'CustomModelSuffix', 'disable_in_hpo': True}}`
AG_args: Dictionary of customization options related to meta properties of the model such as its name, the order it is trained, and the problem types it is valid for.
Valid keys:
name: (str) The name of the model. This overrides AutoGluon's naming logic and all other name arguments if present.
name_main: (str) The main name of the model. In 'RandomForestClassifier', this is 'RandomForest'.
name_prefix: (str) Add a custom prefix to the model name. Unused by default.
name_type_suffix: (str) Override the type suffix of the model name. In 'RandomForestClassifier', this is 'Classifier'. This comes before 'name_suffix'.
name_suffix: (str) Add a custom suffix to the model name. Unused by default.
priority: (int) Determines the order in which the model is trained. Larger values result in the model being trained earlier. Default values range from 100 (RF) to 0 (custom), dictated by model type. If you want this model to be trained first, set priority = 999.
problem_types: (list) List of valid problem types for the model. `problem_types=['binary']` will result in the model only being trained if `problem_type` is 'binary'.
disable_in_hpo: (bool) If True, the model will only be trained if `hyperparameter_tune=False`.
Reference the default hyperparameters for example usage of these options.
AG_args_fit: Dictionary of model fit customization options related to how and with what constraints the model is trained. These parameters affect stacker fold models, but not stacker models themselves.
Clarification: `time_limit` is the internal time in seconds given to a particular model to train, which is dictated in part by the `time_limits` argument given during `fit()` but is not the same.
Valid keys:
max_memory_usage_ratio: (float, default=1.0) The ratio of memory usage relative to the default to allow before early stopping or killing the model. Values greater than 1.0 will be increasingly prone to out-of-memory errors.
max_time_limit_ratio: (float, default=1.0) The ratio of the provided time_limit to use during model `fit()`. If `time_limit=10` and `max_time_limit_ratio=0.3`, time_limit would be changed to 3. Does not alter max_time_limit or min_time_limit values.
max_time_limit: (float, default=None) Maximum amount of time to allow this model to train for (in sec). If the provided time_limit is greater than this value, it will be replaced by max_time_limit.
min_time_limit: (float, default=0) Allow this model to train for at least this long (in sec), regardless of the time limit it would otherwise be granted.
If `min_time_limit >= max_time_limit`, time_limit will be set to min_time_limit.
If `min_time_limit=None`, time_limit will be set to None and the model will have no training time restriction.
holdout_frac : float, default = None
Fraction of train_data to holdout as tuning data for optimizing hyperparameters (ignored unless `tuning_data = None`, ignored if `num_bagging_folds != 0`).
Default value (if None) is selected based on the number of rows in the training data. Default values range from 0.2 at 2,500 rows to 0.01 at 250,000 rows.
Default value is doubled if `hyperparameter_tune = True`, up to a maximum of 0.2.
Disabled if `num_bagging_folds >= 2`.
num_bagging_folds : int, default = 0
Number of folds used for bagging of models. When `num_bagging_folds = k`, training time is roughly increased by a factor of `k` (set = 0 to disable bagging).
Disabled by default, but we recommend values between 5-10 to maximize predictive performance.
Increasing num_bagging_folds will result in models with lower bias but that are more prone to overfitting.
Values > 10 may produce diminishing returns, and can even harm overall results due to overfitting.
To further improve predictions, avoid increasing num_bagging_folds much beyond 10 and instead increase num_bagging_sets.
num_bagging_sets : int, default = None
Number of repeats of kfold bagging to perform (values must be >= 1). Total number of models trained during bagging = num_bagging_folds * num_bagging_sets.
Defaults to 1 if time_limits is not specified, otherwise 20 (always disabled if num_bagging_folds is not specified).
Values greater than 1 will result in superior predictive performance, especially on smaller problems and with stacking enabled (reduces overall variance).
stack_ensemble_levels : int, default = 0
Number of stacking levels to use in stack ensemble. Roughly increases model training time by factor of `stack_ensemble_levels+1` (set = 0 to disable stack ensembling).
Disabled by default, but we recommend values between 1-3 to maximize predictive performance.
To prevent overfitting, this argument is ignored unless you have also set `num_bagging_folds >= 2`.
num_trials : int, default = None
Maximal number of different hyperparameter settings of each model type to evaluate during HPO (only matters if `hyperparameter_tune = True`).
If both `time_limits` and `num_trials` are specified, `time_limits` takes precedent.
scheduler_options : dict, default = None
Extra arguments passed to __init__ of scheduler, to configure the orchestration of training jobs during hyperparameter-tuning.
Ignored if `hyperparameter_tune=False`.
search_strategy : str, default = 'random'
Which hyperparameter search algorithm to use (only matters if `hyperparameter_tune=True`).
Options include: 'random' (random search), 'bayesopt' (Gaussian process Bayesian optimization), 'skopt' (SKopt Bayesian optimization), 'grid' (grid search).
search_options : dict, default = None
Auxiliary keyword arguments to pass to the searcher that performs hyperparameter optimization.
nthreads_per_trial : int, default = None
How many CPUs to use in each training run of an individual model.
This is automatically determined by AutoGluon when left as None (based on available compute).
ngpus_per_trial : int, default = None
How many GPUs to use in each trial (ie. single training run of a model).
This is automatically determined by AutoGluon when left as None.
dist_ip_addrs : list, default = None
List of IP addresses corresponding to remote workers, in order to leverage distributed computation.
visualizer : str, default = 'none'
How to visualize the neural network training progress during `fit()`. Options: ['mxboard', 'tensorboard', 'none'].
verbosity : int, default = 2
Verbosity levels range from 0 to 4 and control how much information is printed during fit().
Higher levels correspond to more detailed print statements (you can set verbosity = 0 to suppress warnings).
If using logging, you can alternatively control amount of information printed via `logger.setLevel(L)`,
where `L` ranges from 0 to 50 (Note: higher values of `L` correspond to fewer print statements, opposite of verbosity levels)
Kwargs can include additional arguments for advanced users:
AG_args_fit : dict, default={}
Keyword arguments to pass to all models. See the `AG_args_fit` argument from "Advanced functionality: Custom AutoGluon model arguments" in the `hyperparameters` argument documentation for valid values.
Identical to specifying `AG_args_fit` parameter for all models in `hyperparameters`.
If a key in `AG_args_fit` is already specified for a model in `hyperparameters`, it will not be altered through this argument.
excluded_model_types : list, default = []
Banned subset of model types to avoid training during `fit()`, even if present in `hyperparameters`.
Valid values: ['RF', 'XT', 'KNN', 'GBM', 'CAT', 'NN', 'LR', 'custom']. Reference `hyperparameters` documentation for what models correspond to each value.
Useful when a particular model type such as 'KNN' or 'custom' is not desired but altering the `hyperparameters` dictionary is difficult or time-consuming.
Example: To exclude both 'KNN' and 'custom' models, specify `excluded_model_types=['KNN', 'custom']`.
id_columns : list, default = []
Banned subset of column names that model may not use as predictive features (e.g. contains label, user-ID, etc).
These columns are ignored during `fit()`, but DataFrame of just these columns with appended predictions may be produced, for example to submit in a ML competition.
label_count_threshold : int, default = 10
For multi-class classification problems, this is the minimum number of times a label must appear in dataset in order to be considered an output class.
AutoGluon will ignore any classes whose labels do not appear at least this many times in the dataset (i.e. will never predict them).
save_bagged_folds : bool, default = True
If True, bagged models will save their fold models (the models from each individual fold of bagging). This is required to use bagged models for prediction after `fit()`.
If False, bagged models will not save their fold models. This means that bagged models will not be valid models during inference.
This should only be set to False when planning to call `predictor.refit_full()` or when `refit_full` is set and `set_best_to_refit_full=True`.
Particularly useful if disk usage is a concern. By not saving the fold models, bagged models will use only very small amounts of disk space during training.
In many training runs, this will reduce peak disk usage by >10x.
This parameter has no effect if bagging is disabled.
keep_only_best : bool, default = False
If True, only the best model and its ancestor models are saved in the outputted `predictor`. All other models are deleted.
If you only care about deploying the most accurate predictor with the smallest file-size and no longer need any of the other trained models or functionality beyond prediction on new data, then set: `keep_only_best=True`, `save_space=True`.
This is equivalent to calling `predictor.delete_models(models_to_keep='best', dry_run=False)` directly after `fit()`.
If used with `refit_full` and `set_best_to_refit_full`, the best model will be the refit_full model, and the original bagged best model will be deleted.
`refit_full` will be automatically set to 'best' in this case to avoid training models which will be later deleted.
save_space : bool, default = False
If True, reduces the memory and disk size of predictor by deleting auxiliary model files that aren't needed for prediction on new data.
This is equivalent to calling `predictor.save_space()` directly after `fit()`.
This has NO impact on inference accuracy.
It is recommended if the only goal is to use the trained model for prediction.
Certain advanced functionality may no longer be available if `save_space=True`. Refer to `predictor.save_space()` documentation for more details.
cache_data : bool, default = True
When enabled, the training and validation data are saved to disk for future reuse.
Enables advanced functionality in the resulting Predictor object such as feature importance calculation on the original data.
refit_full : bool or str, default = False
Whether to retrain all models on all of the data (training + validation) after the normal training procedure.
This is equivalent to calling `predictor.refit_full(model=refit_full)` after training.
If `refit_full=True`, it will be treated as `refit_full='all'`.
If `refit_full=False`, refitting will not occur.
Valid str values:
`all`: refits all models.
`best`: refits only the best model (and its ancestors if it is a stacker model).
`{model_name}`: refits only the specified model (and its ancestors if it is a stacker model).
For bagged models:
Reduces a model's inference time by collapsing bagged ensembles into a single model fit on all of the training data.
This process will typically result in a slight accuracy reduction and a large inference speedup.
The inference speedup will generally be between 10-200x faster than the original bagged ensemble model.
The inference speedup factor is equivalent to (k * n), where k is the number of folds (`num_bagging_folds`) and n is the number of finished repeats (`num_bagging_sets`) in the bagged ensemble.
The runtime is generally 10% or less of the original fit runtime.
The runtime can be roughly estimated as 1 / (k * n) of the original fit runtime, with k and n defined above.
For non-bagged models:
Optimizes a model's accuracy by retraining on 100% of the data without using a validation set.
Will typically result in a slight accuracy increase and no change to inference time.
The runtime will be approximately equal to the original fit runtime.
This process does not alter the original models, but instead adds additional models.
If stacker models are refit by this process, they will use the refit_full versions of the ancestor models during inference.
Models produced by this process will not have validation scores, as they use all of the data for training.
Therefore, it is up to the user to determine if the models are of sufficient quality by including test data in `predictor.leaderboard(dataset=test_data)`.
If the user does not have additional test data, they should reference the original model's score for an estimate of the performance of the refit_full model.
Warning: Be aware that utilizing refit_full models without separately verifying on test data means that the model is untested, and has no guarantee of being consistent with the original model.
The time taken by this process is not enforced by `time_limits`.
`cache_data` must be set to `True` to enable this functionality.
set_best_to_refit_full : bool, default = False
If True, will set Trainer.best_model = Trainer.full_model_dict[Trainer.best_model]
This will change the default model that Predictor uses for prediction when model is not specified to the refit_full version of the model that previously exhibited the highest validation score.
Only valid if `refit_full` is set.
feature_generator : `autogluon.utils.tabular.features.generators.abstract.AbstractFeatureGenerator`, default = `autogluon.utils.tabular.features.generators.auto_ml_pipeline.AutoMLPipelineFeatureGenerator()`
The feature generator used by AutoGluon to process the input data to the form sent to the models. This often includes automated feature generation and data cleaning.
It is generally recommended to keep the default feature generator unless handling an advanced use-case.
To control aspects of the default feature generation process, you can pass in an AutoMLPipelineFeatureGenerator object constructed using some of these kwargs:
enable_numeric_features : bool, default True
Whether to keep features of 'int' and 'float' raw types.
These features are passed without alteration to the models.
Appends IdentityFeatureGenerator(infer_features_in_args=dict(valid_raw_types=['int', 'float']))) to the generator group.
enable_categorical_features : bool, default True
Whether to keep features of 'object' and 'category' raw types.
These features are processed into memory optimized 'category' features.
Appends CategoryFeatureGenerator() to the generator group.
enable_datetime_features : bool, default True
Whether to keep features of 'datetime' raw type and 'object' features identified as 'datetime_as_object' features.
These features will be converted to 'int' features representing milliseconds since epoch.
Appends DatetimeFeatureGenerator() to the generator group.
enable_text_special_features : bool, default True
Whether to use 'object' features identified as 'text' features to generate 'text_special' features such as word count, capital letter ratio, and symbol counts.
Appends TextSpecialFeatureGenerator() to the generator group.
enable_text_ngram_features : bool, default True
Whether to use 'object' features identified as 'text' features to generate 'text_ngram' features.
Appends TextNgramFeatureGenerator(vectorizer=vectorizer) to the generator group.
vectorizer : CountVectorizer, default CountVectorizer(min_df=30, ngram_range=(1, 3), max_features=10000, dtype=np.uint8)
sklearn CountVectorizer object to use in TextNgramFeatureGenerator.
Only used if `enable_text_ngram_features=True`.
trainer_type : `Trainer` class, default=`AutoTrainer`
A class inheriting from `autogluon.utils.tabular.ml.trainer.abstract_trainer.AbstractTrainer` that controls training/ensembling of many models.
Note: In order to use a custom `Trainer` class, you must import the class file that defines it into the current Python session.
random_seed : int, default = 0
Seed to use when generating data split indices such as kfold splits and train/validation splits.
Caution: This seed only enables reproducible data splits (and the ability to randomize splits in each run by changing seed values).
This seed is NOT used in the training of individual models, for that you need to explicitly set the corresponding seed hyperparameter (usually called 'seed_value') of each individual model.
If stacking is enabled:
The seed used for stack level L is equal to `seed+L`.
This means `random_seed=1` will have the same split indices at L=0 as `random_seed=0` will have at L=1.
If `random_seed=None`, a random integer is used.
Returns
-------
:class:`autogluon.task.tabular_prediction.TabularPredictor` object which can make predictions on new data and summarize what happened during `fit()`.
Examples
--------
>>> from autogluon import TabularPrediction as task
>>> train_data = task.Dataset(file_path='https://autogluon.s3.amazonaws.com/datasets/Inc/train.csv')
>>> label_column = 'class'
>>> predictor = task.fit(train_data=train_data, label=label_column)
>>> test_data = task.Dataset(file_path='https://autogluon.s3.amazonaws.com/datasets/Inc/test.csv')
>>> y_test = test_data[label_column]
>>> test_data = test_data.drop(labels=[label_column], axis=1)
>>> y_pred = predictor.predict(test_data)
>>> perf = predictor.evaluate_predictions(y_true=y_test, y_pred=y_pred)
>>> results = predictor.fit_summary()
To maximize predictive performance, use the following:
>>> eval_metric = 'roc_auc' # set this to the metric you ultimately care about
>>> time_limits = 360 # set as long as you are willing to wait (in sec)
>>> predictor = task.fit(train_data=train_data, label=label_column, eval_metric=eval_metric, auto_stack=True, time_limits=time_limits)
"""
assert search_strategy != "bayesopt_hyperband", (
"search_strategy == 'bayesopt_hyperband' not yet supported"
)
if verbosity < 0:
verbosity = 0
elif verbosity > 4:
verbosity = 4
logger.setLevel(verbosity2loglevel(verbosity))
allowed_kwarg_names = {
"feature_generator",
"feature_generator_type", # TODO: Remove on 0.1.0 release
"feature_generator_kwargs", # TODO: Remove on 0.1.0 release
"trainer_type",
"AG_args_fit",
"excluded_model_types",
"label_count_threshold",
"id_columns",
"set_best_to_refit_full",
"save_bagged_folds",
"keep_only_best",
"save_space",
"cache_data",
"refit_full",
"random_seed",
"enable_fit_continuation", # TODO: Remove on 0.1.0 release
}
for kwarg_name in kwargs.keys():
if kwarg_name not in allowed_kwarg_names:
raise ValueError("Unknown keyword argument specified: %s" % kwarg_name)
if isinstance(train_data, str):
train_data = TabularDataset(file_path=train_data)
if tuning_data is not None and isinstance(tuning_data, str):
tuning_data = TabularDataset(file_path=tuning_data)
if len(set(train_data.columns)) < len(train_data.columns):
raise ValueError(
"Column names are not unique, please change duplicated column names (in pandas: train_data.rename(columns={'current_name':'new_name'})"
)
if tuning_data is not None:
train_features = np.array(
[column for column in train_data.columns if column != label]
)
tuning_features = np.array(
[column for column in tuning_data.columns if column != label]
)
if np.any(train_features != tuning_features):
raise ValueError("Column names must match between training and tuning data")
if feature_prune:
feature_prune = False # TODO: Fix feature pruning to add back as an option
# Currently disabled, needs to be updated to align with new model class functionality
logger.log(
30, "Warning: feature_prune does not currently work, setting to False."
)
cache_data = kwargs.get("cache_data", True)
refit_full = kwargs.get("refit_full", False)
# TODO: Remove on 0.1.0 release
if "enable_fit_continuation" in kwargs.keys():
logger.log(
30,
"Warning: `enable_fit_continuation` is a deprecated parameter. It has been renamed to `cache_data`. Starting from AutoGluon 0.1.0, specifying `enable_fit_continuation` as a parameter will cause an exception.",
)
logger.log(
30, "Setting `cache_data` value equal to `enable_fit_continuation` value."
)
cache_data = kwargs["enable_fit_continuation"]
if not cache_data:
logger.log(
30,
"Warning: `cache_data=False` will disable or limit advanced functionality after training such as feature importance calculations. It is recommended to set `cache_data=True` unless you explicitly wish to not have the data saved to disk.",
)
if refit_full:
raise ValueError(
"`refit_full=True` is only available when `cache_data=True`. Set `cache_data=True` to utilize `refit_full`."
)
set_best_to_refit_full = kwargs.get("set_best_to_refit_full", False)
if set_best_to_refit_full and not refit_full:
raise ValueError(
"`set_best_to_refit_full=True` is only available when `refit_full=True`. Set `refit_full=True` to utilize `set_best_to_refit_full`."
)
save_bagged_folds = kwargs.get("save_bagged_folds", True)
if hyperparameter_tune:
logger.log(
30,
"Warning: `hyperparameter_tune=True` is currently experimental and may cause the process to hang. Setting `auto_stack=True` instead is recommended to achieve maximum quality models.",
)
if dist_ip_addrs is None:
dist_ip_addrs = []
if search_options is None:
search_options = dict()
if hyperparameters is None:
hyperparameters = "default"
if isinstance(hyperparameters, str):
hyperparameters = get_hyperparameter_config(hyperparameters)
# Process kwargs to create feature generator, trainer, schedulers, searchers for each model:
output_directory = setup_outputdir(output_directory) # Format directory name
if "feature_generator_type" in kwargs or "feature_generator_kwargs" in kwargs:
logger.log(
30,
"Warning: `feature_generator_type` and `feature_generator_kwargs` are deprecated arguments. Use `feature_generator` instead. Starting from AutoGluon 0.1.0, specifying these arguments will cause an exception.",
)
feature_generator_type = kwargs.get(
"feature_generator_type", AutoMLPipelineFeatureGenerator
)
feature_generator_kwargs = kwargs.get("feature_generator_kwargs", {})
feature_generator = feature_generator_type(
**feature_generator_kwargs
) # instantiate FeatureGenerator object
else:
feature_generator = kwargs.get(
"feature_generator", AutoMLPipelineFeatureGenerator()
)
id_columns = kwargs.get("id_columns", [])
trainer_type = kwargs.get("trainer_type", AutoTrainer)
ag_args_fit = kwargs.get("AG_args_fit", {})
excluded_model_types = kwargs.get("excluded_model_types", [])
random_seed = kwargs.get("random_seed", 0)
nthreads_per_trial, ngpus_per_trial = setup_compute(
nthreads_per_trial, ngpus_per_trial
)
num_train_rows = len(train_data)
if auto_stack:
# TODO: What about datasets that are 100k+? At a certain point should we not bag?
# TODO: What about time_limits? Metalearning can tell us expected runtime of each model, then we can select optimal folds + stack levels to fit time constraint
num_bagging_folds = min(10, max(5, math.floor(num_train_rows / 100)))
stack_ensemble_levels = min(1, max(0, math.floor(num_train_rows / 750)))
if num_bagging_sets is None:
if num_bagging_folds >= 2:
if time_limits is not None:
num_bagging_sets = 20
else:
num_bagging_sets = 1
else:
num_bagging_sets = 1
label_count_threshold = kwargs.get("label_count_threshold", 10)
if (
num_bagging_folds is not None
): # Ensure there exist sufficient labels for stratified splits across all bags
label_count_threshold = max(label_count_threshold, num_bagging_folds)
time_limits_orig = copy.deepcopy(time_limits)
time_limits_hpo = copy.deepcopy(time_limits)
if num_bagging_folds >= 2 and (time_limits_hpo is not None):
time_limits_hpo = time_limits_hpo / (
1 + num_bagging_folds * (1 + stack_ensemble_levels)
)
time_limits_hpo, num_trials = setup_trial_limits(
time_limits_hpo, num_trials, hyperparameters
) # TODO: Move HPO time allocation to Trainer
if (num_trials is not None) and hyperparameter_tune and (num_trials == 1):
hyperparameter_tune = False
logger.log(
30,
"Warning: Specified num_trials == 1 or time_limits is too small for hyperparameter_tune, setting to False.",
)
if holdout_frac is None:
holdout_frac = default_holdout_frac(num_train_rows, hyperparameter_tune)
# Add visualizer to NN hyperparameters:
if (
(visualizer is not None)
and (visualizer != "none")
and ("NN" in hyperparameters)
):
hyperparameters["NN"]["visualizer"] = visualizer
eval_metric = get_metric(eval_metric, problem_type, "eval_metric")
stopping_metric = get_metric(stopping_metric, problem_type, "stopping_metric")
# All models use the same scheduler:
scheduler_options = compile_scheduler_options(
scheduler_options=scheduler_options,
search_strategy=search_strategy,
search_options=search_options,
nthreads_per_trial=nthreads_per_trial,
ngpus_per_trial=ngpus_per_trial,
checkpoint=None,
num_trials=num_trials,
time_out=time_limits_hpo,
resume=False,
visualizer=visualizer,
time_attr="epoch",
reward_attr="validation_performance",
dist_ip_addrs=dist_ip_addrs,
)
scheduler_cls = schedulers[search_strategy.lower()]
scheduler_options = (scheduler_cls, scheduler_options) # wrap into tuple
learner = Learner(
path_context=output_directory,
label=label,
problem_type=problem_type,
eval_metric=eval_metric,
stopping_metric=stopping_metric,
id_columns=id_columns,
feature_generator=feature_generator,
trainer_type=trainer_type,
label_count_threshold=label_count_threshold,
random_seed=random_seed,
)
learner.fit(
X=train_data,
X_val=tuning_data,
scheduler_options=scheduler_options,
hyperparameter_tune=hyperparameter_tune,
feature_prune=feature_prune,
holdout_frac=holdout_frac,
num_bagging_folds=num_bagging_folds,
num_bagging_sets=num_bagging_sets,
stack_ensemble_levels=stack_ensemble_levels,
hyperparameters=hyperparameters,
ag_args_fit=ag_args_fit,
excluded_model_types=excluded_model_types,
time_limit=time_limits_orig,
save_data=cache_data,
save_bagged_folds=save_bagged_folds,
verbosity=verbosity,
)
predictor = TabularPredictor(learner=learner)
keep_only_best = kwargs.get("keep_only_best", False)
if refit_full is True:
if keep_only_best is True:
if set_best_to_refit_full is True:
refit_full = "best"
else:
logger.warning(
f"refit_full was set to {refit_full}, but keep_only_best=True and set_best_to_refit_full=False. Disabling refit_full to avoid training models which would be automatically deleted."
)
refit_full = False
else:
refit_full = "all"
if refit_full is not False:
trainer = predictor._trainer
trainer_model_best = trainer.get_model_best()
predictor.refit_full(model=refit_full)
if set_best_to_refit_full:
if trainer_model_best in trainer.model_full_dict.keys():
trainer.model_best = trainer.model_full_dict[trainer_model_best]
# Note: model_best will be overwritten if additional training is done with new models, since model_best will have validation score of None and any new model will have a better validation score.
# This has the side-effect of having the possibility of model_best being overwritten by a worse model than the original model_best.
trainer.save()
else:
logger.warning(
f"Best model ({trainer_model_best}) is not present in refit_full dictionary. Training may have failed on the refit model. AutoGluon will default to using {trainer_model_best} for predictions."
)
if keep_only_best:
predictor.delete_models(models_to_keep="best", dry_run=False)
save_space = kwargs.get("save_space", False)
if save_space:
predictor.save_space()
return predictor
|
def fit(
train_data,
label,
tuning_data=None,
time_limits=None,
output_directory=None,
presets=None,
problem_type=None,
eval_metric=None,
stopping_metric=None,
auto_stack=False,
hyperparameter_tune=False,
feature_prune=False,
holdout_frac=None,
num_bagging_folds=0,
num_bagging_sets=None,
stack_ensemble_levels=0,
hyperparameters=None,
num_trials=None,
scheduler_options=None,
search_strategy="random",
search_options=None,
nthreads_per_trial=None,
ngpus_per_trial=None,
dist_ip_addrs=None,
visualizer="none",
verbosity=2,
**kwargs,
):
"""
Fit models to predict a column of data table based on the other columns.
Parameters
----------
train_data : str or :class:`autogluon.task.tabular_prediction.TabularDataset` or `pandas.DataFrame`
Table of the training data, which is similar to pandas DataFrame.
If str is passed, `train_data` will be loaded using the str value as the file path.
label : str
Name of the column that contains the target variable to predict.
tuning_data : str or :class:`autogluon.task.tabular_prediction.TabularDataset` or `pandas.DataFrame`, default = None
Another dataset containing validation data reserved for hyperparameter tuning (in same format as training data).
If str is passed, `tuning_data` will be loaded using the str value as the file path.
Note: final model returned may be fit on this tuning_data as well as train_data. Do not provide your evaluation test data here!
In particular, when `num_bagging_folds` > 0 or `stack_ensemble_levels` > 0, models will be trained on both `tuning_data` and `train_data`.
If `tuning_data = None`, `fit()` will automatically hold out some random validation examples from `train_data`.
time_limits : int, default = None
Approximately how long `fit()` should run for (wallclock time in seconds).
If not specified, `fit()` will run until all models have completed training, but will not repeatedly bag models unless `num_bagging_sets` or `auto_stack` is specified.
output_directory : str, default = None
Path to directory where models and intermediate outputs should be saved.
If unspecified, a time-stamped folder called "AutogluonModels/ag-[TIMESTAMP]" will be created in the working directory to store all models.
Note: To call `fit()` twice and save all results of each fit, you must specify different `output_directory` locations.
Otherwise files from first `fit()` will be overwritten by second `fit()`.
presets : list or str or dict, default = 'medium_quality_faster_train'
List of preset configurations for various arguments in `fit()`. Can significantly impact predictive accuracy, memory-footprint, and inference latency of trained models, and various other properties of the returned `predictor`.
It is recommended to specify presets and avoid specifying most other `fit()` arguments or model hyperparameters prior to becoming familiar with AutoGluon.
As an example, to get the most accurate overall predictor (regardless of its efficiency), set `presets='best_quality'`.
To get good quality with minimal disk usage, set `presets=['good_quality_faster_inference_only_refit', 'optimize_for_deployment']`
Any user-specified arguments in `fit()` will override the values used by presets.
If specifying a list of presets, later presets will override earlier presets if they alter the same argument.
For precise definitions of the provided presets, see file: `autogluon/tasks/tabular_prediction/presets_configs.py`.
Users can specify custom presets by passing in a dictionary of argument values as an element to the list.
Available Presets: ['best_quality', 'best_quality_with_high_quality_refit', 'high_quality_fast_inference_only_refit', 'good_quality_faster_inference_only_refit', 'medium_quality_faster_train', 'optimize_for_deployment', 'ignore_text']
It is recommended to only use one `quality` based preset in a given call to `fit()` as they alter many of the same arguments and are not compatible with each-other.
In-depth Preset Info:
best_quality={'auto_stack': True}
Best predictive accuracy with little consideration to inference time or disk usage. Achieve even better results by specifying a large time_limits value.
Recommended for applications that benefit from the best possible model accuracy.
best_quality_with_high_quality_refit={'auto_stack': True, 'refit_full': True}
Identical to best_quality but additionally trains refit_full models that have slightly lower predictive accuracy but are over 10x faster during inference and require 10x less disk space.
high_quality_fast_inference_only_refit={'auto_stack': True, 'refit_full': True, 'set_best_to_refit_full': True, 'save_bagged_folds': False}
High predictive accuracy with fast inference. ~10x-200x faster inference and ~10x-200x lower disk usage than `best_quality`.
Recommended for applications that require reasonable inference speed and/or model size.
good_quality_faster_inference_only_refit={'auto_stack': True, 'refit_full': True, 'set_best_to_refit_full': True, 'save_bagged_folds': False, 'hyperparameters': 'light'}
Good predictive accuracy with very fast inference. ~4x faster inference and ~4x lower disk usage than `high_quality_fast_inference_only_refit`.
Recommended for applications that require fast inference speed.
medium_quality_faster_train={'auto_stack': False}
Medium predictive accuracy with very fast inference and very fast training time. ~20x faster training than `good_quality_faster_inference_only_refit`.
This is the default preset in AutoGluon, but should generally only be used for quick prototyping, as `good_quality_faster_inference_only_refit` results in significantly better predictive accuracy and faster inference time.
optimize_for_deployment={'keep_only_best': True, 'save_space': True}
Optimizes result immediately for deployment by deleting unused models and removing training artifacts.
Often can reduce disk usage by ~2-4x with no negatives to model accuracy or inference speed.
This will disable numerous advanced functionality, but has no impact on inference.
This will make certain functionality less informative, such as `predictor.leaderboard()` and `predictor.fit_summary()`.
Because unused models will be deleted under this preset, methods like `predictor.leaderboard()` and `predictor.fit_summary()` will no longer show the full set of models that were trained during `fit()`.
Recommended for applications where the inner details of AutoGluon's training is not important and there is no intention of manually choosing between the final models.
This preset pairs well with the other presets such as `good_quality_faster_inference_only_refit` to make a very compact final model.
Identical to calling `predictor.delete_models(models_to_keep='best', dry_run=False)` and `predictor.save_space()` directly after `fit()`.
ignore_text={'feature_generator_kwargs': {'enable_text_ngram_features': False, 'enable_text_special_features': False}}
Disables automated feature generation when text features are detected.
This is useful to determine how beneficial text features are to the end result, as well as to ensure features are not mistaken for text when they are not.
problem_type : str, default = None
Type of prediction problem, i.e. is this a binary/multiclass classification or regression problem (options: 'binary', 'multiclass', 'regression').
If `problem_type = None`, the prediction problem type is inferred based on the label-values in provided dataset.
eval_metric : function or str, default = None
Metric by which predictions will be ultimately evaluated on test data.
AutoGluon tunes factors such as hyperparameters, early-stopping, ensemble-weights, etc. in order to improve this metric on validation data.
If `eval_metric = None`, it is automatically chosen based on `problem_type`.
Defaults to 'accuracy' for binary and multiclass classification and 'root_mean_squared_error' for regression.
Otherwise, options for classification:
['accuracy', 'balanced_accuracy', 'f1', 'f1_macro', 'f1_micro', 'f1_weighted',
'roc_auc', 'average_precision', 'precision', 'precision_macro', 'precision_micro', 'precision_weighted',
'recall', 'recall_macro', 'recall_micro', 'recall_weighted', 'log_loss', 'pac_score']
Options for regression:
['root_mean_squared_error', 'mean_squared_error', 'mean_absolute_error', 'median_absolute_error', 'r2']
For more information on these options, see `sklearn.metrics`: https://scikit-learn.org/stable/modules/classes.html#sklearn-metrics-metrics
You can also pass your own evaluation function here as long as it follows formatting of the functions defined in `autogluon/utils/tabular/metrics/`.
stopping_metric : function or str, default = None
Metric which iteratively-trained models use to early stop to avoid overfitting.
`stopping_metric` is not used by weighted ensembles, instead weighted ensembles maximize `eval_metric`.
Defaults to `eval_metric` value except when `eval_metric='roc_auc'`, where it defaults to `log_loss`.
Options are identical to options for `eval_metric`.
auto_stack : bool, default = False
Whether AutoGluon should automatically utilize bagging and multi-layer stack ensembling to boost predictive accuracy.
Set this = True if you are willing to tolerate longer training times in order to maximize predictive accuracy!
Note: This overrides `num_bagging_folds` and `stack_ensemble_levels` arguments (selects optimal values for these parameters based on dataset properties).
Note: This can increase training time (and inference time) by up to 20x, but can greatly improve predictive performance.
hyperparameter_tune : bool, default = False
Whether to tune hyperparameters or just use fixed hyperparameter values for each model. Setting as True will increase `fit()` runtimes.
It is currently not recommended to use `hyperparameter_tune` with `auto_stack` due to potential overfitting.
Use `auto_stack` to maximize predictive accuracy; use `hyperparameter_tune` if you prefer to deploy just a single model rather than an ensemble.
feature_prune : bool, default = False
Whether or not to perform feature selection.
hyperparameters : str or dict, default = 'default'
Determines the hyperparameters used by the models.
If `str` is passed, will use a preset hyperparameter configuration.
Valid `str` options: ['default', 'light', 'very_light', 'toy']
'default': Default AutoGluon hyperparameters intended to maximize accuracy without significant regard to inference time or disk usage.
'light': Results in smaller models. Generally will make inference speed much faster and disk usage much lower, but with worse accuracy.
'very_light': Results in much smaller models. Behaves similarly to 'light', but in many cases with over 10x less disk usage and a further reduction in accuracy.
'toy': Results in extremely small models. Only use this when prototyping, as the model quality will be severely reduced.
Reference `autogluon/task/tabular_prediction/hyperparameter_configs.py` for information on the hyperparameters associated with each preset.
Keys are strings that indicate which model types to train.
Options include: 'NN' (neural network), 'GBM' (lightGBM boosted trees), 'CAT' (CatBoost boosted trees), 'RF' (random forest), 'XT' (extremely randomized trees), 'KNN' (k-nearest neighbors), 'LR' (linear regression)
If certain key is missing from hyperparameters, then `fit()` will not train any models of that type. Omitting a model key from hyperparameters is equivalent to including this model key in `excluded_model_types`.
For example, set `hyperparameters = { 'NN':{...} }` if say you only want to train neural networks and no other types of models.
Values = dict of hyperparameter settings for each model type, or list of dicts.
Each hyperparameter can either be a single fixed value or a search space containing many possible values.
Unspecified hyperparameters will be set to default values (or default search spaces if `hyperparameter_tune = True`).
Caution: Any provided search spaces will be overridden by fixed defaults if `hyperparameter_tune = False`.
To train multiple models of a given type, set the value to a list of hyperparameter dictionaries.
For example, `hyperparameters = {'RF': [{'criterion': 'gini'}, {'criterion': 'entropy'}]}` will result in 2 random forest models being trained with separate hyperparameters.
Advanced functionality: Custom models
`hyperparameters` can also take a special key 'custom', which maps to a list of model names (currently supported options = 'GBM').
If `hyperparameter_tune = False`, then these additional models will also be trained using custom pre-specified hyperparameter settings that are known to work well.
Advanced functionality: Custom stack levels
By default, AutoGluon re-uses the same models and model hyperparameters at each level during stack ensembling.
To customize this behaviour, create a hyperparameters dictionary separately for each stack level, and then add them as values to a new dictionary, with keys equal to the stack level.
Example: `hyperparameters = {0: {'RF': rf_params1}, 1: {'CAT': [cat_params1, cat_params2], 'NN': {}}}`
This will result in a stack ensemble that has one custom random forest in level 0 followed by two CatBoost models with custom hyperparameters and a default neural network in level 1, for a total of 4 models.
If a level is not specified in `hyperparameters`, it will default to using the highest specified level to train models. This can also be explicitly controlled by adding a 'default' key.
Default:
hyperparameters = {
'NN': {},
'GBM': {},
'CAT': {},
'RF': [
{'criterion': 'gini', 'AG_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}},
{'criterion': 'entropy', 'AG_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}},
{'criterion': 'mse', 'AG_args': {'name_suffix': 'MSE', 'problem_types': ['regression']}},
],
'XT': [
{'criterion': 'gini', 'AG_args': {'name_suffix': 'Gini', 'problem_types': ['binary', 'multiclass']}},
{'criterion': 'entropy', 'AG_args': {'name_suffix': 'Entr', 'problem_types': ['binary', 'multiclass']}},
{'criterion': 'mse', 'AG_args': {'name_suffix': 'MSE', 'problem_types': ['regression']}},
],
'KNN': [
{'weights': 'uniform', 'AG_args': {'name_suffix': 'Unif'}},
{'weights': 'distance', 'AG_args': {'name_suffix': 'Dist'}},
],
'custom': ['GBM']
}
Details regarding the hyperparameters you can specify for each model are provided in the following files:
NN: `autogluon/utils/tabular/ml/models/tabular_nn/hyperparameters/parameters.py`
Note: certain hyperparameter settings may cause these neural networks to train much slower.
GBM: `autogluon/utils/tabular/ml/models/lgb/hyperparameters/parameters.py`
See also the lightGBM docs: https://lightgbm.readthedocs.io/en/latest/Parameters.html
CAT: `autogluon/utils/tabular/ml/models/catboost/hyperparameters/parameters.py`
See also the CatBoost docs: https://catboost.ai/docs/concepts/parameter-tuning.html
RF: See sklearn documentation: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html
Note: Hyperparameter tuning is disabled for this model.
XT: See sklearn documentation: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.ExtraTreesClassifier.html
Note: Hyperparameter tuning is disabled for this model.
KNN: See sklearn documentation: https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html
Note: Hyperparameter tuning is disabled for this model.
LR: `autogluon/utils/tabular/ml/models/lr/hyperparameters/parameters.py`
Note: Hyperparameter tuning is disabled for this model.
Note: 'penalty' parameter can be used for regression to specify regularization method: 'L1' and 'L2' values are supported.
Advanced functionality: Custom AutoGluon model arguments
These arguments are optional and can be specified in any model's hyperparameters.
Example: `hyperparameters = {'RF': {..., 'AG_args': {'name_suffix': 'CustomModelSuffix', 'disable_in_hpo': True}}`
AG_args: Dictionary of customization options related to meta properties of the model such as its name, the order it is trained, and the problem types it is valid for.
Valid keys:
name: (str) The name of the model. This overrides AutoGluon's naming logic and all other name arguments if present.
name_main: (str) The main name of the model. In 'RandomForestClassifier', this is 'RandomForest'.
name_prefix: (str) Add a custom prefix to the model name. Unused by default.
name_type_suffix: (str) Override the type suffix of the model name. In 'RandomForestClassifier', this is 'Classifier'. This comes before 'name_suffix'.
name_suffix: (str) Add a custom suffix to the model name. Unused by default.
priority: (int) Determines the order in which the model is trained. Larger values result in the model being trained earlier. Default values range from 100 (RF) to 0 (custom), dictated by model type. If you want this model to be trained first, set priority = 999.
problem_types: (list) List of valid problem types for the model. `problem_types=['binary']` will result in the model only being trained if `problem_type` is 'binary'.
disable_in_hpo: (bool) If True, the model will only be trained if `hyperparameter_tune=False`.
Reference the default hyperparameters for example usage of these options.
AG_args_fit: Dictionary of model fit customization options related to how and with what constraints the model is trained. These parameters affect stacker fold models, but not stacker models themselves.
Clarification: `time_limit` is the internal time in seconds given to a particular model to train, which is dictated in part by the `time_limits` argument given during `fit()` but is not the same.
Valid keys:
max_memory_usage_ratio: (float, default=1.0) The ratio of memory usage relative to the default to allow before early stopping or killing the model. Values greater than 1.0 will be increasingly prone to out-of-memory errors.
max_time_limit_ratio: (float, default=1.0) The ratio of the provided time_limit to use during model `fit()`. If `time_limit=10` and `max_time_limit_ratio=0.3`, time_limit would be changed to 3. Does not alter max_time_limit or min_time_limit values.
max_time_limit: (float, default=None) Maximum amount of time to allow this model to train for (in sec). If the provided time_limit is greater than this value, it will be replaced by max_time_limit.
min_time_limit: (float, default=0) Allow this model to train for at least this long (in sec), regardless of the time limit it would otherwise be granted.
If `min_time_limit >= max_time_limit`, time_limit will be set to min_time_limit.
If `min_time_limit=None`, time_limit will be set to None and the model will have no training time restriction.
holdout_frac : float, default = None
Fraction of train_data to holdout as tuning data for optimizing hyperparameters (ignored unless `tuning_data = None`, ignored if `num_bagging_folds != 0`).
Default value (if None) is selected based on the number of rows in the training data. Default values range from 0.2 at 2,500 rows to 0.01 at 250,000 rows.
Default value is doubled if `hyperparameter_tune = True`, up to a maximum of 0.2.
Disabled if `num_bagging_folds >= 2`.
num_bagging_folds : int, default = 0
Number of folds used for bagging of models. When `num_bagging_folds = k`, training time is roughly increased by a factor of `k` (set = 0 to disable bagging).
Disabled by default, but we recommend values between 5-10 to maximize predictive performance.
Increasing num_bagging_folds will result in models with lower bias but that are more prone to overfitting.
Values > 10 may produce diminishing returns, and can even harm overall results due to overfitting.
To further improve predictions, avoid increasing num_bagging_folds much beyond 10 and instead increase num_bagging_sets.
num_bagging_sets : int, default = None
Number of repeats of kfold bagging to perform (values must be >= 1). Total number of models trained during bagging = num_bagging_folds * num_bagging_sets.
Defaults to 1 if time_limits is not specified, otherwise 20 (always disabled if num_bagging_folds is not specified).
Values greater than 1 will result in superior predictive performance, especially on smaller problems and with stacking enabled (reduces overall variance).
stack_ensemble_levels : int, default = 0
Number of stacking levels to use in stack ensemble. Roughly increases model training time by factor of `stack_ensemble_levels+1` (set = 0 to disable stack ensembling).
Disabled by default, but we recommend values between 1-3 to maximize predictive performance.
To prevent overfitting, this argument is ignored unless you have also set `num_bagging_folds >= 2`.
num_trials : int, default = None
Maximal number of different hyperparameter settings of each model type to evaluate during HPO (only matters if `hyperparameter_tune = True`).
If both `time_limits` and `num_trials` are specified, `time_limits` takes precedent.
scheduler_options : dict, default = None
Extra arguments passed to __init__ of scheduler, to configure the orchestration of training jobs during hyperparameter-tuning.
Ignored if `hyperparameter_tune=False`.
search_strategy : str, default = 'random'
Which hyperparameter search algorithm to use (only matters if `hyperparameter_tune=True`).
Options include: 'random' (random search), 'bayesopt' (Gaussian process Bayesian optimization), 'skopt' (SKopt Bayesian optimization), 'grid' (grid search).
search_options : dict, default = None
Auxiliary keyword arguments to pass to the searcher that performs hyperparameter optimization.
nthreads_per_trial : int, default = None
How many CPUs to use in each training run of an individual model.
This is automatically determined by AutoGluon when left as None (based on available compute).
ngpus_per_trial : int, default = None
How many GPUs to use in each trial (ie. single training run of a model).
This is automatically determined by AutoGluon when left as None.
dist_ip_addrs : list, default = None
List of IP addresses corresponding to remote workers, in order to leverage distributed computation.
visualizer : str, default = 'none'
How to visualize the neural network training progress during `fit()`. Options: ['mxboard', 'tensorboard', 'none'].
verbosity : int, default = 2
Verbosity levels range from 0 to 4 and control how much information is printed during fit().
Higher levels correspond to more detailed print statements (you can set verbosity = 0 to suppress warnings).
If using logging, you can alternatively control amount of information printed via `logger.setLevel(L)`,
where `L` ranges from 0 to 50 (Note: higher values of `L` correspond to fewer print statements, opposite of verbosity levels)
Kwargs can include additional arguments for advanced users:
AG_args_fit : dict, default={}
Keyword arguments to pass to all models. See the `AG_args_fit` argument from "Advanced functionality: Custom AutoGluon model arguments" in the `hyperparameters` argument documentation for valid values.
Identical to specifying `AG_args_fit` parameter for all models in `hyperparameters`.
If a key in `AG_args_fit` is already specified for a model in `hyperparameters`, it will not be altered through this argument.
excluded_model_types : list, default = []
Banned subset of model types to avoid training during `fit()`, even if present in `hyperparameters`.
Valid values: ['RF', 'XT', 'KNN', 'GBM', 'CAT', 'NN', 'LR', 'custom']. Reference `hyperparameters` documentation for what models correspond to each value.
Useful when a particular model type such as 'KNN' or 'custom' is not desired but altering the `hyperparameters` dictionary is difficult or time-consuming.
Example: To exclude both 'KNN' and 'custom' models, specify `excluded_model_types=['KNN', 'custom']`.
id_columns : list, default = []
Banned subset of column names that model may not use as predictive features (e.g. contains label, user-ID, etc).
These columns are ignored during `fit()`, but DataFrame of just these columns with appended predictions may be produced, for example to submit in a ML competition.
label_count_threshold : int, default = 10
For multi-class classification problems, this is the minimum number of times a label must appear in dataset in order to be considered an output class.
AutoGluon will ignore any classes whose labels do not appear at least this many times in the dataset (i.e. will never predict them).
save_bagged_folds : bool, default = True
If True, bagged models will save their fold models (the models from each individual fold of bagging). This is required to use bagged models for prediction after `fit()`.
If False, bagged models will not save their fold models. This means that bagged models will not be valid models during inference.
This should only be set to False when planning to call `predictor.refit_full()` or when `refit_full` is set and `set_best_to_refit_full=True`.
Particularly useful if disk usage is a concern. By not saving the fold models, bagged models will use only very small amounts of disk space during training.
In many training runs, this will reduce peak disk usage by >10x.
This parameter has no effect if bagging is disabled.
keep_only_best : bool, default = False
If True, only the best model and its ancestor models are saved in the outputted `predictor`. All other models are deleted.
If you only care about deploying the most accurate predictor with the smallest file-size and no longer need any of the other trained models or functionality beyond prediction on new data, then set: `keep_only_best=True`, `save_space=True`.
This is equivalent to calling `predictor.delete_models(models_to_keep='best', dry_run=False)` directly after `fit()`.
If used with `refit_full` and `set_best_to_refit_full`, the best model will be the refit_full model, and the original bagged best model will be deleted.
`refit_full` will be automatically set to 'best' in this case to avoid training models which will be later deleted.
save_space : bool, default = False
If True, reduces the memory and disk size of predictor by deleting auxiliary model files that aren't needed for prediction on new data.
This is equivalent to calling `predictor.save_space()` directly after `fit()`.
This has NO impact on inference accuracy.
It is recommended if the only goal is to use the trained model for prediction.
Certain advanced functionality may no longer be available if `save_space=True`. Refer to `predictor.save_space()` documentation for more details.
cache_data : bool, default = True
When enabled, the training and validation data are saved to disk for future reuse.
Enables advanced functionality in the resulting Predictor object such as feature importance calculation on the original data.
refit_full : bool or str, default = False
Whether to retrain all models on all of the data (training + validation) after the normal training procedure.
This is equivalent to calling `predictor.refit_full(model=refit_full)` after training.
If `refit_full=True`, it will be treated as `refit_full='all'`.
If `refit_full=False`, refitting will not occur.
Valid str values:
`all`: refits all models.
`best`: refits only the best model (and its ancestors if it is a stacker model).
`{model_name}`: refits only the specified model (and its ancestors if it is a stacker model).
For bagged models:
Reduces a model's inference time by collapsing bagged ensembles into a single model fit on all of the training data.
This process will typically result in a slight accuracy reduction and a large inference speedup.
The inference speedup will generally be between 10-200x faster than the original bagged ensemble model.
The inference speedup factor is equivalent to (k * n), where k is the number of folds (`num_bagging_folds`) and n is the number of finished repeats (`num_bagging_sets`) in the bagged ensemble.
The runtime is generally 10% or less of the original fit runtime.
The runtime can be roughly estimated as 1 / (k * n) of the original fit runtime, with k and n defined above.
For non-bagged models:
Optimizes a model's accuracy by retraining on 100% of the data without using a validation set.
Will typically result in a slight accuracy increase and no change to inference time.
The runtime will be approximately equal to the original fit runtime.
This process does not alter the original models, but instead adds additional models.
If stacker models are refit by this process, they will use the refit_full versions of the ancestor models during inference.
Models produced by this process will not have validation scores, as they use all of the data for training.
Therefore, it is up to the user to determine if the models are of sufficient quality by including test data in `predictor.leaderboard(dataset=test_data)`.
If the user does not have additional test data, they should reference the original model's score for an estimate of the performance of the refit_full model.
Warning: Be aware that utilizing refit_full models without separately verifying on test data means that the model is untested, and has no guarantee of being consistent with the original model.
The time taken by this process is not enforced by `time_limits`.
`cache_data` must be set to `True` to enable this functionality.
set_best_to_refit_full : bool, default = False
If True, will set Trainer.best_model = Trainer.full_model_dict[Trainer.best_model]
This will change the default model that Predictor uses for prediction when model is not specified to the refit_full version of the model that previously exhibited the highest validation score.
Only valid if `refit_full` is set.
feature_generator_type : :class:`autogluon.utils.tabular.features.auto_ml_feature_generator.AbstractFeatureGenerator` class, default = :class:`autogluon.utils.tabular.features.auto_ml_feature_generator.AutoMLFeatureGenerator`
A `FeatureGenerator` class specifying which feature engineering protocol to follow
Note: The file containing your `FeatureGenerator` class must be imported into current Python session in order to use a custom class.
feature_generator_kwargs : dict, default={}
Keyword arguments to pass into the `FeatureGenerator` constructor.
Valid :class:`autogluon.utils.tabular.features.auto_ml_feature_generator.AutoMLFeatureGenerator` kwargs:
enable_text_ngram_features : bool, default = True
If True, the vectorizer argument value is used to generate 'text_ngram' features from text features if present.
Try setting this to False if you encounter memory issues running AutoGluon on text data and cannot access a machine with more memory.
enable_text_special_features : bool, default = True
If True, generate 'text_special' features from text features if present.
Examples of 'text_special' features include the number of whitespaces and the average word length in a text feature.
vectorizer : `sklearn.feature_extraction.text.CountVectorizer`, default = `CountVectorizer(min_df=30, ngram_range=(1, 3), max_features=10000, dtype=np.uint8)`
Determines the count vectorizer used during feature generation if text features are detected.
If your data contain text fields and you encounter memory issues running AutoGluon (and cannot access a machine with more memory), then consider reducing max_features or setting n_gram_range=(1, 2).
trainer_type : `Trainer` class, default=`AutoTrainer`
A class inheriting from `autogluon.utils.tabular.ml.trainer.abstract_trainer.AbstractTrainer` that controls training/ensembling of many models.
Note: In order to use a custom `Trainer` class, you must import the class file that defines it into the current Python session.
random_seed : int, default = 0
Seed to use when generating data split indices such as kfold splits and train/validation splits.
Caution: This seed only enables reproducible data splits (and the ability to randomize splits in each run by changing seed values).
This seed is NOT used in the training of individual models, for that you need to explicitly set the corresponding seed hyperparameter (usually called 'seed_value') of each individual model.
If stacking is enabled:
The seed used for stack level L is equal to `seed+L`.
This means `random_seed=1` will have the same split indices at L=0 as `random_seed=0` will have at L=1.
If `random_seed=None`, a random integer is used.
Returns
-------
:class:`autogluon.task.tabular_prediction.TabularPredictor` object which can make predictions on new data and summarize what happened during `fit()`.
Examples
--------
>>> from autogluon import TabularPrediction as task
>>> train_data = task.Dataset(file_path='https://autogluon.s3.amazonaws.com/datasets/Inc/train.csv')
>>> label_column = 'class'
>>> predictor = task.fit(train_data=train_data, label=label_column)
>>> test_data = task.Dataset(file_path='https://autogluon.s3.amazonaws.com/datasets/Inc/test.csv')
>>> y_test = test_data[label_column]
>>> test_data = test_data.drop(labels=[label_column], axis=1)
>>> y_pred = predictor.predict(test_data)
>>> perf = predictor.evaluate_predictions(y_true=y_test, y_pred=y_pred)
>>> results = predictor.fit_summary()
To maximize predictive performance, use the following:
>>> eval_metric = 'roc_auc' # set this to the metric you ultimately care about
>>> time_limits = 360 # set as long as you are willing to wait (in sec)
>>> predictor = task.fit(train_data=train_data, label=label_column, eval_metric=eval_metric, auto_stack=True, time_limits=time_limits)
"""
assert search_strategy != "bayesopt_hyperband", (
"search_strategy == 'bayesopt_hyperband' not yet supported"
)
if verbosity < 0:
verbosity = 0
elif verbosity > 4:
verbosity = 4
logger.setLevel(verbosity2loglevel(verbosity))
allowed_kwarg_names = {
"feature_generator_type",
"feature_generator_kwargs",
"trainer_type",
"AG_args_fit",
"excluded_model_types",
"label_count_threshold",
"id_columns",
"set_best_to_refit_full",
"save_bagged_folds",
"keep_only_best",
"save_space",
"cache_data",
"refit_full",
"random_seed",
"enable_fit_continuation", # TODO: Remove on 0.1.0 release
}
for kwarg_name in kwargs.keys():
if kwarg_name not in allowed_kwarg_names:
raise ValueError("Unknown keyword argument specified: %s" % kwarg_name)
if isinstance(train_data, str):
train_data = TabularDataset(file_path=train_data)
if tuning_data is not None and isinstance(tuning_data, str):
tuning_data = TabularDataset(file_path=tuning_data)
if len(set(train_data.columns)) < len(train_data.columns):
raise ValueError(
"Column names are not unique, please change duplicated column names (in pandas: train_data.rename(columns={'current_name':'new_name'})"
)
if tuning_data is not None:
train_features = np.array(
[column for column in train_data.columns if column != label]
)
tuning_features = np.array(
[column for column in tuning_data.columns if column != label]
)
if np.any(train_features != tuning_features):
raise ValueError("Column names must match between training and tuning data")
if feature_prune:
feature_prune = False # TODO: Fix feature pruning to add back as an option
# Currently disabled, needs to be updated to align with new model class functionality
logger.log(
30, "Warning: feature_prune does not currently work, setting to False."
)
cache_data = kwargs.get("cache_data", True)
refit_full = kwargs.get("refit_full", False)
# TODO: Remove on 0.1.0 release
if "enable_fit_continuation" in kwargs.keys():
logger.log(
30,
"Warning: `enable_fit_continuation` is a deprecated parameter. It has been renamed to `cache_data`. Starting from AutoGluon 0.1.0, specifying `enable_fit_continuation` as a parameter will cause an exception.",
)
logger.log(
30, "Setting `cache_data` value equal to `enable_fit_continuation` value."
)
cache_data = kwargs["enable_fit_continuation"]
if not cache_data:
logger.log(
30,
"Warning: `cache_data=False` will disable or limit advanced functionality after training such as feature importance calculations. It is recommended to set `cache_data=True` unless you explicitly wish to not have the data saved to disk.",
)
if refit_full:
raise ValueError(
"`refit_full=True` is only available when `cache_data=True`. Set `cache_data=True` to utilize `refit_full`."
)
set_best_to_refit_full = kwargs.get("set_best_to_refit_full", False)
if set_best_to_refit_full and not refit_full:
raise ValueError(
"`set_best_to_refit_full=True` is only available when `refit_full=True`. Set `refit_full=True` to utilize `set_best_to_refit_full`."
)
save_bagged_folds = kwargs.get("save_bagged_folds", True)
if hyperparameter_tune:
logger.log(
30,
"Warning: `hyperparameter_tune=True` is currently experimental and may cause the process to hang. Setting `auto_stack=True` instead is recommended to achieve maximum quality models.",
)
if dist_ip_addrs is None:
dist_ip_addrs = []
if search_options is None:
search_options = dict()
if hyperparameters is None:
hyperparameters = "default"
if isinstance(hyperparameters, str):
hyperparameters = get_hyperparameter_config(hyperparameters)
# Process kwargs to create feature generator, trainer, schedulers, searchers for each model:
output_directory = setup_outputdir(output_directory) # Format directory name
feature_generator_type = kwargs.get(
"feature_generator_type", AutoMLFeatureGenerator
)
feature_generator_kwargs = kwargs.get("feature_generator_kwargs", {})
feature_generator = feature_generator_type(
**feature_generator_kwargs
) # instantiate FeatureGenerator object
id_columns = kwargs.get("id_columns", [])
trainer_type = kwargs.get("trainer_type", AutoTrainer)
ag_args_fit = kwargs.get("AG_args_fit", {})
excluded_model_types = kwargs.get("excluded_model_types", [])
random_seed = kwargs.get("random_seed", 0)
nthreads_per_trial, ngpus_per_trial = setup_compute(
nthreads_per_trial, ngpus_per_trial
)
num_train_rows = len(train_data)
if auto_stack:
# TODO: What about datasets that are 100k+? At a certain point should we not bag?
# TODO: What about time_limits? Metalearning can tell us expected runtime of each model, then we can select optimal folds + stack levels to fit time constraint
num_bagging_folds = min(10, max(5, math.floor(num_train_rows / 100)))
stack_ensemble_levels = min(1, max(0, math.floor(num_train_rows / 750)))
if num_bagging_sets is None:
if num_bagging_folds >= 2:
if time_limits is not None:
num_bagging_sets = 20
else:
num_bagging_sets = 1
else:
num_bagging_sets = 1
label_count_threshold = kwargs.get("label_count_threshold", 10)
if (
num_bagging_folds is not None
): # Ensure there exist sufficient labels for stratified splits across all bags
label_count_threshold = max(label_count_threshold, num_bagging_folds)
time_limits_orig = copy.deepcopy(time_limits)
time_limits_hpo = copy.deepcopy(time_limits)
if num_bagging_folds >= 2 and (time_limits_hpo is not None):
time_limits_hpo = time_limits_hpo / (
1 + num_bagging_folds * (1 + stack_ensemble_levels)
)
time_limits_hpo, num_trials = setup_trial_limits(
time_limits_hpo, num_trials, hyperparameters
) # TODO: Move HPO time allocation to Trainer
if (num_trials is not None) and hyperparameter_tune and (num_trials == 1):
hyperparameter_tune = False
logger.log(
30,
"Warning: Specified num_trials == 1 or time_limits is too small for hyperparameter_tune, setting to False.",
)
if holdout_frac is None:
holdout_frac = default_holdout_frac(num_train_rows, hyperparameter_tune)
# Add visualizer to NN hyperparameters:
if (
(visualizer is not None)
and (visualizer != "none")
and ("NN" in hyperparameters)
):
hyperparameters["NN"]["visualizer"] = visualizer
eval_metric = get_metric(eval_metric, problem_type, "eval_metric")
stopping_metric = get_metric(stopping_metric, problem_type, "stopping_metric")
# All models use the same scheduler:
scheduler_options = compile_scheduler_options(
scheduler_options=scheduler_options,
search_strategy=search_strategy,
search_options=search_options,
nthreads_per_trial=nthreads_per_trial,
ngpus_per_trial=ngpus_per_trial,
checkpoint=None,
num_trials=num_trials,
time_out=time_limits_hpo,
resume=False,
visualizer=visualizer,
time_attr="epoch",
reward_attr="validation_performance",
dist_ip_addrs=dist_ip_addrs,
)
scheduler_cls = schedulers[search_strategy.lower()]
scheduler_options = (scheduler_cls, scheduler_options) # wrap into tuple
learner = Learner(
path_context=output_directory,
label=label,
problem_type=problem_type,
eval_metric=eval_metric,
stopping_metric=stopping_metric,
id_columns=id_columns,
feature_generator=feature_generator,
trainer_type=trainer_type,
label_count_threshold=label_count_threshold,
random_seed=random_seed,
)
learner.fit(
X=train_data,
X_val=tuning_data,
scheduler_options=scheduler_options,
hyperparameter_tune=hyperparameter_tune,
feature_prune=feature_prune,
holdout_frac=holdout_frac,
num_bagging_folds=num_bagging_folds,
num_bagging_sets=num_bagging_sets,
stack_ensemble_levels=stack_ensemble_levels,
hyperparameters=hyperparameters,
ag_args_fit=ag_args_fit,
excluded_model_types=excluded_model_types,
time_limit=time_limits_orig,
save_data=cache_data,
save_bagged_folds=save_bagged_folds,
verbosity=verbosity,
)
predictor = TabularPredictor(learner=learner)
keep_only_best = kwargs.get("keep_only_best", False)
if refit_full is True:
if keep_only_best is True:
if set_best_to_refit_full is True:
refit_full = "best"
else:
logger.warning(
f"refit_full was set to {refit_full}, but keep_only_best=True and set_best_to_refit_full=False. Disabling refit_full to avoid training models which would be automatically deleted."
)
refit_full = False
else:
refit_full = "all"
if refit_full is not False:
trainer = predictor._trainer
trainer_model_best = trainer.get_model_best()
predictor.refit_full(model=refit_full)
if set_best_to_refit_full:
if trainer_model_best in trainer.model_full_dict.keys():
trainer.model_best = trainer.model_full_dict[trainer_model_best]
# Note: model_best will be overwritten if additional training is done with new models, since model_best will have validation score of None and any new model will have a better validation score.
# This has the side-effect of having the possibility of model_best being overwritten by a worse model than the original model_best.
trainer.save()
else:
logger.warning(
f"Best model ({trainer_model_best}) is not present in refit_full dictionary. Training may have failed on the refit model. AutoGluon will default to using {trainer_model_best} for predictions."
)
if keep_only_best:
predictor.delete_models(models_to_keep="best", dry_run=False)
save_space = kwargs.get("save_space", False)
if save_space:
predictor.save_space()
return predictor
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def augment_data(
X_train,
feature_metadata: FeatureMetadata,
augmentation_data=None,
augment_method="spunge",
augment_args=None,
):
"""augment_method options: ['spunge', 'munge']"""
if augment_args is None:
augment_args = {}
if augmentation_data is not None:
X_aug = augmentation_data
else:
if "num_augmented_samples" not in augment_args:
if "max_size" not in augment_args:
augment_args["max_size"] = np.inf
augment_args["num_augmented_samples"] = int(
min(
augment_args["max_size"], augment_args["size_factor"] * len(X_train)
)
)
if augment_method == "spunge":
X_aug = spunge_augment(X_train, feature_metadata, **augment_args)
elif augment_method == "munge":
X_aug = munge_augment(X_train, feature_metadata, **augment_args)
else:
raise ValueError(f"unknown augment_method: {augment_method}")
# return postprocess_augmented(X_aug, X_train) # TODO: dropping duplicates is much more efficient, but may skew distribution for entirely-categorical data with few categories.
logger.log(15, f"Augmented training dataset with {len(X_aug)} extra datapoints")
return X_aug.reset_index(drop=True)
|
def augment_data(
X_train,
feature_types_metadata: FeatureTypesMetadata,
augmentation_data=None,
augment_method="spunge",
augment_args=None,
):
"""augment_method options: ['spunge', 'munge']"""
if augment_args is None:
augment_args = {}
if augmentation_data is not None:
X_aug = augmentation_data
else:
if "num_augmented_samples" not in augment_args:
if "max_size" not in augment_args:
augment_args["max_size"] = np.inf
augment_args["num_augmented_samples"] = int(
min(
augment_args["max_size"], augment_args["size_factor"] * len(X_train)
)
)
if augment_method == "spunge":
X_aug = spunge_augment(X_train, feature_types_metadata, **augment_args)
elif augment_method == "munge":
X_aug = munge_augment(X_train, feature_types_metadata, **augment_args)
else:
raise ValueError(f"unknown augment_method: {augment_method}")
# return postprocess_augmented(X_aug, X_train) # TODO: dropping duplicates is much more efficient, but may skew distribution for entirely-categorical data with few categories.
logger.log(15, f"Augmented training dataset with {len(X_aug)} extra datapoints")
return X_aug.reset_index(drop=True)
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def spunge_augment(
X,
feature_metadata: FeatureMetadata,
num_augmented_samples=10000,
frac_perturb=0.1,
continuous_feature_noise=0.1,
**kwargs,
):
"""Generates synthetic datapoints for learning to mimic teacher model in distillation
via simplified version of MUNGE strategy (that does not require near-neighbor search).
Args:
num_augmented_samples: number of additional augmented data points to return
frac_perturb: fraction of features/examples that are perturbed during augmentation. Set near 0 to ensure augmented sample distribution remains closer to real data.
continuous_feature_noise: we noise numeric features by this factor times their std-dev. Set near 0 to ensure augmented sample distribution remains closer to real data.
"""
if frac_perturb > 1.0:
raise ValueError("frac_perturb must be <= 1")
logger.log(
20,
f"SPUNGE: Augmenting training data with {num_augmented_samples} synthetic samples for distillation...",
)
num_feature_perturb = max(1, int(frac_perturb * len(X.columns)))
X_aug = pd.concat([X.iloc[[0]].copy()] * num_augmented_samples)
X_aug.reset_index(drop=True, inplace=True)
continuous_types = ["float", "int"]
continuous_featnames = feature_metadata.get_features(
valid_raw_types=continuous_types
) # these features will have shuffled values with added noise
for i in range(
num_augmented_samples
): # hot-deck sample some features per datapoint
og_ind = i % len(X)
augdata_i = X.iloc[og_ind].copy()
num_feature_perturb_i = np.random.choice(
range(1, num_feature_perturb + 1)
) # randomly sample number of features to perturb
cols_toperturb = np.random.choice(
list(X.columns), size=num_feature_perturb_i, replace=False
)
for feature in cols_toperturb:
feature_data = X[feature]
augdata_i[feature] = feature_data.sample(n=1).values[0]
X_aug.iloc[i] = augdata_i
for feature in X.columns:
if feature in continuous_featnames:
feature_data = X[feature]
aug_data = X_aug[feature]
noise = np.random.normal(
scale=np.nanstd(feature_data) * continuous_feature_noise,
size=num_augmented_samples,
)
mask = np.random.binomial(n=1, p=frac_perturb, size=num_augmented_samples)
aug_data = aug_data + noise * mask
X_aug[feature] = pd.Series(aug_data, index=X_aug.index)
return X_aug
|
def spunge_augment(
X,
feature_types_metadata: FeatureTypesMetadata,
num_augmented_samples=10000,
frac_perturb=0.1,
continuous_feature_noise=0.1,
**kwargs,
):
"""Generates synthetic datapoints for learning to mimic teacher model in distillation
via simplified version of MUNGE strategy (that does not require near-neighbor search).
Args:
num_augmented_samples: number of additional augmented data points to return
frac_perturb: fraction of features/examples that are perturbed during augmentation. Set near 0 to ensure augmented sample distribution remains closer to real data.
continuous_feature_noise: we noise numeric features by this factor times their std-dev. Set near 0 to ensure augmented sample distribution remains closer to real data.
"""
if frac_perturb > 1.0:
raise ValueError("frac_perturb must be <= 1")
logger.log(
20,
f"SPUNGE: Augmenting training data with {num_augmented_samples} synthetic samples for distillation...",
)
num_feature_perturb = max(1, int(frac_perturb * len(X.columns)))
X_aug = pd.concat([X.iloc[[0]].copy()] * num_augmented_samples)
X_aug.reset_index(drop=True, inplace=True)
continuous_types = ["float", "int"]
continuous_featnames = [] # these features will have shuffled values with added noise
for contype in continuous_types:
if contype in feature_types_metadata.feature_types_raw:
continuous_featnames += feature_types_metadata.feature_types_raw[contype]
for i in range(
num_augmented_samples
): # hot-deck sample some features per datapoint
og_ind = i % len(X)
augdata_i = X.iloc[og_ind].copy()
num_feature_perturb_i = np.random.choice(
range(1, num_feature_perturb + 1)
) # randomly sample number of features to perturb
cols_toperturb = np.random.choice(
list(X.columns), size=num_feature_perturb_i, replace=False
)
for feature in cols_toperturb:
feature_data = X[feature]
augdata_i[feature] = feature_data.sample(n=1).values[0]
X_aug.iloc[i] = augdata_i
for feature in X.columns:
if feature in continuous_featnames:
feature_data = X[feature]
aug_data = X_aug[feature]
noise = np.random.normal(
scale=np.nanstd(feature_data) * continuous_feature_noise,
size=num_augmented_samples,
)
mask = np.random.binomial(n=1, p=frac_perturb, size=num_augmented_samples)
aug_data = aug_data + noise * mask
X_aug[feature] = pd.Series(aug_data, index=X_aug.index)
return X_aug
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def munge_augment(
X,
feature_metadata: FeatureMetadata,
num_augmented_samples=10000,
perturb_prob=0.5,
s=1.0,
**kwargs,
):
"""Uses MUNGE algorithm to generate synthetic datapoints for learning to mimic teacher model in distillation: https://www.cs.cornell.edu/~caruana/compression.kdd06.pdf
Args:
num_augmented_samples: number of additional augmented data points to return
perturb_prob: probability of perturbing each feature during augmentation. Set near 0 to ensure augmented sample distribution remains closer to real data.
s: We noise numeric features by their std-dev divided by this factor (inverse of continuous_feature_noise). Set large to ensure augmented sample distribution remains closer to real data.
"""
nn_dummy = TabularNeuralNetModel(
path="nn_dummy",
name="nn_dummy",
problem_type=REGRESSION,
eval_metric=mean_squared_error,
hyperparameters={
"num_dataloading_workers": 0,
"proc.embed_min_categories": np.inf,
},
features=list(X.columns),
feature_metadata=feature_metadata,
)
processed_data = nn_dummy.process_train_data(
df=nn_dummy.preprocess(X),
labels=pd.Series([1] * len(X)),
batch_size=nn_dummy.params["batch_size"],
num_dataloading_workers=0,
impute_strategy=nn_dummy.params["proc.impute_strategy"],
max_category_levels=nn_dummy.params["proc.max_category_levels"],
skew_threshold=nn_dummy.params["proc.skew_threshold"],
embed_min_categories=nn_dummy.params["proc.embed_min_categories"],
use_ngram_features=nn_dummy.params["use_ngram_features"],
)
X_vector = processed_data.dataset._data[processed_data.vectordata_index].asnumpy()
processed_data = None
nn_dummy = None
gc.collect()
neighbor_finder = NearestNeighbors(n_neighbors=2)
neighbor_finder.fit(X_vector)
neigh_dist, neigh_ind = neighbor_finder.kneighbors(X_vector)
neigh_ind = neigh_ind[:, 1] # contains indices of nearest neighbors
neigh_dist = None
# neigh_dist = neigh_dist[:,1] # contains distances to nearest neighbors
neighbor_finder = None
gc.collect()
if perturb_prob > 1.0:
raise ValueError("frac_perturb must be <= 1")
logger.log(
20,
f"MUNGE: Augmenting training data with {num_augmented_samples} synthetic samples for distillation...",
)
X = X.copy()
X_aug = pd.concat([X.iloc[[0]].copy()] * num_augmented_samples)
X_aug.reset_index(drop=True, inplace=True)
continuous_types = ["float", "int"]
continuous_featnames = feature_metadata.get_features(
valid_raw_types=continuous_types
) # these features will have shuffled values with added noise
for col in continuous_featnames:
X_aug[col] = X_aug[col].astype(float)
X[col] = X[col].astype(float)
for i in range(num_augmented_samples):
og_ind = i % len(X)
augdata_i = X.iloc[og_ind].copy()
neighbor_i = X.iloc[neigh_ind[og_ind]].copy()
# dist_i = neigh_dist[og_ind]
cols_toperturb = np.random.choice(
list(X.columns),
size=np.random.binomial(X.shape[1], p=perturb_prob, size=1)[0],
replace=False,
)
for col in cols_toperturb:
new_val = neighbor_i[col]
if col in continuous_featnames:
new_val += np.random.normal(scale=np.abs(augdata_i[col] - new_val) / s)
augdata_i[col] = new_val
X_aug.iloc[i] = augdata_i
return X_aug
|
def munge_augment(
X,
feature_types_metadata: FeatureTypesMetadata,
num_augmented_samples=10000,
perturb_prob=0.5,
s=1.0,
**kwargs,
):
"""Uses MUNGE algorithm to generate synthetic datapoints for learning to mimic teacher model in distillation: https://www.cs.cornell.edu/~caruana/compression.kdd06.pdf
Args:
num_augmented_samples: number of additional augmented data points to return
perturb_prob: probability of perturbing each feature during augmentation. Set near 0 to ensure augmented sample distribution remains closer to real data.
s: We noise numeric features by their std-dev divided by this factor (inverse of continuous_feature_noise). Set large to ensure augmented sample distribution remains closer to real data.
"""
nn_dummy = TabularNeuralNetModel(
path="nn_dummy",
name="nn_dummy",
problem_type=REGRESSION,
eval_metric=mean_squared_error,
hyperparameters={
"num_dataloading_workers": 0,
"proc.embed_min_categories": np.inf,
},
features=list(X.columns),
feature_types_metadata=feature_types_metadata,
)
processed_data = nn_dummy.process_train_data(
df=nn_dummy.preprocess(X),
labels=pd.Series([1] * len(X)),
batch_size=nn_dummy.params["batch_size"],
num_dataloading_workers=0,
impute_strategy=nn_dummy.params["proc.impute_strategy"],
max_category_levels=nn_dummy.params["proc.max_category_levels"],
skew_threshold=nn_dummy.params["proc.skew_threshold"],
embed_min_categories=nn_dummy.params["proc.embed_min_categories"],
use_ngram_features=nn_dummy.params["use_ngram_features"],
)
X_vector = processed_data.dataset._data[processed_data.vectordata_index].asnumpy()
processed_data = None
nn_dummy = None
gc.collect()
neighbor_finder = NearestNeighbors(n_neighbors=2)
neighbor_finder.fit(X_vector)
neigh_dist, neigh_ind = neighbor_finder.kneighbors(X_vector)
neigh_ind = neigh_ind[:, 1] # contains indices of nearest neighbors
neigh_dist = None
# neigh_dist = neigh_dist[:,1] # contains distances to nearest neighbors
neighbor_finder = None
gc.collect()
if perturb_prob > 1.0:
raise ValueError("frac_perturb must be <= 1")
logger.log(
20,
f"MUNGE: Augmenting training data with {num_augmented_samples} synthetic samples for distillation...",
)
X = X.copy()
X_aug = pd.concat([X.iloc[[0]].copy()] * num_augmented_samples)
X_aug.reset_index(drop=True, inplace=True)
continuous_types = ["float", "int"]
continuous_featnames = [] # these features will have shuffled values with added noise
for contype in continuous_types:
if contype in feature_types_metadata.feature_types_raw:
continuous_featnames += feature_types_metadata.feature_types_raw[contype]
for col in continuous_featnames:
X_aug[col] = X_aug[col].astype(float)
X[col] = X[col].astype(float)
for i in range(num_augmented_samples):
og_ind = i % len(X)
augdata_i = X.iloc[og_ind].copy()
neighbor_i = X.iloc[neigh_ind[og_ind]].copy()
# dist_i = neigh_dist[og_ind]
cols_toperturb = np.random.choice(
list(X.columns),
size=np.random.binomial(X.shape[1], p=perturb_prob, size=1)[0],
replace=False,
)
for col in cols_toperturb:
new_val = neighbor_i[col]
if col in continuous_featnames:
new_val += np.random.normal(scale=np.abs(augdata_i[col] - new_val) / s)
augdata_i[col] = new_val
X_aug.iloc[i] = augdata_i
return X_aug
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def __init__(
self,
path_context: str,
label: str,
id_columns: list,
feature_generator: PipelineFeatureGenerator,
label_count_threshold=10,
problem_type=None,
eval_metric=None,
stopping_metric=None,
is_trainer_present=False,
random_seed=0,
):
self.path, self.model_context, self.save_path = self.create_contexts(path_context)
self.label = label
self.id_columns = id_columns
self.threshold = label_count_threshold
self.problem_type = problem_type
self.eval_metric = eval_metric
self.stopping_metric = stopping_metric
self.is_trainer_present = is_trainer_present
if random_seed is None:
random_seed = random.randint(0, 1000000)
self.random_seed = random_seed
self.cleaner = None
self.label_cleaner: LabelCleaner = None
self.feature_generator: PipelineFeatureGenerator = feature_generator
self.feature_generators = [self.feature_generator]
self.trainer: AbstractTrainer = None
self.trainer_type = None
self.trainer_path = None
self.reset_paths = False
self.time_fit_total = None
self.time_fit_preprocessing = None
self.time_fit_training = None
self.time_limit = None
try:
from .....version import __version__
self.version = __version__
except:
self.version = None
|
def __init__(
self,
path_context: str,
label: str,
id_columns: list,
feature_generator: AbstractFeatureGenerator,
label_count_threshold=10,
problem_type=None,
eval_metric=None,
stopping_metric=None,
is_trainer_present=False,
random_seed=0,
):
self.path, self.model_context, self.save_path = self.create_contexts(path_context)
self.label = label
self.submission_columns = id_columns
self.threshold = label_count_threshold
self.problem_type = problem_type
self.eval_metric = eval_metric
self.stopping_metric = stopping_metric
self.is_trainer_present = is_trainer_present
if random_seed is None:
random_seed = random.randint(0, 1000000)
self.random_seed = random_seed
self.cleaner = None
self.label_cleaner: LabelCleaner = None
self.feature_generator: AbstractFeatureGenerator = feature_generator
self.feature_generators = [self.feature_generator]
self.trainer: AbstractTrainer = None
self.trainer_type = None
self.trainer_path = None
self.reset_paths = False
self.time_fit_total = None
self.time_fit_preprocessing = None
self.time_fit_training = None
self.time_limit = None
try:
from .....version import __version__
self.version = __version__
except:
self.version = None
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def _fit(
self,
X: DataFrame,
X_val: DataFrame = None,
scheduler_options=None,
hyperparameter_tune=False,
feature_prune=False,
holdout_frac=0.1,
num_bagging_folds=0,
num_bagging_sets=1,
stack_ensemble_levels=0,
hyperparameters=None,
ag_args_fit=None,
excluded_model_types=None,
time_limit=None,
save_data=False,
save_bagged_folds=True,
verbosity=2,
):
"""Arguments:
X (DataFrame): training data
X_val (DataFrame): data used for hyperparameter tuning. Note: final model may be trained using this data as well as training data
hyperparameter_tune (bool): whether to tune hyperparameters or simply use default values
feature_prune (bool): whether to perform feature selection
scheduler_options (tuple: (search_strategy, dict): Options for scheduler
holdout_frac (float): Fraction of data to hold out for evaluating validation performance (ignored if X_val != None, ignored if kfolds != 0)
num_bagging_folds (int): kfolds used for bagging of models, roughly increases model training time by a factor of k (0: disabled)
num_bagging_sets (int): number of repeats of kfold bagging to perform (values must be >= 1),
total number of models trained during bagging = num_bagging_folds * num_bagging_sets
stack_ensemble_levels : (int) Number of stacking levels to use in ensemble stacking. Roughly increases model training time by factor of stack_levels+1 (0: disabled)
Default is 0 (disabled). Use values between 1-3 to improve model quality.
Ignored unless kfolds is also set >= 2
hyperparameters (dict): keys = hyperparameters + search-spaces for each type of model we should train.
"""
if hyperparameters is None:
hyperparameters = {"NN": {}, "GBM": {}}
# TODO: if provided, feature_types in X, X_val are ignored right now, need to pass to Learner/trainer and update this documentation.
if time_limit:
self.time_limit = time_limit
logger.log(20, f"Beginning AutoGluon training ... Time limit = {time_limit}s")
else:
self.time_limit = 1e7
logger.log(20, "Beginning AutoGluon training ...")
logger.log(20, f"AutoGluon will save models to {self.path}")
logger.log(20, f"AutoGluon Version: {self.version}")
logger.log(20, f"Train Data Rows: {len(X)}")
logger.log(20, f"Train Data Columns: {len(X.columns)}")
if X_val is not None:
logger.log(20, f"Tuning Data Rows: {len(X_val)}")
logger.log(20, f"Tuning Data Columns: {len(X_val.columns)}")
time_preprocessing_start = time.time()
logger.log(20, "Preprocessing data ...")
X, y, X_val, y_val, holdout_frac, num_bagging_folds = self.general_data_processing(
X, X_val, holdout_frac, num_bagging_folds
)
time_preprocessing_end = time.time()
self.time_fit_preprocessing = time_preprocessing_end - time_preprocessing_start
logger.log(
20,
f"Data preprocessing and feature engineering runtime = {round(self.time_fit_preprocessing, 2)}s ...",
)
if time_limit:
time_limit_trainer = time_limit - self.time_fit_preprocessing
else:
time_limit_trainer = None
trainer = self.trainer_type(
path=self.model_context,
problem_type=self.label_cleaner.problem_type_transform,
eval_metric=self.eval_metric,
stopping_metric=self.stopping_metric,
num_classes=self.label_cleaner.num_classes,
feature_metadata=self.feature_generator.feature_metadata,
low_memory=True,
kfolds=num_bagging_folds,
n_repeats=num_bagging_sets,
stack_ensemble_levels=stack_ensemble_levels,
scheduler_options=scheduler_options,
time_limit=time_limit_trainer,
save_data=save_data,
save_bagged_folds=save_bagged_folds,
random_seed=self.random_seed,
verbosity=verbosity,
)
self.trainer_path = trainer.path
if self.eval_metric is None:
self.eval_metric = trainer.eval_metric
if self.stopping_metric is None:
self.stopping_metric = trainer.stopping_metric
self.save()
trainer.train(
X,
y,
X_val,
y_val,
hyperparameter_tune=hyperparameter_tune,
feature_prune=feature_prune,
holdout_frac=holdout_frac,
hyperparameters=hyperparameters,
ag_args_fit=ag_args_fit,
excluded_model_types=excluded_model_types,
)
self.save_trainer(trainer=trainer)
time_end = time.time()
self.time_fit_training = time_end - time_preprocessing_end
self.time_fit_total = time_end - time_preprocessing_start
logger.log(
20,
f"AutoGluon training complete, total runtime = {round(self.time_fit_total, 2)}s ...",
)
|
def _fit(
self,
X: DataFrame,
X_val: DataFrame = None,
scheduler_options=None,
hyperparameter_tune=False,
feature_prune=False,
holdout_frac=0.1,
num_bagging_folds=0,
num_bagging_sets=1,
stack_ensemble_levels=0,
hyperparameters=None,
ag_args_fit=None,
excluded_model_types=None,
time_limit=None,
save_data=False,
save_bagged_folds=True,
verbosity=2,
):
"""Arguments:
X (DataFrame): training data
X_val (DataFrame): data used for hyperparameter tuning. Note: final model may be trained using this data as well as training data
hyperparameter_tune (bool): whether to tune hyperparameters or simply use default values
feature_prune (bool): whether to perform feature selection
scheduler_options (tuple: (search_strategy, dict): Options for scheduler
holdout_frac (float): Fraction of data to hold out for evaluating validation performance (ignored if X_val != None, ignored if kfolds != 0)
num_bagging_folds (int): kfolds used for bagging of models, roughly increases model training time by a factor of k (0: disabled)
num_bagging_sets (int): number of repeats of kfold bagging to perform (values must be >= 1),
total number of models trained during bagging = num_bagging_folds * num_bagging_sets
stack_ensemble_levels : (int) Number of stacking levels to use in ensemble stacking. Roughly increases model training time by factor of stack_levels+1 (0: disabled)
Default is 0 (disabled). Use values between 1-3 to improve model quality.
Ignored unless kfolds is also set >= 2
hyperparameters (dict): keys = hyperparameters + search-spaces for each type of model we should train.
"""
if hyperparameters is None:
hyperparameters = {"NN": {}, "GBM": {}}
# TODO: if provided, feature_types in X, X_val are ignored right now, need to pass to Learner/trainer and update this documentation.
if time_limit:
self.time_limit = time_limit
logger.log(20, f"Beginning AutoGluon training ... Time limit = {time_limit}s")
else:
self.time_limit = 1e7
logger.log(20, "Beginning AutoGluon training ...")
logger.log(20, f"AutoGluon will save models to {self.path}")
logger.log(20, f"AutoGluon Version: {self.version}")
logger.log(20, f"Train Data Rows: {len(X)}")
logger.log(20, f"Train Data Columns: {len(X.columns)}")
if X_val is not None:
logger.log(20, f"Tuning Data Rows: {len(X_val)}")
logger.log(20, f"Tuning Data Columns: {len(X_val.columns)}")
time_preprocessing_start = time.time()
logger.log(20, "Preprocessing data ...")
X, y, X_val, y_val, holdout_frac, num_bagging_folds = self.general_data_processing(
X, X_val, holdout_frac, num_bagging_folds
)
time_preprocessing_end = time.time()
self.time_fit_preprocessing = time_preprocessing_end - time_preprocessing_start
logger.log(
20,
f"\tData preprocessing and feature engineering runtime = {round(self.time_fit_preprocessing, 2)}s ...",
)
if time_limit:
time_limit_trainer = time_limit - self.time_fit_preprocessing
else:
time_limit_trainer = None
trainer = self.trainer_type(
path=self.model_context,
problem_type=self.label_cleaner.problem_type_transform,
eval_metric=self.eval_metric,
stopping_metric=self.stopping_metric,
num_classes=self.label_cleaner.num_classes,
feature_types_metadata=self.feature_generator.feature_types_metadata,
low_memory=True,
kfolds=num_bagging_folds,
n_repeats=num_bagging_sets,
stack_ensemble_levels=stack_ensemble_levels,
scheduler_options=scheduler_options,
time_limit=time_limit_trainer,
save_data=save_data,
save_bagged_folds=save_bagged_folds,
random_seed=self.random_seed,
verbosity=verbosity,
)
self.trainer_path = trainer.path
if self.eval_metric is None:
self.eval_metric = trainer.eval_metric
if self.stopping_metric is None:
self.stopping_metric = trainer.stopping_metric
self.save()
trainer.train(
X,
y,
X_val,
y_val,
hyperparameter_tune=hyperparameter_tune,
feature_prune=feature_prune,
holdout_frac=holdout_frac,
hyperparameters=hyperparameters,
ag_args_fit=ag_args_fit,
excluded_model_types=excluded_model_types,
)
self.save_trainer(trainer=trainer)
time_end = time.time()
self.time_fit_training = time_end - time_preprocessing_end
self.time_fit_total = time_end - time_preprocessing_start
logger.log(
20,
f"AutoGluon training complete, total runtime = {round(self.time_fit_total, 2)}s ...",
)
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def general_data_processing(
self, X: DataFrame, X_val: DataFrame, holdout_frac: float, num_bagging_folds: int
):
"""General data processing steps used for all models."""
X = copy.deepcopy(X)
# TODO: We should probably uncomment the below lines, NaN label should be treated as just another value in multiclass classification -> We will have to remove missing, compute problem type, and add back missing if multiclass
# if self.problem_type == MULTICLASS:
# X[self.label] = X[self.label].fillna('')
# Remove all examples with missing labels from this dataset:
missinglabel_inds = [index for index, x in X[self.label].isna().iteritems() if x]
if len(missinglabel_inds) > 0:
logger.warning(
f"Warning: Ignoring {len(missinglabel_inds)} (out of {len(X)}) training examples for which the label value in column '{self.label}' is missing"
)
X = X.drop(missinglabel_inds, axis=0)
if self.problem_type is None:
self.problem_type = self.infer_problem_type(X[self.label])
if X_val is not None and self.label in X_val.columns:
# TODO: This is not an ideal solution, instead check if bagging and X_val exists with label, then merge them prior to entering general data processing.
# This solution should handle virtually all cases correctly, only downside is it might cut more classes than it needs to.
self.threshold, holdout_frac, num_bagging_folds = (
self.adjust_threshold_if_necessary(
X[self.label],
threshold=self.threshold,
holdout_frac=1,
num_bagging_folds=num_bagging_folds,
)
)
else:
self.threshold, holdout_frac, num_bagging_folds = (
self.adjust_threshold_if_necessary(
X[self.label],
threshold=self.threshold,
holdout_frac=holdout_frac,
num_bagging_folds=num_bagging_folds,
)
)
if (
(self.eval_metric is not None)
and (self.eval_metric.name in ["log_loss", "pac_score"])
and (self.problem_type == MULTICLASS)
):
X = augment_rare_classes(X, self.label, self.threshold)
# Gets labels prior to removal of infrequent classes
y_uncleaned = X[self.label].copy()
self.cleaner = Cleaner.construct(
problem_type=self.problem_type, label=self.label, threshold=self.threshold
)
# TODO: What if all classes in X are low frequency in multiclass? Currently we would crash. Not certain how many problems actually have this property
X = self.cleaner.fit_transform(
X
) # TODO: Consider merging cleaner into label_cleaner
X, y = self.extract_label(X)
self.label_cleaner = LabelCleaner.construct(
problem_type=self.problem_type, y=y, y_uncleaned=y_uncleaned
)
y = self.label_cleaner.transform(y)
if self.label_cleaner.num_classes is not None:
logger.log(20, f"Train Data Class Count: {self.label_cleaner.num_classes}")
if X_val is not None and self.label in X_val.columns:
X_val = self.cleaner.transform(X_val)
if len(X_val) == 0:
logger.warning(
"All X_val data contained low frequency classes, ignoring X_val and generating from subset of X"
)
X_val = None
y_val = None
else:
X_val, y_val = self.extract_label(X_val)
y_val = self.label_cleaner.transform(y_val)
else:
y_val = None
if self.id_columns:
logger.log(20, f"Dropping ID columns: {self.id_columns}")
X = X.drop(self.id_columns, axis=1, errors="ignore")
if X_val is not None:
X_val = X_val.drop(self.id_columns, axis=1, errors="ignore")
# TODO: Move this up to top of data before removing data, this way our feature generator is better
if X_val is not None:
# Do this if working with SKLearn models, otherwise categorical features may perform very badly on the test set
logger.log(
15,
"Performing general data preprocessing with merged train & validation data, so validation performance may not accurately reflect performance on new test data",
)
X_super = pd.concat([X, X_val], ignore_index=True)
if self.feature_generator.is_fit():
logger.log(
20,
f"{self.feature_generator.__class__.__name__} is already fit, so the training data will be processed via .transform() instead of .fit_transform().",
)
X_super = self.feature_generator.transform(X_super)
self.feature_generator.print_feature_metadata_info()
else:
X_super = self.feature_generator.fit_transform(X_super)
X = X_super.head(len(X)).set_index(X.index)
X_val = X_super.tail(len(X_val)).set_index(X_val.index)
del X_super
else:
if self.feature_generator.is_fit():
logger.log(
20,
f"{self.feature_generator.__class__.__name__} is already fit, so the training data will be processed via .transform() instead of .fit_transform().",
)
X = self.feature_generator.transform(X)
self.feature_generator.print_feature_metadata_info()
else:
X = self.feature_generator.fit_transform(X)
return X, y, X_val, y_val, holdout_frac, num_bagging_folds
|
def general_data_processing(
self, X: DataFrame, X_val: DataFrame, holdout_frac: float, num_bagging_folds: int
):
"""General data processing steps used for all models."""
X = copy.deepcopy(X)
# TODO: We should probably uncomment the below lines, NaN label should be treated as just another value in multiclass classification -> We will have to remove missing, compute problem type, and add back missing if multiclass
# if self.problem_type == MULTICLASS:
# X[self.label] = X[self.label].fillna('')
# Remove all examples with missing labels from this dataset:
missinglabel_inds = [index for index, x in X[self.label].isna().iteritems() if x]
if len(missinglabel_inds) > 0:
logger.warning(
f"Warning: Ignoring {len(missinglabel_inds)} (out of {len(X)}) training examples for which the label value in column '{self.label}' is missing"
)
X = X.drop(missinglabel_inds, axis=0)
if self.problem_type is None:
self.problem_type = self.infer_problem_type(X[self.label])
if X_val is not None and self.label in X_val.columns:
# TODO: This is not an ideal solution, instead check if bagging and X_val exists with label, then merge them prior to entering general data processing.
# This solution should handle virtually all cases correctly, only downside is it might cut more classes than it needs to.
self.threshold, holdout_frac, num_bagging_folds = (
self.adjust_threshold_if_necessary(
X[self.label],
threshold=self.threshold,
holdout_frac=1,
num_bagging_folds=num_bagging_folds,
)
)
else:
self.threshold, holdout_frac, num_bagging_folds = (
self.adjust_threshold_if_necessary(
X[self.label],
threshold=self.threshold,
holdout_frac=holdout_frac,
num_bagging_folds=num_bagging_folds,
)
)
if (
(self.eval_metric is not None)
and (self.eval_metric.name in ["log_loss", "pac_score"])
and (self.problem_type == MULTICLASS)
):
X = augment_rare_classes(X, self.label, self.threshold)
# Gets labels prior to removal of infrequent classes
y_uncleaned = X[self.label].copy()
self.cleaner = Cleaner.construct(
problem_type=self.problem_type, label=self.label, threshold=self.threshold
)
# TODO: What if all classes in X are low frequency in multiclass? Currently we would crash. Not certain how many problems actually have this property
X = self.cleaner.fit_transform(
X
) # TODO: Consider merging cleaner into label_cleaner
X, y = self.extract_label(X)
self.label_cleaner = LabelCleaner.construct(
problem_type=self.problem_type, y=y, y_uncleaned=y_uncleaned
)
y = self.label_cleaner.transform(y)
if self.label_cleaner.num_classes is not None:
logger.log(20, f"Train Data Class Count: {self.label_cleaner.num_classes}")
if X_val is not None and self.label in X_val.columns:
X_val = self.cleaner.transform(X_val)
if len(X_val) == 0:
logger.warning(
"All X_val data contained low frequency classes, ignoring X_val and generating from subset of X"
)
X_val = None
y_val = None
else:
X_val, y_val = self.extract_label(X_val)
y_val = self.label_cleaner.transform(y_val)
else:
y_val = None
# TODO: Move this up to top of data before removing data, this way our feature generator is better
if X_val is not None:
# Do this if working with SKLearn models, otherwise categorical features may perform very badly on the test set
logger.log(
15,
"Performing general data preprocessing with merged train & validation data, so validation performance may not accurately reflect performance on new test data",
)
X_super = pd.concat([X, X_val], ignore_index=True)
X_super = self.feature_generator.fit_transform(
X_super, banned_features=self.submission_columns, drop_duplicates=False
)
X = X_super.head(len(X)).set_index(X.index)
X_val = X_super.tail(len(X_val)).set_index(X_val.index)
del X_super
else:
X = self.feature_generator.fit_transform(
X, banned_features=self.submission_columns, drop_duplicates=False
)
return X, y, X_val, y_val, holdout_frac, num_bagging_folds
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def __init__(
self,
path: str,
name: str,
problem_type: str,
eval_metric: Union[str, metrics.Scorer] = None,
num_classes=None,
stopping_metric=None,
model=None,
hyperparameters=None,
features=None,
feature_metadata: FeatureMetadata = None,
debug=0,
**kwargs,
):
"""Creates a new model.
Args:
path (str): directory where to store all outputs.
name (str): name of subdirectory inside path where model will be saved.
problem_type (str): type of problem this model will handle. Valid options: ['binary', 'multiclass', 'regression'].
eval_metric (str or autogluon.utils.tabular.metrics.Scorer): objective function the model intends to optimize. If None, will be inferred based on problem_type.
hyperparameters (dict): various hyperparameters that will be used by model (can be search spaces instead of fixed values).
feature_metadata (autogluon.utils.tabular.features.feature_metadata.FeatureMetadata): contains feature type information that can be used to identify special features such as text ngrams and datetime as well as which features are numerical vs categorical
"""
self.name = name
self.path_root = path
self.path_suffix = (
self.name + os.path.sep
) # TODO: Make into function to avoid having to reassign on load?
self.path = self.create_contexts(
self.path_root + self.path_suffix
) # TODO: Make this path a function for consistency.
self.num_classes = num_classes
self.model = model
self.problem_type = problem_type
if eval_metric is not None:
self.eval_metric = metrics.get_metric(
eval_metric, self.problem_type, "eval_metric"
) # Note: we require higher values = better performance
else:
self.eval_metric = infer_eval_metric(problem_type=self.problem_type)
logger.log(
20,
f"Model {self.name}'s eval_metric inferred to be '{self.eval_metric.name}' because problem_type='{self.problem_type}' and eval_metric was not specified during init.",
)
if stopping_metric is None:
self.stopping_metric = self.eval_metric
else:
self.stopping_metric = stopping_metric
if self.eval_metric.name in OBJECTIVES_TO_NORMALIZE:
self.normalize_pred_probas = True
logger.debug(
self.name
+ " predicted probabilities will be transformed to never =0 since eval_metric="
+ self.eval_metric.name
)
else:
self.normalize_pred_probas = False
if isinstance(self.eval_metric, metrics._ProbaScorer):
self.metric_needs_y_pred = False
elif isinstance(self.eval_metric, metrics._ThresholdScorer):
self.metric_needs_y_pred = False
else:
self.metric_needs_y_pred = True
if isinstance(self.stopping_metric, metrics._ProbaScorer):
self.stopping_metric_needs_y_pred = False
elif isinstance(self.stopping_metric, metrics._ThresholdScorer):
self.stopping_metric_needs_y_pred = False
else:
self.stopping_metric_needs_y_pred = True
self.feature_metadata = feature_metadata # TODO: Should this be passed to a model on creation? Should it live in a Dataset object and passed during fit? Currently it is being updated prior to fit by trainer
self.features = features
self.debug = debug
self.fit_time = None # Time taken to fit in seconds (Training data)
self.predict_time = None # Time taken to predict in seconds (Validation data)
self.val_score = None # Score with eval_metric (Validation data)
self.params = {}
self.params_aux = {}
self._set_default_auxiliary_params()
if hyperparameters is not None:
hyperparameters = hyperparameters.copy()
if AG_ARGS_FIT in hyperparameters:
ag_args_fit = hyperparameters.pop(AG_ARGS_FIT)
self.params_aux.update(ag_args_fit)
self._set_default_params()
self.nondefault_params = []
if hyperparameters is not None:
self.params.update(hyperparameters)
self.nondefault_params = list(hyperparameters.keys())[
:
] # These are hyperparameters that user has specified.
self.params_trained = dict()
|
def __init__(
self,
path: str,
name: str,
problem_type: str,
eval_metric: Union[str, metrics.Scorer] = None,
num_classes=None,
stopping_metric=None,
model=None,
hyperparameters=None,
features=None,
feature_types_metadata: FeatureTypesMetadata = None,
debug=0,
**kwargs,
):
"""Creates a new model.
Args:
path (str): directory where to store all outputs.
name (str): name of subdirectory inside path where model will be saved.
problem_type (str): type of problem this model will handle. Valid options: ['binary', 'multiclass', 'regression'].
eval_metric (str or autogluon.utils.tabular.metrics.Scorer): objective function the model intends to optimize. If None, will be inferred based on problem_type.
hyperparameters (dict): various hyperparameters that will be used by model (can be search spaces instead of fixed values).
feature_types_metadata (autogluon.utils.tabular.features.feature_types_metadata.FeatureTypesMetadata): contains feature type information that can be used to identify special features such as text ngrams and datetime.
"""
self.name = name
self.path_root = path
self.path_suffix = (
self.name + os.path.sep
) # TODO: Make into function to avoid having to reassign on load?
self.path = self.create_contexts(
self.path_root + self.path_suffix
) # TODO: Make this path a function for consistency.
self.num_classes = num_classes
self.model = model
self.problem_type = problem_type
if eval_metric is not None:
self.eval_metric = metrics.get_metric(
eval_metric, self.problem_type, "eval_metric"
) # Note: we require higher values = better performance
else:
self.eval_metric = infer_eval_metric(problem_type=self.problem_type)
logger.log(
20,
f"Model {self.name}'s eval_metric inferred to be '{self.eval_metric.name}' because problem_type='{self.problem_type}' and eval_metric was not specified during init.",
)
if stopping_metric is None:
self.stopping_metric = self.eval_metric
else:
self.stopping_metric = stopping_metric
if self.eval_metric.name in OBJECTIVES_TO_NORMALIZE:
self.normalize_pred_probas = True
logger.debug(
self.name
+ " predicted probabilities will be transformed to never =0 since eval_metric="
+ self.eval_metric.name
)
else:
self.normalize_pred_probas = False
if isinstance(self.eval_metric, metrics._ProbaScorer):
self.metric_needs_y_pred = False
elif isinstance(self.eval_metric, metrics._ThresholdScorer):
self.metric_needs_y_pred = False
else:
self.metric_needs_y_pred = True
if isinstance(self.stopping_metric, metrics._ProbaScorer):
self.stopping_metric_needs_y_pred = False
elif isinstance(self.stopping_metric, metrics._ThresholdScorer):
self.stopping_metric_needs_y_pred = False
else:
self.stopping_metric_needs_y_pred = True
self.feature_types_metadata = feature_types_metadata # TODO: Should this be passed to a model on creation? Should it live in a Dataset object and passed during fit? Currently it is being updated prior to fit by trainer
self.features = features
self.debug = debug
self.fit_time = None # Time taken to fit in seconds (Training data)
self.predict_time = None # Time taken to predict in seconds (Validation data)
self.val_score = None # Score with eval_metric (Validation data)
self.params = {}
self.params_aux = {}
self._set_default_auxiliary_params()
if hyperparameters is not None:
hyperparameters = hyperparameters.copy()
if AG_ARGS_FIT in hyperparameters:
ag_args_fit = hyperparameters.pop(AG_ARGS_FIT)
self.params_aux.update(ag_args_fit)
self._set_default_params()
self.nondefault_params = []
if hyperparameters is not None:
self.params.update(hyperparameters)
self.nondefault_params = list(hyperparameters.keys())[
:
] # These are hyperparameters that user has specified.
self.params_trained = dict()
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def _set_default_auxiliary_params(self):
# TODO: Consider adding to get_info() output
default_auxiliary_params = dict(
max_memory_usage_ratio=1.0, # Ratio of memory usage allowed by the model. Values > 1.0 have an increased risk of causing OOM errors.
# TODO: Add more params
# max_memory_usage=None,
# max_disk_usage=None,
max_time_limit_ratio=1.0, # ratio of given time_limit to use during fit(). If time_limit == 10 and max_time_limit_ratio=0.3, time_limit would be changed to 3.
max_time_limit=None, # max time_limit value during fit(). If the provided time_limit is greater than this value, it will be replaced by max_time_limit. Occurs after max_time_limit_ratio is applied.
min_time_limit=0, # min time_limit value during fit(). If the provided time_limit is less than this value, it will be replaced by min_time_limit. Occurs after max_time_limit is applied.
# num_cpu=None,
# num_gpu=None,
# ignore_hpo=False,
# max_early_stopping_rounds=None,
# use_orig_features=True, # TODO: Only for stackers
# TODO: add option for only top-k ngrams
ignored_type_group_special=[], # List, drops any features in `self.feature_metadata.type_group_map_special[type]` for type in `ignored_type_group_special`. | Currently undocumented in task.
ignored_type_group_raw=[], # List, drops any features in `self.feature_metadata.type_group_map_raw[type]` for type in `ignored_type_group_raw`. | Currently undocumented in task.
)
for key, value in default_auxiliary_params.items():
self._set_default_param_value(key, value, params=self.params_aux)
|
def _set_default_auxiliary_params(self):
# TODO: Consider adding to get_info() output
default_auxiliary_params = dict(
max_memory_usage_ratio=1.0, # Ratio of memory usage allowed by the model. Values > 1.0 have an increased risk of causing OOM errors.
# TODO: Add more params
# max_memory_usage=None,
# max_disk_usage=None,
max_time_limit_ratio=1.0, # ratio of given time_limit to use during fit(). If time_limit == 10 and max_time_limit_ratio=0.3, time_limit would be changed to 3.
max_time_limit=None, # max time_limit value during fit(). If the provided time_limit is greater than this value, it will be replaced by max_time_limit. Occurs after max_time_limit_ratio is applied.
min_time_limit=0, # min time_limit value during fit(). If the provided time_limit is less than this value, it will be replaced by min_time_limit. Occurs after max_time_limit is applied.
# num_cpu=None,
# num_gpu=None,
# ignore_hpo=False,
# max_early_stopping_rounds=None,
# use_orig_features=True, # TODO: Only for stackers
# TODO: add option for only top-k ngrams
ignored_feature_types_special=[], # List, drops any features in `self.feature_types_metadata.feature_types_special[type]` for type in `ignored_feature_types_special`. | Currently undocumented in task.
ignored_feature_types_raw=[], # List, drops any features in `self.feature_types_metadata.feature_types_raw[type]` for type in `ignored_feature_types_raw`. | Currently undocumented in task.
)
for key, value in default_auxiliary_params.items():
self._set_default_param_value(key, value, params=self.params_aux)
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def preprocess(self, X):
if self.features is not None:
# TODO: In online-inference this becomes expensive, add option to remove it (only safe in controlled environment where it is already known features are present
if list(X.columns) != self.features:
return X[self.features]
else:
self.features = list(
X.columns
) # TODO: add fit and transform versions of preprocess instead of doing this
ignored_type_group_raw = self.params_aux.get("ignored_type_group_raw", [])
ignored_type_group_special = self.params_aux.get(
"ignored_type_group_special", []
)
valid_features = self.feature_metadata.get_features(
invalid_raw_types=ignored_type_group_raw,
invalid_special_types=ignored_type_group_special,
)
self.features = [
feature for feature in self.features if feature in valid_features
]
if not self.features:
raise NoValidFeatures
if list(X.columns) != self.features:
X = X[self.features]
return X
|
def preprocess(self, X):
if self.features is not None:
# TODO: In online-inference this becomes expensive, add option to remove it (only safe in controlled environment where it is already known features are present
if list(X.columns) != self.features:
return X[self.features]
else:
self.features = list(
X.columns
) # TODO: add fit and transform versions of preprocess instead of doing this
ignored_feature_types_raw = self.params_aux.get("ignored_feature_types_raw", [])
if ignored_feature_types_raw:
for ignored_feature_type in ignored_feature_types_raw:
self.features = [
feature
for feature in self.features
if feature
not in self.feature_types_metadata.feature_types_raw[
ignored_feature_type
]
]
ignored_feature_types_special = self.params_aux.get(
"ignored_feature_types_special", []
)
if ignored_feature_types_special:
for ignored_feature_type in ignored_feature_types_special:
self.features = [
feature
for feature in self.features
if feature
not in self.feature_types_metadata.feature_types_special[
ignored_feature_type
]
]
if not self.features:
raise NoValidFeatures
if ignored_feature_types_raw or ignored_feature_types_special:
if list(X.columns) != self.features:
X = X[self.features]
return X
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def preprocess(self, X):
if self.features is not None:
# TODO: In online-inference this becomes expensive, add option to remove it (only safe in controlled environment where it is already known features are present
if list(X.columns) != self.features:
return X[self.features]
else:
self.features = list(
X.columns
) # TODO: add fit and transform versions of preprocess instead of doing this
ignored_type_group_raw = self.params_aux.get("ignored_type_group_raw", [])
ignored_type_group_special = self.params_aux.get(
"ignored_type_group_special", []
)
valid_features = self.feature_metadata.get_features(
invalid_raw_types=ignored_type_group_raw,
invalid_special_types=ignored_type_group_special,
)
self.features = [
feature for feature in self.features if feature in valid_features
]
if not self.features:
raise NoValidFeatures
if list(X.columns) != self.features:
X = X[self.features]
return X
|
def preprocess(self, X):
X = convert_categorical_to_int(X)
return super().preprocess(X)
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def __init__(
self, model_base: AbstractModel, save_bagged_folds=True, random_state=0, **kwargs
):
self.model_base = model_base
self._child_type = type(self.model_base)
self.models = []
self._oof_pred_proba = None
self._oof_pred_model_repeats = None
self._n_repeats = 0 # Number of n_repeats with at least 1 model fit, if kfold=5 and 8 models have been fit, _n_repeats is 2
self._n_repeats_finished = 0 # Number of n_repeats finished, if kfold=5 and 8 models have been fit, _n_repeats_finished is 1
self._k_fold_end = 0 # Number of models fit in current n_repeat (0 if completed), if kfold=5 and 8 models have been fit, _k_fold_end is 3
self._k = None # k models per n_repeat, equivalent to kfold value
self._k_per_n_repeat = [] # k-fold used for each n_repeat. == [5, 10, 3] if first kfold was 5, second was 10, and third was 3
self._random_state = random_state
self.low_memory = True
self.bagged_mode = None
self.save_bagged_folds = save_bagged_folds
try:
feature_metadata = self.model_base.feature_metadata
except:
feature_metadata = None
eval_metric = kwargs.pop("eval_metric", self.model_base.eval_metric)
stopping_metric = kwargs.pop("stopping_metric", self.model_base.stopping_metric)
super().__init__(
problem_type=self.model_base.problem_type,
eval_metric=eval_metric,
stopping_metric=stopping_metric,
feature_metadata=feature_metadata,
**kwargs,
)
|
def __init__(
self, model_base: AbstractModel, save_bagged_folds=True, random_state=0, **kwargs
):
self.model_base = model_base
self._child_type = type(self.model_base)
self.models = []
self._oof_pred_proba = None
self._oof_pred_model_repeats = None
self._n_repeats = 0 # Number of n_repeats with at least 1 model fit, if kfold=5 and 8 models have been fit, _n_repeats is 2
self._n_repeats_finished = 0 # Number of n_repeats finished, if kfold=5 and 8 models have been fit, _n_repeats_finished is 1
self._k_fold_end = 0 # Number of models fit in current n_repeat (0 if completed), if kfold=5 and 8 models have been fit, _k_fold_end is 3
self._k = None # k models per n_repeat, equivalent to kfold value
self._k_per_n_repeat = [] # k-fold used for each n_repeat. == [5, 10, 3] if first kfold was 5, second was 10, and third was 3
self._random_state = random_state
self.low_memory = True
self.bagged_mode = None
self.save_bagged_folds = save_bagged_folds
try:
feature_types_metadata = self.model_base.feature_types_metadata
except:
feature_types_metadata = None
eval_metric = kwargs.pop("eval_metric", self.model_base.eval_metric)
stopping_metric = kwargs.pop("stopping_metric", self.model_base.stopping_metric)
super().__init__(
problem_type=self.model_base.problem_type,
eval_metric=eval_metric,
stopping_metric=stopping_metric,
feature_types_metadata=feature_types_metadata,
**kwargs,
)
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def _fit(
self,
X,
y,
k_fold=5,
k_fold_start=0,
k_fold_end=None,
n_repeats=1,
n_repeat_start=0,
time_limit=None,
**kwargs,
):
if k_fold < 1:
k_fold = 1
if k_fold_end is None:
k_fold_end = k_fold
if self._oof_pred_proba is None and (k_fold_start != 0 or n_repeat_start != 0):
self._load_oof()
if n_repeat_start != self._n_repeats_finished:
raise ValueError(
f"n_repeat_start must equal self._n_repeats_finished, values: ({n_repeat_start}, {self._n_repeats_finished})"
)
if n_repeats <= n_repeat_start:
raise ValueError(
f"n_repeats must be greater than n_repeat_start, values: ({n_repeats}, {n_repeat_start})"
)
if k_fold_start != self._k_fold_end:
raise ValueError(
f"k_fold_start must equal previous k_fold_end, values: ({k_fold_start}, {self._k_fold_end})"
)
if k_fold_start >= k_fold_end:
# TODO: Remove this limitation if n_repeats > 1
raise ValueError(
f"k_fold_end must be greater than k_fold_start, values: ({k_fold_end}, {k_fold_start})"
)
if (n_repeats - n_repeat_start) > 1 and k_fold_end != k_fold:
# TODO: Remove this limitation
raise ValueError(
f"k_fold_end must equal k_fold when (n_repeats - n_repeat_start) > 1, values: ({k_fold_end}, {k_fold})"
)
if self._k is not None and self._k != k_fold:
raise ValueError(
f"k_fold must equal previously fit k_fold value for the current n_repeat, values: (({k_fold}, {self._k})"
)
fold_start = n_repeat_start * k_fold + k_fold_start
fold_end = (n_repeats - 1) * k_fold + k_fold_end
time_start = time.time()
model_base = self._get_model_base()
if self.features is not None:
model_base.features = self.features
model_base.feature_metadata = self.feature_metadata # TODO: Don't pass this here
if self.model_base is not None:
self.save_model_base(self.model_base)
self.model_base = None
if k_fold == 1:
if self._n_repeats != 0:
raise ValueError(
f"n_repeats must equal 0 when fitting a single model with k_fold < 2, values: ({self._n_repeats}, {k_fold})"
)
model_base.set_contexts(path_context=self.path + model_base.name + os.path.sep)
time_start_fit = time.time()
model_base.fit(X_train=X, y_train=y, time_limit=time_limit, **kwargs)
model_base.fit_time = time.time() - time_start_fit
model_base.predict_time = None
self._oof_pred_proba = model_base.predict_proba(
X=X
) # TODO: Cheater value, will be overfit to valid set
self._oof_pred_model_repeats = np.ones(shape=len(X), dtype=np.uint8)
self._n_repeats = 1
self._n_repeats_finished = 1
self._k_per_n_repeat = [1]
self.bagged_mode = False
model_base.reduce_memory_size(
remove_fit=True, remove_info=False, requires_save=True
)
if not self.save_bagged_folds:
model_base.model = None
if self.low_memory:
self.save_child(model_base, verbose=False)
self.models = [model_base.name]
else:
self.models = [model_base]
self._add_child_times_to_bag(model=model_base)
return
# TODO: Preprocess data here instead of repeatedly
kfolds = generate_kfold(
X=X,
y=y,
n_splits=k_fold,
stratified=self.is_stratified(),
random_state=self._random_state,
n_repeats=n_repeats,
)
if self.problem_type == MULTICLASS:
oof_pred_proba = np.zeros(shape=(len(X), len(y.unique())), dtype=np.float32)
elif self.problem_type == SOFTCLASS:
oof_pred_proba = np.zeros(shape=y.shape, dtype=np.float32)
else:
oof_pred_proba = np.zeros(shape=len(X))
oof_pred_model_repeats = np.zeros(shape=len(X), dtype=np.uint8)
models = []
folds_to_fit = fold_end - fold_start
for j in range(n_repeat_start, n_repeats): # For each n_repeat
cur_repeat_count = j - n_repeat_start
fold_start_n_repeat = fold_start + cur_repeat_count * k_fold
fold_end_n_repeat = min(fold_start_n_repeat + k_fold, fold_end)
# TODO: Consider moving model fit inner for loop to a function to simply this code
for i in range(fold_start_n_repeat, fold_end_n_repeat): # For each fold
folds_finished = i - fold_start
folds_left = fold_end - i
fold = kfolds[i]
time_elapsed = time.time() - time_start
if time_limit is not None:
time_left = time_limit - time_elapsed
required_time_per_fold = time_left / folds_left
time_limit_fold = required_time_per_fold * 0.8
if folds_finished > 0:
expected_time_required = (
time_elapsed * folds_to_fit / folds_finished
)
expected_remaining_time_required = (
expected_time_required * folds_left / folds_to_fit
)
if expected_remaining_time_required > time_left:
raise TimeLimitExceeded
if time_left <= 0:
raise TimeLimitExceeded
else:
time_limit_fold = None
time_start_fold = time.time()
train_index, val_index = fold
X_train, X_val = X.iloc[train_index, :], X.iloc[val_index, :]
y_train, y_val = y.iloc[train_index], y.iloc[val_index]
fold_model = copy.deepcopy(model_base)
fold_model.name = f"{fold_model.name}_fold_{i}"
fold_model.set_contexts(self.path + fold_model.name + os.path.sep)
fold_model.fit(
X_train=X_train,
y_train=y_train,
X_val=X_val,
y_val=y_val,
time_limit=time_limit_fold,
**kwargs,
)
time_train_end_fold = time.time()
if (
time_limit is not None
): # Check to avoid unnecessarily predicting and saving a model when an Exception is going to be raised later
if i != (fold_end - 1):
time_elapsed = time.time() - time_start
time_left = time_limit - time_elapsed
expected_time_required = (
time_elapsed * folds_to_fit / (folds_finished + 1)
)
expected_remaining_time_required = (
expected_time_required * (folds_left - 1) / folds_to_fit
)
if expected_remaining_time_required > time_left:
raise TimeLimitExceeded
pred_proba = fold_model.predict_proba(X_val)
time_predict_end_fold = time.time()
fold_model.fit_time = time_train_end_fold - time_start_fold
fold_model.predict_time = time_predict_end_fold - time_train_end_fold
fold_model.val_score = fold_model.score_with_y_pred_proba(
y=y_val, y_pred_proba=pred_proba
)
fold_model.reduce_memory_size(
remove_fit=True, remove_info=False, requires_save=True
)
if not self.save_bagged_folds:
fold_model.model = None
if self.low_memory:
self.save_child(fold_model, verbose=False)
models.append(fold_model.name)
else:
models.append(fold_model)
oof_pred_proba[val_index] += pred_proba
oof_pred_model_repeats[val_index] += 1
self._add_child_times_to_bag(model=fold_model)
if (fold_end_n_repeat != fold_end) or (k_fold == k_fold_end):
self._k_per_n_repeat.append(k_fold)
self.models += models
self.bagged_mode = True
if self._oof_pred_proba is None:
self._oof_pred_proba = oof_pred_proba
self._oof_pred_model_repeats = oof_pred_model_repeats
else:
self._oof_pred_proba += oof_pred_proba
self._oof_pred_model_repeats += oof_pred_model_repeats
self._n_repeats = n_repeats
if k_fold == k_fold_end:
self._k = None
self._k_fold_end = 0
self._n_repeats_finished = self._n_repeats
else:
self._k = k_fold
self._k_fold_end = k_fold_end
self._n_repeats_finished = self._n_repeats - 1
|
def _fit(
self,
X,
y,
k_fold=5,
k_fold_start=0,
k_fold_end=None,
n_repeats=1,
n_repeat_start=0,
time_limit=None,
**kwargs,
):
if k_fold < 1:
k_fold = 1
if k_fold_end is None:
k_fold_end = k_fold
if self._oof_pred_proba is None and (k_fold_start != 0 or n_repeat_start != 0):
self._load_oof()
if n_repeat_start != self._n_repeats_finished:
raise ValueError(
f"n_repeat_start must equal self._n_repeats_finished, values: ({n_repeat_start}, {self._n_repeats_finished})"
)
if n_repeats <= n_repeat_start:
raise ValueError(
f"n_repeats must be greater than n_repeat_start, values: ({n_repeats}, {n_repeat_start})"
)
if k_fold_start != self._k_fold_end:
raise ValueError(
f"k_fold_start must equal previous k_fold_end, values: ({k_fold_start}, {self._k_fold_end})"
)
if k_fold_start >= k_fold_end:
# TODO: Remove this limitation if n_repeats > 1
raise ValueError(
f"k_fold_end must be greater than k_fold_start, values: ({k_fold_end}, {k_fold_start})"
)
if (n_repeats - n_repeat_start) > 1 and k_fold_end != k_fold:
# TODO: Remove this limitation
raise ValueError(
f"k_fold_end must equal k_fold when (n_repeats - n_repeat_start) > 1, values: ({k_fold_end}, {k_fold})"
)
if self._k is not None and self._k != k_fold:
raise ValueError(
f"k_fold must equal previously fit k_fold value for the current n_repeat, values: (({k_fold}, {self._k})"
)
fold_start = n_repeat_start * k_fold + k_fold_start
fold_end = (n_repeats - 1) * k_fold + k_fold_end
time_start = time.time()
model_base = self._get_model_base()
if self.features is not None:
model_base.features = self.features
model_base.feature_types_metadata = (
self.feature_types_metadata
) # TODO: Don't pass this here
if self.model_base is not None:
self.save_model_base(self.model_base)
self.model_base = None
if k_fold == 1:
if self._n_repeats != 0:
raise ValueError(
f"n_repeats must equal 0 when fitting a single model with k_fold < 2, values: ({self._n_repeats}, {k_fold})"
)
model_base.set_contexts(path_context=self.path + model_base.name + os.path.sep)
time_start_fit = time.time()
model_base.fit(X_train=X, y_train=y, time_limit=time_limit, **kwargs)
model_base.fit_time = time.time() - time_start_fit
model_base.predict_time = None
self._oof_pred_proba = model_base.predict_proba(
X=X
) # TODO: Cheater value, will be overfit to valid set
self._oof_pred_model_repeats = np.ones(shape=len(X), dtype=np.uint8)
self._n_repeats = 1
self._n_repeats_finished = 1
self._k_per_n_repeat = [1]
self.bagged_mode = False
model_base.reduce_memory_size(
remove_fit=True, remove_info=False, requires_save=True
)
if not self.save_bagged_folds:
model_base.model = None
if self.low_memory:
self.save_child(model_base, verbose=False)
self.models = [model_base.name]
else:
self.models = [model_base]
self._add_child_times_to_bag(model=model_base)
return
# TODO: Preprocess data here instead of repeatedly
kfolds = generate_kfold(
X=X,
y=y,
n_splits=k_fold,
stratified=self.is_stratified(),
random_state=self._random_state,
n_repeats=n_repeats,
)
if self.problem_type == MULTICLASS:
oof_pred_proba = np.zeros(shape=(len(X), len(y.unique())), dtype=np.float32)
elif self.problem_type == SOFTCLASS:
oof_pred_proba = np.zeros(shape=y.shape, dtype=np.float32)
else:
oof_pred_proba = np.zeros(shape=len(X))
oof_pred_model_repeats = np.zeros(shape=len(X), dtype=np.uint8)
models = []
folds_to_fit = fold_end - fold_start
for j in range(n_repeat_start, n_repeats): # For each n_repeat
cur_repeat_count = j - n_repeat_start
fold_start_n_repeat = fold_start + cur_repeat_count * k_fold
fold_end_n_repeat = min(fold_start_n_repeat + k_fold, fold_end)
# TODO: Consider moving model fit inner for loop to a function to simply this code
for i in range(fold_start_n_repeat, fold_end_n_repeat): # For each fold
folds_finished = i - fold_start
folds_left = fold_end - i
fold = kfolds[i]
time_elapsed = time.time() - time_start
if time_limit is not None:
time_left = time_limit - time_elapsed
required_time_per_fold = time_left / folds_left
time_limit_fold = required_time_per_fold * 0.8
if folds_finished > 0:
expected_time_required = (
time_elapsed * folds_to_fit / folds_finished
)
expected_remaining_time_required = (
expected_time_required * folds_left / folds_to_fit
)
if expected_remaining_time_required > time_left:
raise TimeLimitExceeded
if time_left <= 0:
raise TimeLimitExceeded
else:
time_limit_fold = None
time_start_fold = time.time()
train_index, val_index = fold
X_train, X_val = X.iloc[train_index, :], X.iloc[val_index, :]
y_train, y_val = y.iloc[train_index], y.iloc[val_index]
fold_model = copy.deepcopy(model_base)
fold_model.name = f"{fold_model.name}_fold_{i}"
fold_model.set_contexts(self.path + fold_model.name + os.path.sep)
fold_model.fit(
X_train=X_train,
y_train=y_train,
X_val=X_val,
y_val=y_val,
time_limit=time_limit_fold,
**kwargs,
)
time_train_end_fold = time.time()
if (
time_limit is not None
): # Check to avoid unnecessarily predicting and saving a model when an Exception is going to be raised later
if i != (fold_end - 1):
time_elapsed = time.time() - time_start
time_left = time_limit - time_elapsed
expected_time_required = (
time_elapsed * folds_to_fit / (folds_finished + 1)
)
expected_remaining_time_required = (
expected_time_required * (folds_left - 1) / folds_to_fit
)
if expected_remaining_time_required > time_left:
raise TimeLimitExceeded
pred_proba = fold_model.predict_proba(X_val)
time_predict_end_fold = time.time()
fold_model.fit_time = time_train_end_fold - time_start_fold
fold_model.predict_time = time_predict_end_fold - time_train_end_fold
fold_model.val_score = fold_model.score_with_y_pred_proba(
y=y_val, y_pred_proba=pred_proba
)
fold_model.reduce_memory_size(
remove_fit=True, remove_info=False, requires_save=True
)
if not self.save_bagged_folds:
fold_model.model = None
if self.low_memory:
self.save_child(fold_model, verbose=False)
models.append(fold_model.name)
else:
models.append(fold_model)
oof_pred_proba[val_index] += pred_proba
oof_pred_model_repeats[val_index] += 1
self._add_child_times_to_bag(model=fold_model)
if (fold_end_n_repeat != fold_end) or (k_fold == k_fold_end):
self._k_per_n_repeat.append(k_fold)
self.models += models
self.bagged_mode = True
if self._oof_pred_proba is None:
self._oof_pred_proba = oof_pred_proba
self._oof_pred_model_repeats = oof_pred_model_repeats
else:
self._oof_pred_proba += oof_pred_proba
self._oof_pred_model_repeats += oof_pred_model_repeats
self._n_repeats = n_repeats
if k_fold == k_fold_end:
self._k = None
self._k_fold_end = 0
self._n_repeats_finished = self._n_repeats
else:
self._k = k_fold
self._k_fold_end = k_fold_end
self._n_repeats_finished = self._n_repeats - 1
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def convert_to_refitfull_template(self):
compressed_params = self._get_compressed_params()
model_compressed = copy.deepcopy(self._get_model_base())
model_compressed.feature_metadata = (
self.feature_metadata
) # TODO: Don't pass this here
model_compressed.params = compressed_params
model_compressed.name = model_compressed.name + REFIT_FULL_SUFFIX
model_compressed.set_contexts(self.path_root + model_compressed.name + os.path.sep)
return model_compressed
|
def convert_to_refitfull_template(self):
compressed_params = self._get_compressed_params()
model_compressed = copy.deepcopy(self._get_model_base())
model_compressed.feature_types_metadata = (
self.feature_types_metadata
) # TODO: Don't pass this here
model_compressed.params = compressed_params
model_compressed.name = model_compressed.name + REFIT_FULL_SUFFIX
model_compressed.set_contexts(self.path_root + model_compressed.name + os.path.sep)
return model_compressed
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def _fit(
self,
X,
y,
k_fold=5,
k_fold_start=0,
k_fold_end=None,
n_repeats=1,
n_repeat_start=0,
compute_base_preds=True,
time_limit=None,
**kwargs,
):
start_time = time.time()
X = self.preprocess(
X=X, preprocess=False, fit=True, compute_base_preds=compute_base_preds
)
if time_limit is not None:
time_limit = time_limit - (time.time() - start_time)
if len(self.models) == 0:
type_map_raw = {column: R_FLOAT for column in self.stack_columns}
type_group_map_special = {S_STACK: self.stack_columns}
stacker_feature_metadata = FeatureMetadata(
type_map_raw=type_map_raw, type_group_map_special=type_group_map_special
)
if (
self.feature_metadata is None
): # TODO: This is probably not the best way to do this
self.feature_metadata = stacker_feature_metadata
else:
self.feature_metadata = self.feature_metadata.join_metadata(
stacker_feature_metadata
)
super()._fit(
X=X,
y=y,
k_fold=k_fold,
k_fold_start=k_fold_start,
k_fold_end=k_fold_end,
n_repeats=n_repeats,
n_repeat_start=n_repeat_start,
time_limit=time_limit,
**kwargs,
)
|
def _fit(
self,
X,
y,
k_fold=5,
k_fold_start=0,
k_fold_end=None,
n_repeats=1,
n_repeat_start=0,
compute_base_preds=True,
time_limit=None,
**kwargs,
):
start_time = time.time()
X = self.preprocess(
X=X, preprocess=False, fit=True, compute_base_preds=compute_base_preds
)
if time_limit is not None:
time_limit = time_limit - (time.time() - start_time)
if len(self.models) == 0:
if (
self.feature_types_metadata is None
): # TODO: This is probably not the best way to do this
feature_types_raw = defaultdict(list)
feature_types_raw["float"] = self.stack_columns
feature_types_special = defaultdict(list)
feature_types_special["stack"] = self.stack_columns
self.feature_types_metadata = FeatureTypesMetadata(
feature_types_raw=feature_types_raw,
feature_types_special=feature_types_special,
)
else:
self.feature_types_metadata = copy.deepcopy(self.feature_types_metadata)
self.feature_types_metadata.feature_types_raw["float"] += self.stack_columns
self.feature_types_metadata.feature_types_special["stack"] += (
self.stack_columns
)
super()._fit(
X=X,
y=y,
k_fold=k_fold,
k_fold_start=k_fold_start,
k_fold_end=k_fold_end,
n_repeats=n_repeats,
n_repeat_start=n_repeat_start,
time_limit=time_limit,
**kwargs,
)
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def hyperparameter_tune(
self, X, y, k_fold, scheduler_options=None, compute_base_preds=True, **kwargs
):
if len(self.models) != 0:
raise ValueError(
"self.models must be empty to call hyperparameter_tune, value: %s"
% self.models
)
if len(self.models) == 0:
type_map_raw = {column: R_FLOAT for column in self.stack_columns}
type_group_map_special = {S_STACK: self.stack_columns}
stacker_feature_metadata = FeatureMetadata(
type_map_raw=type_map_raw, type_group_map_special=type_group_map_special
)
if (
self.feature_metadata is None
): # TODO: This is probably not the best way to do this
self.feature_metadata = stacker_feature_metadata
else:
self.feature_metadata = self.feature_metadata.join_metadata(
stacker_feature_metadata
)
self.model_base.feature_metadata = self.feature_metadata # TODO: Move this
# TODO: Preprocess data here instead of repeatedly
X = self.preprocess(
X=X, preprocess=False, fit=True, compute_base_preds=compute_base_preds
)
kfolds = generate_kfold(
X=X,
y=y,
n_splits=k_fold,
stratified=self.is_stratified(),
random_state=self._random_state,
n_repeats=1,
)
train_index, test_index = kfolds[0]
X_train, X_val = X.iloc[train_index, :], X.iloc[test_index, :]
y_train, y_val = y.iloc[train_index], y.iloc[test_index]
orig_time = scheduler_options[1]["time_out"]
scheduler_options[1]["time_out"] = (
orig_time * 0.8
) # TODO: Scheduler doesn't early stop on final model, this is a safety net. Scheduler should be updated to early stop
hpo_models, hpo_model_performances, hpo_results = (
self.model_base.hyperparameter_tune(
X_train=X_train,
y_train=y_train,
X_val=X_val,
y_val=y_val,
scheduler_options=scheduler_options,
**kwargs,
)
)
scheduler_options[1]["time_out"] = orig_time
stackers = {}
stackers_performance = {}
for i, (model_name, model_path) in enumerate(hpo_models.items()):
child: AbstractModel = self._child_type.load(path=model_path)
y_pred_proba = child.predict_proba(X_val)
# TODO: Create new StackerEnsemble Here
stacker = copy.deepcopy(self)
stacker.name = stacker.name + os.path.sep + str(i)
stacker.set_contexts(self.path_root + stacker.name + os.path.sep)
if self.problem_type == MULTICLASS:
oof_pred_proba = np.zeros(shape=(len(X), len(y.unique())))
else:
oof_pred_proba = np.zeros(shape=len(X))
oof_pred_model_repeats = np.zeros(shape=len(X))
oof_pred_proba[test_index] += y_pred_proba
oof_pred_model_repeats[test_index] += 1
stacker.model_base = None
child.set_contexts(stacker.path + child.name + os.path.sep)
stacker.save_model_base(child.convert_to_template())
stacker._k = k_fold
stacker._k_fold_end = 1
stacker._n_repeats = 1
stacker._oof_pred_proba = oof_pred_proba
stacker._oof_pred_model_repeats = oof_pred_model_repeats
child.name = child.name + "_fold_0"
child.set_contexts(stacker.path + child.name + os.path.sep)
if not self.save_bagged_folds:
child.model = None
if stacker.low_memory:
stacker.save_child(child, verbose=False)
stacker.models.append(child.name)
else:
stacker.models.append(child)
stacker.val_score = child.val_score
stacker._add_child_times_to_bag(model=child)
stacker.save()
stackers[stacker.name] = stacker.path
stackers_performance[stacker.name] = stacker.val_score
# TODO: hpo_results likely not correct because no renames
return stackers, stackers_performance, hpo_results
|
def hyperparameter_tune(
self, X, y, k_fold, scheduler_options=None, compute_base_preds=True, **kwargs
):
if len(self.models) != 0:
raise ValueError(
"self.models must be empty to call hyperparameter_tune, value: %s"
% self.models
)
if len(self.models) == 0:
if (
self.feature_types_metadata is None
): # TODO: This is probably not the best way to do this
feature_types_raw = defaultdict(list)
feature_types_raw["float"] = self.stack_columns
feature_types_special = defaultdict(list)
feature_types_special["stack"] = self.stack_columns
self.feature_types_metadata = FeatureTypesMetadata(
feature_types_raw=feature_types_raw,
feature_types_special=feature_types_special,
)
else:
self.feature_types_metadata = copy.deepcopy(self.feature_types_metadata)
self.feature_types_metadata.feature_types_raw["float"] += self.stack_columns
self.feature_types_metadata.feature_types_special["stack"] += (
self.stack_columns
)
self.model_base.feature_types_metadata = (
self.feature_types_metadata
) # TODO: Move this
# TODO: Preprocess data here instead of repeatedly
X = self.preprocess(
X=X, preprocess=False, fit=True, compute_base_preds=compute_base_preds
)
kfolds = generate_kfold(
X=X,
y=y,
n_splits=k_fold,
stratified=self.is_stratified(),
random_state=self._random_state,
n_repeats=1,
)
train_index, test_index = kfolds[0]
X_train, X_val = X.iloc[train_index, :], X.iloc[test_index, :]
y_train, y_val = y.iloc[train_index], y.iloc[test_index]
orig_time = scheduler_options[1]["time_out"]
scheduler_options[1]["time_out"] = (
orig_time * 0.8
) # TODO: Scheduler doesn't early stop on final model, this is a safety net. Scheduler should be updated to early stop
hpo_models, hpo_model_performances, hpo_results = (
self.model_base.hyperparameter_tune(
X_train=X_train,
y_train=y_train,
X_val=X_val,
y_val=y_val,
scheduler_options=scheduler_options,
**kwargs,
)
)
scheduler_options[1]["time_out"] = orig_time
stackers = {}
stackers_performance = {}
for i, (model_name, model_path) in enumerate(hpo_models.items()):
child: AbstractModel = self._child_type.load(path=model_path)
y_pred_proba = child.predict_proba(X_val)
# TODO: Create new StackerEnsemble Here
stacker = copy.deepcopy(self)
stacker.name = stacker.name + os.path.sep + str(i)
stacker.set_contexts(self.path_root + stacker.name + os.path.sep)
if self.problem_type == MULTICLASS:
oof_pred_proba = np.zeros(shape=(len(X), len(y.unique())))
else:
oof_pred_proba = np.zeros(shape=len(X))
oof_pred_model_repeats = np.zeros(shape=len(X))
oof_pred_proba[test_index] += y_pred_proba
oof_pred_model_repeats[test_index] += 1
stacker.model_base = None
child.set_contexts(stacker.path + child.name + os.path.sep)
stacker.save_model_base(child.convert_to_template())
stacker._k = k_fold
stacker._k_fold_end = 1
stacker._n_repeats = 1
stacker._oof_pred_proba = oof_pred_proba
stacker._oof_pred_model_repeats = oof_pred_model_repeats
child.name = child.name + "_fold_0"
child.set_contexts(stacker.path + child.name + os.path.sep)
if not self.save_bagged_folds:
child.model = None
if stacker.low_memory:
stacker.save_child(child, verbose=False)
stacker.models.append(child.name)
else:
stacker.models.append(child)
stacker.val_score = child.val_score
stacker._add_child_times_to_bag(model=child)
stacker.save()
stackers[stacker.name] = stacker.path
stackers_performance[stacker.name] = stacker.val_score
# TODO: hpo_results likely not correct because no renames
return stackers, stackers_performance, hpo_results
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def _set_default_auxiliary_params(self):
default_auxiliary_params = dict(
ignored_type_group_raw=[
R_CATEGORY,
R_OBJECT,
], # TODO: Eventually use category features
ignored_type_group_special=[S_TEXT_NGRAM, S_TEXT_SPECIAL, S_DATETIME_AS_INT],
)
for key, value in default_auxiliary_params.items():
self._set_default_param_value(key, value, params=self.params_aux)
super()._set_default_auxiliary_params()
|
def _set_default_auxiliary_params(self):
default_auxiliary_params = dict(
ignored_feature_types_special=["text_ngram", "text_special"],
ignored_feature_types_raw=[
"category",
"object",
], # TODO: Eventually use category features
)
for key, value in default_auxiliary_params.items():
self._set_default_param_value(key, value, params=self.params_aux)
super()._set_default_auxiliary_params()
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def _get_types_of_features(self, df):
"""Returns dict with keys: : 'continuous', 'skewed', 'onehot', 'embed', 'language', values = ordered list of feature-names falling into each category.
Each value is a list of feature-names corresponding to columns in original dataframe.
TODO: ensure features with zero variance have already been removed before this function is called.
"""
if self.types_of_features is not None:
logger.warning(
"Attempting to _get_types_of_features for LRModel, but previously already did this."
)
feature_types = self.feature_metadata.get_type_group_map_raw()
categorical_featnames = (
feature_types[R_CATEGORY] + feature_types[R_OBJECT] + feature_types["bool"]
)
continuous_featnames = (
feature_types[R_FLOAT] + feature_types[R_INT]
) # + self.__get_feature_type_if_present('datetime')
language_featnames = [] # TODO: Disabled currently, have to pass raw text data features here to function properly
valid_features = categorical_featnames + continuous_featnames + language_featnames
if (
len(categorical_featnames) + len(continuous_featnames) + len(language_featnames)
!= df.shape[1]
):
unknown_features = [
feature for feature in df.columns if feature not in valid_features
]
df = df.drop(columns=unknown_features)
self.features = list(df.columns)
types_of_features = {"continuous": [], "skewed": [], "onehot": [], "language": []}
return self._select_features(
df,
types_of_features,
categorical_featnames,
language_featnames,
continuous_featnames,
)
|
def _get_types_of_features(self, df):
"""Returns dict with keys: : 'continuous', 'skewed', 'onehot', 'embed', 'language', values = ordered list of feature-names falling into each category.
Each value is a list of feature-names corresponding to columns in original dataframe.
TODO: ensure features with zero variance have already been removed before this function is called.
"""
if self.types_of_features is not None:
logger.warning(
"Attempting to _get_types_of_features for LRModel, but previously already did this."
)
feature_types = self.feature_types_metadata.feature_types_raw
categorical_featnames = (
feature_types["category"] + feature_types["object"] + feature_types["bool"]
)
continuous_featnames = (
feature_types["float"] + feature_types["int"]
) # + self.__get_feature_type_if_present('datetime')
language_featnames = [] # TODO: Disabled currently, have to pass raw text data features here to function properly
valid_features = categorical_featnames + continuous_featnames + language_featnames
if (
len(categorical_featnames) + len(continuous_featnames) + len(language_featnames)
!= df.shape[1]
):
unknown_features = [
feature for feature in df.columns if feature not in valid_features
]
df = df.drop(columns=unknown_features)
self.features = list(df.columns)
types_of_features = {"continuous": [], "skewed": [], "onehot": [], "language": []}
return self._select_features(
df,
types_of_features,
categorical_featnames,
language_featnames,
continuous_featnames,
)
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._model_type = self._get_model_type()
self._feature_generator = None
|
def __init__(self, **kwargs):
super().__init__(**kwargs)
self._model_type = self._get_model_type()
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def preprocess(self, X):
X = super().preprocess(X)
if self._feature_generator is None:
self._feature_generator = LabelEncoderFeatureGenerator(verbosity=0)
self._feature_generator.fit(X=X)
if self._feature_generator.features_in:
X = X.copy()
X[self._feature_generator.features_in] = self._feature_generator.transform(X=X)
X = X.fillna(0).to_numpy(dtype=np.float32)
return X
|
def preprocess(self, X):
X = super().preprocess(X).fillna(0)
return X
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def _set_default_auxiliary_params(self):
default_auxiliary_params = dict(
ignored_type_group_special=["text_ngram", "text_as_category"],
)
for key, value in default_auxiliary_params.items():
self._set_default_param_value(key, value, params=self.params_aux)
super()._set_default_auxiliary_params()
|
def _set_default_auxiliary_params(self):
default_auxiliary_params = dict(
ignored_feature_types_special=["text_ngram", "text_as_category"],
)
for key, value in default_auxiliary_params.items():
self._set_default_param_value(key, value, params=self.params_aux)
super()._set_default_auxiliary_params()
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def _fit(
self,
X_train,
y_train,
X_val=None,
y_val=None,
time_limit=None,
reporter=None,
**kwargs,
):
"""X_train (pd.DataFrame): training data features (not necessarily preprocessed yet)
X_val (pd.DataFrame): test data features (should have same column names as Xtrain)
y_train (pd.Series):
y_val (pd.Series): are pandas Series
kwargs: Can specify amount of compute resources to utilize (num_cpus, num_gpus).
"""
start_time = time.time()
params = self.params.copy()
self.verbosity = kwargs.get("verbosity", 2)
params = fixedvals_from_searchspaces(params)
if self.feature_metadata is None:
raise ValueError("Trainer class must set feature_metadata for this model")
# print('features: ', self.features)
if "num_cpus" in kwargs:
self.num_dataloading_workers = max(1, int(kwargs["num_cpus"] / 2.0))
else:
self.num_dataloading_workers = 1
if self.num_dataloading_workers == 1:
self.num_dataloading_workers = (
0 # 0 is always faster and uses less memory than 1
)
self.batch_size = params["batch_size"]
train_dataset, val_dataset = self.generate_datasets(
X_train=X_train, y_train=y_train, params=params, X_val=X_val, y_val=y_val
)
logger.log(
15,
"Training data for neural network has: %d examples, %d features (%d vector, %d embedding, %d language)"
% (
train_dataset.num_examples,
train_dataset.num_features,
len(train_dataset.feature_groups["vector"]),
len(train_dataset.feature_groups["embed"]),
len(train_dataset.feature_groups["language"]),
),
)
# self._save_preprocessor() # TODO: should save these things for hyperparam tunning. Need one HP tuner for network-specific HPs, another for preprocessing HPs.
if "num_gpus" in kwargs and kwargs["num_gpus"] >= 1: # Currently cannot use >1 GPU
self.ctx = mx.gpu() # Currently cannot use more than 1 GPU
else:
self.ctx = mx.cpu()
self.get_net(train_dataset, params=params)
if time_limit:
time_elapsed = time.time() - start_time
time_limit = time_limit - time_elapsed
self.train_net(
train_dataset=train_dataset,
params=params,
val_dataset=val_dataset,
initialize=True,
setup_trainer=True,
time_limit=time_limit,
reporter=reporter,
)
self.params_post_fit = params
"""
# TODO: if we don't want to save intermediate network parameters, need to do something like saving in temp directory to clean up after training:
with make_temp_directory() as temp_dir:
save_callback = SaveModelCallback(self.model, monitor=self.metric, mode=save_callback_mode, name=self.name)
with progress_disabled_ctx(self.model) as model:
original_path = model.path
model.path = Path(temp_dir)
model.fit_one_cycle(self.epochs, self.lr, callbacks=save_callback)
# Load the best one and export it
model.load(self.name)
print(f'Model validation metrics: {model.validate()}')
model.path = original_path\
"""
|
def _fit(
self,
X_train,
y_train,
X_val=None,
y_val=None,
time_limit=None,
reporter=None,
**kwargs,
):
"""X_train (pd.DataFrame): training data features (not necessarily preprocessed yet)
X_val (pd.DataFrame): test data features (should have same column names as Xtrain)
y_train (pd.Series):
y_val (pd.Series): are pandas Series
kwargs: Can specify amount of compute resources to utilize (num_cpus, num_gpus).
"""
start_time = time.time()
params = self.params.copy()
self.verbosity = kwargs.get("verbosity", 2)
params = fixedvals_from_searchspaces(params)
if self.feature_types_metadata is None:
raise ValueError("Trainer class must set feature_types_metadata for this model")
# print('features: ', self.features)
if "num_cpus" in kwargs:
self.num_dataloading_workers = max(1, int(kwargs["num_cpus"] / 2.0))
else:
self.num_dataloading_workers = 1
if self.num_dataloading_workers == 1:
self.num_dataloading_workers = (
0 # 0 is always faster and uses less memory than 1
)
self.batch_size = params["batch_size"]
train_dataset, val_dataset = self.generate_datasets(
X_train=X_train, y_train=y_train, params=params, X_val=X_val, y_val=y_val
)
logger.log(
15,
"Training data for neural network has: %d examples, %d features (%d vector, %d embedding, %d language)"
% (
train_dataset.num_examples,
train_dataset.num_features,
len(train_dataset.feature_groups["vector"]),
len(train_dataset.feature_groups["embed"]),
len(train_dataset.feature_groups["language"]),
),
)
# self._save_preprocessor() # TODO: should save these things for hyperparam tunning. Need one HP tuner for network-specific HPs, another for preprocessing HPs.
if "num_gpus" in kwargs and kwargs["num_gpus"] >= 1: # Currently cannot use >1 GPU
self.ctx = mx.gpu() # Currently cannot use more than 1 GPU
else:
self.ctx = mx.cpu()
self.get_net(train_dataset, params=params)
if time_limit:
time_elapsed = time.time() - start_time
time_limit = time_limit - time_elapsed
self.train_net(
train_dataset=train_dataset,
params=params,
val_dataset=val_dataset,
initialize=True,
setup_trainer=True,
time_limit=time_limit,
reporter=reporter,
)
self.params_post_fit = params
"""
# TODO: if we don't want to save intermediate network parameters, need to do something like saving in temp directory to clean up after training:
with make_temp_directory() as temp_dir:
save_callback = SaveModelCallback(self.model, monitor=self.metric, mode=save_callback_mode, name=self.name)
with progress_disabled_ctx(self.model) as model:
original_path = model.path
model.path = Path(temp_dir)
model.fit_one_cycle(self.epochs, self.lr, callbacks=save_callback)
# Load the best one and export it
model.load(self.name)
print(f'Model validation metrics: {model.validate()}')
model.path = original_path\
"""
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def _get_types_of_features(
self, df, skew_threshold, embed_min_categories, use_ngram_features
):
"""Returns dict with keys: : 'continuous', 'skewed', 'onehot', 'embed', 'language', values = ordered list of feature-names falling into each category.
Each value is a list of feature-names corresponding to columns in original dataframe.
TODO: ensure features with zero variance have already been removed before this function is called.
"""
if self.types_of_features is not None:
Warning(
"Attempting to _get_types_of_features for TabularNeuralNetModel, but previously already did this."
)
feature_types = self.feature_metadata.get_type_group_map_raw()
categorical_featnames = (
feature_types[R_CATEGORY] + feature_types[R_OBJECT] + feature_types["bool"]
)
continuous_featnames = (
feature_types[R_FLOAT] + feature_types[R_INT]
) # + self.__get_feature_type_if_present('datetime')
language_featnames = [] # TODO: not implemented. This should fetch text features present in the data
valid_features = categorical_featnames + continuous_featnames + language_featnames
if (
len(categorical_featnames) + len(continuous_featnames) + len(language_featnames)
!= df.shape[1]
):
unknown_features = [
feature for feature in df.columns if feature not in valid_features
]
# print('unknown features:', unknown_features)
df = df.drop(columns=unknown_features)
self.features = list(df.columns)
# raise ValueError("unknown feature types present in DataFrame")
types_of_features = {
"continuous": [],
"skewed": [],
"onehot": [],
"embed": [],
"language": [],
}
# continuous = numeric features to rescale
# skewed = features to which we will apply power (ie. log / box-cox) transform before normalization
# onehot = features to one-hot encode (unknown categories for these features encountered at test-time are encoded as all zeros). We one-hot encode any features encountered that only have two unique values.
for feature in self.features:
feature_data = df[feature] # pd.Series
num_unique_vals = len(feature_data.unique())
if (
num_unique_vals == 2
): # will be onehot encoded regardless of proc.embed_min_categories value
types_of_features["onehot"].append(feature)
elif feature in continuous_featnames:
if np.abs(feature_data.skew()) > skew_threshold:
types_of_features["skewed"].append(feature)
else:
types_of_features["continuous"].append(feature)
elif feature in categorical_featnames:
if (
num_unique_vals >= embed_min_categories
): # sufficiently many categories to warrant learned embedding dedicated to this feature
types_of_features["embed"].append(feature)
else:
types_of_features["onehot"].append(feature)
elif feature in language_featnames:
types_of_features["language"].append(feature)
return types_of_features
|
def _get_types_of_features(
self, df, skew_threshold, embed_min_categories, use_ngram_features
):
"""Returns dict with keys: : 'continuous', 'skewed', 'onehot', 'embed', 'language', values = ordered list of feature-names falling into each category.
Each value is a list of feature-names corresponding to columns in original dataframe.
TODO: ensure features with zero variance have already been removed before this function is called.
"""
if self.types_of_features is not None:
Warning(
"Attempting to _get_types_of_features for TabularNeuralNetModel, but previously already did this."
)
feature_types = self.feature_types_metadata.feature_types_raw
categorical_featnames = (
feature_types["category"] + feature_types["object"] + feature_types["bool"]
)
continuous_featnames = (
feature_types["float"] + feature_types["int"]
) # + self.__get_feature_type_if_present('datetime')
language_featnames = [] # TODO: not implemented. This should fetch text features present in the data
valid_features = categorical_featnames + continuous_featnames + language_featnames
if (
len(categorical_featnames) + len(continuous_featnames) + len(language_featnames)
!= df.shape[1]
):
unknown_features = [
feature for feature in df.columns if feature not in valid_features
]
# print('unknown features:', unknown_features)
df = df.drop(columns=unknown_features)
self.features = list(df.columns)
# raise ValueError("unknown feature types present in DataFrame")
types_of_features = {
"continuous": [],
"skewed": [],
"onehot": [],
"embed": [],
"language": [],
}
# continuous = numeric features to rescale
# skewed = features to which we will apply power (ie. log / box-cox) transform before normalization
# onehot = features to one-hot encode (unknown categories for these features encountered at test-time are encoded as all zeros). We one-hot encode any features encountered that only have two unique values.
for feature in self.features:
feature_data = df[feature] # pd.Series
num_unique_vals = len(feature_data.unique())
if (
num_unique_vals == 2
): # will be onehot encoded regardless of proc.embed_min_categories value
types_of_features["onehot"].append(feature)
elif feature in continuous_featnames:
if np.abs(feature_data.skew()) > skew_threshold:
types_of_features["skewed"].append(feature)
else:
types_of_features["continuous"].append(feature)
elif feature in categorical_featnames:
if (
num_unique_vals >= embed_min_categories
): # sufficiently many categories to warrant learned embedding dedicated to this feature
types_of_features["embed"].append(feature)
else:
types_of_features["onehot"].append(feature)
elif feature in language_featnames:
types_of_features["language"].append(feature)
return types_of_features
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def hyperparameter_tune(
self, X_train, y_train, X_val, y_val, scheduler_options, **kwargs
):
time_start = time.time()
""" Performs HPO and sets self.params to best hyperparameter values """
self.verbosity = kwargs.get("verbosity", 2)
logger.log(15, "Beginning hyperparameter tuning for Neural Network...")
self._set_default_searchspace() # changes non-specified default hyperparams from fixed values to search-spaces.
if self.feature_metadata is None:
raise ValueError("Trainer class must set feature_metadata for this model")
scheduler_func = scheduler_options[0]
scheduler_options = scheduler_options[1]
if scheduler_func is None or scheduler_options is None:
raise ValueError(
"scheduler_func and scheduler_options cannot be None for hyperparameter tuning"
)
num_cpus = scheduler_options["resource"]["num_cpus"]
# num_gpus = scheduler_options['resource']['num_gpus'] # TODO: Currently unused
params_copy = self.params.copy()
self.num_dataloading_workers = max(1, int(num_cpus / 2.0))
self.batch_size = params_copy["batch_size"]
train_dataset, val_dataset = self.generate_datasets(
X_train=X_train, y_train=y_train, params=params_copy, X_val=X_val, y_val=y_val
)
train_path = self.path + "train"
val_path = self.path + "validation"
train_dataset.save(file_prefix=train_path)
val_dataset.save(file_prefix=val_path)
if not np.any(
[isinstance(params_copy[hyperparam], Space) for hyperparam in params_copy]
):
logger.warning(
"Warning: Attempting to do hyperparameter optimization without any search space (all hyperparameters are already fixed values)"
)
else:
logger.log(15, "Hyperparameter search space for Neural Network: ")
for hyperparam in params_copy:
if isinstance(params_copy[hyperparam], Space):
logger.log(15, str(hyperparam) + ": " + str(params_copy[hyperparam]))
util_args = dict(
train_path=train_path,
val_path=val_path,
model=self,
time_start=time_start,
time_limit=scheduler_options["time_out"],
)
tabular_nn_trial.register_args(util_args=util_args, **params_copy)
scheduler = scheduler_func(tabular_nn_trial, **scheduler_options)
if ("dist_ip_addrs" in scheduler_options) and (
len(scheduler_options["dist_ip_addrs"]) > 0
):
# TODO: Ensure proper working directory setup on remote machines
# This is multi-machine setting, so need to copy dataset to workers:
logger.log(15, "Uploading preprocessed data to remote workers...")
scheduler.upload_files(
[
train_path + TabularNNDataset.DATAOBJ_SUFFIX,
train_path + TabularNNDataset.DATAVALUES_SUFFIX,
val_path + TabularNNDataset.DATAOBJ_SUFFIX,
val_path + TabularNNDataset.DATAVALUES_SUFFIX,
]
) # TODO: currently does not work.
logger.log(15, "uploaded")
scheduler.run()
scheduler.join_jobs()
scheduler.get_training_curves(plot=False, use_legend=False)
return self._get_hpo_results(
scheduler=scheduler, scheduler_options=scheduler_options, time_start=time_start
)
|
def hyperparameter_tune(
self, X_train, y_train, X_val, y_val, scheduler_options, **kwargs
):
time_start = time.time()
""" Performs HPO and sets self.params to best hyperparameter values """
self.verbosity = kwargs.get("verbosity", 2)
logger.log(15, "Beginning hyperparameter tuning for Neural Network...")
self._set_default_searchspace() # changes non-specified default hyperparams from fixed values to search-spaces.
if self.feature_types_metadata is None:
raise ValueError("Trainer class must set feature_types_metadata for this model")
scheduler_func = scheduler_options[0]
scheduler_options = scheduler_options[1]
if scheduler_func is None or scheduler_options is None:
raise ValueError(
"scheduler_func and scheduler_options cannot be None for hyperparameter tuning"
)
num_cpus = scheduler_options["resource"]["num_cpus"]
# num_gpus = scheduler_options['resource']['num_gpus'] # TODO: Currently unused
params_copy = self.params.copy()
self.num_dataloading_workers = max(1, int(num_cpus / 2.0))
self.batch_size = params_copy["batch_size"]
train_dataset, val_dataset = self.generate_datasets(
X_train=X_train, y_train=y_train, params=params_copy, X_val=X_val, y_val=y_val
)
train_path = self.path + "train"
val_path = self.path + "validation"
train_dataset.save(file_prefix=train_path)
val_dataset.save(file_prefix=val_path)
if not np.any(
[isinstance(params_copy[hyperparam], Space) for hyperparam in params_copy]
):
logger.warning(
"Warning: Attempting to do hyperparameter optimization without any search space (all hyperparameters are already fixed values)"
)
else:
logger.log(15, "Hyperparameter search space for Neural Network: ")
for hyperparam in params_copy:
if isinstance(params_copy[hyperparam], Space):
logger.log(15, str(hyperparam) + ": " + str(params_copy[hyperparam]))
util_args = dict(
train_path=train_path,
val_path=val_path,
model=self,
time_start=time_start,
time_limit=scheduler_options["time_out"],
)
tabular_nn_trial.register_args(util_args=util_args, **params_copy)
scheduler = scheduler_func(tabular_nn_trial, **scheduler_options)
if ("dist_ip_addrs" in scheduler_options) and (
len(scheduler_options["dist_ip_addrs"]) > 0
):
# TODO: Ensure proper working directory setup on remote machines
# This is multi-machine setting, so need to copy dataset to workers:
logger.log(15, "Uploading preprocessed data to remote workers...")
scheduler.upload_files(
[
train_path + TabularNNDataset.DATAOBJ_SUFFIX,
train_path + TabularNNDataset.DATAVALUES_SUFFIX,
val_path + TabularNNDataset.DATAOBJ_SUFFIX,
val_path + TabularNNDataset.DATAVALUES_SUFFIX,
]
) # TODO: currently does not work.
logger.log(15, "uploaded")
scheduler.run()
scheduler.join_jobs()
scheduler.get_training_curves(plot=False, use_legend=False)
return self._get_hpo_results(
scheduler=scheduler, scheduler_options=scheduler_options, time_start=time_start
)
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def __init__(
self,
path: str,
problem_type: str,
scheduler_options=None,
eval_metric=None,
stopping_metric=None,
num_classes=None,
low_memory=False,
feature_metadata=None,
kfolds=0,
n_repeats=1,
stack_ensemble_levels=0,
time_limit=None,
save_data=False,
save_bagged_folds=True,
random_seed=0,
verbosity=2,
):
self.path = path
self.problem_type = problem_type
self.feature_metadata = feature_metadata
self.save_data = save_data
self.random_seed = random_seed # Integer value added to the stack level to get the random_seed for kfold splits or the train/val split if bagging is disabled
self.verbosity = verbosity
if eval_metric is not None:
self.eval_metric = eval_metric
else:
self.eval_metric = infer_eval_metric(problem_type=self.problem_type)
# stopping_metric is used to early stop all models except for aux models.
if stopping_metric is not None:
self.stopping_metric = stopping_metric
elif self.eval_metric.name == "roc_auc":
self.stopping_metric = log_loss
else:
self.stopping_metric = self.eval_metric
self.eval_metric_expects_y_pred = scorer_expects_y_pred(scorer=self.eval_metric)
logger.log(
25,
"AutoGluon will gauge predictive performance using evaluation metric: %s"
% self.eval_metric.name,
)
if not self.eval_metric_expects_y_pred:
logger.log(
25,
"This metric expects predicted probabilities rather than predicted class labels, so you'll need to use predict_proba() instead of predict()",
)
logger.log(20, "To change this, specify the eval_metric argument of fit()")
logger.log(
25,
"AutoGluon will early stop models using evaluation metric: %s"
% self.stopping_metric.name,
)
self.num_classes = num_classes
self.feature_prune = False # will be set to True if feature-pruning is turned on.
self.low_memory = low_memory
self.bagged_mode = True if kfolds >= 2 else False
if self.bagged_mode:
self.kfolds = (
kfolds # int number of folds to do model bagging, < 2 means disabled
)
self.stack_ensemble_levels = stack_ensemble_levels
self.stack_mode = True if self.stack_ensemble_levels >= 1 else False
self.n_repeats = n_repeats
else:
self.kfolds = 0
self.stack_ensemble_levels = 0
self.stack_mode = False
self.n_repeats = 1
self.save_bagged_folds = save_bagged_folds
self.hyperparameters = {} # TODO: This is currently required for fetching stacking layer models. Consider incorporating more elegantly
# self.models_level_all['core'][0] # Includes base models
# self.models_level_all['core'][1] # Stacker level 1
# self.models_level_all['aux1'][1] # Stacker level 1 aux models, such as weighted_ensemble
# self.models_level_all['core'][2] # Stacker level 2
self.models_level = defaultdict(dd_list)
self.model_best = None
self.model_performance = {} # TODO: Remove in future, use networkx.
self.model_paths = {}
self.model_types = {} # Outer type, can be BaggedEnsemble, StackEnsemble (Type that is able to load the model)
self.model_types_inner = {} # Inner type, if Ensemble then it is the type of the inner model (May not be able to load with this type)
self.models = {}
self.model_graph = nx.DiGraph()
self.model_full_dict = {} # Dict of normal Model -> FULL Model
self.reset_paths = False
self.hpo_results = {} # Stores summary of HPO process
# Scheduler attributes:
if scheduler_options is not None:
self.scheduler_func = scheduler_options[0] # unpack tuple
self.scheduler_options = scheduler_options[1]
else:
self.scheduler_func = None
self.scheduler_options = None
self.time_limit = time_limit
if self.time_limit is None:
self.time_limit = 1e7
self.ignore_time_limit = True
else:
self.ignore_time_limit = False
self.time_train_start = None
self.time_train_level_start = None
self.time_limit_per_level = self.time_limit / (self.stack_ensemble_levels + 1)
self.num_rows_train = None
self.num_cols_train = None
self.is_data_saved = False
self.regress_preds_asprobas = False # whether to treat regression predictions as class-probabilities (during distillation)
|
def __init__(
self,
path: str,
problem_type: str,
scheduler_options=None,
eval_metric=None,
stopping_metric=None,
num_classes=None,
low_memory=False,
feature_types_metadata=None,
kfolds=0,
n_repeats=1,
stack_ensemble_levels=0,
time_limit=None,
save_data=False,
save_bagged_folds=True,
random_seed=0,
verbosity=2,
):
self.path = path
self.problem_type = problem_type
self.feature_types_metadata = feature_types_metadata
self.save_data = save_data
self.random_seed = random_seed # Integer value added to the stack level to get the random_seed for kfold splits or the train/val split if bagging is disabled
self.verbosity = verbosity
if eval_metric is not None:
self.eval_metric = eval_metric
else:
self.eval_metric = infer_eval_metric(problem_type=self.problem_type)
# stopping_metric is used to early stop all models except for aux models.
if stopping_metric is not None:
self.stopping_metric = stopping_metric
elif self.eval_metric.name == "roc_auc":
self.stopping_metric = log_loss
else:
self.stopping_metric = self.eval_metric
self.eval_metric_expects_y_pred = scorer_expects_y_pred(scorer=self.eval_metric)
logger.log(
25,
"AutoGluon will gauge predictive performance using evaluation metric: %s"
% self.eval_metric.name,
)
if not self.eval_metric_expects_y_pred:
logger.log(
25,
"This metric expects predicted probabilities rather than predicted class labels, so you'll need to use predict_proba() instead of predict()",
)
logger.log(20, "To change this, specify the eval_metric argument of fit()")
logger.log(
25,
"AutoGluon will early stop models using evaluation metric: %s"
% self.stopping_metric.name,
)
self.num_classes = num_classes
self.feature_prune = False # will be set to True if feature-pruning is turned on.
self.low_memory = low_memory
self.bagged_mode = True if kfolds >= 2 else False
if self.bagged_mode:
self.kfolds = (
kfolds # int number of folds to do model bagging, < 2 means disabled
)
self.stack_ensemble_levels = stack_ensemble_levels
self.stack_mode = True if self.stack_ensemble_levels >= 1 else False
self.n_repeats = n_repeats
else:
self.kfolds = 0
self.stack_ensemble_levels = 0
self.stack_mode = False
self.n_repeats = 1
self.save_bagged_folds = save_bagged_folds
self.hyperparameters = {} # TODO: This is currently required for fetching stacking layer models. Consider incorporating more elegantly
# self.models_level_all['core'][0] # Includes base models
# self.models_level_all['core'][1] # Stacker level 1
# self.models_level_all['aux1'][1] # Stacker level 1 aux models, such as weighted_ensemble
# self.models_level_all['core'][2] # Stacker level 2
self.models_level = defaultdict(dd_list)
self.model_best = None
self.model_performance = {} # TODO: Remove in future, use networkx.
self.model_paths = {}
self.model_types = {} # Outer type, can be BaggedEnsemble, StackEnsemble (Type that is able to load the model)
self.model_types_inner = {} # Inner type, if Ensemble then it is the type of the inner model (May not be able to load with this type)
self.models = {}
self.model_graph = nx.DiGraph()
self.model_full_dict = {} # Dict of normal Model -> FULL Model
self.reset_paths = False
self.hpo_results = {} # Stores summary of HPO process
# Scheduler attributes:
if scheduler_options is not None:
self.scheduler_func = scheduler_options[0] # unpack tuple
self.scheduler_options = scheduler_options[1]
else:
self.scheduler_func = None
self.scheduler_options = None
self.time_limit = time_limit
if self.time_limit is None:
self.time_limit = 1e7
self.ignore_time_limit = True
else:
self.ignore_time_limit = False
self.time_train_start = None
self.time_train_level_start = None
self.time_limit_per_level = self.time_limit / (self.stack_ensemble_levels + 1)
self.num_rows_train = None
self.num_cols_train = None
self.is_data_saved = False
self.regress_preds_asprobas = False # whether to treat regression predictions as class-probabilities (during distillation)
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def train_single(
self,
X_train,
y_train,
X_val,
y_val,
model,
kfolds=None,
k_fold_start=0,
k_fold_end=None,
n_repeats=None,
n_repeat_start=0,
level=0,
time_limit=None,
):
if kfolds is None:
kfolds = self.kfolds
if n_repeats is None:
n_repeats = self.n_repeats
if model.feature_metadata is None:
model.feature_metadata = copy.deepcopy(
self.feature_metadata
) # TODO: move this into model creation process?
model_fit_kwargs = {}
if self.scheduler_options is not None:
model_fit_kwargs = {
"verbosity": self.verbosity,
"num_cpus": self.scheduler_options["resource"]["num_cpus"],
"num_gpus": self.scheduler_options["resource"]["num_gpus"],
} # Additional configurations for model.fit
if self.bagged_mode or isinstance(model, WeightedEnsembleModel):
model.fit(
X=X_train,
y=y_train,
k_fold=kfolds,
k_fold_start=k_fold_start,
k_fold_end=k_fold_end,
n_repeats=n_repeats,
n_repeat_start=n_repeat_start,
compute_base_preds=False,
time_limit=time_limit,
**model_fit_kwargs,
)
else:
model.fit(
X_train=X_train,
y_train=y_train,
X_val=X_val,
y_val=y_val,
time_limit=time_limit,
**model_fit_kwargs,
)
return model
|
def train_single(
self,
X_train,
y_train,
X_val,
y_val,
model,
kfolds=None,
k_fold_start=0,
k_fold_end=None,
n_repeats=None,
n_repeat_start=0,
level=0,
time_limit=None,
):
if kfolds is None:
kfolds = self.kfolds
if n_repeats is None:
n_repeats = self.n_repeats
if model.feature_types_metadata is None:
model.feature_types_metadata = copy.deepcopy(
self.feature_types_metadata
) # TODO: move this into model creation process?
model_fit_kwargs = {}
if self.scheduler_options is not None:
model_fit_kwargs = {
"verbosity": self.verbosity,
"num_cpus": self.scheduler_options["resource"]["num_cpus"],
"num_gpus": self.scheduler_options["resource"]["num_gpus"],
} # Additional configurations for model.fit
if self.bagged_mode or isinstance(model, WeightedEnsembleModel):
model.fit(
X=X_train,
y=y_train,
k_fold=kfolds,
k_fold_start=k_fold_start,
k_fold_end=k_fold_end,
n_repeats=n_repeats,
n_repeat_start=n_repeat_start,
compute_base_preds=False,
time_limit=time_limit,
**model_fit_kwargs,
)
else:
model.fit(
X_train=X_train,
y_train=y_train,
X_val=X_val,
y_val=y_val,
time_limit=time_limit,
**model_fit_kwargs,
)
return model
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def train_single_full(
self,
X_train,
y_train,
X_val,
y_val,
model: AbstractModel,
feature_prune=False,
hyperparameter_tune=True,
stack_name="core",
kfolds=None,
k_fold_start=0,
k_fold_end=None,
n_repeats=None,
n_repeat_start=0,
level=0,
time_limit=None,
):
if (n_repeat_start == 0) and (k_fold_start == 0):
model.feature_metadata = copy.deepcopy(
self.feature_metadata
) # TODO: Don't set feature_metadata here
if feature_prune:
if n_repeat_start != 0:
raise ValueError(
"n_repeat_start must be 0 to feature_prune, value = "
+ str(n_repeat_start)
)
elif k_fold_start != 0:
raise ValueError(
"k_fold_start must be 0 to feature_prune, value = " + str(k_fold_start)
)
self.autotune(
X_train=X_train,
X_holdout=X_val,
y_train=y_train,
y_holdout=y_val,
model_base=model,
) # TODO: Update to use CV instead of holdout
if hyperparameter_tune:
if self.scheduler_func is None or self.scheduler_options is None:
raise ValueError(
"scheduler_options cannot be None when hyperparameter_tune = True"
)
if n_repeat_start != 0:
raise ValueError(
"n_repeat_start must be 0 to hyperparameter_tune, value = "
+ str(n_repeat_start)
)
elif k_fold_start != 0:
raise ValueError(
"k_fold_start must be 0 to hyperparameter_tune, value = "
+ str(k_fold_start)
)
# hpo_models (dict): keys = model_names, values = model_paths
try:
if isinstance(model, BaggedEnsembleModel):
hpo_models, hpo_model_performances, hpo_results = (
model.hyperparameter_tune(
X=X_train,
y=y_train,
k_fold=kfolds,
scheduler_options=(self.scheduler_func, self.scheduler_options),
verbosity=self.verbosity,
)
)
else:
hpo_models, hpo_model_performances, hpo_results = (
model.hyperparameter_tune(
X_train=X_train,
y_train=y_train,
X_val=X_val,
y_val=y_val,
scheduler_options=(self.scheduler_func, self.scheduler_options),
verbosity=self.verbosity,
)
)
except Exception as err:
if self.verbosity >= 1:
traceback.print_tb(err.__traceback__)
logger.exception(
"Warning: Exception caused "
+ model.name
+ " to fail during hyperparameter tuning... Skipping this model."
)
logger.debug(err)
del model
model_names_trained = []
else:
self.hpo_results[model.name] = hpo_results
model_names_trained = []
for model_hpo_name, model_path in hpo_models.items():
model_hpo = self.load_model(
model_hpo_name, path=model_path, model_type=type(model)
)
self.add_model(model=model_hpo, stack_name=stack_name, level=level)
model_names_trained.append(model_hpo.name)
else:
model_names_trained = self.train_and_save(
X_train,
y_train,
X_val,
y_val,
model,
stack_name=stack_name,
kfolds=kfolds,
k_fold_start=k_fold_start,
k_fold_end=k_fold_end,
n_repeats=n_repeats,
n_repeat_start=n_repeat_start,
level=level,
time_limit=time_limit,
)
self.save()
return model_names_trained
|
def train_single_full(
self,
X_train,
y_train,
X_val,
y_val,
model: AbstractModel,
feature_prune=False,
hyperparameter_tune=True,
stack_name="core",
kfolds=None,
k_fold_start=0,
k_fold_end=None,
n_repeats=None,
n_repeat_start=0,
level=0,
time_limit=None,
):
if (n_repeat_start == 0) and (k_fold_start == 0):
model.feature_types_metadata = copy.deepcopy(
self.feature_types_metadata
) # TODO: Don't set feature_types_metadata here
if feature_prune:
if n_repeat_start != 0:
raise ValueError(
"n_repeat_start must be 0 to feature_prune, value = "
+ str(n_repeat_start)
)
elif k_fold_start != 0:
raise ValueError(
"k_fold_start must be 0 to feature_prune, value = " + str(k_fold_start)
)
self.autotune(
X_train=X_train,
X_holdout=X_val,
y_train=y_train,
y_holdout=y_val,
model_base=model,
) # TODO: Update to use CV instead of holdout
if hyperparameter_tune:
if self.scheduler_func is None or self.scheduler_options is None:
raise ValueError(
"scheduler_options cannot be None when hyperparameter_tune = True"
)
if n_repeat_start != 0:
raise ValueError(
"n_repeat_start must be 0 to hyperparameter_tune, value = "
+ str(n_repeat_start)
)
elif k_fold_start != 0:
raise ValueError(
"k_fold_start must be 0 to hyperparameter_tune, value = "
+ str(k_fold_start)
)
# hpo_models (dict): keys = model_names, values = model_paths
try:
if isinstance(model, BaggedEnsembleModel):
hpo_models, hpo_model_performances, hpo_results = (
model.hyperparameter_tune(
X=X_train,
y=y_train,
k_fold=kfolds,
scheduler_options=(self.scheduler_func, self.scheduler_options),
verbosity=self.verbosity,
)
)
else:
hpo_models, hpo_model_performances, hpo_results = (
model.hyperparameter_tune(
X_train=X_train,
y_train=y_train,
X_val=X_val,
y_val=y_val,
scheduler_options=(self.scheduler_func, self.scheduler_options),
verbosity=self.verbosity,
)
)
except Exception as err:
if self.verbosity >= 1:
traceback.print_tb(err.__traceback__)
logger.exception(
"Warning: Exception caused "
+ model.name
+ " to fail during hyperparameter tuning... Skipping this model."
)
logger.debug(err)
del model
model_names_trained = []
else:
self.hpo_results[model.name] = hpo_results
model_names_trained = []
for model_hpo_name, model_path in hpo_models.items():
model_hpo = self.load_model(
model_hpo_name, path=model_path, model_type=type(model)
)
self.add_model(model=model_hpo, stack_name=stack_name, level=level)
model_names_trained.append(model_hpo.name)
else:
model_names_trained = self.train_and_save(
X_train,
y_train,
X_val,
y_val,
model,
stack_name=stack_name,
kfolds=kfolds,
k_fold_start=k_fold_start,
k_fold_end=k_fold_end,
n_repeats=n_repeats,
n_repeat_start=n_repeat_start,
level=level,
time_limit=time_limit,
)
self.save()
return model_names_trained
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
def distill(
self,
X_train=None,
y_train=None,
X_val=None,
y_val=None,
time_limits=None,
hyperparameters=None,
holdout_frac=None,
verbosity=None,
models_name_suffix=None,
teacher_preds="soft",
augmentation_data=None,
augment_method="spunge",
augment_args={"size_factor": 5, "max_size": int(1e5)},
):
"""Various distillation algorithms.
Args:
X_train, y_train: pd.DataFrame and pd.Series of training data.
If None, original training data used during TabularPrediction.fit() will be loaded.
This data is split into train/validation if X_val, y_val are None.
X_val, y_val: pd.DataFrame and pd.Series of validation data.
time_limits, hyperparameters, holdout_frac: defined as in TabularPrediction.fit()
teacher_preds (None or str): If None, we only train with original labels (no data augmentation, overrides augment_method)
If 'hard', labels are hard teacher predictions given by: teacher.predict()
If 'soft', labels are soft teacher predictions given by: teacher.predict_proba()
Note: 'hard' and 'soft' are equivalent for regression problems.
If augment_method specified, teacher predictions are only used to label augmented data (training data keeps original labels).
To apply label-smoothing: teacher_preds='onehot' will use original training data labels converted to one-hots for multiclass (no data augmentation). # TODO: expose smoothing-hyperparameter.
models_name_suffix (str): Suffix to append to each student model's name, new names will look like: 'MODELNAME_dstl_SUFFIX'
augmentation_data: pd.DataFrame of additional data to use as "augmented data" (does not contain labels).
When specified, augment_method, augment_args are ignored, and this is the only augmented data that is used (teacher_preds cannot be None).
augment_method (None or str): specifies which augmentation strategy to utilize. Options: [None, 'spunge','munge']
If None, no augmentation gets applied.
}
augment_args (dict): args passed into the augmentation function corresponding to augment_method.
"""
if verbosity is None:
verbosity = self.verbosity
hyperparameter_tune = False # TODO: add as argument with scheduler options.
if augmentation_data is not None and teacher_preds is None:
raise ValueError("augmentation_data must be None if teacher_preds is None")
logger.log(
20,
f"Distilling with teacher_preds={str(teacher_preds)}, augment_method={str(augment_method)} ...",
)
if X_train is None:
if y_train is not None:
raise ValueError("X cannot be None when y specified.")
X_train = self.load_X_train()
if not self.bagged_mode:
try:
X_val = self.load_X_val()
except FileNotFoundError:
pass
if y_train is None:
y_train = self.load_y_train()
if not self.bagged_mode:
try:
y_val = self.load_y_val()
except FileNotFoundError:
pass
if X_val is None:
if y_val is not None:
raise ValueError("X_val cannot be None when y_val specified.")
if holdout_frac is None:
holdout_frac = default_holdout_frac(len(X_train), hyperparameter_tune)
X_train, X_val, y_train, y_val = generate_train_test_split(
X_train, y_train, problem_type=self.problem_type, test_size=holdout_frac
)
y_val_og = y_val.copy()
og_bagged_mode = self.bagged_mode
og_verbosity = self.verbosity
self.bagged_mode = False # turn off bagging
self.verbosity = verbosity # change verbosity for distillation
if teacher_preds is None or teacher_preds == "onehot":
augment_method = None
logger.log(
20,
"Training students without a teacher model. Set teacher_preds = 'soft' or 'hard' to distill using the best AutoGluon predictor as teacher.",
)
if teacher_preds in ["onehot", "soft"]:
y_train = format_distillation_labels(
y_train, self.problem_type, self.num_classes
)
y_val = format_distillation_labels(y_val, self.problem_type, self.num_classes)
if augment_method is None and augmentation_data is None:
if teacher_preds == "hard":
y_pred = pd.Series(self.predict(X_train))
if (self.problem_type != REGRESSION) and (
len(y_pred.unique()) < len(y_train.unique())
): # add missing labels
logger.log(
15,
"Adding missing labels to distillation dataset by including some real training examples",
)
indices_to_add = []
for clss in y_train.unique():
if clss not in y_pred.unique():
logger.log(
15, f"Fetching a row with label={clss} from training data"
)
clss_index = y_train[y_train == clss].index[0]
indices_to_add.append(clss_index)
X_extra = X_train.loc[indices_to_add].copy()
y_extra = y_train.loc[
indices_to_add
].copy() # these are actually real training examples
X_train = pd.concat([X_train, X_extra])
y_pred = pd.concat([y_pred, y_extra])
y_train = y_pred
elif teacher_preds == "soft":
y_train = self.predict_proba(X_train)
if self.problem_type == MULTICLASS:
y_train = pd.DataFrame(y_train)
else:
y_train = pd.Series(y_train)
else:
X_aug = augment_data(
X_train=X_train,
feature_metadata=self.feature_metadata,
augmentation_data=augmentation_data,
augment_method=augment_method,
augment_args=augment_args,
)
if len(X_aug) > 0:
if teacher_preds == "hard":
y_aug = pd.Series(self.predict(X_aug))
elif teacher_preds == "soft":
y_aug = self.predict_proba(X_aug)
if self.problem_type == MULTICLASS:
y_aug = pd.DataFrame(y_aug)
else:
y_aug = pd.Series(y_aug)
else:
raise ValueError(f"Unknown teacher_preds specified: {teacher_preds}")
X_train = pd.concat([X_train, X_aug])
y_train = pd.concat([y_train, y_aug])
X_train.reset_index(drop=True, inplace=True)
y_train.reset_index(drop=True, inplace=True)
student_suffix = "_DSTL" # all student model names contain this substring
if models_name_suffix is not None:
student_suffix = student_suffix + "_" + models_name_suffix
if hyperparameters is None:
hyperparameters = copy.deepcopy(self.hyperparameters)
student_model_types = [
"GBM",
"CAT",
"NN",
"RF",
] # only model types considered for distillation
default_level_key = "default"
if default_level_key in hyperparameters:
hyperparameters[default_level_key] = {
key: hyperparameters[default_level_key][key]
for key in hyperparameters[default_level_key]
if key in student_model_types
}
else:
hyperparameters = {
key: hyperparameters[key]
for key in hyperparameters
if key in student_model_types
}
if len(hyperparameters) == 0:
raise ValueError(
"Distillation not yet supported for fit() with per-stack level hyperparameters. "
"Please either manually specify `hyperparameters` in `distill()` or call `fit()` again without per-level hyperparameters before distillation."
"Also at least one of the following model-types must be present in hyperparameters: ['GBM','CAT','NN','RF']"
)
else:
hyperparameters = self._process_hyperparameters(
hyperparameters=hyperparameters, ag_args_fit=None, excluded_model_types=None
) # TODO: consider exposing ag_args_fit, excluded_model_types as distill() arguments.
if teacher_preds is None or teacher_preds == "hard":
models_distill = get_preset_models(
path=self.path,
problem_type=self.problem_type,
eval_metric=self.eval_metric,
stopping_metric=self.stopping_metric,
num_classes=self.num_classes,
hyperparameters=hyperparameters,
name_suffix=student_suffix,
)
else:
models_distill = get_preset_models_distillation(
path=self.path,
problem_type=self.problem_type,
eval_metric=self.eval_metric,
stopping_metric=self.stopping_metric,
num_classes=self.num_classes,
hyperparameters=hyperparameters,
name_suffix=student_suffix,
)
if self.problem_type != REGRESSION:
self.regress_preds_asprobas = True
self.time_train_start = time.time()
self.time_limit = time_limits
distilled_model_names = []
for model in models_distill:
time_left = None
if time_limits is not None:
time_start_model = time.time()
time_left = time_limits - (time_start_model - self.time_train_start)
logger.log(
15,
f"Distilling student {str(model.name)} with teacher_preds={str(teacher_preds)}, augment_method={str(augment_method)}...",
)
models = self.train_single_full(
X_train=X_train,
y_train=y_train,
X_val=X_val,
y_val=y_val,
model=model,
hyperparameter_tune=False,
stack_name=self.distill_stackname,
time_limit=time_left,
)
for model_name in models: # finally measure original metric on validation data and overwrite stored val_scores
model_score = self.score(X_val, y_val_og, model=model_name)
self.model_performance[model_name] = model_score
model_obj = self.load_model(model_name)
model_obj.val_score = model_score
model_obj.save() # TODO: consider omitting for sake of efficiency
self.model_graph.nodes[model_name]["val_score"] = model_score
distilled_model_names.append(model_name)
logger.log(
20,
"\t"
+ str(round(model_obj.val_score, 4))
+ "\t = Validation "
+ self.eval_metric.name
+ " score",
)
# reset trainer to old state before distill() was called:
self.bagged_mode = og_bagged_mode # TODO: Confirm if safe to train future models after training models in both bagged and non-bagged modes
self.verbosity = og_verbosity
return distilled_model_names
|
def distill(
self,
X_train=None,
y_train=None,
X_val=None,
y_val=None,
time_limits=None,
hyperparameters=None,
holdout_frac=None,
verbosity=None,
models_name_suffix=None,
teacher_preds="soft",
augmentation_data=None,
augment_method="spunge",
augment_args={"size_factor": 5, "max_size": int(1e5)},
):
"""Various distillation algorithms.
Args:
X_train, y_train: pd.DataFrame and pd.Series of training data.
If None, original training data used during TabularPrediction.fit() will be loaded.
This data is split into train/validation if X_val, y_val are None.
X_val, y_val: pd.DataFrame and pd.Series of validation data.
time_limits, hyperparameters, holdout_frac: defined as in TabularPrediction.fit()
teacher_preds (None or str): If None, we only train with original labels (no data augmentation, overrides augment_method)
If 'hard', labels are hard teacher predictions given by: teacher.predict()
If 'soft', labels are soft teacher predictions given by: teacher.predict_proba()
Note: 'hard' and 'soft' are equivalent for regression problems.
If augment_method specified, teacher predictions are only used to label augmented data (training data keeps original labels).
To apply label-smoothing: teacher_preds='onehot' will use original training data labels converted to one-hots for multiclass (no data augmentation). # TODO: expose smoothing-hyperparameter.
models_name_suffix (str): Suffix to append to each student model's name, new names will look like: 'MODELNAME_dstl_SUFFIX'
augmentation_data: pd.DataFrame of additional data to use as "augmented data" (does not contain labels).
When specified, augment_method, augment_args are ignored, and this is the only augmented data that is used (teacher_preds cannot be None).
augment_method (None or str): specifies which augmentation strategy to utilize. Options: [None, 'spunge','munge']
If None, no augmentation gets applied.
}
augment_args (dict): args passed into the augmentation function corresponding to augment_method.
"""
if verbosity is None:
verbosity = self.verbosity
hyperparameter_tune = False # TODO: add as argument with scheduler options.
if augmentation_data is not None and teacher_preds is None:
raise ValueError("augmentation_data must be None if teacher_preds is None")
logger.log(
20,
f"Distilling with teacher_preds={str(teacher_preds)}, augment_method={str(augment_method)} ...",
)
if X_train is None:
if y_train is not None:
raise ValueError("X cannot be None when y specified.")
X_train = self.load_X_train()
if not self.bagged_mode:
try:
X_val = self.load_X_val()
except FileNotFoundError:
pass
if y_train is None:
y_train = self.load_y_train()
if not self.bagged_mode:
try:
y_val = self.load_y_val()
except FileNotFoundError:
pass
if X_val is None:
if y_val is not None:
raise ValueError("X_val cannot be None when y_val specified.")
if holdout_frac is None:
holdout_frac = default_holdout_frac(len(X_train), hyperparameter_tune)
X_train, X_val, y_train, y_val = generate_train_test_split(
X_train, y_train, problem_type=self.problem_type, test_size=holdout_frac
)
y_val_og = y_val.copy()
og_bagged_mode = self.bagged_mode
og_verbosity = self.verbosity
self.bagged_mode = False # turn off bagging
self.verbosity = verbosity # change verbosity for distillation
if teacher_preds is None or teacher_preds == "onehot":
augment_method = None
logger.log(
20,
"Training students without a teacher model. Set teacher_preds = 'soft' or 'hard' to distill using the best AutoGluon predictor as teacher.",
)
if teacher_preds in ["onehot", "soft"]:
y_train = format_distillation_labels(
y_train, self.problem_type, self.num_classes
)
y_val = format_distillation_labels(y_val, self.problem_type, self.num_classes)
if augment_method is None and augmentation_data is None:
if teacher_preds == "hard":
y_pred = pd.Series(self.predict(X_train))
if (self.problem_type != REGRESSION) and (
len(y_pred.unique()) < len(y_train.unique())
): # add missing labels
logger.log(
15,
"Adding missing labels to distillation dataset by including some real training examples",
)
indices_to_add = []
for clss in y_train.unique():
if clss not in y_pred.unique():
logger.log(
15, f"Fetching a row with label={clss} from training data"
)
clss_index = y_train[y_train == clss].index[0]
indices_to_add.append(clss_index)
X_extra = X_train.loc[indices_to_add].copy()
y_extra = y_train.loc[
indices_to_add
].copy() # these are actually real training examples
X_train = pd.concat([X_train, X_extra])
y_pred = pd.concat([y_pred, y_extra])
y_train = y_pred
elif teacher_preds == "soft":
y_train = self.predict_proba(X_train)
if self.problem_type == MULTICLASS:
y_train = pd.DataFrame(y_train)
else:
y_train = pd.Series(y_train)
else:
X_aug = augment_data(
X_train=X_train,
feature_types_metadata=self.feature_types_metadata,
augmentation_data=augmentation_data,
augment_method=augment_method,
augment_args=augment_args,
)
if len(X_aug) > 0:
if teacher_preds == "hard":
y_aug = pd.Series(self.predict(X_aug))
elif teacher_preds == "soft":
y_aug = self.predict_proba(X_aug)
if self.problem_type == MULTICLASS:
y_aug = pd.DataFrame(y_aug)
else:
y_aug = pd.Series(y_aug)
else:
raise ValueError(f"Unknown teacher_preds specified: {teacher_preds}")
X_train = pd.concat([X_train, X_aug])
y_train = pd.concat([y_train, y_aug])
X_train.reset_index(drop=True, inplace=True)
y_train.reset_index(drop=True, inplace=True)
student_suffix = "_DSTL" # all student model names contain this substring
if models_name_suffix is not None:
student_suffix = student_suffix + "_" + models_name_suffix
if hyperparameters is None:
hyperparameters = copy.deepcopy(self.hyperparameters)
student_model_types = [
"GBM",
"CAT",
"NN",
"RF",
] # only model types considered for distillation
default_level_key = "default"
if default_level_key in hyperparameters:
hyperparameters[default_level_key] = {
key: hyperparameters[default_level_key][key]
for key in hyperparameters[default_level_key]
if key in student_model_types
}
else:
hyperparameters = {
key: hyperparameters[key]
for key in hyperparameters
if key in student_model_types
}
if len(hyperparameters) == 0:
raise ValueError(
"Distillation not yet supported for fit() with per-stack level hyperparameters. "
"Please either manually specify `hyperparameters` in `distill()` or call `fit()` again without per-level hyperparameters before distillation."
"Also at least one of the following model-types must be present in hyperparameters: ['GBM','CAT','NN','RF']"
)
else:
hyperparameters = self._process_hyperparameters(
hyperparameters=hyperparameters, ag_args_fit=None, excluded_model_types=None
) # TODO: consider exposing ag_args_fit, excluded_model_types as distill() arguments.
if teacher_preds is None or teacher_preds == "hard":
models_distill = get_preset_models(
path=self.path,
problem_type=self.problem_type,
eval_metric=self.eval_metric,
stopping_metric=self.stopping_metric,
num_classes=self.num_classes,
hyperparameters=hyperparameters,
name_suffix=student_suffix,
)
else:
models_distill = get_preset_models_distillation(
path=self.path,
problem_type=self.problem_type,
eval_metric=self.eval_metric,
stopping_metric=self.stopping_metric,
num_classes=self.num_classes,
hyperparameters=hyperparameters,
name_suffix=student_suffix,
)
if self.problem_type != REGRESSION:
self.regress_preds_asprobas = True
self.time_train_start = time.time()
self.time_limit = time_limits
distilled_model_names = []
for model in models_distill:
time_left = None
if time_limits is not None:
time_start_model = time.time()
time_left = time_limits - (time_start_model - self.time_train_start)
logger.log(
15,
f"Distilling student {str(model.name)} with teacher_preds={str(teacher_preds)}, augment_method={str(augment_method)}...",
)
models = self.train_single_full(
X_train=X_train,
y_train=y_train,
X_val=X_val,
y_val=y_val,
model=model,
hyperparameter_tune=False,
stack_name=self.distill_stackname,
time_limit=time_left,
)
for model_name in models: # finally measure original metric on validation data and overwrite stored val_scores
model_score = self.score(X_val, y_val_og, model=model_name)
self.model_performance[model_name] = model_score
model_obj = self.load_model(model_name)
model_obj.val_score = model_score
model_obj.save() # TODO: consider omitting for sake of efficiency
self.model_graph.nodes[model_name]["val_score"] = model_score
distilled_model_names.append(model_name)
logger.log(
20,
"\t"
+ str(round(model_obj.val_score, 4))
+ "\t = Validation "
+ self.eval_metric.name
+ " score",
)
# reset trainer to old state before distill() was called:
self.bagged_mode = og_bagged_mode # TODO: Confirm if safe to train future models after training models in both bagged and non-bagged modes
self.verbosity = og_verbosity
return distilled_model_names
|
https://github.com/awslabs/autogluon/issues/601
|
File delimiter for ../input/house-prices-advanced-regression-techniques/test.csv inferred as ',' (comma). If this is incorrect, please manually load the data as a pandas DataFrame.
Loaded data from: ../input/house-prices-advanced-regression-techniques/test.csv | Columns = 80 / 80 | Rows = 1459 -> 1459
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-48-91455d7b9940> in <module>
1 test_data = task.Dataset(file_path=os.path.join(in_dir, job, 'test.csv'))
2
----> 3 preds = model.predict(test_data)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/task/tabular_prediction/predictor.py in predict(self, dataset, model, as_pandas, use_pred_cache, add_to_pred_cache)
130 """
131 dataset = self.__get_dataset(dataset)
--> 132 return self._learner.predict(X=dataset, model=model, as_pandas=as_pandas, use_pred_cache=use_pred_cache, add_to_pred_cache=add_to_pred_cache)
133
134 def predict_proba(self, dataset, model=None, as_pandas=False, as_multiclass=False):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict(self, X, model, as_pandas, use_pred_cache, add_to_pred_cache)
127
128 if len(X_cache_miss) > 0:
--> 129 y_pred_proba = self.predict_proba(X=X_cache_miss, model=model, inverse_transform=False)
130 problem_type = self.trainer_problem_type or self.problem_type
131 y_pred = get_pred_from_proba(y_pred_proba=y_pred_proba, problem_type=problem_type)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in predict_proba(self, X, model, as_pandas, as_multiclass, inverse_transform)
92 # TODO: Add pred_proba_cache functionality as in predict()
93 def predict_proba(self, X: DataFrame, model=None, as_pandas=False, as_multiclass=False, inverse_transform=True):
---> 94 X = self.transform_features(X)
95 y_pred_proba = self.load_trainer().predict_proba(X, model=model)
96 if inverse_transform:
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/ml/learner/abstract_learner.py in transform_features(self, X)
216 def transform_features(self, X):
217 for feature_generator in self.feature_generators:
--> 218 X = feature_generator.transform(X)
219 return X
220
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/utils/decorators.py in inner1(*args, **kwargs)
12 begin = time.time()
13
---> 14 output = func(*args, **kwargs)
15
16 # storing time after function execution
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/autogluon/utils/tabular/features/abstract_feature_generator.py in transform(self, X)
183 raise ValueError(f'Required columns are missing from the provided dataset. Missing columns: {missing_cols}')
184
--> 185 X = X.astype(self.features_init_types)
186 X.reset_index(drop=True, inplace=True)
187 X_features = self.generate_features(X)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5863 results.append(
5864 col.astype(
-> 5865 dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
5866 )
5867 )
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
5880 # else, only a single dtype is given
5881 new_data = self._data.astype(
-> 5882 dtype=dtype, copy=copy, errors=errors, **kwargs
5883 )
5884 return self._constructor(new_data).__finalize__(self)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in astype(self, dtype, **kwargs)
579
580 def astype(self, dtype, **kwargs):
--> 581 return self.apply("astype", dtype=dtype, **kwargs)
582
583 def convert(self, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/managers.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
436 kwargs[k] = obj.reindex(b_items, axis=axis, copy=align_copy)
437
--> 438 applied = getattr(b, f)(**kwargs)
439 result_blocks = _extend_blocks(applied, result_blocks)
440
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in astype(self, dtype, copy, errors, values, **kwargs)
557
558 def astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
--> 559 return self._astype(dtype, copy=copy, errors=errors, values=values, **kwargs)
560
561 def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/internals/blocks.py in _astype(self, dtype, copy, errors, values, **kwargs)
641 # _astype_nansafe works fine with 1-d only
642 vals1d = values.ravel()
--> 643 values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
644
645 # TODO(extension)
~/miniconda3/envs/autogluon/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy, skipna)
698 if not np.isfinite(arr).all():
699 raise ValueError(
--> 700 "Cannot convert non-finite values (NA or inf) to " "integer"
701 )
702
ValueError: Cannot convert non-finite values (NA or inf) to integer
|
ValueError
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.