Skip to content

Commit

Permalink
Merge pull request #1142 from sinhrks/flake8
Browse files Browse the repository at this point in the history
Enable flake8 for Python
  • Loading branch information
tqchen committed Apr 27, 2016
2 parents 5d7a696 + 8fc2456 commit 43c073d
Show file tree
Hide file tree
Showing 19 changed files with 291 additions and 208 deletions.
14 changes: 7 additions & 7 deletions python-package/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import os
from setuptools import setup, find_packages
#import subprocess
# import subprocess
sys.path.insert(0, '.')

CURRENT_DIR = os.path.dirname(__file__)
Expand All @@ -18,12 +18,12 @@

LIB_PATH = libpath['find_lib_path']()
print("Install libxgboost from: %s" % LIB_PATH)
#Please use setup_pip.py for generating and deploying pip installation
#detailed instruction in setup_pip.py
# Please use setup_pip.py for generating and deploying pip installation
# detailed instruction in setup_pip.py
setup(name='xgboost',
version=open(os.path.join(CURRENT_DIR, 'xgboost/VERSION')).read().strip(),
#version='0.4a23',
description = "XGBoost Python Package",
# version='0.4a23',
description="XGBoost Python Package",
long_description=open(os.path.join(CURRENT_DIR, 'README.rst')).read(),
install_requires=[
'numpy',
Expand All @@ -33,8 +33,8 @@
maintainer_email='[email protected]',
zip_safe=False,
packages=find_packages(),
#this will use MANIFEST.in during install where we specify additional files,
#this is the golden line
# this will use MANIFEST.in during install where we specify additional files,
# this is the golden line
include_package_data=True,
data_files=[('xgboost', LIB_PATH)],
url='https://github.com/dmlc/xgboost')
38 changes: 19 additions & 19 deletions python-package/setup_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import sys
import os
from setuptools import setup, find_packages
#import subprocess
# import subprocess
sys.path.insert(0, '.')

#this script is for packing and shipping pip installation
#it builds xgboost code on the fly and packs for pip
#please don't use this file for installing from github
# this script is for packing and shipping pip installation
# it builds xgboost code on the fly and packs for pip
# please don't use this file for installing from github

if os.name != 'nt': #if not windows, compile and install
if os.name != 'nt': # if not windows, compile and install
os.system('sh ./xgboost/build-python.sh')
else:
print('Windows users please use github installation.')
Expand All @@ -28,12 +28,12 @@

LIB_PATH = libpath['find_lib_path']()

#to deploy to pip, please use
#make pythonpack
#python setup.py register sdist upload
#and be sure to test it firstly using "python setup.py register sdist upload -r pypitest"
# to deploy to pip, please use
# make pythonpack
# python setup.py register sdist upload
# and be sure to test it firstly using "python setup.py register sdist upload -r pypitest"
setup(name='xgboost',
#version=open(os.path.join(CURRENT_DIR, 'xgboost/VERSION')).read().strip(),
# version=open(os.path.join(CURRENT_DIR, 'xgboost/VERSION')).read().strip(),
version='0.4a30',
description=open(os.path.join(CURRENT_DIR, 'README.rst')).read(),
install_requires=[
Expand All @@ -44,15 +44,15 @@
maintainer_email='[email protected]',
zip_safe=False,
packages=find_packages(),
#don't need this and don't use this, give everything to MANIFEST.in
#package_dir = {'':'xgboost'},
#package_data = {'': ['*.txt','*.md','*.sh'],
# don't need this and don't use this, give everything to MANIFEST.in
# package_dir = {'':'xgboost'},
# package_data = {'': ['*.txt','*.md','*.sh'],
# }
#this will use MANIFEST.in during install where we specify additional files,
#this is the golden line
# this will use MANIFEST.in during install where we specify additional files,
# this is the golden line
include_package_data=True,
#!!! don't use data_files for creating pip installation,
#otherwise install_data process will copy it to
#root directory for some machines, and cause confusions on building
#data_files=[('xgboost', LIB_PATH)],
# !!! don't use data_files for creating pip installation,
# otherwise install_data process will copy it to
# root directory for some machines, and cause confusions on building
# data_files=[('xgboost', LIB_PATH)],
url='https://github.com/dmlc/xgboost')
2 changes: 1 addition & 1 deletion python-package/xgboost/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from .core import DMatrix, Booster
from .training import train, cv
from . import rabit
from . import rabit # noqa
try:
from .sklearn import XGBModel, XGBClassifier, XGBRegressor
from .plotting import plot_importance, plot_tree, to_graphviz
Expand Down
16 changes: 13 additions & 3 deletions python-package/xgboost/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@
if PY3:
# pylint: disable=invalid-name, redefined-builtin
STRING_TYPES = str,
py_str = lambda x: x.decode('utf-8')

def py_str(x):
return x.decode('utf-8')
else:
# pylint: disable=invalid-name
STRING_TYPES = basestring,
py_str = lambda x: x

def py_str(x):
return x

try:
import cPickle as pickle # noqa
except ImportError:
import pickle # noqa


# pandas
try:
Expand All @@ -34,7 +44,7 @@ class DataFrame(object):
try:
from sklearn.base import BaseEstimator
from sklearn.base import RegressorMixin, ClassifierMixin
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import LabelEncoder # noqa
from sklearn.cross_validation import KFold, StratifiedKFold
SKLEARN_INSTALLED = True

Expand Down
22 changes: 17 additions & 5 deletions python-package/xgboost/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from .compat import STRING_TYPES, PY3, DataFrame, py_str


class XGBoostError(Exception):
"""Error throwed by xgboost trainer."""
pass
Expand Down Expand Up @@ -82,6 +83,7 @@ def _load_lib():
# load the XGBoost library globally
_LIB = _load_lib()


def _check_call(ret):
"""Check the return value of C API call
Expand Down Expand Up @@ -129,7 +131,6 @@ def c_array(ctype, values):
return (ctype * len(values))(*values)



PANDAS_DTYPE_MAPPER = {'int8': 'int', 'int16': 'int', 'int32': 'int', 'int64': 'int',
'uint8': 'int', 'uint16': 'int', 'uint32': 'int', 'uint64': 'int',
'float16': 'float', 'float32': 'float', 'float64': 'float',
Expand All @@ -144,8 +145,12 @@ def _maybe_pandas_data(data, feature_names, feature_types):

data_dtypes = data.dtypes
if not all(dtype.name in PANDAS_DTYPE_MAPPER for dtype in data_dtypes):
bad_fields = [data.columns[i] for i, dtype in enumerate(data_dtypes) if dtype.name not in PANDAS_DTYPE_MAPPER ]
raise ValueError('DataFrame.dtypes for data must be int, float or bool.\nDid not expect the data types in fie lds '+', '.join(bad_fields))
bad_fields = [data.columns[i] for i, dtype in
enumerate(data_dtypes) if dtype.name not in PANDAS_DTYPE_MAPPER]

msg = """DataFrame.dtypes for data must be int, float or bool.
Did not expect the data types in fields """
raise ValueError(msg + ', '.join(bad_fields))

if feature_names is None:
feature_names = data.columns.format()
Expand Down Expand Up @@ -174,6 +179,7 @@ def _maybe_pandas_label(label):

return label


class DMatrix(object):
"""Data Matrix used in XGBoost.
Expand Down Expand Up @@ -1041,8 +1047,14 @@ def _validate_features(self, data):
if self.feature_names != data.feature_names:
dat_missing = set(self.feature_names) - set(data.feature_names)
my_missing = set(data.feature_names) - set(self.feature_names)

msg = 'feature_names mismatch: {0} {1}'
if dat_missing: msg +='\nexpected ' + ', '.join(str(s) for s in dat_missing) +' in input data'
if my_missing: msg +='\ntraining data did not have the following fields: ' + ', '.join(str(s) for s in my_missing)

if dat_missing:
msg += '\nexpected ' + ', '.join(str(s) for s in dat_missing) + ' in input data'

if my_missing:
msg += '\ntraining data did not have the following fields: ' + ', '.join(str(s) for s in my_missing)

raise ValueError(msg.format(self.feature_names,
data.feature_names))
3 changes: 2 additions & 1 deletion python-package/xgboost/libpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def find_lib_path():
else:
dll_path = [os.path.join(p, 'libxgboost.so') for p in dll_path]
lib_path = [p for p in dll_path if os.path.exists(p) and os.path.isfile(p)]
#From github issues, most of installation errors come from machines w/o compilers

# From github issues, most of installation errors come from machines w/o compilers
if len(lib_path) == 0 and not os.environ.get('XGBOOST_BUILD_DOC', False):
raise XGBoostLibraryNotFound(
'Cannot find XGBoost Libarary in the candicate path, ' +
Expand Down
2 changes: 2 additions & 0 deletions python-package/xgboost/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .core import Booster
from .sklearn import XGBModel


def plot_importance(booster, ax=None, height=0.2,
xlim=None, ylim=None, title='Feature importance',
xlabel='F score', ylabel='Features',
Expand Down Expand Up @@ -105,6 +106,7 @@ def plot_importance(booster, ax=None, height=0.2,
_EDGEPAT = re.compile(r'yes=(\d+),no=(\d+),missing=(\d+)')
_EDGEPAT2 = re.compile(r'yes=(\d+),no=(\d+)')


def _parse_node(graph, text):
"""parse dumped node"""
match = _NODEPAT.match(text)
Expand Down
22 changes: 13 additions & 9 deletions python-package/xgboost/rabit.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""Distributed XGBoost Rabit related API."""
from __future__ import absolute_import
import sys
import atexit
import ctypes
import numpy as np

from .core import _LIB, c_str, STRING_TYPES
from .compat import pickle


def _init_rabit():
"""internal libary initializer."""
Expand All @@ -15,6 +16,7 @@ def _init_rabit():
_LIB.RabitIsDistributed.restype = ctypes.c_int
_LIB.RabitVersionNumber.restype = ctypes.c_int


def init(args=None):
"""Initialize the rabit libary with arguments"""
if args is None:
Expand Down Expand Up @@ -73,6 +75,7 @@ def tracker_print(msg):
sys.stdout.write(msg)
sys.stdout.flush()


def get_processor_name():
"""Get the processor name.
Expand Down Expand Up @@ -127,14 +130,14 @@ def broadcast(data, root):

# enumeration of dtypes
DTYPE_ENUM__ = {
np.dtype('int8') : 0,
np.dtype('uint8') : 1,
np.dtype('int32') : 2,
np.dtype('uint32') : 3,
np.dtype('int64') : 4,
np.dtype('uint64') : 5,
np.dtype('float32') : 6,
np.dtype('float64') : 7
np.dtype('int8'): 0,
np.dtype('uint8'): 1,
np.dtype('int32'): 2,
np.dtype('uint32'): 3,
np.dtype('int64'): 4,
np.dtype('uint64'): 5,
np.dtype('float32'): 6,
np.dtype('float64'): 7
}


Expand Down Expand Up @@ -175,6 +178,7 @@ def allreduce(data, op, prepare_fun=None):
op, None, None)
else:
func_ptr = ctypes.CFUNCTYPE(None, ctypes.c_void_p)

def pfunc(args):
"""prepare function."""
prepare_fun(data)
Expand Down
1 change: 0 additions & 1 deletion python-package/xgboost/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ def fit(self, X, y, sample_weight=None, eval_set=None, eval_metric=None,
self.classes_ = np.unique(y)
self.n_classes_ = len(self.classes_)


xgb_options = self.get_xgb_params()

if callable(self.objective):
Expand Down
Loading

0 comments on commit 43c073d

Please sign in to comment.