repository_name
stringlengths
5
67
func_path_in_repository
stringlengths
4
234
func_name
stringlengths
0
153
whole_func_string
stringlengths
52
3.87M
language
stringclasses
6 values
func_code_string
stringlengths
52
3.87M
func_code_tokens
sequence
func_documentation_string
stringlengths
1
46.9k
func_documentation_tokens
sequence
split_name
stringclasses
1 value
func_code_url
stringlengths
85
339
apache/spark
python/pyspark/java_gateway.py
launch_gateway
def launch_gateway(conf=None, popen_kwargs=None): """ launch jvm gateway :param conf: spark configuration passed to spark-submit :param popen_kwargs: Dictionary of kwargs to pass to Popen when spawning the py4j JVM. This is a developer feature intended for use in customizing how pyspark interacts with the py4j JVM (e.g., capturing stdout/stderr). :return: """ if "PYSPARK_GATEWAY_PORT" in os.environ: gateway_port = int(os.environ["PYSPARK_GATEWAY_PORT"]) gateway_secret = os.environ["PYSPARK_GATEWAY_SECRET"] # Process already exists proc = None else: SPARK_HOME = _find_spark_home() # Launch the Py4j gateway using Spark's run command so that we pick up the # proper classpath and settings from spark-env.sh on_windows = platform.system() == "Windows" script = "./bin/spark-submit.cmd" if on_windows else "./bin/spark-submit" command = [os.path.join(SPARK_HOME, script)] if conf: for k, v in conf.getAll(): command += ['--conf', '%s=%s' % (k, v)] submit_args = os.environ.get("PYSPARK_SUBMIT_ARGS", "pyspark-shell") if os.environ.get("SPARK_TESTING"): submit_args = ' '.join([ "--conf spark.ui.enabled=false", submit_args ]) command = command + shlex.split(submit_args) # Create a temporary directory where the gateway server should write the connection # information. conn_info_dir = tempfile.mkdtemp() try: fd, conn_info_file = tempfile.mkstemp(dir=conn_info_dir) os.close(fd) os.unlink(conn_info_file) env = dict(os.environ) env["_PYSPARK_DRIVER_CONN_INFO_PATH"] = conn_info_file # Launch the Java gateway. popen_kwargs = {} if popen_kwargs is None else popen_kwargs # We open a pipe to stdin so that the Java gateway can die when the pipe is broken popen_kwargs['stdin'] = PIPE # We always set the necessary environment variables. popen_kwargs['env'] = env if not on_windows: # Don't send ctrl-c / SIGINT to the Java gateway: def preexec_func(): signal.signal(signal.SIGINT, signal.SIG_IGN) popen_kwargs['preexec_fn'] = preexec_func proc = Popen(command, **popen_kwargs) else: # preexec_fn not supported on Windows proc = Popen(command, **popen_kwargs) # Wait for the file to appear, or for the process to exit, whichever happens first. while not proc.poll() and not os.path.isfile(conn_info_file): time.sleep(0.1) if not os.path.isfile(conn_info_file): raise Exception("Java gateway process exited before sending its port number") with open(conn_info_file, "rb") as info: gateway_port = read_int(info) gateway_secret = UTF8Deserializer().loads(info) finally: shutil.rmtree(conn_info_dir) # In Windows, ensure the Java child processes do not linger after Python has exited. # In UNIX-based systems, the child process can kill itself on broken pipe (i.e. when # the parent process' stdin sends an EOF). In Windows, however, this is not possible # because java.lang.Process reads directly from the parent process' stdin, contending # with any opportunity to read an EOF from the parent. Note that this is only best # effort and will not take effect if the python process is violently terminated. if on_windows: # In Windows, the child process here is "spark-submit.cmd", not the JVM itself # (because the UNIX "exec" command is not available). This means we cannot simply # call proc.kill(), which kills only the "spark-submit.cmd" process but not the # JVMs. Instead, we use "taskkill" with the tree-kill option "/t" to terminate all # child processes in the tree (http://technet.microsoft.com/en-us/library/bb491009.aspx) def killChild(): Popen(["cmd", "/c", "taskkill", "/f", "/t", "/pid", str(proc.pid)]) atexit.register(killChild) # Connect to the gateway gateway = JavaGateway( gateway_parameters=GatewayParameters(port=gateway_port, auth_token=gateway_secret, auto_convert=True)) # Store a reference to the Popen object for use by the caller (e.g., in reading stdout/stderr) gateway.proc = proc # Import the classes used by PySpark java_import(gateway.jvm, "org.apache.spark.SparkConf") java_import(gateway.jvm, "org.apache.spark.api.java.*") java_import(gateway.jvm, "org.apache.spark.api.python.*") java_import(gateway.jvm, "org.apache.spark.ml.python.*") java_import(gateway.jvm, "org.apache.spark.mllib.api.python.*") # TODO(davies): move into sql java_import(gateway.jvm, "org.apache.spark.sql.*") java_import(gateway.jvm, "org.apache.spark.sql.api.python.*") java_import(gateway.jvm, "org.apache.spark.sql.hive.*") java_import(gateway.jvm, "scala.Tuple2") return gateway
python
def launch_gateway(conf=None, popen_kwargs=None): """ launch jvm gateway :param conf: spark configuration passed to spark-submit :param popen_kwargs: Dictionary of kwargs to pass to Popen when spawning the py4j JVM. This is a developer feature intended for use in customizing how pyspark interacts with the py4j JVM (e.g., capturing stdout/stderr). :return: """ if "PYSPARK_GATEWAY_PORT" in os.environ: gateway_port = int(os.environ["PYSPARK_GATEWAY_PORT"]) gateway_secret = os.environ["PYSPARK_GATEWAY_SECRET"] # Process already exists proc = None else: SPARK_HOME = _find_spark_home() # Launch the Py4j gateway using Spark's run command so that we pick up the # proper classpath and settings from spark-env.sh on_windows = platform.system() == "Windows" script = "./bin/spark-submit.cmd" if on_windows else "./bin/spark-submit" command = [os.path.join(SPARK_HOME, script)] if conf: for k, v in conf.getAll(): command += ['--conf', '%s=%s' % (k, v)] submit_args = os.environ.get("PYSPARK_SUBMIT_ARGS", "pyspark-shell") if os.environ.get("SPARK_TESTING"): submit_args = ' '.join([ "--conf spark.ui.enabled=false", submit_args ]) command = command + shlex.split(submit_args) # Create a temporary directory where the gateway server should write the connection # information. conn_info_dir = tempfile.mkdtemp() try: fd, conn_info_file = tempfile.mkstemp(dir=conn_info_dir) os.close(fd) os.unlink(conn_info_file) env = dict(os.environ) env["_PYSPARK_DRIVER_CONN_INFO_PATH"] = conn_info_file # Launch the Java gateway. popen_kwargs = {} if popen_kwargs is None else popen_kwargs # We open a pipe to stdin so that the Java gateway can die when the pipe is broken popen_kwargs['stdin'] = PIPE # We always set the necessary environment variables. popen_kwargs['env'] = env if not on_windows: # Don't send ctrl-c / SIGINT to the Java gateway: def preexec_func(): signal.signal(signal.SIGINT, signal.SIG_IGN) popen_kwargs['preexec_fn'] = preexec_func proc = Popen(command, **popen_kwargs) else: # preexec_fn not supported on Windows proc = Popen(command, **popen_kwargs) # Wait for the file to appear, or for the process to exit, whichever happens first. while not proc.poll() and not os.path.isfile(conn_info_file): time.sleep(0.1) if not os.path.isfile(conn_info_file): raise Exception("Java gateway process exited before sending its port number") with open(conn_info_file, "rb") as info: gateway_port = read_int(info) gateway_secret = UTF8Deserializer().loads(info) finally: shutil.rmtree(conn_info_dir) # In Windows, ensure the Java child processes do not linger after Python has exited. # In UNIX-based systems, the child process can kill itself on broken pipe (i.e. when # the parent process' stdin sends an EOF). In Windows, however, this is not possible # because java.lang.Process reads directly from the parent process' stdin, contending # with any opportunity to read an EOF from the parent. Note that this is only best # effort and will not take effect if the python process is violently terminated. if on_windows: # In Windows, the child process here is "spark-submit.cmd", not the JVM itself # (because the UNIX "exec" command is not available). This means we cannot simply # call proc.kill(), which kills only the "spark-submit.cmd" process but not the # JVMs. Instead, we use "taskkill" with the tree-kill option "/t" to terminate all # child processes in the tree (http://technet.microsoft.com/en-us/library/bb491009.aspx) def killChild(): Popen(["cmd", "/c", "taskkill", "/f", "/t", "/pid", str(proc.pid)]) atexit.register(killChild) # Connect to the gateway gateway = JavaGateway( gateway_parameters=GatewayParameters(port=gateway_port, auth_token=gateway_secret, auto_convert=True)) # Store a reference to the Popen object for use by the caller (e.g., in reading stdout/stderr) gateway.proc = proc # Import the classes used by PySpark java_import(gateway.jvm, "org.apache.spark.SparkConf") java_import(gateway.jvm, "org.apache.spark.api.java.*") java_import(gateway.jvm, "org.apache.spark.api.python.*") java_import(gateway.jvm, "org.apache.spark.ml.python.*") java_import(gateway.jvm, "org.apache.spark.mllib.api.python.*") # TODO(davies): move into sql java_import(gateway.jvm, "org.apache.spark.sql.*") java_import(gateway.jvm, "org.apache.spark.sql.api.python.*") java_import(gateway.jvm, "org.apache.spark.sql.hive.*") java_import(gateway.jvm, "scala.Tuple2") return gateway
[ "def", "launch_gateway", "(", "conf", "=", "None", ",", "popen_kwargs", "=", "None", ")", ":", "if", "\"PYSPARK_GATEWAY_PORT\"", "in", "os", ".", "environ", ":", "gateway_port", "=", "int", "(", "os", ".", "environ", "[", "\"PYSPARK_GATEWAY_PORT\"", "]", ")", "gateway_secret", "=", "os", ".", "environ", "[", "\"PYSPARK_GATEWAY_SECRET\"", "]", "# Process already exists", "proc", "=", "None", "else", ":", "SPARK_HOME", "=", "_find_spark_home", "(", ")", "# Launch the Py4j gateway using Spark's run command so that we pick up the", "# proper classpath and settings from spark-env.sh", "on_windows", "=", "platform", ".", "system", "(", ")", "==", "\"Windows\"", "script", "=", "\"./bin/spark-submit.cmd\"", "if", "on_windows", "else", "\"./bin/spark-submit\"", "command", "=", "[", "os", ".", "path", ".", "join", "(", "SPARK_HOME", ",", "script", ")", "]", "if", "conf", ":", "for", "k", ",", "v", "in", "conf", ".", "getAll", "(", ")", ":", "command", "+=", "[", "'--conf'", ",", "'%s=%s'", "%", "(", "k", ",", "v", ")", "]", "submit_args", "=", "os", ".", "environ", ".", "get", "(", "\"PYSPARK_SUBMIT_ARGS\"", ",", "\"pyspark-shell\"", ")", "if", "os", ".", "environ", ".", "get", "(", "\"SPARK_TESTING\"", ")", ":", "submit_args", "=", "' '", ".", "join", "(", "[", "\"--conf spark.ui.enabled=false\"", ",", "submit_args", "]", ")", "command", "=", "command", "+", "shlex", ".", "split", "(", "submit_args", ")", "# Create a temporary directory where the gateway server should write the connection", "# information.", "conn_info_dir", "=", "tempfile", ".", "mkdtemp", "(", ")", "try", ":", "fd", ",", "conn_info_file", "=", "tempfile", ".", "mkstemp", "(", "dir", "=", "conn_info_dir", ")", "os", ".", "close", "(", "fd", ")", "os", ".", "unlink", "(", "conn_info_file", ")", "env", "=", "dict", "(", "os", ".", "environ", ")", "env", "[", "\"_PYSPARK_DRIVER_CONN_INFO_PATH\"", "]", "=", "conn_info_file", "# Launch the Java gateway.", "popen_kwargs", "=", "{", "}", "if", "popen_kwargs", "is", "None", "else", "popen_kwargs", "# We open a pipe to stdin so that the Java gateway can die when the pipe is broken", "popen_kwargs", "[", "'stdin'", "]", "=", "PIPE", "# We always set the necessary environment variables.", "popen_kwargs", "[", "'env'", "]", "=", "env", "if", "not", "on_windows", ":", "# Don't send ctrl-c / SIGINT to the Java gateway:", "def", "preexec_func", "(", ")", ":", "signal", ".", "signal", "(", "signal", ".", "SIGINT", ",", "signal", ".", "SIG_IGN", ")", "popen_kwargs", "[", "'preexec_fn'", "]", "=", "preexec_func", "proc", "=", "Popen", "(", "command", ",", "*", "*", "popen_kwargs", ")", "else", ":", "# preexec_fn not supported on Windows", "proc", "=", "Popen", "(", "command", ",", "*", "*", "popen_kwargs", ")", "# Wait for the file to appear, or for the process to exit, whichever happens first.", "while", "not", "proc", ".", "poll", "(", ")", "and", "not", "os", ".", "path", ".", "isfile", "(", "conn_info_file", ")", ":", "time", ".", "sleep", "(", "0.1", ")", "if", "not", "os", ".", "path", ".", "isfile", "(", "conn_info_file", ")", ":", "raise", "Exception", "(", "\"Java gateway process exited before sending its port number\"", ")", "with", "open", "(", "conn_info_file", ",", "\"rb\"", ")", "as", "info", ":", "gateway_port", "=", "read_int", "(", "info", ")", "gateway_secret", "=", "UTF8Deserializer", "(", ")", ".", "loads", "(", "info", ")", "finally", ":", "shutil", ".", "rmtree", "(", "conn_info_dir", ")", "# In Windows, ensure the Java child processes do not linger after Python has exited.", "# In UNIX-based systems, the child process can kill itself on broken pipe (i.e. when", "# the parent process' stdin sends an EOF). In Windows, however, this is not possible", "# because java.lang.Process reads directly from the parent process' stdin, contending", "# with any opportunity to read an EOF from the parent. Note that this is only best", "# effort and will not take effect if the python process is violently terminated.", "if", "on_windows", ":", "# In Windows, the child process here is \"spark-submit.cmd\", not the JVM itself", "# (because the UNIX \"exec\" command is not available). This means we cannot simply", "# call proc.kill(), which kills only the \"spark-submit.cmd\" process but not the", "# JVMs. Instead, we use \"taskkill\" with the tree-kill option \"/t\" to terminate all", "# child processes in the tree (http://technet.microsoft.com/en-us/library/bb491009.aspx)", "def", "killChild", "(", ")", ":", "Popen", "(", "[", "\"cmd\"", ",", "\"/c\"", ",", "\"taskkill\"", ",", "\"/f\"", ",", "\"/t\"", ",", "\"/pid\"", ",", "str", "(", "proc", ".", "pid", ")", "]", ")", "atexit", ".", "register", "(", "killChild", ")", "# Connect to the gateway", "gateway", "=", "JavaGateway", "(", "gateway_parameters", "=", "GatewayParameters", "(", "port", "=", "gateway_port", ",", "auth_token", "=", "gateway_secret", ",", "auto_convert", "=", "True", ")", ")", "# Store a reference to the Popen object for use by the caller (e.g., in reading stdout/stderr)", "gateway", ".", "proc", "=", "proc", "# Import the classes used by PySpark", "java_import", "(", "gateway", ".", "jvm", ",", "\"org.apache.spark.SparkConf\"", ")", "java_import", "(", "gateway", ".", "jvm", ",", "\"org.apache.spark.api.java.*\"", ")", "java_import", "(", "gateway", ".", "jvm", ",", "\"org.apache.spark.api.python.*\"", ")", "java_import", "(", "gateway", ".", "jvm", ",", "\"org.apache.spark.ml.python.*\"", ")", "java_import", "(", "gateway", ".", "jvm", ",", "\"org.apache.spark.mllib.api.python.*\"", ")", "# TODO(davies): move into sql", "java_import", "(", "gateway", ".", "jvm", ",", "\"org.apache.spark.sql.*\"", ")", "java_import", "(", "gateway", ".", "jvm", ",", "\"org.apache.spark.sql.api.python.*\"", ")", "java_import", "(", "gateway", ".", "jvm", ",", "\"org.apache.spark.sql.hive.*\"", ")", "java_import", "(", "gateway", ".", "jvm", ",", "\"scala.Tuple2\"", ")", "return", "gateway" ]
launch jvm gateway :param conf: spark configuration passed to spark-submit :param popen_kwargs: Dictionary of kwargs to pass to Popen when spawning the py4j JVM. This is a developer feature intended for use in customizing how pyspark interacts with the py4j JVM (e.g., capturing stdout/stderr). :return:
[ "launch", "jvm", "gateway", ":", "param", "conf", ":", "spark", "configuration", "passed", "to", "spark", "-", "submit", ":", "param", "popen_kwargs", ":", "Dictionary", "of", "kwargs", "to", "pass", "to", "Popen", "when", "spawning", "the", "py4j", "JVM", ".", "This", "is", "a", "developer", "feature", "intended", "for", "use", "in", "customizing", "how", "pyspark", "interacts", "with", "the", "py4j", "JVM", "(", "e", ".", "g", ".", "capturing", "stdout", "/", "stderr", ")", ".", ":", "return", ":" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/java_gateway.py#L39-L147
apache/spark
python/pyspark/java_gateway.py
_do_server_auth
def _do_server_auth(conn, auth_secret): """ Performs the authentication protocol defined by the SocketAuthHelper class on the given file-like object 'conn'. """ write_with_length(auth_secret.encode("utf-8"), conn) conn.flush() reply = UTF8Deserializer().loads(conn) if reply != "ok": conn.close() raise Exception("Unexpected reply from iterator server.")
python
def _do_server_auth(conn, auth_secret): """ Performs the authentication protocol defined by the SocketAuthHelper class on the given file-like object 'conn'. """ write_with_length(auth_secret.encode("utf-8"), conn) conn.flush() reply = UTF8Deserializer().loads(conn) if reply != "ok": conn.close() raise Exception("Unexpected reply from iterator server.")
[ "def", "_do_server_auth", "(", "conn", ",", "auth_secret", ")", ":", "write_with_length", "(", "auth_secret", ".", "encode", "(", "\"utf-8\"", ")", ",", "conn", ")", "conn", ".", "flush", "(", ")", "reply", "=", "UTF8Deserializer", "(", ")", ".", "loads", "(", "conn", ")", "if", "reply", "!=", "\"ok\"", ":", "conn", ".", "close", "(", ")", "raise", "Exception", "(", "\"Unexpected reply from iterator server.\"", ")" ]
Performs the authentication protocol defined by the SocketAuthHelper class on the given file-like object 'conn'.
[ "Performs", "the", "authentication", "protocol", "defined", "by", "the", "SocketAuthHelper", "class", "on", "the", "given", "file", "-", "like", "object", "conn", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/java_gateway.py#L150-L160
apache/spark
python/pyspark/java_gateway.py
local_connect_and_auth
def local_connect_and_auth(port, auth_secret): """ Connect to local host, authenticate with it, and return a (sockfile,sock) for that connection. Handles IPV4 & IPV6, does some error handling. :param port :param auth_secret :return: a tuple with (sockfile, sock) """ sock = None errors = [] # Support for both IPv4 and IPv6. # On most of IPv6-ready systems, IPv6 will take precedence. for res in socket.getaddrinfo("127.0.0.1", port, socket.AF_UNSPEC, socket.SOCK_STREAM): af, socktype, proto, _, sa = res try: sock = socket.socket(af, socktype, proto) sock.settimeout(15) sock.connect(sa) sockfile = sock.makefile("rwb", 65536) _do_server_auth(sockfile, auth_secret) return (sockfile, sock) except socket.error as e: emsg = _exception_message(e) errors.append("tried to connect to %s, but an error occured: %s" % (sa, emsg)) sock.close() sock = None raise Exception("could not open socket: %s" % errors)
python
def local_connect_and_auth(port, auth_secret): """ Connect to local host, authenticate with it, and return a (sockfile,sock) for that connection. Handles IPV4 & IPV6, does some error handling. :param port :param auth_secret :return: a tuple with (sockfile, sock) """ sock = None errors = [] # Support for both IPv4 and IPv6. # On most of IPv6-ready systems, IPv6 will take precedence. for res in socket.getaddrinfo("127.0.0.1", port, socket.AF_UNSPEC, socket.SOCK_STREAM): af, socktype, proto, _, sa = res try: sock = socket.socket(af, socktype, proto) sock.settimeout(15) sock.connect(sa) sockfile = sock.makefile("rwb", 65536) _do_server_auth(sockfile, auth_secret) return (sockfile, sock) except socket.error as e: emsg = _exception_message(e) errors.append("tried to connect to %s, but an error occured: %s" % (sa, emsg)) sock.close() sock = None raise Exception("could not open socket: %s" % errors)
[ "def", "local_connect_and_auth", "(", "port", ",", "auth_secret", ")", ":", "sock", "=", "None", "errors", "=", "[", "]", "# Support for both IPv4 and IPv6.", "# On most of IPv6-ready systems, IPv6 will take precedence.", "for", "res", "in", "socket", ".", "getaddrinfo", "(", "\"127.0.0.1\"", ",", "port", ",", "socket", ".", "AF_UNSPEC", ",", "socket", ".", "SOCK_STREAM", ")", ":", "af", ",", "socktype", ",", "proto", ",", "_", ",", "sa", "=", "res", "try", ":", "sock", "=", "socket", ".", "socket", "(", "af", ",", "socktype", ",", "proto", ")", "sock", ".", "settimeout", "(", "15", ")", "sock", ".", "connect", "(", "sa", ")", "sockfile", "=", "sock", ".", "makefile", "(", "\"rwb\"", ",", "65536", ")", "_do_server_auth", "(", "sockfile", ",", "auth_secret", ")", "return", "(", "sockfile", ",", "sock", ")", "except", "socket", ".", "error", "as", "e", ":", "emsg", "=", "_exception_message", "(", "e", ")", "errors", ".", "append", "(", "\"tried to connect to %s, but an error occured: %s\"", "%", "(", "sa", ",", "emsg", ")", ")", "sock", ".", "close", "(", ")", "sock", "=", "None", "raise", "Exception", "(", "\"could not open socket: %s\"", "%", "errors", ")" ]
Connect to local host, authenticate with it, and return a (sockfile,sock) for that connection. Handles IPV4 & IPV6, does some error handling. :param port :param auth_secret :return: a tuple with (sockfile, sock)
[ "Connect", "to", "local", "host", "authenticate", "with", "it", "and", "return", "a", "(", "sockfile", "sock", ")", "for", "that", "connection", ".", "Handles", "IPV4", "&", "IPV6", "does", "some", "error", "handling", ".", ":", "param", "port", ":", "param", "auth_secret", ":", "return", ":", "a", "tuple", "with", "(", "sockfile", "sock", ")" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/java_gateway.py#L163-L189
apache/spark
python/pyspark/java_gateway.py
ensure_callback_server_started
def ensure_callback_server_started(gw): """ Start callback server if not already started. The callback server is needed if the Java driver process needs to callback into the Python driver process to execute Python code. """ # getattr will fallback to JVM, so we cannot test by hasattr() if "_callback_server" not in gw.__dict__ or gw._callback_server is None: gw.callback_server_parameters.eager_load = True gw.callback_server_parameters.daemonize = True gw.callback_server_parameters.daemonize_connections = True gw.callback_server_parameters.port = 0 gw.start_callback_server(gw.callback_server_parameters) cbport = gw._callback_server.server_socket.getsockname()[1] gw._callback_server.port = cbport # gateway with real port gw._python_proxy_port = gw._callback_server.port # get the GatewayServer object in JVM by ID jgws = JavaObject("GATEWAY_SERVER", gw._gateway_client) # update the port of CallbackClient with real port jgws.resetCallbackClient(jgws.getCallbackClient().getAddress(), gw._python_proxy_port)
python
def ensure_callback_server_started(gw): """ Start callback server if not already started. The callback server is needed if the Java driver process needs to callback into the Python driver process to execute Python code. """ # getattr will fallback to JVM, so we cannot test by hasattr() if "_callback_server" not in gw.__dict__ or gw._callback_server is None: gw.callback_server_parameters.eager_load = True gw.callback_server_parameters.daemonize = True gw.callback_server_parameters.daemonize_connections = True gw.callback_server_parameters.port = 0 gw.start_callback_server(gw.callback_server_parameters) cbport = gw._callback_server.server_socket.getsockname()[1] gw._callback_server.port = cbport # gateway with real port gw._python_proxy_port = gw._callback_server.port # get the GatewayServer object in JVM by ID jgws = JavaObject("GATEWAY_SERVER", gw._gateway_client) # update the port of CallbackClient with real port jgws.resetCallbackClient(jgws.getCallbackClient().getAddress(), gw._python_proxy_port)
[ "def", "ensure_callback_server_started", "(", "gw", ")", ":", "# getattr will fallback to JVM, so we cannot test by hasattr()", "if", "\"_callback_server\"", "not", "in", "gw", ".", "__dict__", "or", "gw", ".", "_callback_server", "is", "None", ":", "gw", ".", "callback_server_parameters", ".", "eager_load", "=", "True", "gw", ".", "callback_server_parameters", ".", "daemonize", "=", "True", "gw", ".", "callback_server_parameters", ".", "daemonize_connections", "=", "True", "gw", ".", "callback_server_parameters", ".", "port", "=", "0", "gw", ".", "start_callback_server", "(", "gw", ".", "callback_server_parameters", ")", "cbport", "=", "gw", ".", "_callback_server", ".", "server_socket", ".", "getsockname", "(", ")", "[", "1", "]", "gw", ".", "_callback_server", ".", "port", "=", "cbport", "# gateway with real port", "gw", ".", "_python_proxy_port", "=", "gw", ".", "_callback_server", ".", "port", "# get the GatewayServer object in JVM by ID", "jgws", "=", "JavaObject", "(", "\"GATEWAY_SERVER\"", ",", "gw", ".", "_gateway_client", ")", "# update the port of CallbackClient with real port", "jgws", ".", "resetCallbackClient", "(", "jgws", ".", "getCallbackClient", "(", ")", ".", "getAddress", "(", ")", ",", "gw", ".", "_python_proxy_port", ")" ]
Start callback server if not already started. The callback server is needed if the Java driver process needs to callback into the Python driver process to execute Python code.
[ "Start", "callback", "server", "if", "not", "already", "started", ".", "The", "callback", "server", "is", "needed", "if", "the", "Java", "driver", "process", "needs", "to", "callback", "into", "the", "Python", "driver", "process", "to", "execute", "Python", "code", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/java_gateway.py#L192-L212
apache/spark
python/pyspark/find_spark_home.py
_find_spark_home
def _find_spark_home(): """Find the SPARK_HOME.""" # If the environment has SPARK_HOME set trust it. if "SPARK_HOME" in os.environ: return os.environ["SPARK_HOME"] def is_spark_home(path): """Takes a path and returns true if the provided path could be a reasonable SPARK_HOME""" return (os.path.isfile(os.path.join(path, "bin/spark-submit")) and (os.path.isdir(os.path.join(path, "jars")) or os.path.isdir(os.path.join(path, "assembly")))) paths = ["../", os.path.dirname(os.path.realpath(__file__))] # Add the path of the PySpark module if it exists if sys.version < "3": import imp try: module_home = imp.find_module("pyspark")[1] paths.append(module_home) # If we are installed in edit mode also look two dirs up paths.append(os.path.join(module_home, "../../")) except ImportError: # Not pip installed no worries pass else: from importlib.util import find_spec try: module_home = os.path.dirname(find_spec("pyspark").origin) paths.append(module_home) # If we are installed in edit mode also look two dirs up paths.append(os.path.join(module_home, "../../")) except ImportError: # Not pip installed no worries pass # Normalize the paths paths = [os.path.abspath(p) for p in paths] try: return next(path for path in paths if is_spark_home(path)) except StopIteration: print("Could not find valid SPARK_HOME while searching {0}".format(paths), file=sys.stderr) sys.exit(-1)
python
def _find_spark_home(): """Find the SPARK_HOME.""" # If the environment has SPARK_HOME set trust it. if "SPARK_HOME" in os.environ: return os.environ["SPARK_HOME"] def is_spark_home(path): """Takes a path and returns true if the provided path could be a reasonable SPARK_HOME""" return (os.path.isfile(os.path.join(path, "bin/spark-submit")) and (os.path.isdir(os.path.join(path, "jars")) or os.path.isdir(os.path.join(path, "assembly")))) paths = ["../", os.path.dirname(os.path.realpath(__file__))] # Add the path of the PySpark module if it exists if sys.version < "3": import imp try: module_home = imp.find_module("pyspark")[1] paths.append(module_home) # If we are installed in edit mode also look two dirs up paths.append(os.path.join(module_home, "../../")) except ImportError: # Not pip installed no worries pass else: from importlib.util import find_spec try: module_home = os.path.dirname(find_spec("pyspark").origin) paths.append(module_home) # If we are installed in edit mode also look two dirs up paths.append(os.path.join(module_home, "../../")) except ImportError: # Not pip installed no worries pass # Normalize the paths paths = [os.path.abspath(p) for p in paths] try: return next(path for path in paths if is_spark_home(path)) except StopIteration: print("Could not find valid SPARK_HOME while searching {0}".format(paths), file=sys.stderr) sys.exit(-1)
[ "def", "_find_spark_home", "(", ")", ":", "# If the environment has SPARK_HOME set trust it.", "if", "\"SPARK_HOME\"", "in", "os", ".", "environ", ":", "return", "os", ".", "environ", "[", "\"SPARK_HOME\"", "]", "def", "is_spark_home", "(", "path", ")", ":", "\"\"\"Takes a path and returns true if the provided path could be a reasonable SPARK_HOME\"\"\"", "return", "(", "os", ".", "path", ".", "isfile", "(", "os", ".", "path", ".", "join", "(", "path", ",", "\"bin/spark-submit\"", ")", ")", "and", "(", "os", ".", "path", ".", "isdir", "(", "os", ".", "path", ".", "join", "(", "path", ",", "\"jars\"", ")", ")", "or", "os", ".", "path", ".", "isdir", "(", "os", ".", "path", ".", "join", "(", "path", ",", "\"assembly\"", ")", ")", ")", ")", "paths", "=", "[", "\"../\"", ",", "os", ".", "path", ".", "dirname", "(", "os", ".", "path", ".", "realpath", "(", "__file__", ")", ")", "]", "# Add the path of the PySpark module if it exists", "if", "sys", ".", "version", "<", "\"3\"", ":", "import", "imp", "try", ":", "module_home", "=", "imp", ".", "find_module", "(", "\"pyspark\"", ")", "[", "1", "]", "paths", ".", "append", "(", "module_home", ")", "# If we are installed in edit mode also look two dirs up", "paths", ".", "append", "(", "os", ".", "path", ".", "join", "(", "module_home", ",", "\"../../\"", ")", ")", "except", "ImportError", ":", "# Not pip installed no worries", "pass", "else", ":", "from", "importlib", ".", "util", "import", "find_spec", "try", ":", "module_home", "=", "os", ".", "path", ".", "dirname", "(", "find_spec", "(", "\"pyspark\"", ")", ".", "origin", ")", "paths", ".", "append", "(", "module_home", ")", "# If we are installed in edit mode also look two dirs up", "paths", ".", "append", "(", "os", ".", "path", ".", "join", "(", "module_home", ",", "\"../../\"", ")", ")", "except", "ImportError", ":", "# Not pip installed no worries", "pass", "# Normalize the paths", "paths", "=", "[", "os", ".", "path", ".", "abspath", "(", "p", ")", "for", "p", "in", "paths", "]", "try", ":", "return", "next", "(", "path", "for", "path", "in", "paths", "if", "is_spark_home", "(", "path", ")", ")", "except", "StopIteration", ":", "print", "(", "\"Could not find valid SPARK_HOME while searching {0}\"", ".", "format", "(", "paths", ")", ",", "file", "=", "sys", ".", "stderr", ")", "sys", ".", "exit", "(", "-", "1", ")" ]
Find the SPARK_HOME.
[ "Find", "the", "SPARK_HOME", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/find_spark_home.py#L28-L71
apache/spark
examples/src/main/python/pagerank.py
computeContribs
def computeContribs(urls, rank): """Calculates URL contributions to the rank of other URLs.""" num_urls = len(urls) for url in urls: yield (url, rank / num_urls)
python
def computeContribs(urls, rank): """Calculates URL contributions to the rank of other URLs.""" num_urls = len(urls) for url in urls: yield (url, rank / num_urls)
[ "def", "computeContribs", "(", "urls", ",", "rank", ")", ":", "num_urls", "=", "len", "(", "urls", ")", "for", "url", "in", "urls", ":", "yield", "(", "url", ",", "rank", "/", "num_urls", ")" ]
Calculates URL contributions to the rank of other URLs.
[ "Calculates", "URL", "contributions", "to", "the", "rank", "of", "other", "URLs", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/examples/src/main/python/pagerank.py#L34-L38
apache/spark
python/pyspark/ml/clustering.py
GaussianMixtureModel.summary
def summary(self): """ Gets summary (e.g. cluster assignments, cluster sizes) of the model trained on the training set. An exception is thrown if no summary exists. """ if self.hasSummary: return GaussianMixtureSummary(super(GaussianMixtureModel, self).summary) else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
python
def summary(self): """ Gets summary (e.g. cluster assignments, cluster sizes) of the model trained on the training set. An exception is thrown if no summary exists. """ if self.hasSummary: return GaussianMixtureSummary(super(GaussianMixtureModel, self).summary) else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
[ "def", "summary", "(", "self", ")", ":", "if", "self", ".", "hasSummary", ":", "return", "GaussianMixtureSummary", "(", "super", "(", "GaussianMixtureModel", ",", "self", ")", ".", "summary", ")", "else", ":", "raise", "RuntimeError", "(", "\"No training summary available for this %s\"", "%", "self", ".", "__class__", ".", "__name__", ")" ]
Gets summary (e.g. cluster assignments, cluster sizes) of the model trained on the training set. An exception is thrown if no summary exists.
[ "Gets", "summary", "(", "e", ".", "g", ".", "cluster", "assignments", "cluster", "sizes", ")", "of", "the", "model", "trained", "on", "the", "training", "set", ".", "An", "exception", "is", "thrown", "if", "no", "summary", "exists", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/clustering.py#L129-L138
apache/spark
python/pyspark/ml/clustering.py
KMeansModel.summary
def summary(self): """ Gets summary (e.g. cluster assignments, cluster sizes) of the model trained on the training set. An exception is thrown if no summary exists. """ if self.hasSummary: return KMeansSummary(super(KMeansModel, self).summary) else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
python
def summary(self): """ Gets summary (e.g. cluster assignments, cluster sizes) of the model trained on the training set. An exception is thrown if no summary exists. """ if self.hasSummary: return KMeansSummary(super(KMeansModel, self).summary) else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
[ "def", "summary", "(", "self", ")", ":", "if", "self", ".", "hasSummary", ":", "return", "KMeansSummary", "(", "super", "(", "KMeansModel", ",", "self", ")", ".", "summary", ")", "else", ":", "raise", "RuntimeError", "(", "\"No training summary available for this %s\"", "%", "self", ".", "__class__", ".", "__name__", ")" ]
Gets summary (e.g. cluster assignments, cluster sizes) of the model trained on the training set. An exception is thrown if no summary exists.
[ "Gets", "summary", "(", "e", ".", "g", ".", "cluster", "assignments", "cluster", "sizes", ")", "of", "the", "model", "trained", "on", "the", "training", "set", ".", "An", "exception", "is", "thrown", "if", "no", "summary", "exists", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/clustering.py#L331-L340
apache/spark
python/pyspark/ml/clustering.py
BisectingKMeansModel.summary
def summary(self): """ Gets summary (e.g. cluster assignments, cluster sizes) of the model trained on the training set. An exception is thrown if no summary exists. """ if self.hasSummary: return BisectingKMeansSummary(super(BisectingKMeansModel, self).summary) else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
python
def summary(self): """ Gets summary (e.g. cluster assignments, cluster sizes) of the model trained on the training set. An exception is thrown if no summary exists. """ if self.hasSummary: return BisectingKMeansSummary(super(BisectingKMeansModel, self).summary) else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
[ "def", "summary", "(", "self", ")", ":", "if", "self", ".", "hasSummary", ":", "return", "BisectingKMeansSummary", "(", "super", "(", "BisectingKMeansModel", ",", "self", ")", ".", "summary", ")", "else", ":", "raise", "RuntimeError", "(", "\"No training summary available for this %s\"", "%", "self", ".", "__class__", ".", "__name__", ")" ]
Gets summary (e.g. cluster assignments, cluster sizes) of the model trained on the training set. An exception is thrown if no summary exists.
[ "Gets", "summary", "(", "e", ".", "g", ".", "cluster", "assignments", "cluster", "sizes", ")", "of", "the", "model", "trained", "on", "the", "training", "set", ".", "An", "exception", "is", "thrown", "if", "no", "summary", "exists", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/clustering.py#L522-L531
apache/spark
python/pyspark/ml/image.py
_ImageSchema.imageSchema
def imageSchema(self): """ Returns the image schema. :return: a :class:`StructType` with a single column of images named "image" (nullable) and having the same type returned by :meth:`columnSchema`. .. versionadded:: 2.3.0 """ if self._imageSchema is None: ctx = SparkContext._active_spark_context jschema = ctx._jvm.org.apache.spark.ml.image.ImageSchema.imageSchema() self._imageSchema = _parse_datatype_json_string(jschema.json()) return self._imageSchema
python
def imageSchema(self): """ Returns the image schema. :return: a :class:`StructType` with a single column of images named "image" (nullable) and having the same type returned by :meth:`columnSchema`. .. versionadded:: 2.3.0 """ if self._imageSchema is None: ctx = SparkContext._active_spark_context jschema = ctx._jvm.org.apache.spark.ml.image.ImageSchema.imageSchema() self._imageSchema = _parse_datatype_json_string(jschema.json()) return self._imageSchema
[ "def", "imageSchema", "(", "self", ")", ":", "if", "self", ".", "_imageSchema", "is", "None", ":", "ctx", "=", "SparkContext", ".", "_active_spark_context", "jschema", "=", "ctx", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "ml", ".", "image", ".", "ImageSchema", ".", "imageSchema", "(", ")", "self", ".", "_imageSchema", "=", "_parse_datatype_json_string", "(", "jschema", ".", "json", "(", ")", ")", "return", "self", ".", "_imageSchema" ]
Returns the image schema. :return: a :class:`StructType` with a single column of images named "image" (nullable) and having the same type returned by :meth:`columnSchema`. .. versionadded:: 2.3.0
[ "Returns", "the", "image", "schema", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/image.py#L55-L69
apache/spark
python/pyspark/ml/image.py
_ImageSchema.ocvTypes
def ocvTypes(self): """ Returns the OpenCV type mapping supported. :return: a dictionary containing the OpenCV type mapping supported. .. versionadded:: 2.3.0 """ if self._ocvTypes is None: ctx = SparkContext._active_spark_context self._ocvTypes = dict(ctx._jvm.org.apache.spark.ml.image.ImageSchema.javaOcvTypes()) return self._ocvTypes
python
def ocvTypes(self): """ Returns the OpenCV type mapping supported. :return: a dictionary containing the OpenCV type mapping supported. .. versionadded:: 2.3.0 """ if self._ocvTypes is None: ctx = SparkContext._active_spark_context self._ocvTypes = dict(ctx._jvm.org.apache.spark.ml.image.ImageSchema.javaOcvTypes()) return self._ocvTypes
[ "def", "ocvTypes", "(", "self", ")", ":", "if", "self", ".", "_ocvTypes", "is", "None", ":", "ctx", "=", "SparkContext", ".", "_active_spark_context", "self", ".", "_ocvTypes", "=", "dict", "(", "ctx", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "ml", ".", "image", ".", "ImageSchema", ".", "javaOcvTypes", "(", ")", ")", "return", "self", ".", "_ocvTypes" ]
Returns the OpenCV type mapping supported. :return: a dictionary containing the OpenCV type mapping supported. .. versionadded:: 2.3.0
[ "Returns", "the", "OpenCV", "type", "mapping", "supported", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/image.py#L72-L84
apache/spark
python/pyspark/ml/image.py
_ImageSchema.columnSchema
def columnSchema(self): """ Returns the schema for the image column. :return: a :class:`StructType` for image column, ``struct<origin:string, height:int, width:int, nChannels:int, mode:int, data:binary>``. .. versionadded:: 2.4.0 """ if self._columnSchema is None: ctx = SparkContext._active_spark_context jschema = ctx._jvm.org.apache.spark.ml.image.ImageSchema.columnSchema() self._columnSchema = _parse_datatype_json_string(jschema.json()) return self._columnSchema
python
def columnSchema(self): """ Returns the schema for the image column. :return: a :class:`StructType` for image column, ``struct<origin:string, height:int, width:int, nChannels:int, mode:int, data:binary>``. .. versionadded:: 2.4.0 """ if self._columnSchema is None: ctx = SparkContext._active_spark_context jschema = ctx._jvm.org.apache.spark.ml.image.ImageSchema.columnSchema() self._columnSchema = _parse_datatype_json_string(jschema.json()) return self._columnSchema
[ "def", "columnSchema", "(", "self", ")", ":", "if", "self", ".", "_columnSchema", "is", "None", ":", "ctx", "=", "SparkContext", ".", "_active_spark_context", "jschema", "=", "ctx", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "ml", ".", "image", ".", "ImageSchema", ".", "columnSchema", "(", ")", "self", ".", "_columnSchema", "=", "_parse_datatype_json_string", "(", "jschema", ".", "json", "(", ")", ")", "return", "self", ".", "_columnSchema" ]
Returns the schema for the image column. :return: a :class:`StructType` for image column, ``struct<origin:string, height:int, width:int, nChannels:int, mode:int, data:binary>``. .. versionadded:: 2.4.0
[ "Returns", "the", "schema", "for", "the", "image", "column", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/image.py#L87-L101
apache/spark
python/pyspark/ml/image.py
_ImageSchema.imageFields
def imageFields(self): """ Returns field names of image columns. :return: a list of field names. .. versionadded:: 2.3.0 """ if self._imageFields is None: ctx = SparkContext._active_spark_context self._imageFields = list(ctx._jvm.org.apache.spark.ml.image.ImageSchema.imageFields()) return self._imageFields
python
def imageFields(self): """ Returns field names of image columns. :return: a list of field names. .. versionadded:: 2.3.0 """ if self._imageFields is None: ctx = SparkContext._active_spark_context self._imageFields = list(ctx._jvm.org.apache.spark.ml.image.ImageSchema.imageFields()) return self._imageFields
[ "def", "imageFields", "(", "self", ")", ":", "if", "self", ".", "_imageFields", "is", "None", ":", "ctx", "=", "SparkContext", ".", "_active_spark_context", "self", ".", "_imageFields", "=", "list", "(", "ctx", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "ml", ".", "image", ".", "ImageSchema", ".", "imageFields", "(", ")", ")", "return", "self", ".", "_imageFields" ]
Returns field names of image columns. :return: a list of field names. .. versionadded:: 2.3.0
[ "Returns", "field", "names", "of", "image", "columns", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/image.py#L104-L116
apache/spark
python/pyspark/ml/image.py
_ImageSchema.undefinedImageType
def undefinedImageType(self): """ Returns the name of undefined image type for the invalid image. .. versionadded:: 2.3.0 """ if self._undefinedImageType is None: ctx = SparkContext._active_spark_context self._undefinedImageType = \ ctx._jvm.org.apache.spark.ml.image.ImageSchema.undefinedImageType() return self._undefinedImageType
python
def undefinedImageType(self): """ Returns the name of undefined image type for the invalid image. .. versionadded:: 2.3.0 """ if self._undefinedImageType is None: ctx = SparkContext._active_spark_context self._undefinedImageType = \ ctx._jvm.org.apache.spark.ml.image.ImageSchema.undefinedImageType() return self._undefinedImageType
[ "def", "undefinedImageType", "(", "self", ")", ":", "if", "self", ".", "_undefinedImageType", "is", "None", ":", "ctx", "=", "SparkContext", ".", "_active_spark_context", "self", ".", "_undefinedImageType", "=", "ctx", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "ml", ".", "image", ".", "ImageSchema", ".", "undefinedImageType", "(", ")", "return", "self", ".", "_undefinedImageType" ]
Returns the name of undefined image type for the invalid image. .. versionadded:: 2.3.0
[ "Returns", "the", "name", "of", "undefined", "image", "type", "for", "the", "invalid", "image", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/image.py#L119-L130
apache/spark
python/pyspark/ml/image.py
_ImageSchema.toNDArray
def toNDArray(self, image): """ Converts an image to an array with metadata. :param `Row` image: A row that contains the image to be converted. It should have the attributes specified in `ImageSchema.imageSchema`. :return: a `numpy.ndarray` that is an image. .. versionadded:: 2.3.0 """ if not isinstance(image, Row): raise TypeError( "image argument should be pyspark.sql.types.Row; however, " "it got [%s]." % type(image)) if any(not hasattr(image, f) for f in self.imageFields): raise ValueError( "image argument should have attributes specified in " "ImageSchema.imageSchema [%s]." % ", ".join(self.imageFields)) height = image.height width = image.width nChannels = image.nChannels return np.ndarray( shape=(height, width, nChannels), dtype=np.uint8, buffer=image.data, strides=(width * nChannels, nChannels, 1))
python
def toNDArray(self, image): """ Converts an image to an array with metadata. :param `Row` image: A row that contains the image to be converted. It should have the attributes specified in `ImageSchema.imageSchema`. :return: a `numpy.ndarray` that is an image. .. versionadded:: 2.3.0 """ if not isinstance(image, Row): raise TypeError( "image argument should be pyspark.sql.types.Row; however, " "it got [%s]." % type(image)) if any(not hasattr(image, f) for f in self.imageFields): raise ValueError( "image argument should have attributes specified in " "ImageSchema.imageSchema [%s]." % ", ".join(self.imageFields)) height = image.height width = image.width nChannels = image.nChannels return np.ndarray( shape=(height, width, nChannels), dtype=np.uint8, buffer=image.data, strides=(width * nChannels, nChannels, 1))
[ "def", "toNDArray", "(", "self", ",", "image", ")", ":", "if", "not", "isinstance", "(", "image", ",", "Row", ")", ":", "raise", "TypeError", "(", "\"image argument should be pyspark.sql.types.Row; however, \"", "\"it got [%s].\"", "%", "type", "(", "image", ")", ")", "if", "any", "(", "not", "hasattr", "(", "image", ",", "f", ")", "for", "f", "in", "self", ".", "imageFields", ")", ":", "raise", "ValueError", "(", "\"image argument should have attributes specified in \"", "\"ImageSchema.imageSchema [%s].\"", "%", "\", \"", ".", "join", "(", "self", ".", "imageFields", ")", ")", "height", "=", "image", ".", "height", "width", "=", "image", ".", "width", "nChannels", "=", "image", ".", "nChannels", "return", "np", ".", "ndarray", "(", "shape", "=", "(", "height", ",", "width", ",", "nChannels", ")", ",", "dtype", "=", "np", ".", "uint8", ",", "buffer", "=", "image", ".", "data", ",", "strides", "=", "(", "width", "*", "nChannels", ",", "nChannels", ",", "1", ")", ")" ]
Converts an image to an array with metadata. :param `Row` image: A row that contains the image to be converted. It should have the attributes specified in `ImageSchema.imageSchema`. :return: a `numpy.ndarray` that is an image. .. versionadded:: 2.3.0
[ "Converts", "an", "image", "to", "an", "array", "with", "metadata", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/image.py#L132-L160
apache/spark
python/pyspark/ml/image.py
_ImageSchema.toImage
def toImage(self, array, origin=""): """ Converts an array with metadata to a two-dimensional image. :param `numpy.ndarray` array: The array to convert to image. :param str origin: Path to the image, optional. :return: a :class:`Row` that is a two dimensional image. .. versionadded:: 2.3.0 """ if not isinstance(array, np.ndarray): raise TypeError( "array argument should be numpy.ndarray; however, it got [%s]." % type(array)) if array.ndim != 3: raise ValueError("Invalid array shape") height, width, nChannels = array.shape ocvTypes = ImageSchema.ocvTypes if nChannels == 1: mode = ocvTypes["CV_8UC1"] elif nChannels == 3: mode = ocvTypes["CV_8UC3"] elif nChannels == 4: mode = ocvTypes["CV_8UC4"] else: raise ValueError("Invalid number of channels") # Running `bytearray(numpy.array([1]))` fails in specific Python versions # with a specific Numpy version, for example in Python 3.6.0 and NumPy 1.13.3. # Here, it avoids it by converting it to bytes. if LooseVersion(np.__version__) >= LooseVersion('1.9'): data = bytearray(array.astype(dtype=np.uint8).ravel().tobytes()) else: # Numpy prior to 1.9 don't have `tobytes` method. data = bytearray(array.astype(dtype=np.uint8).ravel()) # Creating new Row with _create_row(), because Row(name = value, ... ) # orders fields by name, which conflicts with expected schema order # when the new DataFrame is created by UDF return _create_row(self.imageFields, [origin, height, width, nChannels, mode, data])
python
def toImage(self, array, origin=""): """ Converts an array with metadata to a two-dimensional image. :param `numpy.ndarray` array: The array to convert to image. :param str origin: Path to the image, optional. :return: a :class:`Row` that is a two dimensional image. .. versionadded:: 2.3.0 """ if not isinstance(array, np.ndarray): raise TypeError( "array argument should be numpy.ndarray; however, it got [%s]." % type(array)) if array.ndim != 3: raise ValueError("Invalid array shape") height, width, nChannels = array.shape ocvTypes = ImageSchema.ocvTypes if nChannels == 1: mode = ocvTypes["CV_8UC1"] elif nChannels == 3: mode = ocvTypes["CV_8UC3"] elif nChannels == 4: mode = ocvTypes["CV_8UC4"] else: raise ValueError("Invalid number of channels") # Running `bytearray(numpy.array([1]))` fails in specific Python versions # with a specific Numpy version, for example in Python 3.6.0 and NumPy 1.13.3. # Here, it avoids it by converting it to bytes. if LooseVersion(np.__version__) >= LooseVersion('1.9'): data = bytearray(array.astype(dtype=np.uint8).ravel().tobytes()) else: # Numpy prior to 1.9 don't have `tobytes` method. data = bytearray(array.astype(dtype=np.uint8).ravel()) # Creating new Row with _create_row(), because Row(name = value, ... ) # orders fields by name, which conflicts with expected schema order # when the new DataFrame is created by UDF return _create_row(self.imageFields, [origin, height, width, nChannels, mode, data])
[ "def", "toImage", "(", "self", ",", "array", ",", "origin", "=", "\"\"", ")", ":", "if", "not", "isinstance", "(", "array", ",", "np", ".", "ndarray", ")", ":", "raise", "TypeError", "(", "\"array argument should be numpy.ndarray; however, it got [%s].\"", "%", "type", "(", "array", ")", ")", "if", "array", ".", "ndim", "!=", "3", ":", "raise", "ValueError", "(", "\"Invalid array shape\"", ")", "height", ",", "width", ",", "nChannels", "=", "array", ".", "shape", "ocvTypes", "=", "ImageSchema", ".", "ocvTypes", "if", "nChannels", "==", "1", ":", "mode", "=", "ocvTypes", "[", "\"CV_8UC1\"", "]", "elif", "nChannels", "==", "3", ":", "mode", "=", "ocvTypes", "[", "\"CV_8UC3\"", "]", "elif", "nChannels", "==", "4", ":", "mode", "=", "ocvTypes", "[", "\"CV_8UC4\"", "]", "else", ":", "raise", "ValueError", "(", "\"Invalid number of channels\"", ")", "# Running `bytearray(numpy.array([1]))` fails in specific Python versions", "# with a specific Numpy version, for example in Python 3.6.0 and NumPy 1.13.3.", "# Here, it avoids it by converting it to bytes.", "if", "LooseVersion", "(", "np", ".", "__version__", ")", ">=", "LooseVersion", "(", "'1.9'", ")", ":", "data", "=", "bytearray", "(", "array", ".", "astype", "(", "dtype", "=", "np", ".", "uint8", ")", ".", "ravel", "(", ")", ".", "tobytes", "(", ")", ")", "else", ":", "# Numpy prior to 1.9 don't have `tobytes` method.", "data", "=", "bytearray", "(", "array", ".", "astype", "(", "dtype", "=", "np", ".", "uint8", ")", ".", "ravel", "(", ")", ")", "# Creating new Row with _create_row(), because Row(name = value, ... )", "# orders fields by name, which conflicts with expected schema order", "# when the new DataFrame is created by UDF", "return", "_create_row", "(", "self", ".", "imageFields", ",", "[", "origin", ",", "height", ",", "width", ",", "nChannels", ",", "mode", ",", "data", "]", ")" ]
Converts an array with metadata to a two-dimensional image. :param `numpy.ndarray` array: The array to convert to image. :param str origin: Path to the image, optional. :return: a :class:`Row` that is a two dimensional image. .. versionadded:: 2.3.0
[ "Converts", "an", "array", "with", "metadata", "to", "a", "two", "-", "dimensional", "image", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/image.py#L162-L204
apache/spark
python/pyspark/ml/image.py
_ImageSchema.readImages
def readImages(self, path, recursive=False, numPartitions=-1, dropImageFailures=False, sampleRatio=1.0, seed=0): """ Reads the directory of images from the local or remote source. .. note:: If multiple jobs are run in parallel with different sampleRatio or recursive flag, there may be a race condition where one job overwrites the hadoop configs of another. .. note:: If sample ratio is less than 1, sampling uses a PathFilter that is efficient but potentially non-deterministic. .. note:: Deprecated in 2.4.0. Use `spark.read.format("image").load(path)` instead and this `readImages` will be removed in 3.0.0. :param str path: Path to the image directory. :param bool recursive: Recursive search flag. :param int numPartitions: Number of DataFrame partitions. :param bool dropImageFailures: Drop the files that are not valid images. :param float sampleRatio: Fraction of the images loaded. :param int seed: Random number seed. :return: a :class:`DataFrame` with a single column of "images", see ImageSchema for details. >>> df = ImageSchema.readImages('data/mllib/images/origin/kittens', recursive=True) >>> df.count() 5 .. versionadded:: 2.3.0 """ warnings.warn("`ImageSchema.readImage` is deprecated. " + "Use `spark.read.format(\"image\").load(path)` instead.", DeprecationWarning) spark = SparkSession.builder.getOrCreate() image_schema = spark._jvm.org.apache.spark.ml.image.ImageSchema jsession = spark._jsparkSession jresult = image_schema.readImages(path, jsession, recursive, numPartitions, dropImageFailures, float(sampleRatio), seed) return DataFrame(jresult, spark._wrapped)
python
def readImages(self, path, recursive=False, numPartitions=-1, dropImageFailures=False, sampleRatio=1.0, seed=0): """ Reads the directory of images from the local or remote source. .. note:: If multiple jobs are run in parallel with different sampleRatio or recursive flag, there may be a race condition where one job overwrites the hadoop configs of another. .. note:: If sample ratio is less than 1, sampling uses a PathFilter that is efficient but potentially non-deterministic. .. note:: Deprecated in 2.4.0. Use `spark.read.format("image").load(path)` instead and this `readImages` will be removed in 3.0.0. :param str path: Path to the image directory. :param bool recursive: Recursive search flag. :param int numPartitions: Number of DataFrame partitions. :param bool dropImageFailures: Drop the files that are not valid images. :param float sampleRatio: Fraction of the images loaded. :param int seed: Random number seed. :return: a :class:`DataFrame` with a single column of "images", see ImageSchema for details. >>> df = ImageSchema.readImages('data/mllib/images/origin/kittens', recursive=True) >>> df.count() 5 .. versionadded:: 2.3.0 """ warnings.warn("`ImageSchema.readImage` is deprecated. " + "Use `spark.read.format(\"image\").load(path)` instead.", DeprecationWarning) spark = SparkSession.builder.getOrCreate() image_schema = spark._jvm.org.apache.spark.ml.image.ImageSchema jsession = spark._jsparkSession jresult = image_schema.readImages(path, jsession, recursive, numPartitions, dropImageFailures, float(sampleRatio), seed) return DataFrame(jresult, spark._wrapped)
[ "def", "readImages", "(", "self", ",", "path", ",", "recursive", "=", "False", ",", "numPartitions", "=", "-", "1", ",", "dropImageFailures", "=", "False", ",", "sampleRatio", "=", "1.0", ",", "seed", "=", "0", ")", ":", "warnings", ".", "warn", "(", "\"`ImageSchema.readImage` is deprecated. \"", "+", "\"Use `spark.read.format(\\\"image\\\").load(path)` instead.\"", ",", "DeprecationWarning", ")", "spark", "=", "SparkSession", ".", "builder", ".", "getOrCreate", "(", ")", "image_schema", "=", "spark", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "ml", ".", "image", ".", "ImageSchema", "jsession", "=", "spark", ".", "_jsparkSession", "jresult", "=", "image_schema", ".", "readImages", "(", "path", ",", "jsession", ",", "recursive", ",", "numPartitions", ",", "dropImageFailures", ",", "float", "(", "sampleRatio", ")", ",", "seed", ")", "return", "DataFrame", "(", "jresult", ",", "spark", ".", "_wrapped", ")" ]
Reads the directory of images from the local or remote source. .. note:: If multiple jobs are run in parallel with different sampleRatio or recursive flag, there may be a race condition where one job overwrites the hadoop configs of another. .. note:: If sample ratio is less than 1, sampling uses a PathFilter that is efficient but potentially non-deterministic. .. note:: Deprecated in 2.4.0. Use `spark.read.format("image").load(path)` instead and this `readImages` will be removed in 3.0.0. :param str path: Path to the image directory. :param bool recursive: Recursive search flag. :param int numPartitions: Number of DataFrame partitions. :param bool dropImageFailures: Drop the files that are not valid images. :param float sampleRatio: Fraction of the images loaded. :param int seed: Random number seed. :return: a :class:`DataFrame` with a single column of "images", see ImageSchema for details. >>> df = ImageSchema.readImages('data/mllib/images/origin/kittens', recursive=True) >>> df.count() 5 .. versionadded:: 2.3.0
[ "Reads", "the", "directory", "of", "images", "from", "the", "local", "or", "remote", "source", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/image.py#L206-L242
apache/spark
python/pyspark/ml/wrapper.py
JavaWrapper._create_from_java_class
def _create_from_java_class(cls, java_class, *args): """ Construct this object from given Java classname and arguments """ java_obj = JavaWrapper._new_java_obj(java_class, *args) return cls(java_obj)
python
def _create_from_java_class(cls, java_class, *args): """ Construct this object from given Java classname and arguments """ java_obj = JavaWrapper._new_java_obj(java_class, *args) return cls(java_obj)
[ "def", "_create_from_java_class", "(", "cls", ",", "java_class", ",", "*", "args", ")", ":", "java_obj", "=", "JavaWrapper", ".", "_new_java_obj", "(", "java_class", ",", "*", "args", ")", "return", "cls", "(", "java_obj", ")" ]
Construct this object from given Java classname and arguments
[ "Construct", "this", "object", "from", "given", "Java", "classname", "and", "arguments" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/wrapper.py#L44-L49
apache/spark
python/pyspark/ml/wrapper.py
JavaWrapper._new_java_array
def _new_java_array(pylist, java_class): """ Create a Java array of given java_class type. Useful for calling a method with a Scala Array from Python with Py4J. If the param pylist is a 2D array, then a 2D java array will be returned. The returned 2D java array is a square, non-jagged 2D array that is big enough for all elements. The empty slots in the inner Java arrays will be filled with null to make the non-jagged 2D array. :param pylist: Python list to convert to a Java Array. :param java_class: Java class to specify the type of Array. Should be in the form of sc._gateway.jvm.* (sc is a valid Spark Context). :return: Java Array of converted pylist. Example primitive Java classes: - basestring -> sc._gateway.jvm.java.lang.String - int -> sc._gateway.jvm.java.lang.Integer - float -> sc._gateway.jvm.java.lang.Double - bool -> sc._gateway.jvm.java.lang.Boolean """ sc = SparkContext._active_spark_context java_array = None if len(pylist) > 0 and isinstance(pylist[0], list): # If pylist is a 2D array, then a 2D java array will be created. # The 2D array is a square, non-jagged 2D array that is big enough for all elements. inner_array_length = 0 for i in xrange(len(pylist)): inner_array_length = max(inner_array_length, len(pylist[i])) java_array = sc._gateway.new_array(java_class, len(pylist), inner_array_length) for i in xrange(len(pylist)): for j in xrange(len(pylist[i])): java_array[i][j] = pylist[i][j] else: java_array = sc._gateway.new_array(java_class, len(pylist)) for i in xrange(len(pylist)): java_array[i] = pylist[i] return java_array
python
def _new_java_array(pylist, java_class): """ Create a Java array of given java_class type. Useful for calling a method with a Scala Array from Python with Py4J. If the param pylist is a 2D array, then a 2D java array will be returned. The returned 2D java array is a square, non-jagged 2D array that is big enough for all elements. The empty slots in the inner Java arrays will be filled with null to make the non-jagged 2D array. :param pylist: Python list to convert to a Java Array. :param java_class: Java class to specify the type of Array. Should be in the form of sc._gateway.jvm.* (sc is a valid Spark Context). :return: Java Array of converted pylist. Example primitive Java classes: - basestring -> sc._gateway.jvm.java.lang.String - int -> sc._gateway.jvm.java.lang.Integer - float -> sc._gateway.jvm.java.lang.Double - bool -> sc._gateway.jvm.java.lang.Boolean """ sc = SparkContext._active_spark_context java_array = None if len(pylist) > 0 and isinstance(pylist[0], list): # If pylist is a 2D array, then a 2D java array will be created. # The 2D array is a square, non-jagged 2D array that is big enough for all elements. inner_array_length = 0 for i in xrange(len(pylist)): inner_array_length = max(inner_array_length, len(pylist[i])) java_array = sc._gateway.new_array(java_class, len(pylist), inner_array_length) for i in xrange(len(pylist)): for j in xrange(len(pylist[i])): java_array[i][j] = pylist[i][j] else: java_array = sc._gateway.new_array(java_class, len(pylist)) for i in xrange(len(pylist)): java_array[i] = pylist[i] return java_array
[ "def", "_new_java_array", "(", "pylist", ",", "java_class", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "java_array", "=", "None", "if", "len", "(", "pylist", ")", ">", "0", "and", "isinstance", "(", "pylist", "[", "0", "]", ",", "list", ")", ":", "# If pylist is a 2D array, then a 2D java array will be created.", "# The 2D array is a square, non-jagged 2D array that is big enough for all elements.", "inner_array_length", "=", "0", "for", "i", "in", "xrange", "(", "len", "(", "pylist", ")", ")", ":", "inner_array_length", "=", "max", "(", "inner_array_length", ",", "len", "(", "pylist", "[", "i", "]", ")", ")", "java_array", "=", "sc", ".", "_gateway", ".", "new_array", "(", "java_class", ",", "len", "(", "pylist", ")", ",", "inner_array_length", ")", "for", "i", "in", "xrange", "(", "len", "(", "pylist", ")", ")", ":", "for", "j", "in", "xrange", "(", "len", "(", "pylist", "[", "i", "]", ")", ")", ":", "java_array", "[", "i", "]", "[", "j", "]", "=", "pylist", "[", "i", "]", "[", "j", "]", "else", ":", "java_array", "=", "sc", ".", "_gateway", ".", "new_array", "(", "java_class", ",", "len", "(", "pylist", ")", ")", "for", "i", "in", "xrange", "(", "len", "(", "pylist", ")", ")", ":", "java_array", "[", "i", "]", "=", "pylist", "[", "i", "]", "return", "java_array" ]
Create a Java array of given java_class type. Useful for calling a method with a Scala Array from Python with Py4J. If the param pylist is a 2D array, then a 2D java array will be returned. The returned 2D java array is a square, non-jagged 2D array that is big enough for all elements. The empty slots in the inner Java arrays will be filled with null to make the non-jagged 2D array. :param pylist: Python list to convert to a Java Array. :param java_class: Java class to specify the type of Array. Should be in the form of sc._gateway.jvm.* (sc is a valid Spark Context). :return: Java Array of converted pylist. Example primitive Java classes: - basestring -> sc._gateway.jvm.java.lang.String - int -> sc._gateway.jvm.java.lang.Integer - float -> sc._gateway.jvm.java.lang.Double - bool -> sc._gateway.jvm.java.lang.Boolean
[ "Create", "a", "Java", "array", "of", "given", "java_class", "type", ".", "Useful", "for", "calling", "a", "method", "with", "a", "Scala", "Array", "from", "Python", "with", "Py4J", ".", "If", "the", "param", "pylist", "is", "a", "2D", "array", "then", "a", "2D", "java", "array", "will", "be", "returned", ".", "The", "returned", "2D", "java", "array", "is", "a", "square", "non", "-", "jagged", "2D", "array", "that", "is", "big", "enough", "for", "all", "elements", ".", "The", "empty", "slots", "in", "the", "inner", "Java", "arrays", "will", "be", "filled", "with", "null", "to", "make", "the", "non", "-", "jagged", "2D", "array", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/wrapper.py#L70-L109
apache/spark
python/docs/epytext.py
_convert_epytext
def _convert_epytext(line): """ >>> _convert_epytext("L{A}") :class:`A` """ line = line.replace('@', ':') for p, sub in RULES: line = re.sub(p, sub, line) return line
python
def _convert_epytext(line): """ >>> _convert_epytext("L{A}") :class:`A` """ line = line.replace('@', ':') for p, sub in RULES: line = re.sub(p, sub, line) return line
[ "def", "_convert_epytext", "(", "line", ")", ":", "line", "=", "line", ".", "replace", "(", "'@'", ",", "':'", ")", "for", "p", ",", "sub", "in", "RULES", ":", "line", "=", "re", ".", "sub", "(", "p", ",", "sub", ",", "line", ")", "return", "line" ]
>>> _convert_epytext("L{A}") :class:`A`
[ ">>>", "_convert_epytext", "(", "L", "{", "A", "}", ")", ":", "class", ":", "A" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/docs/epytext.py#L13-L21
apache/spark
python/pyspark/streaming/util.py
rddToFileName
def rddToFileName(prefix, suffix, timestamp): """ Return string prefix-time(.suffix) >>> rddToFileName("spark", None, 12345678910) 'spark-12345678910' >>> rddToFileName("spark", "tmp", 12345678910) 'spark-12345678910.tmp' """ if isinstance(timestamp, datetime): seconds = time.mktime(timestamp.timetuple()) timestamp = int(seconds * 1000) + timestamp.microsecond // 1000 if suffix is None: return prefix + "-" + str(timestamp) else: return prefix + "-" + str(timestamp) + "." + suffix
python
def rddToFileName(prefix, suffix, timestamp): """ Return string prefix-time(.suffix) >>> rddToFileName("spark", None, 12345678910) 'spark-12345678910' >>> rddToFileName("spark", "tmp", 12345678910) 'spark-12345678910.tmp' """ if isinstance(timestamp, datetime): seconds = time.mktime(timestamp.timetuple()) timestamp = int(seconds * 1000) + timestamp.microsecond // 1000 if suffix is None: return prefix + "-" + str(timestamp) else: return prefix + "-" + str(timestamp) + "." + suffix
[ "def", "rddToFileName", "(", "prefix", ",", "suffix", ",", "timestamp", ")", ":", "if", "isinstance", "(", "timestamp", ",", "datetime", ")", ":", "seconds", "=", "time", ".", "mktime", "(", "timestamp", ".", "timetuple", "(", ")", ")", "timestamp", "=", "int", "(", "seconds", "*", "1000", ")", "+", "timestamp", ".", "microsecond", "//", "1000", "if", "suffix", "is", "None", ":", "return", "prefix", "+", "\"-\"", "+", "str", "(", "timestamp", ")", "else", ":", "return", "prefix", "+", "\"-\"", "+", "str", "(", "timestamp", ")", "+", "\".\"", "+", "suffix" ]
Return string prefix-time(.suffix) >>> rddToFileName("spark", None, 12345678910) 'spark-12345678910' >>> rddToFileName("spark", "tmp", 12345678910) 'spark-12345678910.tmp'
[ "Return", "string", "prefix", "-", "time", "(", ".", "suffix", ")" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/streaming/util.py#L138-L153
apache/spark
python/pyspark/profiler.py
ProfilerCollector.add_profiler
def add_profiler(self, id, profiler): """ Add a profiler for RDD `id` """ if not self.profilers: if self.profile_dump_path: atexit.register(self.dump_profiles, self.profile_dump_path) else: atexit.register(self.show_profiles) self.profilers.append([id, profiler, False])
python
def add_profiler(self, id, profiler): """ Add a profiler for RDD `id` """ if not self.profilers: if self.profile_dump_path: atexit.register(self.dump_profiles, self.profile_dump_path) else: atexit.register(self.show_profiles) self.profilers.append([id, profiler, False])
[ "def", "add_profiler", "(", "self", ",", "id", ",", "profiler", ")", ":", "if", "not", "self", ".", "profilers", ":", "if", "self", ".", "profile_dump_path", ":", "atexit", ".", "register", "(", "self", ".", "dump_profiles", ",", "self", ".", "profile_dump_path", ")", "else", ":", "atexit", ".", "register", "(", "self", ".", "show_profiles", ")", "self", ".", "profilers", ".", "append", "(", "[", "id", ",", "profiler", ",", "False", "]", ")" ]
Add a profiler for RDD `id`
[ "Add", "a", "profiler", "for", "RDD", "id" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/profiler.py#L43-L51
apache/spark
python/pyspark/profiler.py
ProfilerCollector.dump_profiles
def dump_profiles(self, path): """ Dump the profile stats into directory `path` """ for id, profiler, _ in self.profilers: profiler.dump(id, path) self.profilers = []
python
def dump_profiles(self, path): """ Dump the profile stats into directory `path` """ for id, profiler, _ in self.profilers: profiler.dump(id, path) self.profilers = []
[ "def", "dump_profiles", "(", "self", ",", "path", ")", ":", "for", "id", ",", "profiler", ",", "_", "in", "self", ".", "profilers", ":", "profiler", ".", "dump", "(", "id", ",", "path", ")", "self", ".", "profilers", "=", "[", "]" ]
Dump the profile stats into directory `path`
[ "Dump", "the", "profile", "stats", "into", "directory", "path" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/profiler.py#L53-L57
apache/spark
python/pyspark/profiler.py
ProfilerCollector.show_profiles
def show_profiles(self): """ Print the profile stats to stdout """ for i, (id, profiler, showed) in enumerate(self.profilers): if not showed and profiler: profiler.show(id) # mark it as showed self.profilers[i][2] = True
python
def show_profiles(self): """ Print the profile stats to stdout """ for i, (id, profiler, showed) in enumerate(self.profilers): if not showed and profiler: profiler.show(id) # mark it as showed self.profilers[i][2] = True
[ "def", "show_profiles", "(", "self", ")", ":", "for", "i", ",", "(", "id", ",", "profiler", ",", "showed", ")", "in", "enumerate", "(", "self", ".", "profilers", ")", ":", "if", "not", "showed", "and", "profiler", ":", "profiler", ".", "show", "(", "id", ")", "# mark it as showed", "self", ".", "profilers", "[", "i", "]", "[", "2", "]", "=", "True" ]
Print the profile stats to stdout
[ "Print", "the", "profile", "stats", "to", "stdout" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/profiler.py#L59-L65
apache/spark
python/pyspark/profiler.py
Profiler.show
def show(self, id): """ Print the profile stats to stdout, id is the RDD id """ stats = self.stats() if stats: print("=" * 60) print("Profile of RDD<id=%d>" % id) print("=" * 60) stats.sort_stats("time", "cumulative").print_stats()
python
def show(self, id): """ Print the profile stats to stdout, id is the RDD id """ stats = self.stats() if stats: print("=" * 60) print("Profile of RDD<id=%d>" % id) print("=" * 60) stats.sort_stats("time", "cumulative").print_stats()
[ "def", "show", "(", "self", ",", "id", ")", ":", "stats", "=", "self", ".", "stats", "(", ")", "if", "stats", ":", "print", "(", "\"=\"", "*", "60", ")", "print", "(", "\"Profile of RDD<id=%d>\"", "%", "id", ")", "print", "(", "\"=\"", "*", "60", ")", "stats", ".", "sort_stats", "(", "\"time\"", ",", "\"cumulative\"", ")", ".", "print_stats", "(", ")" ]
Print the profile stats to stdout, id is the RDD id
[ "Print", "the", "profile", "stats", "to", "stdout", "id", "is", "the", "RDD", "id" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/profiler.py#L113-L120
apache/spark
python/pyspark/profiler.py
Profiler.dump
def dump(self, id, path): """ Dump the profile into path, id is the RDD id """ if not os.path.exists(path): os.makedirs(path) stats = self.stats() if stats: p = os.path.join(path, "rdd_%d.pstats" % id) stats.dump_stats(p)
python
def dump(self, id, path): """ Dump the profile into path, id is the RDD id """ if not os.path.exists(path): os.makedirs(path) stats = self.stats() if stats: p = os.path.join(path, "rdd_%d.pstats" % id) stats.dump_stats(p)
[ "def", "dump", "(", "self", ",", "id", ",", "path", ")", ":", "if", "not", "os", ".", "path", ".", "exists", "(", "path", ")", ":", "os", ".", "makedirs", "(", "path", ")", "stats", "=", "self", ".", "stats", "(", ")", "if", "stats", ":", "p", "=", "os", ".", "path", ".", "join", "(", "path", ",", "\"rdd_%d.pstats\"", "%", "id", ")", "stats", ".", "dump_stats", "(", "p", ")" ]
Dump the profile into path, id is the RDD id
[ "Dump", "the", "profile", "into", "path", "id", "is", "the", "RDD", "id" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/profiler.py#L122-L129
apache/spark
python/pyspark/profiler.py
BasicProfiler.profile
def profile(self, func): """ Runs and profiles the method to_profile passed in. A profile object is returned. """ pr = cProfile.Profile() pr.runcall(func) st = pstats.Stats(pr) st.stream = None # make it picklable st.strip_dirs() # Adds a new profile to the existing accumulated value self._accumulator.add(st)
python
def profile(self, func): """ Runs and profiles the method to_profile passed in. A profile object is returned. """ pr = cProfile.Profile() pr.runcall(func) st = pstats.Stats(pr) st.stream = None # make it picklable st.strip_dirs() # Adds a new profile to the existing accumulated value self._accumulator.add(st)
[ "def", "profile", "(", "self", ",", "func", ")", ":", "pr", "=", "cProfile", ".", "Profile", "(", ")", "pr", ".", "runcall", "(", "func", ")", "st", "=", "pstats", ".", "Stats", "(", "pr", ")", "st", ".", "stream", "=", "None", "# make it picklable", "st", ".", "strip_dirs", "(", ")", "# Adds a new profile to the existing accumulated value", "self", ".", "_accumulator", ".", "add", "(", "st", ")" ]
Runs and profiles the method to_profile passed in. A profile object is returned.
[ "Runs", "and", "profiles", "the", "method", "to_profile", "passed", "in", ".", "A", "profile", "object", "is", "returned", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/profiler.py#L158-L167
apache/spark
python/pyspark/sql/context.py
SQLContext.getOrCreate
def getOrCreate(cls, sc): """ Get the existing SQLContext or create a new one with given SparkContext. :param sc: SparkContext """ if cls._instantiatedContext is None: jsqlContext = sc._jvm.SQLContext.getOrCreate(sc._jsc.sc()) sparkSession = SparkSession(sc, jsqlContext.sparkSession()) cls(sc, sparkSession, jsqlContext) return cls._instantiatedContext
python
def getOrCreate(cls, sc): """ Get the existing SQLContext or create a new one with given SparkContext. :param sc: SparkContext """ if cls._instantiatedContext is None: jsqlContext = sc._jvm.SQLContext.getOrCreate(sc._jsc.sc()) sparkSession = SparkSession(sc, jsqlContext.sparkSession()) cls(sc, sparkSession, jsqlContext) return cls._instantiatedContext
[ "def", "getOrCreate", "(", "cls", ",", "sc", ")", ":", "if", "cls", ".", "_instantiatedContext", "is", "None", ":", "jsqlContext", "=", "sc", ".", "_jvm", ".", "SQLContext", ".", "getOrCreate", "(", "sc", ".", "_jsc", ".", "sc", "(", ")", ")", "sparkSession", "=", "SparkSession", "(", "sc", ",", "jsqlContext", ".", "sparkSession", "(", ")", ")", "cls", "(", "sc", ",", "sparkSession", ",", "jsqlContext", ")", "return", "cls", ".", "_instantiatedContext" ]
Get the existing SQLContext or create a new one with given SparkContext. :param sc: SparkContext
[ "Get", "the", "existing", "SQLContext", "or", "create", "a", "new", "one", "with", "given", "SparkContext", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/context.py#L103-L113
apache/spark
python/pyspark/sql/context.py
SQLContext.setConf
def setConf(self, key, value): """Sets the given Spark SQL configuration property. """ self.sparkSession.conf.set(key, value)
python
def setConf(self, key, value): """Sets the given Spark SQL configuration property. """ self.sparkSession.conf.set(key, value)
[ "def", "setConf", "(", "self", ",", "key", ",", "value", ")", ":", "self", ".", "sparkSession", ".", "conf", ".", "set", "(", "key", ",", "value", ")" ]
Sets the given Spark SQL configuration property.
[ "Sets", "the", "given", "Spark", "SQL", "configuration", "property", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/context.py#L125-L128
apache/spark
python/pyspark/sql/context.py
SQLContext.getConf
def getConf(self, key, defaultValue=_NoValue): """Returns the value of Spark SQL configuration property for the given key. If the key is not set and defaultValue is set, return defaultValue. If the key is not set and defaultValue is not set, return the system default value. >>> sqlContext.getConf("spark.sql.shuffle.partitions") u'200' >>> sqlContext.getConf("spark.sql.shuffle.partitions", u"10") u'10' >>> sqlContext.setConf("spark.sql.shuffle.partitions", u"50") >>> sqlContext.getConf("spark.sql.shuffle.partitions", u"10") u'50' """ return self.sparkSession.conf.get(key, defaultValue)
python
def getConf(self, key, defaultValue=_NoValue): """Returns the value of Spark SQL configuration property for the given key. If the key is not set and defaultValue is set, return defaultValue. If the key is not set and defaultValue is not set, return the system default value. >>> sqlContext.getConf("spark.sql.shuffle.partitions") u'200' >>> sqlContext.getConf("spark.sql.shuffle.partitions", u"10") u'10' >>> sqlContext.setConf("spark.sql.shuffle.partitions", u"50") >>> sqlContext.getConf("spark.sql.shuffle.partitions", u"10") u'50' """ return self.sparkSession.conf.get(key, defaultValue)
[ "def", "getConf", "(", "self", ",", "key", ",", "defaultValue", "=", "_NoValue", ")", ":", "return", "self", ".", "sparkSession", ".", "conf", ".", "get", "(", "key", ",", "defaultValue", ")" ]
Returns the value of Spark SQL configuration property for the given key. If the key is not set and defaultValue is set, return defaultValue. If the key is not set and defaultValue is not set, return the system default value. >>> sqlContext.getConf("spark.sql.shuffle.partitions") u'200' >>> sqlContext.getConf("spark.sql.shuffle.partitions", u"10") u'10' >>> sqlContext.setConf("spark.sql.shuffle.partitions", u"50") >>> sqlContext.getConf("spark.sql.shuffle.partitions", u"10") u'50'
[ "Returns", "the", "value", "of", "Spark", "SQL", "configuration", "property", "for", "the", "given", "key", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/context.py#L132-L147
apache/spark
python/pyspark/sql/context.py
SQLContext.range
def range(self, start, end=None, step=1, numPartitions=None): """ Create a :class:`DataFrame` with single :class:`pyspark.sql.types.LongType` column named ``id``, containing elements in a range from ``start`` to ``end`` (exclusive) with step value ``step``. :param start: the start value :param end: the end value (exclusive) :param step: the incremental step (default: 1) :param numPartitions: the number of partitions of the DataFrame :return: :class:`DataFrame` >>> sqlContext.range(1, 7, 2).collect() [Row(id=1), Row(id=3), Row(id=5)] If only one argument is specified, it will be used as the end value. >>> sqlContext.range(3).collect() [Row(id=0), Row(id=1), Row(id=2)] """ return self.sparkSession.range(start, end, step, numPartitions)
python
def range(self, start, end=None, step=1, numPartitions=None): """ Create a :class:`DataFrame` with single :class:`pyspark.sql.types.LongType` column named ``id``, containing elements in a range from ``start`` to ``end`` (exclusive) with step value ``step``. :param start: the start value :param end: the end value (exclusive) :param step: the incremental step (default: 1) :param numPartitions: the number of partitions of the DataFrame :return: :class:`DataFrame` >>> sqlContext.range(1, 7, 2).collect() [Row(id=1), Row(id=3), Row(id=5)] If only one argument is specified, it will be used as the end value. >>> sqlContext.range(3).collect() [Row(id=0), Row(id=1), Row(id=2)] """ return self.sparkSession.range(start, end, step, numPartitions)
[ "def", "range", "(", "self", ",", "start", ",", "end", "=", "None", ",", "step", "=", "1", ",", "numPartitions", "=", "None", ")", ":", "return", "self", ".", "sparkSession", ".", "range", "(", "start", ",", "end", ",", "step", ",", "numPartitions", ")" ]
Create a :class:`DataFrame` with single :class:`pyspark.sql.types.LongType` column named ``id``, containing elements in a range from ``start`` to ``end`` (exclusive) with step value ``step``. :param start: the start value :param end: the end value (exclusive) :param step: the incremental step (default: 1) :param numPartitions: the number of partitions of the DataFrame :return: :class:`DataFrame` >>> sqlContext.range(1, 7, 2).collect() [Row(id=1), Row(id=3), Row(id=5)] If only one argument is specified, it will be used as the end value. >>> sqlContext.range(3).collect() [Row(id=0), Row(id=1), Row(id=2)]
[ "Create", "a", ":", "class", ":", "DataFrame", "with", "single", ":", "class", ":", "pyspark", ".", "sql", ".", "types", ".", "LongType", "column", "named", "id", "containing", "elements", "in", "a", "range", "from", "start", "to", "end", "(", "exclusive", ")", "with", "step", "value", "step", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/context.py#L159-L179
apache/spark
python/pyspark/sql/context.py
SQLContext.registerFunction
def registerFunction(self, name, f, returnType=None): """An alias for :func:`spark.udf.register`. See :meth:`pyspark.sql.UDFRegistration.register`. .. note:: Deprecated in 2.3.0. Use :func:`spark.udf.register` instead. """ warnings.warn( "Deprecated in 2.3.0. Use spark.udf.register instead.", DeprecationWarning) return self.sparkSession.udf.register(name, f, returnType)
python
def registerFunction(self, name, f, returnType=None): """An alias for :func:`spark.udf.register`. See :meth:`pyspark.sql.UDFRegistration.register`. .. note:: Deprecated in 2.3.0. Use :func:`spark.udf.register` instead. """ warnings.warn( "Deprecated in 2.3.0. Use spark.udf.register instead.", DeprecationWarning) return self.sparkSession.udf.register(name, f, returnType)
[ "def", "registerFunction", "(", "self", ",", "name", ",", "f", ",", "returnType", "=", "None", ")", ":", "warnings", ".", "warn", "(", "\"Deprecated in 2.3.0. Use spark.udf.register instead.\"", ",", "DeprecationWarning", ")", "return", "self", ".", "sparkSession", ".", "udf", ".", "register", "(", "name", ",", "f", ",", "returnType", ")" ]
An alias for :func:`spark.udf.register`. See :meth:`pyspark.sql.UDFRegistration.register`. .. note:: Deprecated in 2.3.0. Use :func:`spark.udf.register` instead.
[ "An", "alias", "for", ":", "func", ":", "spark", ".", "udf", ".", "register", ".", "See", ":", "meth", ":", "pyspark", ".", "sql", ".", "UDFRegistration", ".", "register", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/context.py#L182-L191
apache/spark
python/pyspark/sql/context.py
SQLContext.registerJavaFunction
def registerJavaFunction(self, name, javaClassName, returnType=None): """An alias for :func:`spark.udf.registerJavaFunction`. See :meth:`pyspark.sql.UDFRegistration.registerJavaFunction`. .. note:: Deprecated in 2.3.0. Use :func:`spark.udf.registerJavaFunction` instead. """ warnings.warn( "Deprecated in 2.3.0. Use spark.udf.registerJavaFunction instead.", DeprecationWarning) return self.sparkSession.udf.registerJavaFunction(name, javaClassName, returnType)
python
def registerJavaFunction(self, name, javaClassName, returnType=None): """An alias for :func:`spark.udf.registerJavaFunction`. See :meth:`pyspark.sql.UDFRegistration.registerJavaFunction`. .. note:: Deprecated in 2.3.0. Use :func:`spark.udf.registerJavaFunction` instead. """ warnings.warn( "Deprecated in 2.3.0. Use spark.udf.registerJavaFunction instead.", DeprecationWarning) return self.sparkSession.udf.registerJavaFunction(name, javaClassName, returnType)
[ "def", "registerJavaFunction", "(", "self", ",", "name", ",", "javaClassName", ",", "returnType", "=", "None", ")", ":", "warnings", ".", "warn", "(", "\"Deprecated in 2.3.0. Use spark.udf.registerJavaFunction instead.\"", ",", "DeprecationWarning", ")", "return", "self", ".", "sparkSession", ".", "udf", ".", "registerJavaFunction", "(", "name", ",", "javaClassName", ",", "returnType", ")" ]
An alias for :func:`spark.udf.registerJavaFunction`. See :meth:`pyspark.sql.UDFRegistration.registerJavaFunction`. .. note:: Deprecated in 2.3.0. Use :func:`spark.udf.registerJavaFunction` instead.
[ "An", "alias", "for", ":", "func", ":", "spark", ".", "udf", ".", "registerJavaFunction", ".", "See", ":", "meth", ":", "pyspark", ".", "sql", ".", "UDFRegistration", ".", "registerJavaFunction", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/context.py#L194-L203
apache/spark
python/pyspark/sql/context.py
SQLContext.createDataFrame
def createDataFrame(self, data, schema=None, samplingRatio=None, verifySchema=True): """ Creates a :class:`DataFrame` from an :class:`RDD`, a list or a :class:`pandas.DataFrame`. When ``schema`` is a list of column names, the type of each column will be inferred from ``data``. When ``schema`` is ``None``, it will try to infer the schema (column names and types) from ``data``, which should be an RDD of :class:`Row`, or :class:`namedtuple`, or :class:`dict`. When ``schema`` is :class:`pyspark.sql.types.DataType` or a datatype string it must match the real data, or an exception will be thrown at runtime. If the given schema is not :class:`pyspark.sql.types.StructType`, it will be wrapped into a :class:`pyspark.sql.types.StructType` as its only field, and the field name will be "value", each record will also be wrapped into a tuple, which can be converted to row later. If schema inference is needed, ``samplingRatio`` is used to determined the ratio of rows used for schema inference. The first row will be used if ``samplingRatio`` is ``None``. :param data: an RDD of any kind of SQL data representation(e.g. :class:`Row`, :class:`tuple`, ``int``, ``boolean``, etc.), or :class:`list`, or :class:`pandas.DataFrame`. :param schema: a :class:`pyspark.sql.types.DataType` or a datatype string or a list of column names, default is None. The data type string format equals to :class:`pyspark.sql.types.DataType.simpleString`, except that top level struct type can omit the ``struct<>`` and atomic types use ``typeName()`` as their format, e.g. use ``byte`` instead of ``tinyint`` for :class:`pyspark.sql.types.ByteType`. We can also use ``int`` as a short name for :class:`pyspark.sql.types.IntegerType`. :param samplingRatio: the sample ratio of rows used for inferring :param verifySchema: verify data types of every row against schema. :return: :class:`DataFrame` .. versionchanged:: 2.0 The ``schema`` parameter can be a :class:`pyspark.sql.types.DataType` or a datatype string after 2.0. If it's not a :class:`pyspark.sql.types.StructType`, it will be wrapped into a :class:`pyspark.sql.types.StructType` and each record will also be wrapped into a tuple. .. versionchanged:: 2.1 Added verifySchema. >>> l = [('Alice', 1)] >>> sqlContext.createDataFrame(l).collect() [Row(_1=u'Alice', _2=1)] >>> sqlContext.createDataFrame(l, ['name', 'age']).collect() [Row(name=u'Alice', age=1)] >>> d = [{'name': 'Alice', 'age': 1}] >>> sqlContext.createDataFrame(d).collect() [Row(age=1, name=u'Alice')] >>> rdd = sc.parallelize(l) >>> sqlContext.createDataFrame(rdd).collect() [Row(_1=u'Alice', _2=1)] >>> df = sqlContext.createDataFrame(rdd, ['name', 'age']) >>> df.collect() [Row(name=u'Alice', age=1)] >>> from pyspark.sql import Row >>> Person = Row('name', 'age') >>> person = rdd.map(lambda r: Person(*r)) >>> df2 = sqlContext.createDataFrame(person) >>> df2.collect() [Row(name=u'Alice', age=1)] >>> from pyspark.sql.types import * >>> schema = StructType([ ... StructField("name", StringType(), True), ... StructField("age", IntegerType(), True)]) >>> df3 = sqlContext.createDataFrame(rdd, schema) >>> df3.collect() [Row(name=u'Alice', age=1)] >>> sqlContext.createDataFrame(df.toPandas()).collect() # doctest: +SKIP [Row(name=u'Alice', age=1)] >>> sqlContext.createDataFrame(pandas.DataFrame([[1, 2]])).collect() # doctest: +SKIP [Row(0=1, 1=2)] >>> sqlContext.createDataFrame(rdd, "a: string, b: int").collect() [Row(a=u'Alice', b=1)] >>> rdd = rdd.map(lambda row: row[1]) >>> sqlContext.createDataFrame(rdd, "int").collect() [Row(value=1)] >>> sqlContext.createDataFrame(rdd, "boolean").collect() # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... Py4JJavaError: ... """ return self.sparkSession.createDataFrame(data, schema, samplingRatio, verifySchema)
python
def createDataFrame(self, data, schema=None, samplingRatio=None, verifySchema=True): """ Creates a :class:`DataFrame` from an :class:`RDD`, a list or a :class:`pandas.DataFrame`. When ``schema`` is a list of column names, the type of each column will be inferred from ``data``. When ``schema`` is ``None``, it will try to infer the schema (column names and types) from ``data``, which should be an RDD of :class:`Row`, or :class:`namedtuple`, or :class:`dict`. When ``schema`` is :class:`pyspark.sql.types.DataType` or a datatype string it must match the real data, or an exception will be thrown at runtime. If the given schema is not :class:`pyspark.sql.types.StructType`, it will be wrapped into a :class:`pyspark.sql.types.StructType` as its only field, and the field name will be "value", each record will also be wrapped into a tuple, which can be converted to row later. If schema inference is needed, ``samplingRatio`` is used to determined the ratio of rows used for schema inference. The first row will be used if ``samplingRatio`` is ``None``. :param data: an RDD of any kind of SQL data representation(e.g. :class:`Row`, :class:`tuple`, ``int``, ``boolean``, etc.), or :class:`list`, or :class:`pandas.DataFrame`. :param schema: a :class:`pyspark.sql.types.DataType` or a datatype string or a list of column names, default is None. The data type string format equals to :class:`pyspark.sql.types.DataType.simpleString`, except that top level struct type can omit the ``struct<>`` and atomic types use ``typeName()`` as their format, e.g. use ``byte`` instead of ``tinyint`` for :class:`pyspark.sql.types.ByteType`. We can also use ``int`` as a short name for :class:`pyspark.sql.types.IntegerType`. :param samplingRatio: the sample ratio of rows used for inferring :param verifySchema: verify data types of every row against schema. :return: :class:`DataFrame` .. versionchanged:: 2.0 The ``schema`` parameter can be a :class:`pyspark.sql.types.DataType` or a datatype string after 2.0. If it's not a :class:`pyspark.sql.types.StructType`, it will be wrapped into a :class:`pyspark.sql.types.StructType` and each record will also be wrapped into a tuple. .. versionchanged:: 2.1 Added verifySchema. >>> l = [('Alice', 1)] >>> sqlContext.createDataFrame(l).collect() [Row(_1=u'Alice', _2=1)] >>> sqlContext.createDataFrame(l, ['name', 'age']).collect() [Row(name=u'Alice', age=1)] >>> d = [{'name': 'Alice', 'age': 1}] >>> sqlContext.createDataFrame(d).collect() [Row(age=1, name=u'Alice')] >>> rdd = sc.parallelize(l) >>> sqlContext.createDataFrame(rdd).collect() [Row(_1=u'Alice', _2=1)] >>> df = sqlContext.createDataFrame(rdd, ['name', 'age']) >>> df.collect() [Row(name=u'Alice', age=1)] >>> from pyspark.sql import Row >>> Person = Row('name', 'age') >>> person = rdd.map(lambda r: Person(*r)) >>> df2 = sqlContext.createDataFrame(person) >>> df2.collect() [Row(name=u'Alice', age=1)] >>> from pyspark.sql.types import * >>> schema = StructType([ ... StructField("name", StringType(), True), ... StructField("age", IntegerType(), True)]) >>> df3 = sqlContext.createDataFrame(rdd, schema) >>> df3.collect() [Row(name=u'Alice', age=1)] >>> sqlContext.createDataFrame(df.toPandas()).collect() # doctest: +SKIP [Row(name=u'Alice', age=1)] >>> sqlContext.createDataFrame(pandas.DataFrame([[1, 2]])).collect() # doctest: +SKIP [Row(0=1, 1=2)] >>> sqlContext.createDataFrame(rdd, "a: string, b: int").collect() [Row(a=u'Alice', b=1)] >>> rdd = rdd.map(lambda row: row[1]) >>> sqlContext.createDataFrame(rdd, "int").collect() [Row(value=1)] >>> sqlContext.createDataFrame(rdd, "boolean").collect() # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... Py4JJavaError: ... """ return self.sparkSession.createDataFrame(data, schema, samplingRatio, verifySchema)
[ "def", "createDataFrame", "(", "self", ",", "data", ",", "schema", "=", "None", ",", "samplingRatio", "=", "None", ",", "verifySchema", "=", "True", ")", ":", "return", "self", ".", "sparkSession", ".", "createDataFrame", "(", "data", ",", "schema", ",", "samplingRatio", ",", "verifySchema", ")" ]
Creates a :class:`DataFrame` from an :class:`RDD`, a list or a :class:`pandas.DataFrame`. When ``schema`` is a list of column names, the type of each column will be inferred from ``data``. When ``schema`` is ``None``, it will try to infer the schema (column names and types) from ``data``, which should be an RDD of :class:`Row`, or :class:`namedtuple`, or :class:`dict`. When ``schema`` is :class:`pyspark.sql.types.DataType` or a datatype string it must match the real data, or an exception will be thrown at runtime. If the given schema is not :class:`pyspark.sql.types.StructType`, it will be wrapped into a :class:`pyspark.sql.types.StructType` as its only field, and the field name will be "value", each record will also be wrapped into a tuple, which can be converted to row later. If schema inference is needed, ``samplingRatio`` is used to determined the ratio of rows used for schema inference. The first row will be used if ``samplingRatio`` is ``None``. :param data: an RDD of any kind of SQL data representation(e.g. :class:`Row`, :class:`tuple`, ``int``, ``boolean``, etc.), or :class:`list`, or :class:`pandas.DataFrame`. :param schema: a :class:`pyspark.sql.types.DataType` or a datatype string or a list of column names, default is None. The data type string format equals to :class:`pyspark.sql.types.DataType.simpleString`, except that top level struct type can omit the ``struct<>`` and atomic types use ``typeName()`` as their format, e.g. use ``byte`` instead of ``tinyint`` for :class:`pyspark.sql.types.ByteType`. We can also use ``int`` as a short name for :class:`pyspark.sql.types.IntegerType`. :param samplingRatio: the sample ratio of rows used for inferring :param verifySchema: verify data types of every row against schema. :return: :class:`DataFrame` .. versionchanged:: 2.0 The ``schema`` parameter can be a :class:`pyspark.sql.types.DataType` or a datatype string after 2.0. If it's not a :class:`pyspark.sql.types.StructType`, it will be wrapped into a :class:`pyspark.sql.types.StructType` and each record will also be wrapped into a tuple. .. versionchanged:: 2.1 Added verifySchema. >>> l = [('Alice', 1)] >>> sqlContext.createDataFrame(l).collect() [Row(_1=u'Alice', _2=1)] >>> sqlContext.createDataFrame(l, ['name', 'age']).collect() [Row(name=u'Alice', age=1)] >>> d = [{'name': 'Alice', 'age': 1}] >>> sqlContext.createDataFrame(d).collect() [Row(age=1, name=u'Alice')] >>> rdd = sc.parallelize(l) >>> sqlContext.createDataFrame(rdd).collect() [Row(_1=u'Alice', _2=1)] >>> df = sqlContext.createDataFrame(rdd, ['name', 'age']) >>> df.collect() [Row(name=u'Alice', age=1)] >>> from pyspark.sql import Row >>> Person = Row('name', 'age') >>> person = rdd.map(lambda r: Person(*r)) >>> df2 = sqlContext.createDataFrame(person) >>> df2.collect() [Row(name=u'Alice', age=1)] >>> from pyspark.sql.types import * >>> schema = StructType([ ... StructField("name", StringType(), True), ... StructField("age", IntegerType(), True)]) >>> df3 = sqlContext.createDataFrame(rdd, schema) >>> df3.collect() [Row(name=u'Alice', age=1)] >>> sqlContext.createDataFrame(df.toPandas()).collect() # doctest: +SKIP [Row(name=u'Alice', age=1)] >>> sqlContext.createDataFrame(pandas.DataFrame([[1, 2]])).collect() # doctest: +SKIP [Row(0=1, 1=2)] >>> sqlContext.createDataFrame(rdd, "a: string, b: int").collect() [Row(a=u'Alice', b=1)] >>> rdd = rdd.map(lambda row: row[1]) >>> sqlContext.createDataFrame(rdd, "int").collect() [Row(value=1)] >>> sqlContext.createDataFrame(rdd, "boolean").collect() # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... Py4JJavaError: ...
[ "Creates", "a", ":", "class", ":", "DataFrame", "from", "an", ":", "class", ":", "RDD", "a", "list", "or", "a", ":", "class", ":", "pandas", ".", "DataFrame", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/context.py#L218-L307
apache/spark
python/pyspark/sql/context.py
SQLContext.createExternalTable
def createExternalTable(self, tableName, path=None, source=None, schema=None, **options): """Creates an external table based on the dataset in a data source. It returns the DataFrame associated with the external table. The data source is specified by the ``source`` and a set of ``options``. If ``source`` is not specified, the default data source configured by ``spark.sql.sources.default`` will be used. Optionally, a schema can be provided as the schema of the returned :class:`DataFrame` and created external table. :return: :class:`DataFrame` """ return self.sparkSession.catalog.createExternalTable( tableName, path, source, schema, **options)
python
def createExternalTable(self, tableName, path=None, source=None, schema=None, **options): """Creates an external table based on the dataset in a data source. It returns the DataFrame associated with the external table. The data source is specified by the ``source`` and a set of ``options``. If ``source`` is not specified, the default data source configured by ``spark.sql.sources.default`` will be used. Optionally, a schema can be provided as the schema of the returned :class:`DataFrame` and created external table. :return: :class:`DataFrame` """ return self.sparkSession.catalog.createExternalTable( tableName, path, source, schema, **options)
[ "def", "createExternalTable", "(", "self", ",", "tableName", ",", "path", "=", "None", ",", "source", "=", "None", ",", "schema", "=", "None", ",", "*", "*", "options", ")", ":", "return", "self", ".", "sparkSession", ".", "catalog", ".", "createExternalTable", "(", "tableName", ",", "path", ",", "source", ",", "schema", ",", "*", "*", "options", ")" ]
Creates an external table based on the dataset in a data source. It returns the DataFrame associated with the external table. The data source is specified by the ``source`` and a set of ``options``. If ``source`` is not specified, the default data source configured by ``spark.sql.sources.default`` will be used. Optionally, a schema can be provided as the schema of the returned :class:`DataFrame` and created external table. :return: :class:`DataFrame`
[ "Creates", "an", "external", "table", "based", "on", "the", "dataset", "in", "a", "data", "source", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/context.py#L329-L344
apache/spark
python/pyspark/sql/context.py
SQLContext.tables
def tables(self, dbName=None): """Returns a :class:`DataFrame` containing names of tables in the given database. If ``dbName`` is not specified, the current database will be used. The returned DataFrame has two columns: ``tableName`` and ``isTemporary`` (a column with :class:`BooleanType` indicating if a table is a temporary one or not). :param dbName: string, name of the database to use. :return: :class:`DataFrame` >>> sqlContext.registerDataFrameAsTable(df, "table1") >>> df2 = sqlContext.tables() >>> df2.filter("tableName = 'table1'").first() Row(database=u'', tableName=u'table1', isTemporary=True) """ if dbName is None: return DataFrame(self._ssql_ctx.tables(), self) else: return DataFrame(self._ssql_ctx.tables(dbName), self)
python
def tables(self, dbName=None): """Returns a :class:`DataFrame` containing names of tables in the given database. If ``dbName`` is not specified, the current database will be used. The returned DataFrame has two columns: ``tableName`` and ``isTemporary`` (a column with :class:`BooleanType` indicating if a table is a temporary one or not). :param dbName: string, name of the database to use. :return: :class:`DataFrame` >>> sqlContext.registerDataFrameAsTable(df, "table1") >>> df2 = sqlContext.tables() >>> df2.filter("tableName = 'table1'").first() Row(database=u'', tableName=u'table1', isTemporary=True) """ if dbName is None: return DataFrame(self._ssql_ctx.tables(), self) else: return DataFrame(self._ssql_ctx.tables(dbName), self)
[ "def", "tables", "(", "self", ",", "dbName", "=", "None", ")", ":", "if", "dbName", "is", "None", ":", "return", "DataFrame", "(", "self", ".", "_ssql_ctx", ".", "tables", "(", ")", ",", "self", ")", "else", ":", "return", "DataFrame", "(", "self", ".", "_ssql_ctx", ".", "tables", "(", "dbName", ")", ",", "self", ")" ]
Returns a :class:`DataFrame` containing names of tables in the given database. If ``dbName`` is not specified, the current database will be used. The returned DataFrame has two columns: ``tableName`` and ``isTemporary`` (a column with :class:`BooleanType` indicating if a table is a temporary one or not). :param dbName: string, name of the database to use. :return: :class:`DataFrame` >>> sqlContext.registerDataFrameAsTable(df, "table1") >>> df2 = sqlContext.tables() >>> df2.filter("tableName = 'table1'").first() Row(database=u'', tableName=u'table1', isTemporary=True)
[ "Returns", "a", ":", "class", ":", "DataFrame", "containing", "names", "of", "tables", "in", "the", "given", "database", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/context.py#L375-L394
apache/spark
python/pyspark/sql/context.py
SQLContext.tableNames
def tableNames(self, dbName=None): """Returns a list of names of tables in the database ``dbName``. :param dbName: string, name of the database to use. Default to the current database. :return: list of table names, in string >>> sqlContext.registerDataFrameAsTable(df, "table1") >>> "table1" in sqlContext.tableNames() True >>> "table1" in sqlContext.tableNames("default") True """ if dbName is None: return [name for name in self._ssql_ctx.tableNames()] else: return [name for name in self._ssql_ctx.tableNames(dbName)]
python
def tableNames(self, dbName=None): """Returns a list of names of tables in the database ``dbName``. :param dbName: string, name of the database to use. Default to the current database. :return: list of table names, in string >>> sqlContext.registerDataFrameAsTable(df, "table1") >>> "table1" in sqlContext.tableNames() True >>> "table1" in sqlContext.tableNames("default") True """ if dbName is None: return [name for name in self._ssql_ctx.tableNames()] else: return [name for name in self._ssql_ctx.tableNames(dbName)]
[ "def", "tableNames", "(", "self", ",", "dbName", "=", "None", ")", ":", "if", "dbName", "is", "None", ":", "return", "[", "name", "for", "name", "in", "self", ".", "_ssql_ctx", ".", "tableNames", "(", ")", "]", "else", ":", "return", "[", "name", "for", "name", "in", "self", ".", "_ssql_ctx", ".", "tableNames", "(", "dbName", ")", "]" ]
Returns a list of names of tables in the database ``dbName``. :param dbName: string, name of the database to use. Default to the current database. :return: list of table names, in string >>> sqlContext.registerDataFrameAsTable(df, "table1") >>> "table1" in sqlContext.tableNames() True >>> "table1" in sqlContext.tableNames("default") True
[ "Returns", "a", "list", "of", "names", "of", "tables", "in", "the", "database", "dbName", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/context.py#L397-L412
apache/spark
python/pyspark/sql/context.py
SQLContext.streams
def streams(self): """Returns a :class:`StreamingQueryManager` that allows managing all the :class:`StreamingQuery` StreamingQueries active on `this` context. .. note:: Evolving. """ from pyspark.sql.streaming import StreamingQueryManager return StreamingQueryManager(self._ssql_ctx.streams())
python
def streams(self): """Returns a :class:`StreamingQueryManager` that allows managing all the :class:`StreamingQuery` StreamingQueries active on `this` context. .. note:: Evolving. """ from pyspark.sql.streaming import StreamingQueryManager return StreamingQueryManager(self._ssql_ctx.streams())
[ "def", "streams", "(", "self", ")", ":", "from", "pyspark", ".", "sql", ".", "streaming", "import", "StreamingQueryManager", "return", "StreamingQueryManager", "(", "self", ".", "_ssql_ctx", ".", "streams", "(", ")", ")" ]
Returns a :class:`StreamingQueryManager` that allows managing all the :class:`StreamingQuery` StreamingQueries active on `this` context. .. note:: Evolving.
[ "Returns", "a", ":", "class", ":", "StreamingQueryManager", "that", "allows", "managing", "all", "the", ":", "class", ":", "StreamingQuery", "StreamingQueries", "active", "on", "this", "context", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/context.py#L459-L466
apache/spark
python/pyspark/sql/avro/functions.py
from_avro
def from_avro(data, jsonFormatSchema, options={}): """ Converts a binary column of avro format into its corresponding catalyst value. The specified schema must match the read data, otherwise the behavior is undefined: it may fail or return arbitrary result. Note: Avro is built-in but external data source module since Spark 2.4. Please deploy the application as per the deployment section of "Apache Avro Data Source Guide". :param data: the binary column. :param jsonFormatSchema: the avro schema in JSON string format. :param options: options to control how the Avro record is parsed. >>> from pyspark.sql import Row >>> from pyspark.sql.avro.functions import from_avro, to_avro >>> data = [(1, Row(name='Alice', age=2))] >>> df = spark.createDataFrame(data, ("key", "value")) >>> avroDf = df.select(to_avro(df.value).alias("avro")) >>> avroDf.collect() [Row(avro=bytearray(b'\\x00\\x00\\x04\\x00\\nAlice'))] >>> jsonFormatSchema = '''{"type":"record","name":"topLevelRecord","fields": ... [{"name":"avro","type":[{"type":"record","name":"value","namespace":"topLevelRecord", ... "fields":[{"name":"age","type":["long","null"]}, ... {"name":"name","type":["string","null"]}]},"null"]}]}''' >>> avroDf.select(from_avro(avroDf.avro, jsonFormatSchema).alias("value")).collect() [Row(value=Row(avro=Row(age=2, name=u'Alice')))] """ sc = SparkContext._active_spark_context try: jc = sc._jvm.org.apache.spark.sql.avro.functions.from_avro( _to_java_column(data), jsonFormatSchema, options) except TypeError as e: if str(e) == "'JavaPackage' object is not callable": _print_missing_jar("Avro", "avro", "avro", sc.version) raise return Column(jc)
python
def from_avro(data, jsonFormatSchema, options={}): """ Converts a binary column of avro format into its corresponding catalyst value. The specified schema must match the read data, otherwise the behavior is undefined: it may fail or return arbitrary result. Note: Avro is built-in but external data source module since Spark 2.4. Please deploy the application as per the deployment section of "Apache Avro Data Source Guide". :param data: the binary column. :param jsonFormatSchema: the avro schema in JSON string format. :param options: options to control how the Avro record is parsed. >>> from pyspark.sql import Row >>> from pyspark.sql.avro.functions import from_avro, to_avro >>> data = [(1, Row(name='Alice', age=2))] >>> df = spark.createDataFrame(data, ("key", "value")) >>> avroDf = df.select(to_avro(df.value).alias("avro")) >>> avroDf.collect() [Row(avro=bytearray(b'\\x00\\x00\\x04\\x00\\nAlice'))] >>> jsonFormatSchema = '''{"type":"record","name":"topLevelRecord","fields": ... [{"name":"avro","type":[{"type":"record","name":"value","namespace":"topLevelRecord", ... "fields":[{"name":"age","type":["long","null"]}, ... {"name":"name","type":["string","null"]}]},"null"]}]}''' >>> avroDf.select(from_avro(avroDf.avro, jsonFormatSchema).alias("value")).collect() [Row(value=Row(avro=Row(age=2, name=u'Alice')))] """ sc = SparkContext._active_spark_context try: jc = sc._jvm.org.apache.spark.sql.avro.functions.from_avro( _to_java_column(data), jsonFormatSchema, options) except TypeError as e: if str(e) == "'JavaPackage' object is not callable": _print_missing_jar("Avro", "avro", "avro", sc.version) raise return Column(jc)
[ "def", "from_avro", "(", "data", ",", "jsonFormatSchema", ",", "options", "=", "{", "}", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "try", ":", "jc", "=", "sc", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "sql", ".", "avro", ".", "functions", ".", "from_avro", "(", "_to_java_column", "(", "data", ")", ",", "jsonFormatSchema", ",", "options", ")", "except", "TypeError", "as", "e", ":", "if", "str", "(", "e", ")", "==", "\"'JavaPackage' object is not callable\"", ":", "_print_missing_jar", "(", "\"Avro\"", ",", "\"avro\"", ",", "\"avro\"", ",", "sc", ".", "version", ")", "raise", "return", "Column", "(", "jc", ")" ]
Converts a binary column of avro format into its corresponding catalyst value. The specified schema must match the read data, otherwise the behavior is undefined: it may fail or return arbitrary result. Note: Avro is built-in but external data source module since Spark 2.4. Please deploy the application as per the deployment section of "Apache Avro Data Source Guide". :param data: the binary column. :param jsonFormatSchema: the avro schema in JSON string format. :param options: options to control how the Avro record is parsed. >>> from pyspark.sql import Row >>> from pyspark.sql.avro.functions import from_avro, to_avro >>> data = [(1, Row(name='Alice', age=2))] >>> df = spark.createDataFrame(data, ("key", "value")) >>> avroDf = df.select(to_avro(df.value).alias("avro")) >>> avroDf.collect() [Row(avro=bytearray(b'\\x00\\x00\\x04\\x00\\nAlice'))] >>> jsonFormatSchema = '''{"type":"record","name":"topLevelRecord","fields": ... [{"name":"avro","type":[{"type":"record","name":"value","namespace":"topLevelRecord", ... "fields":[{"name":"age","type":["long","null"]}, ... {"name":"name","type":["string","null"]}]},"null"]}]}''' >>> avroDf.select(from_avro(avroDf.avro, jsonFormatSchema).alias("value")).collect() [Row(value=Row(avro=Row(age=2, name=u'Alice')))]
[ "Converts", "a", "binary", "column", "of", "avro", "format", "into", "its", "corresponding", "catalyst", "value", ".", "The", "specified", "schema", "must", "match", "the", "read", "data", "otherwise", "the", "behavior", "is", "undefined", ":", "it", "may", "fail", "or", "return", "arbitrary", "result", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/avro/functions.py#L31-L67
apache/spark
python/pyspark/files.py
SparkFiles.get
def get(cls, filename): """ Get the absolute path of a file added through C{SparkContext.addFile()}. """ path = os.path.join(SparkFiles.getRootDirectory(), filename) return os.path.abspath(path)
python
def get(cls, filename): """ Get the absolute path of a file added through C{SparkContext.addFile()}. """ path = os.path.join(SparkFiles.getRootDirectory(), filename) return os.path.abspath(path)
[ "def", "get", "(", "cls", ",", "filename", ")", ":", "path", "=", "os", ".", "path", ".", "join", "(", "SparkFiles", ".", "getRootDirectory", "(", ")", ",", "filename", ")", "return", "os", ".", "path", ".", "abspath", "(", "path", ")" ]
Get the absolute path of a file added through C{SparkContext.addFile()}.
[ "Get", "the", "absolute", "path", "of", "a", "file", "added", "through", "C", "{", "SparkContext", ".", "addFile", "()", "}", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/files.py#L42-L47
apache/spark
python/pyspark/files.py
SparkFiles.getRootDirectory
def getRootDirectory(cls): """ Get the root directory that contains files added through C{SparkContext.addFile()}. """ if cls._is_running_on_worker: return cls._root_directory else: # This will have to change if we support multiple SparkContexts: return cls._sc._jvm.org.apache.spark.SparkFiles.getRootDirectory()
python
def getRootDirectory(cls): """ Get the root directory that contains files added through C{SparkContext.addFile()}. """ if cls._is_running_on_worker: return cls._root_directory else: # This will have to change if we support multiple SparkContexts: return cls._sc._jvm.org.apache.spark.SparkFiles.getRootDirectory()
[ "def", "getRootDirectory", "(", "cls", ")", ":", "if", "cls", ".", "_is_running_on_worker", ":", "return", "cls", ".", "_root_directory", "else", ":", "# This will have to change if we support multiple SparkContexts:", "return", "cls", ".", "_sc", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "SparkFiles", ".", "getRootDirectory", "(", ")" ]
Get the root directory that contains files added through C{SparkContext.addFile()}.
[ "Get", "the", "root", "directory", "that", "contains", "files", "added", "through", "C", "{", "SparkContext", ".", "addFile", "()", "}", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/files.py#L50-L59
apache/spark
python/pyspark/ml/classification.py
LogisticRegressionModel.summary
def summary(self): """ Gets summary (e.g. accuracy/precision/recall, objective history, total iterations) of model trained on the training set. An exception is thrown if `trainingSummary is None`. """ if self.hasSummary: if self.numClasses <= 2: return BinaryLogisticRegressionTrainingSummary(super(LogisticRegressionModel, self).summary) else: return LogisticRegressionTrainingSummary(super(LogisticRegressionModel, self).summary) else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
python
def summary(self): """ Gets summary (e.g. accuracy/precision/recall, objective history, total iterations) of model trained on the training set. An exception is thrown if `trainingSummary is None`. """ if self.hasSummary: if self.numClasses <= 2: return BinaryLogisticRegressionTrainingSummary(super(LogisticRegressionModel, self).summary) else: return LogisticRegressionTrainingSummary(super(LogisticRegressionModel, self).summary) else: raise RuntimeError("No training summary available for this %s" % self.__class__.__name__)
[ "def", "summary", "(", "self", ")", ":", "if", "self", ".", "hasSummary", ":", "if", "self", ".", "numClasses", "<=", "2", ":", "return", "BinaryLogisticRegressionTrainingSummary", "(", "super", "(", "LogisticRegressionModel", ",", "self", ")", ".", "summary", ")", "else", ":", "return", "LogisticRegressionTrainingSummary", "(", "super", "(", "LogisticRegressionModel", ",", "self", ")", ".", "summary", ")", "else", ":", "raise", "RuntimeError", "(", "\"No training summary available for this %s\"", "%", "self", ".", "__class__", ".", "__name__", ")" ]
Gets summary (e.g. accuracy/precision/recall, objective history, total iterations) of model trained on the training set. An exception is thrown if `trainingSummary is None`.
[ "Gets", "summary", "(", "e", ".", "g", ".", "accuracy", "/", "precision", "/", "recall", "objective", "history", "total", "iterations", ")", "of", "model", "trained", "on", "the", "training", "set", ".", "An", "exception", "is", "thrown", "if", "trainingSummary", "is", "None", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/classification.py#L531-L545
apache/spark
python/pyspark/ml/classification.py
LogisticRegressionModel.evaluate
def evaluate(self, dataset): """ Evaluates the model on a test dataset. :param dataset: Test dataset to evaluate model on, where dataset is an instance of :py:class:`pyspark.sql.DataFrame` """ if not isinstance(dataset, DataFrame): raise ValueError("dataset must be a DataFrame but got %s." % type(dataset)) java_blr_summary = self._call_java("evaluate", dataset) return BinaryLogisticRegressionSummary(java_blr_summary)
python
def evaluate(self, dataset): """ Evaluates the model on a test dataset. :param dataset: Test dataset to evaluate model on, where dataset is an instance of :py:class:`pyspark.sql.DataFrame` """ if not isinstance(dataset, DataFrame): raise ValueError("dataset must be a DataFrame but got %s." % type(dataset)) java_blr_summary = self._call_java("evaluate", dataset) return BinaryLogisticRegressionSummary(java_blr_summary)
[ "def", "evaluate", "(", "self", ",", "dataset", ")", ":", "if", "not", "isinstance", "(", "dataset", ",", "DataFrame", ")", ":", "raise", "ValueError", "(", "\"dataset must be a DataFrame but got %s.\"", "%", "type", "(", "dataset", ")", ")", "java_blr_summary", "=", "self", ".", "_call_java", "(", "\"evaluate\"", ",", "dataset", ")", "return", "BinaryLogisticRegressionSummary", "(", "java_blr_summary", ")" ]
Evaluates the model on a test dataset. :param dataset: Test dataset to evaluate model on, where dataset is an instance of :py:class:`pyspark.sql.DataFrame`
[ "Evaluates", "the", "model", "on", "a", "test", "dataset", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/classification.py#L548-L559
apache/spark
python/pyspark/ml/classification.py
OneVsRestModel.copy
def copy(self, extra=None): """ Creates a copy of this instance with a randomly generated uid and some extra params. This creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance """ if extra is None: extra = dict() newModel = Params.copy(self, extra) newModel.models = [model.copy(extra) for model in self.models] return newModel
python
def copy(self, extra=None): """ Creates a copy of this instance with a randomly generated uid and some extra params. This creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance """ if extra is None: extra = dict() newModel = Params.copy(self, extra) newModel.models = [model.copy(extra) for model in self.models] return newModel
[ "def", "copy", "(", "self", ",", "extra", "=", "None", ")", ":", "if", "extra", "is", "None", ":", "extra", "=", "dict", "(", ")", "newModel", "=", "Params", ".", "copy", "(", "self", ",", "extra", ")", "newModel", ".", "models", "=", "[", "model", ".", "copy", "(", "extra", ")", "for", "model", "in", "self", ".", "models", "]", "return", "newModel" ]
Creates a copy of this instance with a randomly generated uid and some extra params. This creates a deep copy of the embedded paramMap, and copies the embedded and extra parameters over. :param extra: Extra parameters to copy to the new instance :return: Copy of this instance
[ "Creates", "a", "copy", "of", "this", "instance", "with", "a", "randomly", "generated", "uid", "and", "some", "extra", "params", ".", "This", "creates", "a", "deep", "copy", "of", "the", "embedded", "paramMap", "and", "copies", "the", "embedded", "and", "extra", "parameters", "over", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/classification.py#L2046-L2059
apache/spark
python/pyspark/ml/classification.py
OneVsRestModel._from_java
def _from_java(cls, java_stage): """ Given a Java OneVsRestModel, create and return a Python wrapper of it. Used for ML persistence. """ featuresCol = java_stage.getFeaturesCol() labelCol = java_stage.getLabelCol() predictionCol = java_stage.getPredictionCol() classifier = JavaParams._from_java(java_stage.getClassifier()) models = [JavaParams._from_java(model) for model in java_stage.models()] py_stage = cls(models=models).setPredictionCol(predictionCol).setLabelCol(labelCol)\ .setFeaturesCol(featuresCol).setClassifier(classifier) py_stage._resetUid(java_stage.uid()) return py_stage
python
def _from_java(cls, java_stage): """ Given a Java OneVsRestModel, create and return a Python wrapper of it. Used for ML persistence. """ featuresCol = java_stage.getFeaturesCol() labelCol = java_stage.getLabelCol() predictionCol = java_stage.getPredictionCol() classifier = JavaParams._from_java(java_stage.getClassifier()) models = [JavaParams._from_java(model) for model in java_stage.models()] py_stage = cls(models=models).setPredictionCol(predictionCol).setLabelCol(labelCol)\ .setFeaturesCol(featuresCol).setClassifier(classifier) py_stage._resetUid(java_stage.uid()) return py_stage
[ "def", "_from_java", "(", "cls", ",", "java_stage", ")", ":", "featuresCol", "=", "java_stage", ".", "getFeaturesCol", "(", ")", "labelCol", "=", "java_stage", ".", "getLabelCol", "(", ")", "predictionCol", "=", "java_stage", ".", "getPredictionCol", "(", ")", "classifier", "=", "JavaParams", ".", "_from_java", "(", "java_stage", ".", "getClassifier", "(", ")", ")", "models", "=", "[", "JavaParams", ".", "_from_java", "(", "model", ")", "for", "model", "in", "java_stage", ".", "models", "(", ")", "]", "py_stage", "=", "cls", "(", "models", "=", "models", ")", ".", "setPredictionCol", "(", "predictionCol", ")", ".", "setLabelCol", "(", "labelCol", ")", ".", "setFeaturesCol", "(", "featuresCol", ")", ".", "setClassifier", "(", "classifier", ")", "py_stage", ".", "_resetUid", "(", "java_stage", ".", "uid", "(", ")", ")", "return", "py_stage" ]
Given a Java OneVsRestModel, create and return a Python wrapper of it. Used for ML persistence.
[ "Given", "a", "Java", "OneVsRestModel", "create", "and", "return", "a", "Python", "wrapper", "of", "it", ".", "Used", "for", "ML", "persistence", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/classification.py#L2062-L2075
apache/spark
python/pyspark/ml/classification.py
OneVsRestModel._to_java
def _to_java(self): """ Transfer this instance to a Java OneVsRestModel. Used for ML persistence. :return: Java object equivalent to this instance. """ sc = SparkContext._active_spark_context java_models = [model._to_java() for model in self.models] java_models_array = JavaWrapper._new_java_array( java_models, sc._gateway.jvm.org.apache.spark.ml.classification.ClassificationModel) metadata = JavaParams._new_java_obj("org.apache.spark.sql.types.Metadata") _java_obj = JavaParams._new_java_obj("org.apache.spark.ml.classification.OneVsRestModel", self.uid, metadata.empty(), java_models_array) _java_obj.set("classifier", self.getClassifier()._to_java()) _java_obj.set("featuresCol", self.getFeaturesCol()) _java_obj.set("labelCol", self.getLabelCol()) _java_obj.set("predictionCol", self.getPredictionCol()) return _java_obj
python
def _to_java(self): """ Transfer this instance to a Java OneVsRestModel. Used for ML persistence. :return: Java object equivalent to this instance. """ sc = SparkContext._active_spark_context java_models = [model._to_java() for model in self.models] java_models_array = JavaWrapper._new_java_array( java_models, sc._gateway.jvm.org.apache.spark.ml.classification.ClassificationModel) metadata = JavaParams._new_java_obj("org.apache.spark.sql.types.Metadata") _java_obj = JavaParams._new_java_obj("org.apache.spark.ml.classification.OneVsRestModel", self.uid, metadata.empty(), java_models_array) _java_obj.set("classifier", self.getClassifier()._to_java()) _java_obj.set("featuresCol", self.getFeaturesCol()) _java_obj.set("labelCol", self.getLabelCol()) _java_obj.set("predictionCol", self.getPredictionCol()) return _java_obj
[ "def", "_to_java", "(", "self", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "java_models", "=", "[", "model", ".", "_to_java", "(", ")", "for", "model", "in", "self", ".", "models", "]", "java_models_array", "=", "JavaWrapper", ".", "_new_java_array", "(", "java_models", ",", "sc", ".", "_gateway", ".", "jvm", ".", "org", ".", "apache", ".", "spark", ".", "ml", ".", "classification", ".", "ClassificationModel", ")", "metadata", "=", "JavaParams", ".", "_new_java_obj", "(", "\"org.apache.spark.sql.types.Metadata\"", ")", "_java_obj", "=", "JavaParams", ".", "_new_java_obj", "(", "\"org.apache.spark.ml.classification.OneVsRestModel\"", ",", "self", ".", "uid", ",", "metadata", ".", "empty", "(", ")", ",", "java_models_array", ")", "_java_obj", ".", "set", "(", "\"classifier\"", ",", "self", ".", "getClassifier", "(", ")", ".", "_to_java", "(", ")", ")", "_java_obj", ".", "set", "(", "\"featuresCol\"", ",", "self", ".", "getFeaturesCol", "(", ")", ")", "_java_obj", ".", "set", "(", "\"labelCol\"", ",", "self", ".", "getLabelCol", "(", ")", ")", "_java_obj", ".", "set", "(", "\"predictionCol\"", ",", "self", ".", "getPredictionCol", "(", ")", ")", "return", "_java_obj" ]
Transfer this instance to a Java OneVsRestModel. Used for ML persistence. :return: Java object equivalent to this instance.
[ "Transfer", "this", "instance", "to", "a", "Java", "OneVsRestModel", ".", "Used", "for", "ML", "persistence", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/ml/classification.py#L2077-L2094
apache/spark
python/pyspark/util.py
_exception_message
def _exception_message(excp): """Return the message from an exception as either a str or unicode object. Supports both Python 2 and Python 3. >>> msg = "Exception message" >>> excp = Exception(msg) >>> msg == _exception_message(excp) True >>> msg = u"unicöde" >>> excp = Exception(msg) >>> msg == _exception_message(excp) True """ if isinstance(excp, Py4JJavaError): # 'Py4JJavaError' doesn't contain the stack trace available on the Java side in 'message' # attribute in Python 2. We should call 'str' function on this exception in general but # 'Py4JJavaError' has an issue about addressing non-ascii strings. So, here we work # around by the direct call, '__str__()'. Please see SPARK-23517. return excp.__str__() if hasattr(excp, "message"): return excp.message return str(excp)
python
def _exception_message(excp): """Return the message from an exception as either a str or unicode object. Supports both Python 2 and Python 3. >>> msg = "Exception message" >>> excp = Exception(msg) >>> msg == _exception_message(excp) True >>> msg = u"unicöde" >>> excp = Exception(msg) >>> msg == _exception_message(excp) True """ if isinstance(excp, Py4JJavaError): # 'Py4JJavaError' doesn't contain the stack trace available on the Java side in 'message' # attribute in Python 2. We should call 'str' function on this exception in general but # 'Py4JJavaError' has an issue about addressing non-ascii strings. So, here we work # around by the direct call, '__str__()'. Please see SPARK-23517. return excp.__str__() if hasattr(excp, "message"): return excp.message return str(excp)
[ "def", "_exception_message", "(", "excp", ")", ":", "if", "isinstance", "(", "excp", ",", "Py4JJavaError", ")", ":", "# 'Py4JJavaError' doesn't contain the stack trace available on the Java side in 'message'", "# attribute in Python 2. We should call 'str' function on this exception in general but", "# 'Py4JJavaError' has an issue about addressing non-ascii strings. So, here we work", "# around by the direct call, '__str__()'. Please see SPARK-23517.", "return", "excp", ".", "__str__", "(", ")", "if", "hasattr", "(", "excp", ",", "\"message\"", ")", ":", "return", "excp", ".", "message", "return", "str", "(", "excp", ")" ]
Return the message from an exception as either a str or unicode object. Supports both Python 2 and Python 3. >>> msg = "Exception message" >>> excp = Exception(msg) >>> msg == _exception_message(excp) True >>> msg = u"unicöde" >>> excp = Exception(msg) >>> msg == _exception_message(excp) True
[ "Return", "the", "message", "from", "an", "exception", "as", "either", "a", "str", "or", "unicode", "object", ".", "Supports", "both", "Python", "2", "and", "Python", "3", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/util.py#L27-L49
apache/spark
python/pyspark/util.py
_get_argspec
def _get_argspec(f): """ Get argspec of a function. Supports both Python 2 and Python 3. """ if sys.version_info[0] < 3: argspec = inspect.getargspec(f) else: # `getargspec` is deprecated since python3.0 (incompatible with function annotations). # See SPARK-23569. argspec = inspect.getfullargspec(f) return argspec
python
def _get_argspec(f): """ Get argspec of a function. Supports both Python 2 and Python 3. """ if sys.version_info[0] < 3: argspec = inspect.getargspec(f) else: # `getargspec` is deprecated since python3.0 (incompatible with function annotations). # See SPARK-23569. argspec = inspect.getfullargspec(f) return argspec
[ "def", "_get_argspec", "(", "f", ")", ":", "if", "sys", ".", "version_info", "[", "0", "]", "<", "3", ":", "argspec", "=", "inspect", ".", "getargspec", "(", "f", ")", "else", ":", "# `getargspec` is deprecated since python3.0 (incompatible with function annotations).", "# See SPARK-23569.", "argspec", "=", "inspect", ".", "getfullargspec", "(", "f", ")", "return", "argspec" ]
Get argspec of a function. Supports both Python 2 and Python 3.
[ "Get", "argspec", "of", "a", "function", ".", "Supports", "both", "Python", "2", "and", "Python", "3", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/util.py#L52-L62
apache/spark
python/pyspark/util.py
fail_on_stopiteration
def fail_on_stopiteration(f): """ Wraps the input function to fail on 'StopIteration' by raising a 'RuntimeError' prevents silent loss of data when 'f' is used in a for loop in Spark code """ def wrapper(*args, **kwargs): try: return f(*args, **kwargs) except StopIteration as exc: raise RuntimeError( "Caught StopIteration thrown from user's code; failing the task", exc ) return wrapper
python
def fail_on_stopiteration(f): """ Wraps the input function to fail on 'StopIteration' by raising a 'RuntimeError' prevents silent loss of data when 'f' is used in a for loop in Spark code """ def wrapper(*args, **kwargs): try: return f(*args, **kwargs) except StopIteration as exc: raise RuntimeError( "Caught StopIteration thrown from user's code; failing the task", exc ) return wrapper
[ "def", "fail_on_stopiteration", "(", "f", ")", ":", "def", "wrapper", "(", "*", "args", ",", "*", "*", "kwargs", ")", ":", "try", ":", "return", "f", "(", "*", "args", ",", "*", "*", "kwargs", ")", "except", "StopIteration", "as", "exc", ":", "raise", "RuntimeError", "(", "\"Caught StopIteration thrown from user's code; failing the task\"", ",", "exc", ")", "return", "wrapper" ]
Wraps the input function to fail on 'StopIteration' by raising a 'RuntimeError' prevents silent loss of data when 'f' is used in a for loop in Spark code
[ "Wraps", "the", "input", "function", "to", "fail", "on", "StopIteration", "by", "raising", "a", "RuntimeError", "prevents", "silent", "loss", "of", "data", "when", "f", "is", "used", "in", "a", "for", "loop", "in", "Spark", "code" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/util.py#L92-L106
apache/spark
python/pyspark/util.py
VersionUtils.majorMinorVersion
def majorMinorVersion(sparkVersion): """ Given a Spark version string, return the (major version number, minor version number). E.g., for 2.0.1-SNAPSHOT, return (2, 0). >>> sparkVersion = "2.4.0" >>> VersionUtils.majorMinorVersion(sparkVersion) (2, 4) >>> sparkVersion = "2.3.0-SNAPSHOT" >>> VersionUtils.majorMinorVersion(sparkVersion) (2, 3) """ m = re.search(r'^(\d+)\.(\d+)(\..*)?$', sparkVersion) if m is not None: return (int(m.group(1)), int(m.group(2))) else: raise ValueError("Spark tried to parse '%s' as a Spark" % sparkVersion + " version string, but it could not find the major and minor" + " version numbers.")
python
def majorMinorVersion(sparkVersion): """ Given a Spark version string, return the (major version number, minor version number). E.g., for 2.0.1-SNAPSHOT, return (2, 0). >>> sparkVersion = "2.4.0" >>> VersionUtils.majorMinorVersion(sparkVersion) (2, 4) >>> sparkVersion = "2.3.0-SNAPSHOT" >>> VersionUtils.majorMinorVersion(sparkVersion) (2, 3) """ m = re.search(r'^(\d+)\.(\d+)(\..*)?$', sparkVersion) if m is not None: return (int(m.group(1)), int(m.group(2))) else: raise ValueError("Spark tried to parse '%s' as a Spark" % sparkVersion + " version string, but it could not find the major and minor" + " version numbers.")
[ "def", "majorMinorVersion", "(", "sparkVersion", ")", ":", "m", "=", "re", ".", "search", "(", "r'^(\\d+)\\.(\\d+)(\\..*)?$'", ",", "sparkVersion", ")", "if", "m", "is", "not", "None", ":", "return", "(", "int", "(", "m", ".", "group", "(", "1", ")", ")", ",", "int", "(", "m", ".", "group", "(", "2", ")", ")", ")", "else", ":", "raise", "ValueError", "(", "\"Spark tried to parse '%s' as a Spark\"", "%", "sparkVersion", "+", "\" version string, but it could not find the major and minor\"", "+", "\" version numbers.\"", ")" ]
Given a Spark version string, return the (major version number, minor version number). E.g., for 2.0.1-SNAPSHOT, return (2, 0). >>> sparkVersion = "2.4.0" >>> VersionUtils.majorMinorVersion(sparkVersion) (2, 4) >>> sparkVersion = "2.3.0-SNAPSHOT" >>> VersionUtils.majorMinorVersion(sparkVersion) (2, 3)
[ "Given", "a", "Spark", "version", "string", "return", "the", "(", "major", "version", "number", "minor", "version", "number", ")", ".", "E", ".", "g", ".", "for", "2", ".", "0", ".", "1", "-", "SNAPSHOT", "return", "(", "2", "0", ")", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/util.py#L70-L89
apache/spark
python/pyspark/context.py
SparkContext._ensure_initialized
def _ensure_initialized(cls, instance=None, gateway=None, conf=None): """ Checks whether a SparkContext is initialized or not. Throws error if a SparkContext is already running. """ with SparkContext._lock: if not SparkContext._gateway: SparkContext._gateway = gateway or launch_gateway(conf) SparkContext._jvm = SparkContext._gateway.jvm if instance: if (SparkContext._active_spark_context and SparkContext._active_spark_context != instance): currentMaster = SparkContext._active_spark_context.master currentAppName = SparkContext._active_spark_context.appName callsite = SparkContext._active_spark_context._callsite # Raise error if there is already a running Spark context raise ValueError( "Cannot run multiple SparkContexts at once; " "existing SparkContext(app=%s, master=%s)" " created by %s at %s:%s " % (currentAppName, currentMaster, callsite.function, callsite.file, callsite.linenum)) else: SparkContext._active_spark_context = instance
python
def _ensure_initialized(cls, instance=None, gateway=None, conf=None): """ Checks whether a SparkContext is initialized or not. Throws error if a SparkContext is already running. """ with SparkContext._lock: if not SparkContext._gateway: SparkContext._gateway = gateway or launch_gateway(conf) SparkContext._jvm = SparkContext._gateway.jvm if instance: if (SparkContext._active_spark_context and SparkContext._active_spark_context != instance): currentMaster = SparkContext._active_spark_context.master currentAppName = SparkContext._active_spark_context.appName callsite = SparkContext._active_spark_context._callsite # Raise error if there is already a running Spark context raise ValueError( "Cannot run multiple SparkContexts at once; " "existing SparkContext(app=%s, master=%s)" " created by %s at %s:%s " % (currentAppName, currentMaster, callsite.function, callsite.file, callsite.linenum)) else: SparkContext._active_spark_context = instance
[ "def", "_ensure_initialized", "(", "cls", ",", "instance", "=", "None", ",", "gateway", "=", "None", ",", "conf", "=", "None", ")", ":", "with", "SparkContext", ".", "_lock", ":", "if", "not", "SparkContext", ".", "_gateway", ":", "SparkContext", ".", "_gateway", "=", "gateway", "or", "launch_gateway", "(", "conf", ")", "SparkContext", ".", "_jvm", "=", "SparkContext", ".", "_gateway", ".", "jvm", "if", "instance", ":", "if", "(", "SparkContext", ".", "_active_spark_context", "and", "SparkContext", ".", "_active_spark_context", "!=", "instance", ")", ":", "currentMaster", "=", "SparkContext", ".", "_active_spark_context", ".", "master", "currentAppName", "=", "SparkContext", ".", "_active_spark_context", ".", "appName", "callsite", "=", "SparkContext", ".", "_active_spark_context", ".", "_callsite", "# Raise error if there is already a running Spark context", "raise", "ValueError", "(", "\"Cannot run multiple SparkContexts at once; \"", "\"existing SparkContext(app=%s, master=%s)\"", "\" created by %s at %s:%s \"", "%", "(", "currentAppName", ",", "currentMaster", ",", "callsite", ".", "function", ",", "callsite", ".", "file", ",", "callsite", ".", "linenum", ")", ")", "else", ":", "SparkContext", ".", "_active_spark_context", "=", "instance" ]
Checks whether a SparkContext is initialized or not. Throws error if a SparkContext is already running.
[ "Checks", "whether", "a", "SparkContext", "is", "initialized", "or", "not", ".", "Throws", "error", "if", "a", "SparkContext", "is", "already", "running", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L303-L328
apache/spark
python/pyspark/context.py
SparkContext.getOrCreate
def getOrCreate(cls, conf=None): """ Get or instantiate a SparkContext and register it as a singleton object. :param conf: SparkConf (optional) """ with SparkContext._lock: if SparkContext._active_spark_context is None: SparkContext(conf=conf or SparkConf()) return SparkContext._active_spark_context
python
def getOrCreate(cls, conf=None): """ Get or instantiate a SparkContext and register it as a singleton object. :param conf: SparkConf (optional) """ with SparkContext._lock: if SparkContext._active_spark_context is None: SparkContext(conf=conf or SparkConf()) return SparkContext._active_spark_context
[ "def", "getOrCreate", "(", "cls", ",", "conf", "=", "None", ")", ":", "with", "SparkContext", ".", "_lock", ":", "if", "SparkContext", ".", "_active_spark_context", "is", "None", ":", "SparkContext", "(", "conf", "=", "conf", "or", "SparkConf", "(", ")", ")", "return", "SparkContext", ".", "_active_spark_context" ]
Get or instantiate a SparkContext and register it as a singleton object. :param conf: SparkConf (optional)
[ "Get", "or", "instantiate", "a", "SparkContext", "and", "register", "it", "as", "a", "singleton", "object", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L353-L362
apache/spark
python/pyspark/context.py
SparkContext.setSystemProperty
def setSystemProperty(cls, key, value): """ Set a Java system property, such as spark.executor.memory. This must must be invoked before instantiating SparkContext. """ SparkContext._ensure_initialized() SparkContext._jvm.java.lang.System.setProperty(key, value)
python
def setSystemProperty(cls, key, value): """ Set a Java system property, such as spark.executor.memory. This must must be invoked before instantiating SparkContext. """ SparkContext._ensure_initialized() SparkContext._jvm.java.lang.System.setProperty(key, value)
[ "def", "setSystemProperty", "(", "cls", ",", "key", ",", "value", ")", ":", "SparkContext", ".", "_ensure_initialized", "(", ")", "SparkContext", ".", "_jvm", ".", "java", ".", "lang", ".", "System", ".", "setProperty", "(", "key", ",", "value", ")" ]
Set a Java system property, such as spark.executor.memory. This must must be invoked before instantiating SparkContext.
[ "Set", "a", "Java", "system", "property", "such", "as", "spark", ".", "executor", ".", "memory", ".", "This", "must", "must", "be", "invoked", "before", "instantiating", "SparkContext", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L372-L378
apache/spark
python/pyspark/context.py
SparkContext.stop
def stop(self): """ Shut down the SparkContext. """ if getattr(self, "_jsc", None): try: self._jsc.stop() except Py4JError: # Case: SPARK-18523 warnings.warn( 'Unable to cleanly shutdown Spark JVM process.' ' It is possible that the process has crashed,' ' been killed or may also be in a zombie state.', RuntimeWarning ) finally: self._jsc = None if getattr(self, "_accumulatorServer", None): self._accumulatorServer.shutdown() self._accumulatorServer = None with SparkContext._lock: SparkContext._active_spark_context = None
python
def stop(self): """ Shut down the SparkContext. """ if getattr(self, "_jsc", None): try: self._jsc.stop() except Py4JError: # Case: SPARK-18523 warnings.warn( 'Unable to cleanly shutdown Spark JVM process.' ' It is possible that the process has crashed,' ' been killed or may also be in a zombie state.', RuntimeWarning ) finally: self._jsc = None if getattr(self, "_accumulatorServer", None): self._accumulatorServer.shutdown() self._accumulatorServer = None with SparkContext._lock: SparkContext._active_spark_context = None
[ "def", "stop", "(", "self", ")", ":", "if", "getattr", "(", "self", ",", "\"_jsc\"", ",", "None", ")", ":", "try", ":", "self", ".", "_jsc", ".", "stop", "(", ")", "except", "Py4JError", ":", "# Case: SPARK-18523", "warnings", ".", "warn", "(", "'Unable to cleanly shutdown Spark JVM process.'", "' It is possible that the process has crashed,'", "' been killed or may also be in a zombie state.'", ",", "RuntimeWarning", ")", "finally", ":", "self", ".", "_jsc", "=", "None", "if", "getattr", "(", "self", ",", "\"_accumulatorServer\"", ",", "None", ")", ":", "self", ".", "_accumulatorServer", ".", "shutdown", "(", ")", "self", ".", "_accumulatorServer", "=", "None", "with", "SparkContext", ".", "_lock", ":", "SparkContext", ".", "_active_spark_context", "=", "None" ]
Shut down the SparkContext.
[ "Shut", "down", "the", "SparkContext", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L427-L448
apache/spark
python/pyspark/context.py
SparkContext.range
def range(self, start, end=None, step=1, numSlices=None): """ Create a new RDD of int containing elements from `start` to `end` (exclusive), increased by `step` every element. Can be called the same way as python's built-in range() function. If called with a single argument, the argument is interpreted as `end`, and `start` is set to 0. :param start: the start value :param end: the end value (exclusive) :param step: the incremental step (default: 1) :param numSlices: the number of partitions of the new RDD :return: An RDD of int >>> sc.range(5).collect() [0, 1, 2, 3, 4] >>> sc.range(2, 4).collect() [2, 3] >>> sc.range(1, 7, 2).collect() [1, 3, 5] """ if end is None: end = start start = 0 return self.parallelize(xrange(start, end, step), numSlices)
python
def range(self, start, end=None, step=1, numSlices=None): """ Create a new RDD of int containing elements from `start` to `end` (exclusive), increased by `step` every element. Can be called the same way as python's built-in range() function. If called with a single argument, the argument is interpreted as `end`, and `start` is set to 0. :param start: the start value :param end: the end value (exclusive) :param step: the incremental step (default: 1) :param numSlices: the number of partitions of the new RDD :return: An RDD of int >>> sc.range(5).collect() [0, 1, 2, 3, 4] >>> sc.range(2, 4).collect() [2, 3] >>> sc.range(1, 7, 2).collect() [1, 3, 5] """ if end is None: end = start start = 0 return self.parallelize(xrange(start, end, step), numSlices)
[ "def", "range", "(", "self", ",", "start", ",", "end", "=", "None", ",", "step", "=", "1", ",", "numSlices", "=", "None", ")", ":", "if", "end", "is", "None", ":", "end", "=", "start", "start", "=", "0", "return", "self", ".", "parallelize", "(", "xrange", "(", "start", ",", "end", ",", "step", ")", ",", "numSlices", ")" ]
Create a new RDD of int containing elements from `start` to `end` (exclusive), increased by `step` every element. Can be called the same way as python's built-in range() function. If called with a single argument, the argument is interpreted as `end`, and `start` is set to 0. :param start: the start value :param end: the end value (exclusive) :param step: the incremental step (default: 1) :param numSlices: the number of partitions of the new RDD :return: An RDD of int >>> sc.range(5).collect() [0, 1, 2, 3, 4] >>> sc.range(2, 4).collect() [2, 3] >>> sc.range(1, 7, 2).collect() [1, 3, 5]
[ "Create", "a", "new", "RDD", "of", "int", "containing", "elements", "from", "start", "to", "end", "(", "exclusive", ")", "increased", "by", "step", "every", "element", ".", "Can", "be", "called", "the", "same", "way", "as", "python", "s", "built", "-", "in", "range", "()", "function", ".", "If", "called", "with", "a", "single", "argument", "the", "argument", "is", "interpreted", "as", "end", "and", "start", "is", "set", "to", "0", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L456-L480
apache/spark
python/pyspark/context.py
SparkContext.parallelize
def parallelize(self, c, numSlices=None): """ Distribute a local Python collection to form an RDD. Using xrange is recommended if the input represents a range for performance. >>> sc.parallelize([0, 2, 3, 4, 6], 5).glom().collect() [[0], [2], [3], [4], [6]] >>> sc.parallelize(xrange(0, 6, 2), 5).glom().collect() [[], [0], [], [2], [4]] """ numSlices = int(numSlices) if numSlices is not None else self.defaultParallelism if isinstance(c, xrange): size = len(c) if size == 0: return self.parallelize([], numSlices) step = c[1] - c[0] if size > 1 else 1 start0 = c[0] def getStart(split): return start0 + int((split * size / numSlices)) * step def f(split, iterator): # it's an empty iterator here but we need this line for triggering the # logic of signal handling in FramedSerializer.load_stream, for instance, # SpecialLengths.END_OF_DATA_SECTION in _read_with_length. Since # FramedSerializer.load_stream produces a generator, the control should # at least be in that function once. Here we do it by explicitly converting # the empty iterator to a list, thus make sure worker reuse takes effect. # See more details in SPARK-26549. assert len(list(iterator)) == 0 return xrange(getStart(split), getStart(split + 1), step) return self.parallelize([], numSlices).mapPartitionsWithIndex(f) # Make sure we distribute data evenly if it's smaller than self.batchSize if "__len__" not in dir(c): c = list(c) # Make it a list so we can compute its length batchSize = max(1, min(len(c) // numSlices, self._batchSize or 1024)) serializer = BatchedSerializer(self._unbatched_serializer, batchSize) def reader_func(temp_filename): return self._jvm.PythonRDD.readRDDFromFile(self._jsc, temp_filename, numSlices) def createRDDServer(): return self._jvm.PythonParallelizeServer(self._jsc.sc(), numSlices) jrdd = self._serialize_to_jvm(c, serializer, reader_func, createRDDServer) return RDD(jrdd, self, serializer)
python
def parallelize(self, c, numSlices=None): """ Distribute a local Python collection to form an RDD. Using xrange is recommended if the input represents a range for performance. >>> sc.parallelize([0, 2, 3, 4, 6], 5).glom().collect() [[0], [2], [3], [4], [6]] >>> sc.parallelize(xrange(0, 6, 2), 5).glom().collect() [[], [0], [], [2], [4]] """ numSlices = int(numSlices) if numSlices is not None else self.defaultParallelism if isinstance(c, xrange): size = len(c) if size == 0: return self.parallelize([], numSlices) step = c[1] - c[0] if size > 1 else 1 start0 = c[0] def getStart(split): return start0 + int((split * size / numSlices)) * step def f(split, iterator): # it's an empty iterator here but we need this line for triggering the # logic of signal handling in FramedSerializer.load_stream, for instance, # SpecialLengths.END_OF_DATA_SECTION in _read_with_length. Since # FramedSerializer.load_stream produces a generator, the control should # at least be in that function once. Here we do it by explicitly converting # the empty iterator to a list, thus make sure worker reuse takes effect. # See more details in SPARK-26549. assert len(list(iterator)) == 0 return xrange(getStart(split), getStart(split + 1), step) return self.parallelize([], numSlices).mapPartitionsWithIndex(f) # Make sure we distribute data evenly if it's smaller than self.batchSize if "__len__" not in dir(c): c = list(c) # Make it a list so we can compute its length batchSize = max(1, min(len(c) // numSlices, self._batchSize or 1024)) serializer = BatchedSerializer(self._unbatched_serializer, batchSize) def reader_func(temp_filename): return self._jvm.PythonRDD.readRDDFromFile(self._jsc, temp_filename, numSlices) def createRDDServer(): return self._jvm.PythonParallelizeServer(self._jsc.sc(), numSlices) jrdd = self._serialize_to_jvm(c, serializer, reader_func, createRDDServer) return RDD(jrdd, self, serializer)
[ "def", "parallelize", "(", "self", ",", "c", ",", "numSlices", "=", "None", ")", ":", "numSlices", "=", "int", "(", "numSlices", ")", "if", "numSlices", "is", "not", "None", "else", "self", ".", "defaultParallelism", "if", "isinstance", "(", "c", ",", "xrange", ")", ":", "size", "=", "len", "(", "c", ")", "if", "size", "==", "0", ":", "return", "self", ".", "parallelize", "(", "[", "]", ",", "numSlices", ")", "step", "=", "c", "[", "1", "]", "-", "c", "[", "0", "]", "if", "size", ">", "1", "else", "1", "start0", "=", "c", "[", "0", "]", "def", "getStart", "(", "split", ")", ":", "return", "start0", "+", "int", "(", "(", "split", "*", "size", "/", "numSlices", ")", ")", "*", "step", "def", "f", "(", "split", ",", "iterator", ")", ":", "# it's an empty iterator here but we need this line for triggering the", "# logic of signal handling in FramedSerializer.load_stream, for instance,", "# SpecialLengths.END_OF_DATA_SECTION in _read_with_length. Since", "# FramedSerializer.load_stream produces a generator, the control should", "# at least be in that function once. Here we do it by explicitly converting", "# the empty iterator to a list, thus make sure worker reuse takes effect.", "# See more details in SPARK-26549.", "assert", "len", "(", "list", "(", "iterator", ")", ")", "==", "0", "return", "xrange", "(", "getStart", "(", "split", ")", ",", "getStart", "(", "split", "+", "1", ")", ",", "step", ")", "return", "self", ".", "parallelize", "(", "[", "]", ",", "numSlices", ")", ".", "mapPartitionsWithIndex", "(", "f", ")", "# Make sure we distribute data evenly if it's smaller than self.batchSize", "if", "\"__len__\"", "not", "in", "dir", "(", "c", ")", ":", "c", "=", "list", "(", "c", ")", "# Make it a list so we can compute its length", "batchSize", "=", "max", "(", "1", ",", "min", "(", "len", "(", "c", ")", "//", "numSlices", ",", "self", ".", "_batchSize", "or", "1024", ")", ")", "serializer", "=", "BatchedSerializer", "(", "self", ".", "_unbatched_serializer", ",", "batchSize", ")", "def", "reader_func", "(", "temp_filename", ")", ":", "return", "self", ".", "_jvm", ".", "PythonRDD", ".", "readRDDFromFile", "(", "self", ".", "_jsc", ",", "temp_filename", ",", "numSlices", ")", "def", "createRDDServer", "(", ")", ":", "return", "self", ".", "_jvm", ".", "PythonParallelizeServer", "(", "self", ".", "_jsc", ".", "sc", "(", ")", ",", "numSlices", ")", "jrdd", "=", "self", ".", "_serialize_to_jvm", "(", "c", ",", "serializer", ",", "reader_func", ",", "createRDDServer", ")", "return", "RDD", "(", "jrdd", ",", "self", ",", "serializer", ")" ]
Distribute a local Python collection to form an RDD. Using xrange is recommended if the input represents a range for performance. >>> sc.parallelize([0, 2, 3, 4, 6], 5).glom().collect() [[0], [2], [3], [4], [6]] >>> sc.parallelize(xrange(0, 6, 2), 5).glom().collect() [[], [0], [], [2], [4]]
[ "Distribute", "a", "local", "Python", "collection", "to", "form", "an", "RDD", ".", "Using", "xrange", "is", "recommended", "if", "the", "input", "represents", "a", "range", "for", "performance", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L482-L529
apache/spark
python/pyspark/context.py
SparkContext._serialize_to_jvm
def _serialize_to_jvm(self, data, serializer, reader_func, createRDDServer): """ Using py4j to send a large dataset to the jvm is really slow, so we use either a file or a socket if we have encryption enabled. :param data: :param serializer: :param reader_func: A function which takes a filename and reads in the data in the jvm and returns a JavaRDD. Only used when encryption is disabled. :param createRDDServer: A function which creates a PythonRDDServer in the jvm to accept the serialized data, for use when encryption is enabled. :return: """ if self._encryption_enabled: # with encryption, we open a server in java and send the data directly server = createRDDServer() (sock_file, _) = local_connect_and_auth(server.port(), server.secret()) chunked_out = ChunkedStream(sock_file, 8192) serializer.dump_stream(data, chunked_out) chunked_out.close() # this call will block until the server has read all the data and processed it (or # throws an exception) r = server.getResult() return r else: # without encryption, we serialize to a file, and we read the file in java and # parallelize from there. tempFile = NamedTemporaryFile(delete=False, dir=self._temp_dir) try: try: serializer.dump_stream(data, tempFile) finally: tempFile.close() return reader_func(tempFile.name) finally: # we eagerily reads the file so we can delete right after. os.unlink(tempFile.name)
python
def _serialize_to_jvm(self, data, serializer, reader_func, createRDDServer): """ Using py4j to send a large dataset to the jvm is really slow, so we use either a file or a socket if we have encryption enabled. :param data: :param serializer: :param reader_func: A function which takes a filename and reads in the data in the jvm and returns a JavaRDD. Only used when encryption is disabled. :param createRDDServer: A function which creates a PythonRDDServer in the jvm to accept the serialized data, for use when encryption is enabled. :return: """ if self._encryption_enabled: # with encryption, we open a server in java and send the data directly server = createRDDServer() (sock_file, _) = local_connect_and_auth(server.port(), server.secret()) chunked_out = ChunkedStream(sock_file, 8192) serializer.dump_stream(data, chunked_out) chunked_out.close() # this call will block until the server has read all the data and processed it (or # throws an exception) r = server.getResult() return r else: # without encryption, we serialize to a file, and we read the file in java and # parallelize from there. tempFile = NamedTemporaryFile(delete=False, dir=self._temp_dir) try: try: serializer.dump_stream(data, tempFile) finally: tempFile.close() return reader_func(tempFile.name) finally: # we eagerily reads the file so we can delete right after. os.unlink(tempFile.name)
[ "def", "_serialize_to_jvm", "(", "self", ",", "data", ",", "serializer", ",", "reader_func", ",", "createRDDServer", ")", ":", "if", "self", ".", "_encryption_enabled", ":", "# with encryption, we open a server in java and send the data directly", "server", "=", "createRDDServer", "(", ")", "(", "sock_file", ",", "_", ")", "=", "local_connect_and_auth", "(", "server", ".", "port", "(", ")", ",", "server", ".", "secret", "(", ")", ")", "chunked_out", "=", "ChunkedStream", "(", "sock_file", ",", "8192", ")", "serializer", ".", "dump_stream", "(", "data", ",", "chunked_out", ")", "chunked_out", ".", "close", "(", ")", "# this call will block until the server has read all the data and processed it (or", "# throws an exception)", "r", "=", "server", ".", "getResult", "(", ")", "return", "r", "else", ":", "# without encryption, we serialize to a file, and we read the file in java and", "# parallelize from there.", "tempFile", "=", "NamedTemporaryFile", "(", "delete", "=", "False", ",", "dir", "=", "self", ".", "_temp_dir", ")", "try", ":", "try", ":", "serializer", ".", "dump_stream", "(", "data", ",", "tempFile", ")", "finally", ":", "tempFile", ".", "close", "(", ")", "return", "reader_func", "(", "tempFile", ".", "name", ")", "finally", ":", "# we eagerily reads the file so we can delete right after.", "os", ".", "unlink", "(", "tempFile", ".", "name", ")" ]
Using py4j to send a large dataset to the jvm is really slow, so we use either a file or a socket if we have encryption enabled. :param data: :param serializer: :param reader_func: A function which takes a filename and reads in the data in the jvm and returns a JavaRDD. Only used when encryption is disabled. :param createRDDServer: A function which creates a PythonRDDServer in the jvm to accept the serialized data, for use when encryption is enabled. :return:
[ "Using", "py4j", "to", "send", "a", "large", "dataset", "to", "the", "jvm", "is", "really", "slow", "so", "we", "use", "either", "a", "file", "or", "a", "socket", "if", "we", "have", "encryption", "enabled", ".", ":", "param", "data", ":", ":", "param", "serializer", ":", ":", "param", "reader_func", ":", "A", "function", "which", "takes", "a", "filename", "and", "reads", "in", "the", "data", "in", "the", "jvm", "and", "returns", "a", "JavaRDD", ".", "Only", "used", "when", "encryption", "is", "disabled", ".", ":", "param", "createRDDServer", ":", "A", "function", "which", "creates", "a", "PythonRDDServer", "in", "the", "jvm", "to", "accept", "the", "serialized", "data", "for", "use", "when", "encryption", "is", "enabled", ".", ":", "return", ":" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L531-L566
apache/spark
python/pyspark/context.py
SparkContext.pickleFile
def pickleFile(self, name, minPartitions=None): """ Load an RDD previously saved using L{RDD.saveAsPickleFile} method. >>> tmpFile = NamedTemporaryFile(delete=True) >>> tmpFile.close() >>> sc.parallelize(range(10)).saveAsPickleFile(tmpFile.name, 5) >>> sorted(sc.pickleFile(tmpFile.name, 3).collect()) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] """ minPartitions = minPartitions or self.defaultMinPartitions return RDD(self._jsc.objectFile(name, minPartitions), self)
python
def pickleFile(self, name, minPartitions=None): """ Load an RDD previously saved using L{RDD.saveAsPickleFile} method. >>> tmpFile = NamedTemporaryFile(delete=True) >>> tmpFile.close() >>> sc.parallelize(range(10)).saveAsPickleFile(tmpFile.name, 5) >>> sorted(sc.pickleFile(tmpFile.name, 3).collect()) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] """ minPartitions = minPartitions or self.defaultMinPartitions return RDD(self._jsc.objectFile(name, minPartitions), self)
[ "def", "pickleFile", "(", "self", ",", "name", ",", "minPartitions", "=", "None", ")", ":", "minPartitions", "=", "minPartitions", "or", "self", ".", "defaultMinPartitions", "return", "RDD", "(", "self", ".", "_jsc", ".", "objectFile", "(", "name", ",", "minPartitions", ")", ",", "self", ")" ]
Load an RDD previously saved using L{RDD.saveAsPickleFile} method. >>> tmpFile = NamedTemporaryFile(delete=True) >>> tmpFile.close() >>> sc.parallelize(range(10)).saveAsPickleFile(tmpFile.name, 5) >>> sorted(sc.pickleFile(tmpFile.name, 3).collect()) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[ "Load", "an", "RDD", "previously", "saved", "using", "L", "{", "RDD", ".", "saveAsPickleFile", "}", "method", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L568-L579
apache/spark
python/pyspark/context.py
SparkContext.textFile
def textFile(self, name, minPartitions=None, use_unicode=True): """ Read a text file from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI, and return it as an RDD of Strings. The text files must be encoded as UTF-8. If use_unicode is False, the strings will be kept as `str` (encoding as `utf-8`), which is faster and smaller than unicode. (Added in Spark 1.2) >>> path = os.path.join(tempdir, "sample-text.txt") >>> with open(path, "w") as testFile: ... _ = testFile.write("Hello world!") >>> textFile = sc.textFile(path) >>> textFile.collect() [u'Hello world!'] """ minPartitions = minPartitions or min(self.defaultParallelism, 2) return RDD(self._jsc.textFile(name, minPartitions), self, UTF8Deserializer(use_unicode))
python
def textFile(self, name, minPartitions=None, use_unicode=True): """ Read a text file from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI, and return it as an RDD of Strings. The text files must be encoded as UTF-8. If use_unicode is False, the strings will be kept as `str` (encoding as `utf-8`), which is faster and smaller than unicode. (Added in Spark 1.2) >>> path = os.path.join(tempdir, "sample-text.txt") >>> with open(path, "w") as testFile: ... _ = testFile.write("Hello world!") >>> textFile = sc.textFile(path) >>> textFile.collect() [u'Hello world!'] """ minPartitions = minPartitions or min(self.defaultParallelism, 2) return RDD(self._jsc.textFile(name, minPartitions), self, UTF8Deserializer(use_unicode))
[ "def", "textFile", "(", "self", ",", "name", ",", "minPartitions", "=", "None", ",", "use_unicode", "=", "True", ")", ":", "minPartitions", "=", "minPartitions", "or", "min", "(", "self", ".", "defaultParallelism", ",", "2", ")", "return", "RDD", "(", "self", ".", "_jsc", ".", "textFile", "(", "name", ",", "minPartitions", ")", ",", "self", ",", "UTF8Deserializer", "(", "use_unicode", ")", ")" ]
Read a text file from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI, and return it as an RDD of Strings. The text files must be encoded as UTF-8. If use_unicode is False, the strings will be kept as `str` (encoding as `utf-8`), which is faster and smaller than unicode. (Added in Spark 1.2) >>> path = os.path.join(tempdir, "sample-text.txt") >>> with open(path, "w") as testFile: ... _ = testFile.write("Hello world!") >>> textFile = sc.textFile(path) >>> textFile.collect() [u'Hello world!']
[ "Read", "a", "text", "file", "from", "HDFS", "a", "local", "file", "system", "(", "available", "on", "all", "nodes", ")", "or", "any", "Hadoop", "-", "supported", "file", "system", "URI", "and", "return", "it", "as", "an", "RDD", "of", "Strings", ".", "The", "text", "files", "must", "be", "encoded", "as", "UTF", "-", "8", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L582-L602
apache/spark
python/pyspark/context.py
SparkContext.wholeTextFiles
def wholeTextFiles(self, path, minPartitions=None, use_unicode=True): """ Read a directory of text files from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI. Each file is read as a single record and returned in a key-value pair, where the key is the path of each file, the value is the content of each file. The text files must be encoded as UTF-8. If use_unicode is False, the strings will be kept as `str` (encoding as `utf-8`), which is faster and smaller than unicode. (Added in Spark 1.2) For example, if you have the following files:: hdfs://a-hdfs-path/part-00000 hdfs://a-hdfs-path/part-00001 ... hdfs://a-hdfs-path/part-nnnnn Do C{rdd = sparkContext.wholeTextFiles("hdfs://a-hdfs-path")}, then C{rdd} contains:: (a-hdfs-path/part-00000, its content) (a-hdfs-path/part-00001, its content) ... (a-hdfs-path/part-nnnnn, its content) .. note:: Small files are preferred, as each file will be loaded fully in memory. >>> dirPath = os.path.join(tempdir, "files") >>> os.mkdir(dirPath) >>> with open(os.path.join(dirPath, "1.txt"), "w") as file1: ... _ = file1.write("1") >>> with open(os.path.join(dirPath, "2.txt"), "w") as file2: ... _ = file2.write("2") >>> textFiles = sc.wholeTextFiles(dirPath) >>> sorted(textFiles.collect()) [(u'.../1.txt', u'1'), (u'.../2.txt', u'2')] """ minPartitions = minPartitions or self.defaultMinPartitions return RDD(self._jsc.wholeTextFiles(path, minPartitions), self, PairDeserializer(UTF8Deserializer(use_unicode), UTF8Deserializer(use_unicode)))
python
def wholeTextFiles(self, path, minPartitions=None, use_unicode=True): """ Read a directory of text files from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI. Each file is read as a single record and returned in a key-value pair, where the key is the path of each file, the value is the content of each file. The text files must be encoded as UTF-8. If use_unicode is False, the strings will be kept as `str` (encoding as `utf-8`), which is faster and smaller than unicode. (Added in Spark 1.2) For example, if you have the following files:: hdfs://a-hdfs-path/part-00000 hdfs://a-hdfs-path/part-00001 ... hdfs://a-hdfs-path/part-nnnnn Do C{rdd = sparkContext.wholeTextFiles("hdfs://a-hdfs-path")}, then C{rdd} contains:: (a-hdfs-path/part-00000, its content) (a-hdfs-path/part-00001, its content) ... (a-hdfs-path/part-nnnnn, its content) .. note:: Small files are preferred, as each file will be loaded fully in memory. >>> dirPath = os.path.join(tempdir, "files") >>> os.mkdir(dirPath) >>> with open(os.path.join(dirPath, "1.txt"), "w") as file1: ... _ = file1.write("1") >>> with open(os.path.join(dirPath, "2.txt"), "w") as file2: ... _ = file2.write("2") >>> textFiles = sc.wholeTextFiles(dirPath) >>> sorted(textFiles.collect()) [(u'.../1.txt', u'1'), (u'.../2.txt', u'2')] """ minPartitions = minPartitions or self.defaultMinPartitions return RDD(self._jsc.wholeTextFiles(path, minPartitions), self, PairDeserializer(UTF8Deserializer(use_unicode), UTF8Deserializer(use_unicode)))
[ "def", "wholeTextFiles", "(", "self", ",", "path", ",", "minPartitions", "=", "None", ",", "use_unicode", "=", "True", ")", ":", "minPartitions", "=", "minPartitions", "or", "self", ".", "defaultMinPartitions", "return", "RDD", "(", "self", ".", "_jsc", ".", "wholeTextFiles", "(", "path", ",", "minPartitions", ")", ",", "self", ",", "PairDeserializer", "(", "UTF8Deserializer", "(", "use_unicode", ")", ",", "UTF8Deserializer", "(", "use_unicode", ")", ")", ")" ]
Read a directory of text files from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI. Each file is read as a single record and returned in a key-value pair, where the key is the path of each file, the value is the content of each file. The text files must be encoded as UTF-8. If use_unicode is False, the strings will be kept as `str` (encoding as `utf-8`), which is faster and smaller than unicode. (Added in Spark 1.2) For example, if you have the following files:: hdfs://a-hdfs-path/part-00000 hdfs://a-hdfs-path/part-00001 ... hdfs://a-hdfs-path/part-nnnnn Do C{rdd = sparkContext.wholeTextFiles("hdfs://a-hdfs-path")}, then C{rdd} contains:: (a-hdfs-path/part-00000, its content) (a-hdfs-path/part-00001, its content) ... (a-hdfs-path/part-nnnnn, its content) .. note:: Small files are preferred, as each file will be loaded fully in memory. >>> dirPath = os.path.join(tempdir, "files") >>> os.mkdir(dirPath) >>> with open(os.path.join(dirPath, "1.txt"), "w") as file1: ... _ = file1.write("1") >>> with open(os.path.join(dirPath, "2.txt"), "w") as file2: ... _ = file2.write("2") >>> textFiles = sc.wholeTextFiles(dirPath) >>> sorted(textFiles.collect()) [(u'.../1.txt', u'1'), (u'.../2.txt', u'2')]
[ "Read", "a", "directory", "of", "text", "files", "from", "HDFS", "a", "local", "file", "system", "(", "available", "on", "all", "nodes", ")", "or", "any", "Hadoop", "-", "supported", "file", "system", "URI", ".", "Each", "file", "is", "read", "as", "a", "single", "record", "and", "returned", "in", "a", "key", "-", "value", "pair", "where", "the", "key", "is", "the", "path", "of", "each", "file", "the", "value", "is", "the", "content", "of", "each", "file", ".", "The", "text", "files", "must", "be", "encoded", "as", "UTF", "-", "8", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L605-L648
apache/spark
python/pyspark/context.py
SparkContext.binaryFiles
def binaryFiles(self, path, minPartitions=None): """ .. note:: Experimental Read a directory of binary files from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI as a byte array. Each file is read as a single record and returned in a key-value pair, where the key is the path of each file, the value is the content of each file. .. note:: Small files are preferred, large file is also allowable, but may cause bad performance. """ minPartitions = minPartitions or self.defaultMinPartitions return RDD(self._jsc.binaryFiles(path, minPartitions), self, PairDeserializer(UTF8Deserializer(), NoOpSerializer()))
python
def binaryFiles(self, path, minPartitions=None): """ .. note:: Experimental Read a directory of binary files from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI as a byte array. Each file is read as a single record and returned in a key-value pair, where the key is the path of each file, the value is the content of each file. .. note:: Small files are preferred, large file is also allowable, but may cause bad performance. """ minPartitions = minPartitions or self.defaultMinPartitions return RDD(self._jsc.binaryFiles(path, minPartitions), self, PairDeserializer(UTF8Deserializer(), NoOpSerializer()))
[ "def", "binaryFiles", "(", "self", ",", "path", ",", "minPartitions", "=", "None", ")", ":", "minPartitions", "=", "minPartitions", "or", "self", ".", "defaultMinPartitions", "return", "RDD", "(", "self", ".", "_jsc", ".", "binaryFiles", "(", "path", ",", "minPartitions", ")", ",", "self", ",", "PairDeserializer", "(", "UTF8Deserializer", "(", ")", ",", "NoOpSerializer", "(", ")", ")", ")" ]
.. note:: Experimental Read a directory of binary files from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI as a byte array. Each file is read as a single record and returned in a key-value pair, where the key is the path of each file, the value is the content of each file. .. note:: Small files are preferred, large file is also allowable, but may cause bad performance.
[ "..", "note", "::", "Experimental" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L650-L665
apache/spark
python/pyspark/context.py
SparkContext.binaryRecords
def binaryRecords(self, path, recordLength): """ .. note:: Experimental Load data from a flat binary file, assuming each record is a set of numbers with the specified numerical format (see ByteBuffer), and the number of bytes per record is constant. :param path: Directory to the input data files :param recordLength: The length at which to split the records """ return RDD(self._jsc.binaryRecords(path, recordLength), self, NoOpSerializer())
python
def binaryRecords(self, path, recordLength): """ .. note:: Experimental Load data from a flat binary file, assuming each record is a set of numbers with the specified numerical format (see ByteBuffer), and the number of bytes per record is constant. :param path: Directory to the input data files :param recordLength: The length at which to split the records """ return RDD(self._jsc.binaryRecords(path, recordLength), self, NoOpSerializer())
[ "def", "binaryRecords", "(", "self", ",", "path", ",", "recordLength", ")", ":", "return", "RDD", "(", "self", ".", "_jsc", ".", "binaryRecords", "(", "path", ",", "recordLength", ")", ",", "self", ",", "NoOpSerializer", "(", ")", ")" ]
.. note:: Experimental Load data from a flat binary file, assuming each record is a set of numbers with the specified numerical format (see ByteBuffer), and the number of bytes per record is constant. :param path: Directory to the input data files :param recordLength: The length at which to split the records
[ "..", "note", "::", "Experimental" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L667-L678
apache/spark
python/pyspark/context.py
SparkContext.sequenceFile
def sequenceFile(self, path, keyClass=None, valueClass=None, keyConverter=None, valueConverter=None, minSplits=None, batchSize=0): """ Read a Hadoop SequenceFile with arbitrary key and value Writable class from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI. The mechanism is as follows: 1. A Java RDD is created from the SequenceFile or other InputFormat, and the key and value Writable classes 2. Serialization is attempted via Pyrolite pickling 3. If this fails, the fallback is to call 'toString' on each key and value 4. C{PickleSerializer} is used to deserialize pickled objects on the Python side :param path: path to sequncefile :param keyClass: fully qualified classname of key Writable class (e.g. "org.apache.hadoop.io.Text") :param valueClass: fully qualified classname of value Writable class (e.g. "org.apache.hadoop.io.LongWritable") :param keyConverter: :param valueConverter: :param minSplits: minimum splits in dataset (default min(2, sc.defaultParallelism)) :param batchSize: The number of Python objects represented as a single Java object. (default 0, choose batchSize automatically) """ minSplits = minSplits or min(self.defaultParallelism, 2) jrdd = self._jvm.PythonRDD.sequenceFile(self._jsc, path, keyClass, valueClass, keyConverter, valueConverter, minSplits, batchSize) return RDD(jrdd, self)
python
def sequenceFile(self, path, keyClass=None, valueClass=None, keyConverter=None, valueConverter=None, minSplits=None, batchSize=0): """ Read a Hadoop SequenceFile with arbitrary key and value Writable class from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI. The mechanism is as follows: 1. A Java RDD is created from the SequenceFile or other InputFormat, and the key and value Writable classes 2. Serialization is attempted via Pyrolite pickling 3. If this fails, the fallback is to call 'toString' on each key and value 4. C{PickleSerializer} is used to deserialize pickled objects on the Python side :param path: path to sequncefile :param keyClass: fully qualified classname of key Writable class (e.g. "org.apache.hadoop.io.Text") :param valueClass: fully qualified classname of value Writable class (e.g. "org.apache.hadoop.io.LongWritable") :param keyConverter: :param valueConverter: :param minSplits: minimum splits in dataset (default min(2, sc.defaultParallelism)) :param batchSize: The number of Python objects represented as a single Java object. (default 0, choose batchSize automatically) """ minSplits = minSplits or min(self.defaultParallelism, 2) jrdd = self._jvm.PythonRDD.sequenceFile(self._jsc, path, keyClass, valueClass, keyConverter, valueConverter, minSplits, batchSize) return RDD(jrdd, self)
[ "def", "sequenceFile", "(", "self", ",", "path", ",", "keyClass", "=", "None", ",", "valueClass", "=", "None", ",", "keyConverter", "=", "None", ",", "valueConverter", "=", "None", ",", "minSplits", "=", "None", ",", "batchSize", "=", "0", ")", ":", "minSplits", "=", "minSplits", "or", "min", "(", "self", ".", "defaultParallelism", ",", "2", ")", "jrdd", "=", "self", ".", "_jvm", ".", "PythonRDD", ".", "sequenceFile", "(", "self", ".", "_jsc", ",", "path", ",", "keyClass", ",", "valueClass", ",", "keyConverter", ",", "valueConverter", ",", "minSplits", ",", "batchSize", ")", "return", "RDD", "(", "jrdd", ",", "self", ")" ]
Read a Hadoop SequenceFile with arbitrary key and value Writable class from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI. The mechanism is as follows: 1. A Java RDD is created from the SequenceFile or other InputFormat, and the key and value Writable classes 2. Serialization is attempted via Pyrolite pickling 3. If this fails, the fallback is to call 'toString' on each key and value 4. C{PickleSerializer} is used to deserialize pickled objects on the Python side :param path: path to sequncefile :param keyClass: fully qualified classname of key Writable class (e.g. "org.apache.hadoop.io.Text") :param valueClass: fully qualified classname of value Writable class (e.g. "org.apache.hadoop.io.LongWritable") :param keyConverter: :param valueConverter: :param minSplits: minimum splits in dataset (default min(2, sc.defaultParallelism)) :param batchSize: The number of Python objects represented as a single Java object. (default 0, choose batchSize automatically)
[ "Read", "a", "Hadoop", "SequenceFile", "with", "arbitrary", "key", "and", "value", "Writable", "class", "from", "HDFS", "a", "local", "file", "system", "(", "available", "on", "all", "nodes", ")", "or", "any", "Hadoop", "-", "supported", "file", "system", "URI", ".", "The", "mechanism", "is", "as", "follows", ":" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L688-L716
apache/spark
python/pyspark/context.py
SparkContext.newAPIHadoopFile
def newAPIHadoopFile(self, path, inputFormatClass, keyClass, valueClass, keyConverter=None, valueConverter=None, conf=None, batchSize=0): """ Read a 'new API' Hadoop InputFormat with arbitrary key and value class from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI. The mechanism is the same as for sc.sequenceFile. A Hadoop configuration can be passed in as a Python dict. This will be converted into a Configuration in Java :param path: path to Hadoop file :param inputFormatClass: fully qualified classname of Hadoop InputFormat (e.g. "org.apache.hadoop.mapreduce.lib.input.TextInputFormat") :param keyClass: fully qualified classname of key Writable class (e.g. "org.apache.hadoop.io.Text") :param valueClass: fully qualified classname of value Writable class (e.g. "org.apache.hadoop.io.LongWritable") :param keyConverter: (None by default) :param valueConverter: (None by default) :param conf: Hadoop configuration, passed in as a dict (None by default) :param batchSize: The number of Python objects represented as a single Java object. (default 0, choose batchSize automatically) """ jconf = self._dictToJavaMap(conf) jrdd = self._jvm.PythonRDD.newAPIHadoopFile(self._jsc, path, inputFormatClass, keyClass, valueClass, keyConverter, valueConverter, jconf, batchSize) return RDD(jrdd, self)
python
def newAPIHadoopFile(self, path, inputFormatClass, keyClass, valueClass, keyConverter=None, valueConverter=None, conf=None, batchSize=0): """ Read a 'new API' Hadoop InputFormat with arbitrary key and value class from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI. The mechanism is the same as for sc.sequenceFile. A Hadoop configuration can be passed in as a Python dict. This will be converted into a Configuration in Java :param path: path to Hadoop file :param inputFormatClass: fully qualified classname of Hadoop InputFormat (e.g. "org.apache.hadoop.mapreduce.lib.input.TextInputFormat") :param keyClass: fully qualified classname of key Writable class (e.g. "org.apache.hadoop.io.Text") :param valueClass: fully qualified classname of value Writable class (e.g. "org.apache.hadoop.io.LongWritable") :param keyConverter: (None by default) :param valueConverter: (None by default) :param conf: Hadoop configuration, passed in as a dict (None by default) :param batchSize: The number of Python objects represented as a single Java object. (default 0, choose batchSize automatically) """ jconf = self._dictToJavaMap(conf) jrdd = self._jvm.PythonRDD.newAPIHadoopFile(self._jsc, path, inputFormatClass, keyClass, valueClass, keyConverter, valueConverter, jconf, batchSize) return RDD(jrdd, self)
[ "def", "newAPIHadoopFile", "(", "self", ",", "path", ",", "inputFormatClass", ",", "keyClass", ",", "valueClass", ",", "keyConverter", "=", "None", ",", "valueConverter", "=", "None", ",", "conf", "=", "None", ",", "batchSize", "=", "0", ")", ":", "jconf", "=", "self", ".", "_dictToJavaMap", "(", "conf", ")", "jrdd", "=", "self", ".", "_jvm", ".", "PythonRDD", ".", "newAPIHadoopFile", "(", "self", ".", "_jsc", ",", "path", ",", "inputFormatClass", ",", "keyClass", ",", "valueClass", ",", "keyConverter", ",", "valueConverter", ",", "jconf", ",", "batchSize", ")", "return", "RDD", "(", "jrdd", ",", "self", ")" ]
Read a 'new API' Hadoop InputFormat with arbitrary key and value class from HDFS, a local file system (available on all nodes), or any Hadoop-supported file system URI. The mechanism is the same as for sc.sequenceFile. A Hadoop configuration can be passed in as a Python dict. This will be converted into a Configuration in Java :param path: path to Hadoop file :param inputFormatClass: fully qualified classname of Hadoop InputFormat (e.g. "org.apache.hadoop.mapreduce.lib.input.TextInputFormat") :param keyClass: fully qualified classname of key Writable class (e.g. "org.apache.hadoop.io.Text") :param valueClass: fully qualified classname of value Writable class (e.g. "org.apache.hadoop.io.LongWritable") :param keyConverter: (None by default) :param valueConverter: (None by default) :param conf: Hadoop configuration, passed in as a dict (None by default) :param batchSize: The number of Python objects represented as a single Java object. (default 0, choose batchSize automatically)
[ "Read", "a", "new", "API", "Hadoop", "InputFormat", "with", "arbitrary", "key", "and", "value", "class", "from", "HDFS", "a", "local", "file", "system", "(", "available", "on", "all", "nodes", ")", "or", "any", "Hadoop", "-", "supported", "file", "system", "URI", ".", "The", "mechanism", "is", "the", "same", "as", "for", "sc", ".", "sequenceFile", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L718-L746
apache/spark
python/pyspark/context.py
SparkContext.union
def union(self, rdds): """ Build the union of a list of RDDs. This supports unions() of RDDs with different serialized formats, although this forces them to be reserialized using the default serializer: >>> path = os.path.join(tempdir, "union-text.txt") >>> with open(path, "w") as testFile: ... _ = testFile.write("Hello") >>> textFile = sc.textFile(path) >>> textFile.collect() [u'Hello'] >>> parallelized = sc.parallelize(["World!"]) >>> sorted(sc.union([textFile, parallelized]).collect()) [u'Hello', 'World!'] """ first_jrdd_deserializer = rdds[0]._jrdd_deserializer if any(x._jrdd_deserializer != first_jrdd_deserializer for x in rdds): rdds = [x._reserialize() for x in rdds] cls = SparkContext._jvm.org.apache.spark.api.java.JavaRDD jrdds = SparkContext._gateway.new_array(cls, len(rdds)) for i in range(0, len(rdds)): jrdds[i] = rdds[i]._jrdd return RDD(self._jsc.union(jrdds), self, rdds[0]._jrdd_deserializer)
python
def union(self, rdds): """ Build the union of a list of RDDs. This supports unions() of RDDs with different serialized formats, although this forces them to be reserialized using the default serializer: >>> path = os.path.join(tempdir, "union-text.txt") >>> with open(path, "w") as testFile: ... _ = testFile.write("Hello") >>> textFile = sc.textFile(path) >>> textFile.collect() [u'Hello'] >>> parallelized = sc.parallelize(["World!"]) >>> sorted(sc.union([textFile, parallelized]).collect()) [u'Hello', 'World!'] """ first_jrdd_deserializer = rdds[0]._jrdd_deserializer if any(x._jrdd_deserializer != first_jrdd_deserializer for x in rdds): rdds = [x._reserialize() for x in rdds] cls = SparkContext._jvm.org.apache.spark.api.java.JavaRDD jrdds = SparkContext._gateway.new_array(cls, len(rdds)) for i in range(0, len(rdds)): jrdds[i] = rdds[i]._jrdd return RDD(self._jsc.union(jrdds), self, rdds[0]._jrdd_deserializer)
[ "def", "union", "(", "self", ",", "rdds", ")", ":", "first_jrdd_deserializer", "=", "rdds", "[", "0", "]", ".", "_jrdd_deserializer", "if", "any", "(", "x", ".", "_jrdd_deserializer", "!=", "first_jrdd_deserializer", "for", "x", "in", "rdds", ")", ":", "rdds", "=", "[", "x", ".", "_reserialize", "(", ")", "for", "x", "in", "rdds", "]", "cls", "=", "SparkContext", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "api", ".", "java", ".", "JavaRDD", "jrdds", "=", "SparkContext", ".", "_gateway", ".", "new_array", "(", "cls", ",", "len", "(", "rdds", ")", ")", "for", "i", "in", "range", "(", "0", ",", "len", "(", "rdds", ")", ")", ":", "jrdds", "[", "i", "]", "=", "rdds", "[", "i", "]", ".", "_jrdd", "return", "RDD", "(", "self", ".", "_jsc", ".", "union", "(", "jrdds", ")", ",", "self", ",", "rdds", "[", "0", "]", ".", "_jrdd_deserializer", ")" ]
Build the union of a list of RDDs. This supports unions() of RDDs with different serialized formats, although this forces them to be reserialized using the default serializer: >>> path = os.path.join(tempdir, "union-text.txt") >>> with open(path, "w") as testFile: ... _ = testFile.write("Hello") >>> textFile = sc.textFile(path) >>> textFile.collect() [u'Hello'] >>> parallelized = sc.parallelize(["World!"]) >>> sorted(sc.union([textFile, parallelized]).collect()) [u'Hello', 'World!']
[ "Build", "the", "union", "of", "a", "list", "of", "RDDs", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L837-L862
apache/spark
python/pyspark/context.py
SparkContext.accumulator
def accumulator(self, value, accum_param=None): """ Create an L{Accumulator} with the given initial value, using a given L{AccumulatorParam} helper object to define how to add values of the data type if provided. Default AccumulatorParams are used for integers and floating-point numbers if you do not provide one. For other types, a custom AccumulatorParam can be used. """ if accum_param is None: if isinstance(value, int): accum_param = accumulators.INT_ACCUMULATOR_PARAM elif isinstance(value, float): accum_param = accumulators.FLOAT_ACCUMULATOR_PARAM elif isinstance(value, complex): accum_param = accumulators.COMPLEX_ACCUMULATOR_PARAM else: raise TypeError("No default accumulator param for type %s" % type(value)) SparkContext._next_accum_id += 1 return Accumulator(SparkContext._next_accum_id - 1, value, accum_param)
python
def accumulator(self, value, accum_param=None): """ Create an L{Accumulator} with the given initial value, using a given L{AccumulatorParam} helper object to define how to add values of the data type if provided. Default AccumulatorParams are used for integers and floating-point numbers if you do not provide one. For other types, a custom AccumulatorParam can be used. """ if accum_param is None: if isinstance(value, int): accum_param = accumulators.INT_ACCUMULATOR_PARAM elif isinstance(value, float): accum_param = accumulators.FLOAT_ACCUMULATOR_PARAM elif isinstance(value, complex): accum_param = accumulators.COMPLEX_ACCUMULATOR_PARAM else: raise TypeError("No default accumulator param for type %s" % type(value)) SparkContext._next_accum_id += 1 return Accumulator(SparkContext._next_accum_id - 1, value, accum_param)
[ "def", "accumulator", "(", "self", ",", "value", ",", "accum_param", "=", "None", ")", ":", "if", "accum_param", "is", "None", ":", "if", "isinstance", "(", "value", ",", "int", ")", ":", "accum_param", "=", "accumulators", ".", "INT_ACCUMULATOR_PARAM", "elif", "isinstance", "(", "value", ",", "float", ")", ":", "accum_param", "=", "accumulators", ".", "FLOAT_ACCUMULATOR_PARAM", "elif", "isinstance", "(", "value", ",", "complex", ")", ":", "accum_param", "=", "accumulators", ".", "COMPLEX_ACCUMULATOR_PARAM", "else", ":", "raise", "TypeError", "(", "\"No default accumulator param for type %s\"", "%", "type", "(", "value", ")", ")", "SparkContext", ".", "_next_accum_id", "+=", "1", "return", "Accumulator", "(", "SparkContext", ".", "_next_accum_id", "-", "1", ",", "value", ",", "accum_param", ")" ]
Create an L{Accumulator} with the given initial value, using a given L{AccumulatorParam} helper object to define how to add values of the data type if provided. Default AccumulatorParams are used for integers and floating-point numbers if you do not provide one. For other types, a custom AccumulatorParam can be used.
[ "Create", "an", "L", "{", "Accumulator", "}", "with", "the", "given", "initial", "value", "using", "a", "given", "L", "{", "AccumulatorParam", "}", "helper", "object", "to", "define", "how", "to", "add", "values", "of", "the", "data", "type", "if", "provided", ".", "Default", "AccumulatorParams", "are", "used", "for", "integers", "and", "floating", "-", "point", "numbers", "if", "you", "do", "not", "provide", "one", ".", "For", "other", "types", "a", "custom", "AccumulatorParam", "can", "be", "used", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L873-L891
apache/spark
python/pyspark/context.py
SparkContext.addFile
def addFile(self, path, recursive=False): """ Add a file to be downloaded with this Spark job on every node. The C{path} passed can be either a local file, a file in HDFS (or other Hadoop-supported filesystems), or an HTTP, HTTPS or FTP URI. To access the file in Spark jobs, use L{SparkFiles.get(fileName)<pyspark.files.SparkFiles.get>} with the filename to find its download location. A directory can be given if the recursive option is set to True. Currently directories are only supported for Hadoop-supported filesystems. .. note:: A path can be added only once. Subsequent additions of the same path are ignored. >>> from pyspark import SparkFiles >>> path = os.path.join(tempdir, "test.txt") >>> with open(path, "w") as testFile: ... _ = testFile.write("100") >>> sc.addFile(path) >>> def func(iterator): ... with open(SparkFiles.get("test.txt")) as testFile: ... fileVal = int(testFile.readline()) ... return [x * fileVal for x in iterator] >>> sc.parallelize([1, 2, 3, 4]).mapPartitions(func).collect() [100, 200, 300, 400] """ self._jsc.sc().addFile(path, recursive)
python
def addFile(self, path, recursive=False): """ Add a file to be downloaded with this Spark job on every node. The C{path} passed can be either a local file, a file in HDFS (or other Hadoop-supported filesystems), or an HTTP, HTTPS or FTP URI. To access the file in Spark jobs, use L{SparkFiles.get(fileName)<pyspark.files.SparkFiles.get>} with the filename to find its download location. A directory can be given if the recursive option is set to True. Currently directories are only supported for Hadoop-supported filesystems. .. note:: A path can be added only once. Subsequent additions of the same path are ignored. >>> from pyspark import SparkFiles >>> path = os.path.join(tempdir, "test.txt") >>> with open(path, "w") as testFile: ... _ = testFile.write("100") >>> sc.addFile(path) >>> def func(iterator): ... with open(SparkFiles.get("test.txt")) as testFile: ... fileVal = int(testFile.readline()) ... return [x * fileVal for x in iterator] >>> sc.parallelize([1, 2, 3, 4]).mapPartitions(func).collect() [100, 200, 300, 400] """ self._jsc.sc().addFile(path, recursive)
[ "def", "addFile", "(", "self", ",", "path", ",", "recursive", "=", "False", ")", ":", "self", ".", "_jsc", ".", "sc", "(", ")", ".", "addFile", "(", "path", ",", "recursive", ")" ]
Add a file to be downloaded with this Spark job on every node. The C{path} passed can be either a local file, a file in HDFS (or other Hadoop-supported filesystems), or an HTTP, HTTPS or FTP URI. To access the file in Spark jobs, use L{SparkFiles.get(fileName)<pyspark.files.SparkFiles.get>} with the filename to find its download location. A directory can be given if the recursive option is set to True. Currently directories are only supported for Hadoop-supported filesystems. .. note:: A path can be added only once. Subsequent additions of the same path are ignored. >>> from pyspark import SparkFiles >>> path = os.path.join(tempdir, "test.txt") >>> with open(path, "w") as testFile: ... _ = testFile.write("100") >>> sc.addFile(path) >>> def func(iterator): ... with open(SparkFiles.get("test.txt")) as testFile: ... fileVal = int(testFile.readline()) ... return [x * fileVal for x in iterator] >>> sc.parallelize([1, 2, 3, 4]).mapPartitions(func).collect() [100, 200, 300, 400]
[ "Add", "a", "file", "to", "be", "downloaded", "with", "this", "Spark", "job", "on", "every", "node", ".", "The", "C", "{", "path", "}", "passed", "can", "be", "either", "a", "local", "file", "a", "file", "in", "HDFS", "(", "or", "other", "Hadoop", "-", "supported", "filesystems", ")", "or", "an", "HTTP", "HTTPS", "or", "FTP", "URI", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L893-L921
apache/spark
python/pyspark/context.py
SparkContext.addPyFile
def addPyFile(self, path): """ Add a .py or .zip dependency for all tasks to be executed on this SparkContext in the future. The C{path} passed can be either a local file, a file in HDFS (or other Hadoop-supported filesystems), or an HTTP, HTTPS or FTP URI. .. note:: A path can be added only once. Subsequent additions of the same path are ignored. """ self.addFile(path) (dirname, filename) = os.path.split(path) # dirname may be directory or HDFS/S3 prefix if filename[-4:].lower() in self.PACKAGE_EXTENSIONS: self._python_includes.append(filename) # for tests in local mode sys.path.insert(1, os.path.join(SparkFiles.getRootDirectory(), filename)) if sys.version > '3': import importlib importlib.invalidate_caches()
python
def addPyFile(self, path): """ Add a .py or .zip dependency for all tasks to be executed on this SparkContext in the future. The C{path} passed can be either a local file, a file in HDFS (or other Hadoop-supported filesystems), or an HTTP, HTTPS or FTP URI. .. note:: A path can be added only once. Subsequent additions of the same path are ignored. """ self.addFile(path) (dirname, filename) = os.path.split(path) # dirname may be directory or HDFS/S3 prefix if filename[-4:].lower() in self.PACKAGE_EXTENSIONS: self._python_includes.append(filename) # for tests in local mode sys.path.insert(1, os.path.join(SparkFiles.getRootDirectory(), filename)) if sys.version > '3': import importlib importlib.invalidate_caches()
[ "def", "addPyFile", "(", "self", ",", "path", ")", ":", "self", ".", "addFile", "(", "path", ")", "(", "dirname", ",", "filename", ")", "=", "os", ".", "path", ".", "split", "(", "path", ")", "# dirname may be directory or HDFS/S3 prefix", "if", "filename", "[", "-", "4", ":", "]", ".", "lower", "(", ")", "in", "self", ".", "PACKAGE_EXTENSIONS", ":", "self", ".", "_python_includes", ".", "append", "(", "filename", ")", "# for tests in local mode", "sys", ".", "path", ".", "insert", "(", "1", ",", "os", ".", "path", ".", "join", "(", "SparkFiles", ".", "getRootDirectory", "(", ")", ",", "filename", ")", ")", "if", "sys", ".", "version", ">", "'3'", ":", "import", "importlib", "importlib", ".", "invalidate_caches", "(", ")" ]
Add a .py or .zip dependency for all tasks to be executed on this SparkContext in the future. The C{path} passed can be either a local file, a file in HDFS (or other Hadoop-supported filesystems), or an HTTP, HTTPS or FTP URI. .. note:: A path can be added only once. Subsequent additions of the same path are ignored.
[ "Add", "a", ".", "py", "or", ".", "zip", "dependency", "for", "all", "tasks", "to", "be", "executed", "on", "this", "SparkContext", "in", "the", "future", ".", "The", "C", "{", "path", "}", "passed", "can", "be", "either", "a", "local", "file", "a", "file", "in", "HDFS", "(", "or", "other", "Hadoop", "-", "supported", "filesystems", ")", "or", "an", "HTTP", "HTTPS", "or", "FTP", "URI", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L923-L940
apache/spark
python/pyspark/context.py
SparkContext._getJavaStorageLevel
def _getJavaStorageLevel(self, storageLevel): """ Returns a Java StorageLevel based on a pyspark.StorageLevel. """ if not isinstance(storageLevel, StorageLevel): raise Exception("storageLevel must be of type pyspark.StorageLevel") newStorageLevel = self._jvm.org.apache.spark.storage.StorageLevel return newStorageLevel(storageLevel.useDisk, storageLevel.useMemory, storageLevel.useOffHeap, storageLevel.deserialized, storageLevel.replication)
python
def _getJavaStorageLevel(self, storageLevel): """ Returns a Java StorageLevel based on a pyspark.StorageLevel. """ if not isinstance(storageLevel, StorageLevel): raise Exception("storageLevel must be of type pyspark.StorageLevel") newStorageLevel = self._jvm.org.apache.spark.storage.StorageLevel return newStorageLevel(storageLevel.useDisk, storageLevel.useMemory, storageLevel.useOffHeap, storageLevel.deserialized, storageLevel.replication)
[ "def", "_getJavaStorageLevel", "(", "self", ",", "storageLevel", ")", ":", "if", "not", "isinstance", "(", "storageLevel", ",", "StorageLevel", ")", ":", "raise", "Exception", "(", "\"storageLevel must be of type pyspark.StorageLevel\"", ")", "newStorageLevel", "=", "self", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "storage", ".", "StorageLevel", "return", "newStorageLevel", "(", "storageLevel", ".", "useDisk", ",", "storageLevel", ".", "useMemory", ",", "storageLevel", ".", "useOffHeap", ",", "storageLevel", ".", "deserialized", ",", "storageLevel", ".", "replication", ")" ]
Returns a Java StorageLevel based on a pyspark.StorageLevel.
[ "Returns", "a", "Java", "StorageLevel", "based", "on", "a", "pyspark", ".", "StorageLevel", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L949-L961
apache/spark
python/pyspark/context.py
SparkContext.setJobGroup
def setJobGroup(self, groupId, description, interruptOnCancel=False): """ Assigns a group ID to all the jobs started by this thread until the group ID is set to a different value or cleared. Often, a unit of execution in an application consists of multiple Spark actions or jobs. Application programmers can use this method to group all those jobs together and give a group description. Once set, the Spark web UI will associate such jobs with this group. The application can use L{SparkContext.cancelJobGroup} to cancel all running jobs in this group. >>> import threading >>> from time import sleep >>> result = "Not Set" >>> lock = threading.Lock() >>> def map_func(x): ... sleep(100) ... raise Exception("Task should have been cancelled") >>> def start_job(x): ... global result ... try: ... sc.setJobGroup("job_to_cancel", "some description") ... result = sc.parallelize(range(x)).map(map_func).collect() ... except Exception as e: ... result = "Cancelled" ... lock.release() >>> def stop_job(): ... sleep(5) ... sc.cancelJobGroup("job_to_cancel") >>> suppress = lock.acquire() >>> suppress = threading.Thread(target=start_job, args=(10,)).start() >>> suppress = threading.Thread(target=stop_job).start() >>> suppress = lock.acquire() >>> print(result) Cancelled If interruptOnCancel is set to true for the job group, then job cancellation will result in Thread.interrupt() being called on the job's executor threads. This is useful to help ensure that the tasks are actually stopped in a timely manner, but is off by default due to HDFS-1208, where HDFS may respond to Thread.interrupt() by marking nodes as dead. """ self._jsc.setJobGroup(groupId, description, interruptOnCancel)
python
def setJobGroup(self, groupId, description, interruptOnCancel=False): """ Assigns a group ID to all the jobs started by this thread until the group ID is set to a different value or cleared. Often, a unit of execution in an application consists of multiple Spark actions or jobs. Application programmers can use this method to group all those jobs together and give a group description. Once set, the Spark web UI will associate such jobs with this group. The application can use L{SparkContext.cancelJobGroup} to cancel all running jobs in this group. >>> import threading >>> from time import sleep >>> result = "Not Set" >>> lock = threading.Lock() >>> def map_func(x): ... sleep(100) ... raise Exception("Task should have been cancelled") >>> def start_job(x): ... global result ... try: ... sc.setJobGroup("job_to_cancel", "some description") ... result = sc.parallelize(range(x)).map(map_func).collect() ... except Exception as e: ... result = "Cancelled" ... lock.release() >>> def stop_job(): ... sleep(5) ... sc.cancelJobGroup("job_to_cancel") >>> suppress = lock.acquire() >>> suppress = threading.Thread(target=start_job, args=(10,)).start() >>> suppress = threading.Thread(target=stop_job).start() >>> suppress = lock.acquire() >>> print(result) Cancelled If interruptOnCancel is set to true for the job group, then job cancellation will result in Thread.interrupt() being called on the job's executor threads. This is useful to help ensure that the tasks are actually stopped in a timely manner, but is off by default due to HDFS-1208, where HDFS may respond to Thread.interrupt() by marking nodes as dead. """ self._jsc.setJobGroup(groupId, description, interruptOnCancel)
[ "def", "setJobGroup", "(", "self", ",", "groupId", ",", "description", ",", "interruptOnCancel", "=", "False", ")", ":", "self", ".", "_jsc", ".", "setJobGroup", "(", "groupId", ",", "description", ",", "interruptOnCancel", ")" ]
Assigns a group ID to all the jobs started by this thread until the group ID is set to a different value or cleared. Often, a unit of execution in an application consists of multiple Spark actions or jobs. Application programmers can use this method to group all those jobs together and give a group description. Once set, the Spark web UI will associate such jobs with this group. The application can use L{SparkContext.cancelJobGroup} to cancel all running jobs in this group. >>> import threading >>> from time import sleep >>> result = "Not Set" >>> lock = threading.Lock() >>> def map_func(x): ... sleep(100) ... raise Exception("Task should have been cancelled") >>> def start_job(x): ... global result ... try: ... sc.setJobGroup("job_to_cancel", "some description") ... result = sc.parallelize(range(x)).map(map_func).collect() ... except Exception as e: ... result = "Cancelled" ... lock.release() >>> def stop_job(): ... sleep(5) ... sc.cancelJobGroup("job_to_cancel") >>> suppress = lock.acquire() >>> suppress = threading.Thread(target=start_job, args=(10,)).start() >>> suppress = threading.Thread(target=stop_job).start() >>> suppress = lock.acquire() >>> print(result) Cancelled If interruptOnCancel is set to true for the job group, then job cancellation will result in Thread.interrupt() being called on the job's executor threads. This is useful to help ensure that the tasks are actually stopped in a timely manner, but is off by default due to HDFS-1208, where HDFS may respond to Thread.interrupt() by marking nodes as dead.
[ "Assigns", "a", "group", "ID", "to", "all", "the", "jobs", "started", "by", "this", "thread", "until", "the", "group", "ID", "is", "set", "to", "a", "different", "value", "or", "cleared", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L963-L1005
apache/spark
python/pyspark/context.py
SparkContext.runJob
def runJob(self, rdd, partitionFunc, partitions=None, allowLocal=False): """ Executes the given partitionFunc on the specified set of partitions, returning the result as an array of elements. If 'partitions' is not specified, this will run over all partitions. >>> myRDD = sc.parallelize(range(6), 3) >>> sc.runJob(myRDD, lambda part: [x * x for x in part]) [0, 1, 4, 9, 16, 25] >>> myRDD = sc.parallelize(range(6), 3) >>> sc.runJob(myRDD, lambda part: [x * x for x in part], [0, 2], True) [0, 1, 16, 25] """ if partitions is None: partitions = range(rdd._jrdd.partitions().size()) # Implementation note: This is implemented as a mapPartitions followed # by runJob() in order to avoid having to pass a Python lambda into # SparkContext#runJob. mappedRDD = rdd.mapPartitions(partitionFunc) sock_info = self._jvm.PythonRDD.runJob(self._jsc.sc(), mappedRDD._jrdd, partitions) return list(_load_from_socket(sock_info, mappedRDD._jrdd_deserializer))
python
def runJob(self, rdd, partitionFunc, partitions=None, allowLocal=False): """ Executes the given partitionFunc on the specified set of partitions, returning the result as an array of elements. If 'partitions' is not specified, this will run over all partitions. >>> myRDD = sc.parallelize(range(6), 3) >>> sc.runJob(myRDD, lambda part: [x * x for x in part]) [0, 1, 4, 9, 16, 25] >>> myRDD = sc.parallelize(range(6), 3) >>> sc.runJob(myRDD, lambda part: [x * x for x in part], [0, 2], True) [0, 1, 16, 25] """ if partitions is None: partitions = range(rdd._jrdd.partitions().size()) # Implementation note: This is implemented as a mapPartitions followed # by runJob() in order to avoid having to pass a Python lambda into # SparkContext#runJob. mappedRDD = rdd.mapPartitions(partitionFunc) sock_info = self._jvm.PythonRDD.runJob(self._jsc.sc(), mappedRDD._jrdd, partitions) return list(_load_from_socket(sock_info, mappedRDD._jrdd_deserializer))
[ "def", "runJob", "(", "self", ",", "rdd", ",", "partitionFunc", ",", "partitions", "=", "None", ",", "allowLocal", "=", "False", ")", ":", "if", "partitions", "is", "None", ":", "partitions", "=", "range", "(", "rdd", ".", "_jrdd", ".", "partitions", "(", ")", ".", "size", "(", ")", ")", "# Implementation note: This is implemented as a mapPartitions followed", "# by runJob() in order to avoid having to pass a Python lambda into", "# SparkContext#runJob.", "mappedRDD", "=", "rdd", ".", "mapPartitions", "(", "partitionFunc", ")", "sock_info", "=", "self", ".", "_jvm", ".", "PythonRDD", ".", "runJob", "(", "self", ".", "_jsc", ".", "sc", "(", ")", ",", "mappedRDD", ".", "_jrdd", ",", "partitions", ")", "return", "list", "(", "_load_from_socket", "(", "sock_info", ",", "mappedRDD", ".", "_jrdd_deserializer", ")", ")" ]
Executes the given partitionFunc on the specified set of partitions, returning the result as an array of elements. If 'partitions' is not specified, this will run over all partitions. >>> myRDD = sc.parallelize(range(6), 3) >>> sc.runJob(myRDD, lambda part: [x * x for x in part]) [0, 1, 4, 9, 16, 25] >>> myRDD = sc.parallelize(range(6), 3) >>> sc.runJob(myRDD, lambda part: [x * x for x in part], [0, 2], True) [0, 1, 16, 25]
[ "Executes", "the", "given", "partitionFunc", "on", "the", "specified", "set", "of", "partitions", "returning", "the", "result", "as", "an", "array", "of", "elements", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L1052-L1075
apache/spark
python/pyspark/context.py
SparkContext.dump_profiles
def dump_profiles(self, path): """ Dump the profile stats into directory `path` """ if self.profiler_collector is not None: self.profiler_collector.dump_profiles(path) else: raise RuntimeError("'spark.python.profile' configuration must be set " "to 'true' to enable Python profile.")
python
def dump_profiles(self, path): """ Dump the profile stats into directory `path` """ if self.profiler_collector is not None: self.profiler_collector.dump_profiles(path) else: raise RuntimeError("'spark.python.profile' configuration must be set " "to 'true' to enable Python profile.")
[ "def", "dump_profiles", "(", "self", ",", "path", ")", ":", "if", "self", ".", "profiler_collector", "is", "not", "None", ":", "self", ".", "profiler_collector", ".", "dump_profiles", "(", "path", ")", "else", ":", "raise", "RuntimeError", "(", "\"'spark.python.profile' configuration must be set \"", "\"to 'true' to enable Python profile.\"", ")" ]
Dump the profile stats into directory `path`
[ "Dump", "the", "profile", "stats", "into", "directory", "path" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/context.py#L1085-L1092
apache/spark
python/pyspark/mllib/recommendation.py
ALS.train
def train(cls, ratings, rank, iterations=5, lambda_=0.01, blocks=-1, nonnegative=False, seed=None): """ Train a matrix factorization model given an RDD of ratings by users for a subset of products. The ratings matrix is approximated as the product of two lower-rank matrices of a given rank (number of features). To solve for these features, ALS is run iteratively with a configurable level of parallelism. :param ratings: RDD of `Rating` or (userID, productID, rating) tuple. :param rank: Number of features to use (also referred to as the number of latent factors). :param iterations: Number of iterations of ALS. (default: 5) :param lambda_: Regularization parameter. (default: 0.01) :param blocks: Number of blocks used to parallelize the computation. A value of -1 will use an auto-configured number of blocks. (default: -1) :param nonnegative: A value of True will solve least-squares with nonnegativity constraints. (default: False) :param seed: Random seed for initial matrix factorization model. A value of None will use system time as the seed. (default: None) """ model = callMLlibFunc("trainALSModel", cls._prepare(ratings), rank, iterations, lambda_, blocks, nonnegative, seed) return MatrixFactorizationModel(model)
python
def train(cls, ratings, rank, iterations=5, lambda_=0.01, blocks=-1, nonnegative=False, seed=None): """ Train a matrix factorization model given an RDD of ratings by users for a subset of products. The ratings matrix is approximated as the product of two lower-rank matrices of a given rank (number of features). To solve for these features, ALS is run iteratively with a configurable level of parallelism. :param ratings: RDD of `Rating` or (userID, productID, rating) tuple. :param rank: Number of features to use (also referred to as the number of latent factors). :param iterations: Number of iterations of ALS. (default: 5) :param lambda_: Regularization parameter. (default: 0.01) :param blocks: Number of blocks used to parallelize the computation. A value of -1 will use an auto-configured number of blocks. (default: -1) :param nonnegative: A value of True will solve least-squares with nonnegativity constraints. (default: False) :param seed: Random seed for initial matrix factorization model. A value of None will use system time as the seed. (default: None) """ model = callMLlibFunc("trainALSModel", cls._prepare(ratings), rank, iterations, lambda_, blocks, nonnegative, seed) return MatrixFactorizationModel(model)
[ "def", "train", "(", "cls", ",", "ratings", ",", "rank", ",", "iterations", "=", "5", ",", "lambda_", "=", "0.01", ",", "blocks", "=", "-", "1", ",", "nonnegative", "=", "False", ",", "seed", "=", "None", ")", ":", "model", "=", "callMLlibFunc", "(", "\"trainALSModel\"", ",", "cls", ".", "_prepare", "(", "ratings", ")", ",", "rank", ",", "iterations", ",", "lambda_", ",", "blocks", ",", "nonnegative", ",", "seed", ")", "return", "MatrixFactorizationModel", "(", "model", ")" ]
Train a matrix factorization model given an RDD of ratings by users for a subset of products. The ratings matrix is approximated as the product of two lower-rank matrices of a given rank (number of features). To solve for these features, ALS is run iteratively with a configurable level of parallelism. :param ratings: RDD of `Rating` or (userID, productID, rating) tuple. :param rank: Number of features to use (also referred to as the number of latent factors). :param iterations: Number of iterations of ALS. (default: 5) :param lambda_: Regularization parameter. (default: 0.01) :param blocks: Number of blocks used to parallelize the computation. A value of -1 will use an auto-configured number of blocks. (default: -1) :param nonnegative: A value of True will solve least-squares with nonnegativity constraints. (default: False) :param seed: Random seed for initial matrix factorization model. A value of None will use system time as the seed. (default: None)
[ "Train", "a", "matrix", "factorization", "model", "given", "an", "RDD", "of", "ratings", "by", "users", "for", "a", "subset", "of", "products", ".", "The", "ratings", "matrix", "is", "approximated", "as", "the", "product", "of", "two", "lower", "-", "rank", "matrices", "of", "a", "given", "rank", "(", "number", "of", "features", ")", ".", "To", "solve", "for", "these", "features", "ALS", "is", "run", "iteratively", "with", "a", "configurable", "level", "of", "parallelism", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/recommendation.py#L241-L275
apache/spark
python/pyspark/mllib/fpm.py
FPGrowth.train
def train(cls, data, minSupport=0.3, numPartitions=-1): """ Computes an FP-Growth model that contains frequent itemsets. :param data: The input data set, each element contains a transaction. :param minSupport: The minimal support level. (default: 0.3) :param numPartitions: The number of partitions used by parallel FP-growth. A value of -1 will use the same number as input data. (default: -1) """ model = callMLlibFunc("trainFPGrowthModel", data, float(minSupport), int(numPartitions)) return FPGrowthModel(model)
python
def train(cls, data, minSupport=0.3, numPartitions=-1): """ Computes an FP-Growth model that contains frequent itemsets. :param data: The input data set, each element contains a transaction. :param minSupport: The minimal support level. (default: 0.3) :param numPartitions: The number of partitions used by parallel FP-growth. A value of -1 will use the same number as input data. (default: -1) """ model = callMLlibFunc("trainFPGrowthModel", data, float(minSupport), int(numPartitions)) return FPGrowthModel(model)
[ "def", "train", "(", "cls", ",", "data", ",", "minSupport", "=", "0.3", ",", "numPartitions", "=", "-", "1", ")", ":", "model", "=", "callMLlibFunc", "(", "\"trainFPGrowthModel\"", ",", "data", ",", "float", "(", "minSupport", ")", ",", "int", "(", "numPartitions", ")", ")", "return", "FPGrowthModel", "(", "model", ")" ]
Computes an FP-Growth model that contains frequent itemsets. :param data: The input data set, each element contains a transaction. :param minSupport: The minimal support level. (default: 0.3) :param numPartitions: The number of partitions used by parallel FP-growth. A value of -1 will use the same number as input data. (default: -1)
[ "Computes", "an", "FP", "-", "Growth", "model", "that", "contains", "frequent", "itemsets", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/fpm.py#L78-L93
apache/spark
python/pyspark/mllib/fpm.py
PrefixSpan.train
def train(cls, data, minSupport=0.1, maxPatternLength=10, maxLocalProjDBSize=32000000): """ Finds the complete set of frequent sequential patterns in the input sequences of itemsets. :param data: The input data set, each element contains a sequence of itemsets. :param minSupport: The minimal support level of the sequential pattern, any pattern that appears more than (minSupport * size-of-the-dataset) times will be output. (default: 0.1) :param maxPatternLength: The maximal length of the sequential pattern, any pattern that appears less than maxPatternLength will be output. (default: 10) :param maxLocalProjDBSize: The maximum number of items (including delimiters used in the internal storage format) allowed in a projected database before local processing. If a projected database exceeds this size, another iteration of distributed prefix growth is run. (default: 32000000) """ model = callMLlibFunc("trainPrefixSpanModel", data, minSupport, maxPatternLength, maxLocalProjDBSize) return PrefixSpanModel(model)
python
def train(cls, data, minSupport=0.1, maxPatternLength=10, maxLocalProjDBSize=32000000): """ Finds the complete set of frequent sequential patterns in the input sequences of itemsets. :param data: The input data set, each element contains a sequence of itemsets. :param minSupport: The minimal support level of the sequential pattern, any pattern that appears more than (minSupport * size-of-the-dataset) times will be output. (default: 0.1) :param maxPatternLength: The maximal length of the sequential pattern, any pattern that appears less than maxPatternLength will be output. (default: 10) :param maxLocalProjDBSize: The maximum number of items (including delimiters used in the internal storage format) allowed in a projected database before local processing. If a projected database exceeds this size, another iteration of distributed prefix growth is run. (default: 32000000) """ model = callMLlibFunc("trainPrefixSpanModel", data, minSupport, maxPatternLength, maxLocalProjDBSize) return PrefixSpanModel(model)
[ "def", "train", "(", "cls", ",", "data", ",", "minSupport", "=", "0.1", ",", "maxPatternLength", "=", "10", ",", "maxLocalProjDBSize", "=", "32000000", ")", ":", "model", "=", "callMLlibFunc", "(", "\"trainPrefixSpanModel\"", ",", "data", ",", "minSupport", ",", "maxPatternLength", ",", "maxLocalProjDBSize", ")", "return", "PrefixSpanModel", "(", "model", ")" ]
Finds the complete set of frequent sequential patterns in the input sequences of itemsets. :param data: The input data set, each element contains a sequence of itemsets. :param minSupport: The minimal support level of the sequential pattern, any pattern that appears more than (minSupport * size-of-the-dataset) times will be output. (default: 0.1) :param maxPatternLength: The maximal length of the sequential pattern, any pattern that appears less than maxPatternLength will be output. (default: 10) :param maxLocalProjDBSize: The maximum number of items (including delimiters used in the internal storage format) allowed in a projected database before local processing. If a projected database exceeds this size, another iteration of distributed prefix growth is run. (default: 32000000)
[ "Finds", "the", "complete", "set", "of", "frequent", "sequential", "patterns", "in", "the", "input", "sequences", "of", "itemsets", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/fpm.py#L140-L166
apache/spark
python/pyspark/mllib/stat/KernelDensity.py
KernelDensity.setSample
def setSample(self, sample): """Set sample points from the population. Should be a RDD""" if not isinstance(sample, RDD): raise TypeError("samples should be a RDD, received %s" % type(sample)) self._sample = sample
python
def setSample(self, sample): """Set sample points from the population. Should be a RDD""" if not isinstance(sample, RDD): raise TypeError("samples should be a RDD, received %s" % type(sample)) self._sample = sample
[ "def", "setSample", "(", "self", ",", "sample", ")", ":", "if", "not", "isinstance", "(", "sample", ",", "RDD", ")", ":", "raise", "TypeError", "(", "\"samples should be a RDD, received %s\"", "%", "type", "(", "sample", ")", ")", "self", ".", "_sample", "=", "sample" ]
Set sample points from the population. Should be a RDD
[ "Set", "sample", "points", "from", "the", "population", ".", "Should", "be", "a", "RDD" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/stat/KernelDensity.py#L48-L52
apache/spark
python/pyspark/mllib/stat/KernelDensity.py
KernelDensity.estimate
def estimate(self, points): """Estimate the probability density at points""" points = list(points) densities = callMLlibFunc( "estimateKernelDensity", self._sample, self._bandwidth, points) return np.asarray(densities)
python
def estimate(self, points): """Estimate the probability density at points""" points = list(points) densities = callMLlibFunc( "estimateKernelDensity", self._sample, self._bandwidth, points) return np.asarray(densities)
[ "def", "estimate", "(", "self", ",", "points", ")", ":", "points", "=", "list", "(", "points", ")", "densities", "=", "callMLlibFunc", "(", "\"estimateKernelDensity\"", ",", "self", ".", "_sample", ",", "self", ".", "_bandwidth", ",", "points", ")", "return", "np", ".", "asarray", "(", "densities", ")" ]
Estimate the probability density at points
[ "Estimate", "the", "probability", "density", "at", "points" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/stat/KernelDensity.py#L54-L59
apache/spark
python/pyspark/accumulators.py
_start_update_server
def _start_update_server(auth_token): """Start a TCP server to receive accumulator updates in a daemon thread, and returns it""" server = AccumulatorServer(("localhost", 0), _UpdateRequestHandler, auth_token) thread = threading.Thread(target=server.serve_forever) thread.daemon = True thread.start() return server
python
def _start_update_server(auth_token): """Start a TCP server to receive accumulator updates in a daemon thread, and returns it""" server = AccumulatorServer(("localhost", 0), _UpdateRequestHandler, auth_token) thread = threading.Thread(target=server.serve_forever) thread.daemon = True thread.start() return server
[ "def", "_start_update_server", "(", "auth_token", ")", ":", "server", "=", "AccumulatorServer", "(", "(", "\"localhost\"", ",", "0", ")", ",", "_UpdateRequestHandler", ",", "auth_token", ")", "thread", "=", "threading", ".", "Thread", "(", "target", "=", "server", ".", "serve_forever", ")", "thread", ".", "daemon", "=", "True", "thread", ".", "start", "(", ")", "return", "server" ]
Start a TCP server to receive accumulator updates in a daemon thread, and returns it
[ "Start", "a", "TCP", "server", "to", "receive", "accumulator", "updates", "in", "a", "daemon", "thread", "and", "returns", "it" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/accumulators.py#L289-L295
apache/spark
python/pyspark/accumulators.py
Accumulator.add
def add(self, term): """Adds a term to this accumulator's value""" self._value = self.accum_param.addInPlace(self._value, term)
python
def add(self, term): """Adds a term to this accumulator's value""" self._value = self.accum_param.addInPlace(self._value, term)
[ "def", "add", "(", "self", ",", "term", ")", ":", "self", ".", "_value", "=", "self", ".", "accum_param", ".", "addInPlace", "(", "self", ".", "_value", ",", "term", ")" ]
Adds a term to this accumulator's value
[ "Adds", "a", "term", "to", "this", "accumulator", "s", "value" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/accumulators.py#L163-L165
apache/spark
python/pyspark/sql/group.py
GroupedData.agg
def agg(self, *exprs): """Compute aggregates and returns the result as a :class:`DataFrame`. The available aggregate functions can be: 1. built-in aggregation functions, such as `avg`, `max`, `min`, `sum`, `count` 2. group aggregate pandas UDFs, created with :func:`pyspark.sql.functions.pandas_udf` .. note:: There is no partial aggregation with group aggregate UDFs, i.e., a full shuffle is required. Also, all the data of a group will be loaded into memory, so the user should be aware of the potential OOM risk if data is skewed and certain groups are too large to fit in memory. .. seealso:: :func:`pyspark.sql.functions.pandas_udf` If ``exprs`` is a single :class:`dict` mapping from string to string, then the key is the column to perform aggregation on, and the value is the aggregate function. Alternatively, ``exprs`` can also be a list of aggregate :class:`Column` expressions. .. note:: Built-in aggregation functions and group aggregate pandas UDFs cannot be mixed in a single call to this function. :param exprs: a dict mapping from column name (string) to aggregate functions (string), or a list of :class:`Column`. >>> gdf = df.groupBy(df.name) >>> sorted(gdf.agg({"*": "count"}).collect()) [Row(name=u'Alice', count(1)=1), Row(name=u'Bob', count(1)=1)] >>> from pyspark.sql import functions as F >>> sorted(gdf.agg(F.min(df.age)).collect()) [Row(name=u'Alice', min(age)=2), Row(name=u'Bob', min(age)=5)] >>> from pyspark.sql.functions import pandas_udf, PandasUDFType >>> @pandas_udf('int', PandasUDFType.GROUPED_AGG) # doctest: +SKIP ... def min_udf(v): ... return v.min() >>> sorted(gdf.agg(min_udf(df.age)).collect()) # doctest: +SKIP [Row(name=u'Alice', min_udf(age)=2), Row(name=u'Bob', min_udf(age)=5)] """ assert exprs, "exprs should not be empty" if len(exprs) == 1 and isinstance(exprs[0], dict): jdf = self._jgd.agg(exprs[0]) else: # Columns assert all(isinstance(c, Column) for c in exprs), "all exprs should be Column" jdf = self._jgd.agg(exprs[0]._jc, _to_seq(self.sql_ctx._sc, [c._jc for c in exprs[1:]])) return DataFrame(jdf, self.sql_ctx)
python
def agg(self, *exprs): """Compute aggregates and returns the result as a :class:`DataFrame`. The available aggregate functions can be: 1. built-in aggregation functions, such as `avg`, `max`, `min`, `sum`, `count` 2. group aggregate pandas UDFs, created with :func:`pyspark.sql.functions.pandas_udf` .. note:: There is no partial aggregation with group aggregate UDFs, i.e., a full shuffle is required. Also, all the data of a group will be loaded into memory, so the user should be aware of the potential OOM risk if data is skewed and certain groups are too large to fit in memory. .. seealso:: :func:`pyspark.sql.functions.pandas_udf` If ``exprs`` is a single :class:`dict` mapping from string to string, then the key is the column to perform aggregation on, and the value is the aggregate function. Alternatively, ``exprs`` can also be a list of aggregate :class:`Column` expressions. .. note:: Built-in aggregation functions and group aggregate pandas UDFs cannot be mixed in a single call to this function. :param exprs: a dict mapping from column name (string) to aggregate functions (string), or a list of :class:`Column`. >>> gdf = df.groupBy(df.name) >>> sorted(gdf.agg({"*": "count"}).collect()) [Row(name=u'Alice', count(1)=1), Row(name=u'Bob', count(1)=1)] >>> from pyspark.sql import functions as F >>> sorted(gdf.agg(F.min(df.age)).collect()) [Row(name=u'Alice', min(age)=2), Row(name=u'Bob', min(age)=5)] >>> from pyspark.sql.functions import pandas_udf, PandasUDFType >>> @pandas_udf('int', PandasUDFType.GROUPED_AGG) # doctest: +SKIP ... def min_udf(v): ... return v.min() >>> sorted(gdf.agg(min_udf(df.age)).collect()) # doctest: +SKIP [Row(name=u'Alice', min_udf(age)=2), Row(name=u'Bob', min_udf(age)=5)] """ assert exprs, "exprs should not be empty" if len(exprs) == 1 and isinstance(exprs[0], dict): jdf = self._jgd.agg(exprs[0]) else: # Columns assert all(isinstance(c, Column) for c in exprs), "all exprs should be Column" jdf = self._jgd.agg(exprs[0]._jc, _to_seq(self.sql_ctx._sc, [c._jc for c in exprs[1:]])) return DataFrame(jdf, self.sql_ctx)
[ "def", "agg", "(", "self", ",", "*", "exprs", ")", ":", "assert", "exprs", ",", "\"exprs should not be empty\"", "if", "len", "(", "exprs", ")", "==", "1", "and", "isinstance", "(", "exprs", "[", "0", "]", ",", "dict", ")", ":", "jdf", "=", "self", ".", "_jgd", ".", "agg", "(", "exprs", "[", "0", "]", ")", "else", ":", "# Columns", "assert", "all", "(", "isinstance", "(", "c", ",", "Column", ")", "for", "c", "in", "exprs", ")", ",", "\"all exprs should be Column\"", "jdf", "=", "self", ".", "_jgd", ".", "agg", "(", "exprs", "[", "0", "]", ".", "_jc", ",", "_to_seq", "(", "self", ".", "sql_ctx", ".", "_sc", ",", "[", "c", ".", "_jc", "for", "c", "in", "exprs", "[", "1", ":", "]", "]", ")", ")", "return", "DataFrame", "(", "jdf", ",", "self", ".", "sql_ctx", ")" ]
Compute aggregates and returns the result as a :class:`DataFrame`. The available aggregate functions can be: 1. built-in aggregation functions, such as `avg`, `max`, `min`, `sum`, `count` 2. group aggregate pandas UDFs, created with :func:`pyspark.sql.functions.pandas_udf` .. note:: There is no partial aggregation with group aggregate UDFs, i.e., a full shuffle is required. Also, all the data of a group will be loaded into memory, so the user should be aware of the potential OOM risk if data is skewed and certain groups are too large to fit in memory. .. seealso:: :func:`pyspark.sql.functions.pandas_udf` If ``exprs`` is a single :class:`dict` mapping from string to string, then the key is the column to perform aggregation on, and the value is the aggregate function. Alternatively, ``exprs`` can also be a list of aggregate :class:`Column` expressions. .. note:: Built-in aggregation functions and group aggregate pandas UDFs cannot be mixed in a single call to this function. :param exprs: a dict mapping from column name (string) to aggregate functions (string), or a list of :class:`Column`. >>> gdf = df.groupBy(df.name) >>> sorted(gdf.agg({"*": "count"}).collect()) [Row(name=u'Alice', count(1)=1), Row(name=u'Bob', count(1)=1)] >>> from pyspark.sql import functions as F >>> sorted(gdf.agg(F.min(df.age)).collect()) [Row(name=u'Alice', min(age)=2), Row(name=u'Bob', min(age)=5)] >>> from pyspark.sql.functions import pandas_udf, PandasUDFType >>> @pandas_udf('int', PandasUDFType.GROUPED_AGG) # doctest: +SKIP ... def min_udf(v): ... return v.min() >>> sorted(gdf.agg(min_udf(df.age)).collect()) # doctest: +SKIP [Row(name=u'Alice', min_udf(age)=2), Row(name=u'Bob', min_udf(age)=5)]
[ "Compute", "aggregates", "and", "returns", "the", "result", "as", "a", ":", "class", ":", "DataFrame", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/group.py#L66-L116
apache/spark
python/pyspark/sql/group.py
GroupedData.pivot
def pivot(self, pivot_col, values=None): """ Pivots a column of the current :class:`DataFrame` and perform the specified aggregation. There are two versions of pivot function: one that requires the caller to specify the list of distinct values to pivot on, and one that does not. The latter is more concise but less efficient, because Spark needs to first compute the list of distinct values internally. :param pivot_col: Name of the column to pivot. :param values: List of values that will be translated to columns in the output DataFrame. # Compute the sum of earnings for each year by course with each course as a separate column >>> df4.groupBy("year").pivot("course", ["dotNET", "Java"]).sum("earnings").collect() [Row(year=2012, dotNET=15000, Java=20000), Row(year=2013, dotNET=48000, Java=30000)] # Or without specifying column values (less efficient) >>> df4.groupBy("year").pivot("course").sum("earnings").collect() [Row(year=2012, Java=20000, dotNET=15000), Row(year=2013, Java=30000, dotNET=48000)] >>> df5.groupBy("sales.year").pivot("sales.course").sum("sales.earnings").collect() [Row(year=2012, Java=20000, dotNET=15000), Row(year=2013, Java=30000, dotNET=48000)] """ if values is None: jgd = self._jgd.pivot(pivot_col) else: jgd = self._jgd.pivot(pivot_col, values) return GroupedData(jgd, self._df)
python
def pivot(self, pivot_col, values=None): """ Pivots a column of the current :class:`DataFrame` and perform the specified aggregation. There are two versions of pivot function: one that requires the caller to specify the list of distinct values to pivot on, and one that does not. The latter is more concise but less efficient, because Spark needs to first compute the list of distinct values internally. :param pivot_col: Name of the column to pivot. :param values: List of values that will be translated to columns in the output DataFrame. # Compute the sum of earnings for each year by course with each course as a separate column >>> df4.groupBy("year").pivot("course", ["dotNET", "Java"]).sum("earnings").collect() [Row(year=2012, dotNET=15000, Java=20000), Row(year=2013, dotNET=48000, Java=30000)] # Or without specifying column values (less efficient) >>> df4.groupBy("year").pivot("course").sum("earnings").collect() [Row(year=2012, Java=20000, dotNET=15000), Row(year=2013, Java=30000, dotNET=48000)] >>> df5.groupBy("sales.year").pivot("sales.course").sum("sales.earnings").collect() [Row(year=2012, Java=20000, dotNET=15000), Row(year=2013, Java=30000, dotNET=48000)] """ if values is None: jgd = self._jgd.pivot(pivot_col) else: jgd = self._jgd.pivot(pivot_col, values) return GroupedData(jgd, self._df)
[ "def", "pivot", "(", "self", ",", "pivot_col", ",", "values", "=", "None", ")", ":", "if", "values", "is", "None", ":", "jgd", "=", "self", ".", "_jgd", ".", "pivot", "(", "pivot_col", ")", "else", ":", "jgd", "=", "self", ".", "_jgd", ".", "pivot", "(", "pivot_col", ",", "values", ")", "return", "GroupedData", "(", "jgd", ",", "self", ".", "_df", ")" ]
Pivots a column of the current :class:`DataFrame` and perform the specified aggregation. There are two versions of pivot function: one that requires the caller to specify the list of distinct values to pivot on, and one that does not. The latter is more concise but less efficient, because Spark needs to first compute the list of distinct values internally. :param pivot_col: Name of the column to pivot. :param values: List of values that will be translated to columns in the output DataFrame. # Compute the sum of earnings for each year by course with each course as a separate column >>> df4.groupBy("year").pivot("course", ["dotNET", "Java"]).sum("earnings").collect() [Row(year=2012, dotNET=15000, Java=20000), Row(year=2013, dotNET=48000, Java=30000)] # Or without specifying column values (less efficient) >>> df4.groupBy("year").pivot("course").sum("earnings").collect() [Row(year=2012, Java=20000, dotNET=15000), Row(year=2013, Java=30000, dotNET=48000)] >>> df5.groupBy("sales.year").pivot("sales.course").sum("sales.earnings").collect() [Row(year=2012, Java=20000, dotNET=15000), Row(year=2013, Java=30000, dotNET=48000)]
[ "Pivots", "a", "column", "of", "the", "current", ":", "class", ":", "DataFrame", "and", "perform", "the", "specified", "aggregation", ".", "There", "are", "two", "versions", "of", "pivot", "function", ":", "one", "that", "requires", "the", "caller", "to", "specify", "the", "list", "of", "distinct", "values", "to", "pivot", "on", "and", "one", "that", "does", "not", ".", "The", "latter", "is", "more", "concise", "but", "less", "efficient", "because", "Spark", "needs", "to", "first", "compute", "the", "list", "of", "distinct", "values", "internally", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/group.py#L195-L221
apache/spark
python/pyspark/sql/group.py
GroupedData.apply
def apply(self, udf): """ Maps each group of the current :class:`DataFrame` using a pandas udf and returns the result as a `DataFrame`. The user-defined function should take a `pandas.DataFrame` and return another `pandas.DataFrame`. For each group, all columns are passed together as a `pandas.DataFrame` to the user-function and the returned `pandas.DataFrame` are combined as a :class:`DataFrame`. The returned `pandas.DataFrame` can be of arbitrary length and its schema must match the returnType of the pandas udf. .. note:: This function requires a full shuffle. all the data of a group will be loaded into memory, so the user should be aware of the potential OOM risk if data is skewed and certain groups are too large to fit in memory. .. note:: Experimental :param udf: a grouped map user-defined function returned by :func:`pyspark.sql.functions.pandas_udf`. >>> from pyspark.sql.functions import pandas_udf, PandasUDFType >>> df = spark.createDataFrame( ... [(1, 1.0), (1, 2.0), (2, 3.0), (2, 5.0), (2, 10.0)], ... ("id", "v")) >>> @pandas_udf("id long, v double", PandasUDFType.GROUPED_MAP) # doctest: +SKIP ... def normalize(pdf): ... v = pdf.v ... return pdf.assign(v=(v - v.mean()) / v.std()) >>> df.groupby("id").apply(normalize).show() # doctest: +SKIP +---+-------------------+ | id| v| +---+-------------------+ | 1|-0.7071067811865475| | 1| 0.7071067811865475| | 2|-0.8320502943378437| | 2|-0.2773500981126146| | 2| 1.1094003924504583| +---+-------------------+ .. seealso:: :meth:`pyspark.sql.functions.pandas_udf` """ # Columns are special because hasattr always return True if isinstance(udf, Column) or not hasattr(udf, 'func') \ or udf.evalType != PythonEvalType.SQL_GROUPED_MAP_PANDAS_UDF: raise ValueError("Invalid udf: the udf argument must be a pandas_udf of type " "GROUPED_MAP.") df = self._df udf_column = udf(*[df[col] for col in df.columns]) jdf = self._jgd.flatMapGroupsInPandas(udf_column._jc.expr()) return DataFrame(jdf, self.sql_ctx)
python
def apply(self, udf): """ Maps each group of the current :class:`DataFrame` using a pandas udf and returns the result as a `DataFrame`. The user-defined function should take a `pandas.DataFrame` and return another `pandas.DataFrame`. For each group, all columns are passed together as a `pandas.DataFrame` to the user-function and the returned `pandas.DataFrame` are combined as a :class:`DataFrame`. The returned `pandas.DataFrame` can be of arbitrary length and its schema must match the returnType of the pandas udf. .. note:: This function requires a full shuffle. all the data of a group will be loaded into memory, so the user should be aware of the potential OOM risk if data is skewed and certain groups are too large to fit in memory. .. note:: Experimental :param udf: a grouped map user-defined function returned by :func:`pyspark.sql.functions.pandas_udf`. >>> from pyspark.sql.functions import pandas_udf, PandasUDFType >>> df = spark.createDataFrame( ... [(1, 1.0), (1, 2.0), (2, 3.0), (2, 5.0), (2, 10.0)], ... ("id", "v")) >>> @pandas_udf("id long, v double", PandasUDFType.GROUPED_MAP) # doctest: +SKIP ... def normalize(pdf): ... v = pdf.v ... return pdf.assign(v=(v - v.mean()) / v.std()) >>> df.groupby("id").apply(normalize).show() # doctest: +SKIP +---+-------------------+ | id| v| +---+-------------------+ | 1|-0.7071067811865475| | 1| 0.7071067811865475| | 2|-0.8320502943378437| | 2|-0.2773500981126146| | 2| 1.1094003924504583| +---+-------------------+ .. seealso:: :meth:`pyspark.sql.functions.pandas_udf` """ # Columns are special because hasattr always return True if isinstance(udf, Column) or not hasattr(udf, 'func') \ or udf.evalType != PythonEvalType.SQL_GROUPED_MAP_PANDAS_UDF: raise ValueError("Invalid udf: the udf argument must be a pandas_udf of type " "GROUPED_MAP.") df = self._df udf_column = udf(*[df[col] for col in df.columns]) jdf = self._jgd.flatMapGroupsInPandas(udf_column._jc.expr()) return DataFrame(jdf, self.sql_ctx)
[ "def", "apply", "(", "self", ",", "udf", ")", ":", "# Columns are special because hasattr always return True", "if", "isinstance", "(", "udf", ",", "Column", ")", "or", "not", "hasattr", "(", "udf", ",", "'func'", ")", "or", "udf", ".", "evalType", "!=", "PythonEvalType", ".", "SQL_GROUPED_MAP_PANDAS_UDF", ":", "raise", "ValueError", "(", "\"Invalid udf: the udf argument must be a pandas_udf of type \"", "\"GROUPED_MAP.\"", ")", "df", "=", "self", ".", "_df", "udf_column", "=", "udf", "(", "*", "[", "df", "[", "col", "]", "for", "col", "in", "df", ".", "columns", "]", ")", "jdf", "=", "self", ".", "_jgd", ".", "flatMapGroupsInPandas", "(", "udf_column", ".", "_jc", ".", "expr", "(", ")", ")", "return", "DataFrame", "(", "jdf", ",", "self", ".", "sql_ctx", ")" ]
Maps each group of the current :class:`DataFrame` using a pandas udf and returns the result as a `DataFrame`. The user-defined function should take a `pandas.DataFrame` and return another `pandas.DataFrame`. For each group, all columns are passed together as a `pandas.DataFrame` to the user-function and the returned `pandas.DataFrame` are combined as a :class:`DataFrame`. The returned `pandas.DataFrame` can be of arbitrary length and its schema must match the returnType of the pandas udf. .. note:: This function requires a full shuffle. all the data of a group will be loaded into memory, so the user should be aware of the potential OOM risk if data is skewed and certain groups are too large to fit in memory. .. note:: Experimental :param udf: a grouped map user-defined function returned by :func:`pyspark.sql.functions.pandas_udf`. >>> from pyspark.sql.functions import pandas_udf, PandasUDFType >>> df = spark.createDataFrame( ... [(1, 1.0), (1, 2.0), (2, 3.0), (2, 5.0), (2, 10.0)], ... ("id", "v")) >>> @pandas_udf("id long, v double", PandasUDFType.GROUPED_MAP) # doctest: +SKIP ... def normalize(pdf): ... v = pdf.v ... return pdf.assign(v=(v - v.mean()) / v.std()) >>> df.groupby("id").apply(normalize).show() # doctest: +SKIP +---+-------------------+ | id| v| +---+-------------------+ | 1|-0.7071067811865475| | 1| 0.7071067811865475| | 2|-0.8320502943378437| | 2|-0.2773500981126146| | 2| 1.1094003924504583| +---+-------------------+ .. seealso:: :meth:`pyspark.sql.functions.pandas_udf`
[ "Maps", "each", "group", "of", "the", "current", ":", "class", ":", "DataFrame", "using", "a", "pandas", "udf", "and", "returns", "the", "result", "as", "a", "DataFrame", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/group.py#L224-L276
apache/spark
python/pyspark/sql/window.py
Window.partitionBy
def partitionBy(*cols): """ Creates a :class:`WindowSpec` with the partitioning defined. """ sc = SparkContext._active_spark_context jspec = sc._jvm.org.apache.spark.sql.expressions.Window.partitionBy(_to_java_cols(cols)) return WindowSpec(jspec)
python
def partitionBy(*cols): """ Creates a :class:`WindowSpec` with the partitioning defined. """ sc = SparkContext._active_spark_context jspec = sc._jvm.org.apache.spark.sql.expressions.Window.partitionBy(_to_java_cols(cols)) return WindowSpec(jspec)
[ "def", "partitionBy", "(", "*", "cols", ")", ":", "sc", "=", "SparkContext", ".", "_active_spark_context", "jspec", "=", "sc", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "sql", ".", "expressions", ".", "Window", ".", "partitionBy", "(", "_to_java_cols", "(", "cols", ")", ")", "return", "WindowSpec", "(", "jspec", ")" ]
Creates a :class:`WindowSpec` with the partitioning defined.
[ "Creates", "a", ":", "class", ":", "WindowSpec", "with", "the", "partitioning", "defined", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/window.py#L67-L73
apache/spark
python/pyspark/sql/window.py
Window.rowsBetween
def rowsBetween(start, end): """ Creates a :class:`WindowSpec` with the frame boundaries defined, from `start` (inclusive) to `end` (inclusive). Both `start` and `end` are relative positions from the current row. For example, "0" means "current row", while "-1" means the row before the current row, and "5" means the fifth row after the current row. We recommend users use ``Window.unboundedPreceding``, ``Window.unboundedFollowing``, and ``Window.currentRow`` to specify special boundary values, rather than using integral values directly. A row based boundary is based on the position of the row within the partition. An offset indicates the number of rows above or below the current row, the frame for the current row starts or ends. For instance, given a row based sliding frame with a lower bound offset of -1 and a upper bound offset of +2. The frame for row with index 5 would range from index 4 to index 6. >>> from pyspark.sql import Window >>> from pyspark.sql import functions as func >>> from pyspark.sql import SQLContext >>> sc = SparkContext.getOrCreate() >>> sqlContext = SQLContext(sc) >>> tup = [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")] >>> df = sqlContext.createDataFrame(tup, ["id", "category"]) >>> window = Window.partitionBy("category").orderBy("id").rowsBetween(Window.currentRow, 1) >>> df.withColumn("sum", func.sum("id").over(window)).show() +---+--------+---+ | id|category|sum| +---+--------+---+ | 1| b| 3| | 2| b| 5| | 3| b| 3| | 1| a| 2| | 1| a| 3| | 2| a| 2| +---+--------+---+ :param start: boundary start, inclusive. The frame is unbounded if this is ``Window.unboundedPreceding``, or any value less than or equal to -9223372036854775808. :param end: boundary end, inclusive. The frame is unbounded if this is ``Window.unboundedFollowing``, or any value greater than or equal to 9223372036854775807. """ if start <= Window._PRECEDING_THRESHOLD: start = Window.unboundedPreceding if end >= Window._FOLLOWING_THRESHOLD: end = Window.unboundedFollowing sc = SparkContext._active_spark_context jspec = sc._jvm.org.apache.spark.sql.expressions.Window.rowsBetween(start, end) return WindowSpec(jspec)
python
def rowsBetween(start, end): """ Creates a :class:`WindowSpec` with the frame boundaries defined, from `start` (inclusive) to `end` (inclusive). Both `start` and `end` are relative positions from the current row. For example, "0" means "current row", while "-1" means the row before the current row, and "5" means the fifth row after the current row. We recommend users use ``Window.unboundedPreceding``, ``Window.unboundedFollowing``, and ``Window.currentRow`` to specify special boundary values, rather than using integral values directly. A row based boundary is based on the position of the row within the partition. An offset indicates the number of rows above or below the current row, the frame for the current row starts or ends. For instance, given a row based sliding frame with a lower bound offset of -1 and a upper bound offset of +2. The frame for row with index 5 would range from index 4 to index 6. >>> from pyspark.sql import Window >>> from pyspark.sql import functions as func >>> from pyspark.sql import SQLContext >>> sc = SparkContext.getOrCreate() >>> sqlContext = SQLContext(sc) >>> tup = [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")] >>> df = sqlContext.createDataFrame(tup, ["id", "category"]) >>> window = Window.partitionBy("category").orderBy("id").rowsBetween(Window.currentRow, 1) >>> df.withColumn("sum", func.sum("id").over(window)).show() +---+--------+---+ | id|category|sum| +---+--------+---+ | 1| b| 3| | 2| b| 5| | 3| b| 3| | 1| a| 2| | 1| a| 3| | 2| a| 2| +---+--------+---+ :param start: boundary start, inclusive. The frame is unbounded if this is ``Window.unboundedPreceding``, or any value less than or equal to -9223372036854775808. :param end: boundary end, inclusive. The frame is unbounded if this is ``Window.unboundedFollowing``, or any value greater than or equal to 9223372036854775807. """ if start <= Window._PRECEDING_THRESHOLD: start = Window.unboundedPreceding if end >= Window._FOLLOWING_THRESHOLD: end = Window.unboundedFollowing sc = SparkContext._active_spark_context jspec = sc._jvm.org.apache.spark.sql.expressions.Window.rowsBetween(start, end) return WindowSpec(jspec)
[ "def", "rowsBetween", "(", "start", ",", "end", ")", ":", "if", "start", "<=", "Window", ".", "_PRECEDING_THRESHOLD", ":", "start", "=", "Window", ".", "unboundedPreceding", "if", "end", ">=", "Window", ".", "_FOLLOWING_THRESHOLD", ":", "end", "=", "Window", ".", "unboundedFollowing", "sc", "=", "SparkContext", ".", "_active_spark_context", "jspec", "=", "sc", ".", "_jvm", ".", "org", ".", "apache", ".", "spark", ".", "sql", ".", "expressions", ".", "Window", ".", "rowsBetween", "(", "start", ",", "end", ")", "return", "WindowSpec", "(", "jspec", ")" ]
Creates a :class:`WindowSpec` with the frame boundaries defined, from `start` (inclusive) to `end` (inclusive). Both `start` and `end` are relative positions from the current row. For example, "0" means "current row", while "-1" means the row before the current row, and "5" means the fifth row after the current row. We recommend users use ``Window.unboundedPreceding``, ``Window.unboundedFollowing``, and ``Window.currentRow`` to specify special boundary values, rather than using integral values directly. A row based boundary is based on the position of the row within the partition. An offset indicates the number of rows above or below the current row, the frame for the current row starts or ends. For instance, given a row based sliding frame with a lower bound offset of -1 and a upper bound offset of +2. The frame for row with index 5 would range from index 4 to index 6. >>> from pyspark.sql import Window >>> from pyspark.sql import functions as func >>> from pyspark.sql import SQLContext >>> sc = SparkContext.getOrCreate() >>> sqlContext = SQLContext(sc) >>> tup = [(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")] >>> df = sqlContext.createDataFrame(tup, ["id", "category"]) >>> window = Window.partitionBy("category").orderBy("id").rowsBetween(Window.currentRow, 1) >>> df.withColumn("sum", func.sum("id").over(window)).show() +---+--------+---+ | id|category|sum| +---+--------+---+ | 1| b| 3| | 2| b| 5| | 3| b| 3| | 1| a| 2| | 1| a| 3| | 2| a| 2| +---+--------+---+ :param start: boundary start, inclusive. The frame is unbounded if this is ``Window.unboundedPreceding``, or any value less than or equal to -9223372036854775808. :param end: boundary end, inclusive. The frame is unbounded if this is ``Window.unboundedFollowing``, or any value greater than or equal to 9223372036854775807.
[ "Creates", "a", ":", "class", ":", "WindowSpec", "with", "the", "frame", "boundaries", "defined", "from", "start", "(", "inclusive", ")", "to", "end", "(", "inclusive", ")", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/window.py#L87-L139
apache/spark
python/pyspark/sql/window.py
WindowSpec.rowsBetween
def rowsBetween(self, start, end): """ Defines the frame boundaries, from `start` (inclusive) to `end` (inclusive). Both `start` and `end` are relative positions from the current row. For example, "0" means "current row", while "-1" means the row before the current row, and "5" means the fifth row after the current row. We recommend users use ``Window.unboundedPreceding``, ``Window.unboundedFollowing``, and ``Window.currentRow`` to specify special boundary values, rather than using integral values directly. :param start: boundary start, inclusive. The frame is unbounded if this is ``Window.unboundedPreceding``, or any value less than or equal to max(-sys.maxsize, -9223372036854775808). :param end: boundary end, inclusive. The frame is unbounded if this is ``Window.unboundedFollowing``, or any value greater than or equal to min(sys.maxsize, 9223372036854775807). """ if start <= Window._PRECEDING_THRESHOLD: start = Window.unboundedPreceding if end >= Window._FOLLOWING_THRESHOLD: end = Window.unboundedFollowing return WindowSpec(self._jspec.rowsBetween(start, end))
python
def rowsBetween(self, start, end): """ Defines the frame boundaries, from `start` (inclusive) to `end` (inclusive). Both `start` and `end` are relative positions from the current row. For example, "0" means "current row", while "-1" means the row before the current row, and "5" means the fifth row after the current row. We recommend users use ``Window.unboundedPreceding``, ``Window.unboundedFollowing``, and ``Window.currentRow`` to specify special boundary values, rather than using integral values directly. :param start: boundary start, inclusive. The frame is unbounded if this is ``Window.unboundedPreceding``, or any value less than or equal to max(-sys.maxsize, -9223372036854775808). :param end: boundary end, inclusive. The frame is unbounded if this is ``Window.unboundedFollowing``, or any value greater than or equal to min(sys.maxsize, 9223372036854775807). """ if start <= Window._PRECEDING_THRESHOLD: start = Window.unboundedPreceding if end >= Window._FOLLOWING_THRESHOLD: end = Window.unboundedFollowing return WindowSpec(self._jspec.rowsBetween(start, end))
[ "def", "rowsBetween", "(", "self", ",", "start", ",", "end", ")", ":", "if", "start", "<=", "Window", ".", "_PRECEDING_THRESHOLD", ":", "start", "=", "Window", ".", "unboundedPreceding", "if", "end", ">=", "Window", ".", "_FOLLOWING_THRESHOLD", ":", "end", "=", "Window", ".", "unboundedFollowing", "return", "WindowSpec", "(", "self", ".", "_jspec", ".", "rowsBetween", "(", "start", ",", "end", ")", ")" ]
Defines the frame boundaries, from `start` (inclusive) to `end` (inclusive). Both `start` and `end` are relative positions from the current row. For example, "0" means "current row", while "-1" means the row before the current row, and "5" means the fifth row after the current row. We recommend users use ``Window.unboundedPreceding``, ``Window.unboundedFollowing``, and ``Window.currentRow`` to specify special boundary values, rather than using integral values directly. :param start: boundary start, inclusive. The frame is unbounded if this is ``Window.unboundedPreceding``, or any value less than or equal to max(-sys.maxsize, -9223372036854775808). :param end: boundary end, inclusive. The frame is unbounded if this is ``Window.unboundedFollowing``, or any value greater than or equal to min(sys.maxsize, 9223372036854775807).
[ "Defines", "the", "frame", "boundaries", "from", "start", "(", "inclusive", ")", "to", "end", "(", "inclusive", ")", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/window.py#L235-L258
apache/spark
python/pyspark/mllib/random.py
RandomRDDs.uniformRDD
def uniformRDD(sc, size, numPartitions=None, seed=None): """ Generates an RDD comprised of i.i.d. samples from the uniform distribution U(0.0, 1.0). To transform the distribution in the generated RDD from U(0.0, 1.0) to U(a, b), use C{RandomRDDs.uniformRDD(sc, n, p, seed)\ .map(lambda v: a + (b - a) * v)} :param sc: SparkContext used to create the RDD. :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ `U(0.0, 1.0)`. >>> x = RandomRDDs.uniformRDD(sc, 100).collect() >>> len(x) 100 >>> max(x) <= 1.0 and min(x) >= 0.0 True >>> RandomRDDs.uniformRDD(sc, 100, 4).getNumPartitions() 4 >>> parts = RandomRDDs.uniformRDD(sc, 100, seed=4).getNumPartitions() >>> parts == sc.defaultParallelism True """ return callMLlibFunc("uniformRDD", sc._jsc, size, numPartitions, seed)
python
def uniformRDD(sc, size, numPartitions=None, seed=None): """ Generates an RDD comprised of i.i.d. samples from the uniform distribution U(0.0, 1.0). To transform the distribution in the generated RDD from U(0.0, 1.0) to U(a, b), use C{RandomRDDs.uniformRDD(sc, n, p, seed)\ .map(lambda v: a + (b - a) * v)} :param sc: SparkContext used to create the RDD. :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ `U(0.0, 1.0)`. >>> x = RandomRDDs.uniformRDD(sc, 100).collect() >>> len(x) 100 >>> max(x) <= 1.0 and min(x) >= 0.0 True >>> RandomRDDs.uniformRDD(sc, 100, 4).getNumPartitions() 4 >>> parts = RandomRDDs.uniformRDD(sc, 100, seed=4).getNumPartitions() >>> parts == sc.defaultParallelism True """ return callMLlibFunc("uniformRDD", sc._jsc, size, numPartitions, seed)
[ "def", "uniformRDD", "(", "sc", ",", "size", ",", "numPartitions", "=", "None", ",", "seed", "=", "None", ")", ":", "return", "callMLlibFunc", "(", "\"uniformRDD\"", ",", "sc", ".", "_jsc", ",", "size", ",", "numPartitions", ",", "seed", ")" ]
Generates an RDD comprised of i.i.d. samples from the uniform distribution U(0.0, 1.0). To transform the distribution in the generated RDD from U(0.0, 1.0) to U(a, b), use C{RandomRDDs.uniformRDD(sc, n, p, seed)\ .map(lambda v: a + (b - a) * v)} :param sc: SparkContext used to create the RDD. :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ `U(0.0, 1.0)`. >>> x = RandomRDDs.uniformRDD(sc, 100).collect() >>> len(x) 100 >>> max(x) <= 1.0 and min(x) >= 0.0 True >>> RandomRDDs.uniformRDD(sc, 100, 4).getNumPartitions() 4 >>> parts = RandomRDDs.uniformRDD(sc, 100, seed=4).getNumPartitions() >>> parts == sc.defaultParallelism True
[ "Generates", "an", "RDD", "comprised", "of", "i", ".", "i", ".", "d", ".", "samples", "from", "the", "uniform", "distribution", "U", "(", "0", ".", "0", "1", ".", "0", ")", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/random.py#L50-L77
apache/spark
python/pyspark/mllib/random.py
RandomRDDs.normalRDD
def normalRDD(sc, size, numPartitions=None, seed=None): """ Generates an RDD comprised of i.i.d. samples from the standard normal distribution. To transform the distribution in the generated RDD from standard normal to some other normal N(mean, sigma^2), use C{RandomRDDs.normal(sc, n, p, seed)\ .map(lambda v: mean + sigma * v)} :param sc: SparkContext used to create the RDD. :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ N(0.0, 1.0). >>> x = RandomRDDs.normalRDD(sc, 1000, seed=1) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - 0.0) < 0.1 True >>> abs(stats.stdev() - 1.0) < 0.1 True """ return callMLlibFunc("normalRDD", sc._jsc, size, numPartitions, seed)
python
def normalRDD(sc, size, numPartitions=None, seed=None): """ Generates an RDD comprised of i.i.d. samples from the standard normal distribution. To transform the distribution in the generated RDD from standard normal to some other normal N(mean, sigma^2), use C{RandomRDDs.normal(sc, n, p, seed)\ .map(lambda v: mean + sigma * v)} :param sc: SparkContext used to create the RDD. :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ N(0.0, 1.0). >>> x = RandomRDDs.normalRDD(sc, 1000, seed=1) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - 0.0) < 0.1 True >>> abs(stats.stdev() - 1.0) < 0.1 True """ return callMLlibFunc("normalRDD", sc._jsc, size, numPartitions, seed)
[ "def", "normalRDD", "(", "sc", ",", "size", ",", "numPartitions", "=", "None", ",", "seed", "=", "None", ")", ":", "return", "callMLlibFunc", "(", "\"normalRDD\"", ",", "sc", ".", "_jsc", ",", "size", ",", "numPartitions", ",", "seed", ")" ]
Generates an RDD comprised of i.i.d. samples from the standard normal distribution. To transform the distribution in the generated RDD from standard normal to some other normal N(mean, sigma^2), use C{RandomRDDs.normal(sc, n, p, seed)\ .map(lambda v: mean + sigma * v)} :param sc: SparkContext used to create the RDD. :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ N(0.0, 1.0). >>> x = RandomRDDs.normalRDD(sc, 1000, seed=1) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - 0.0) < 0.1 True >>> abs(stats.stdev() - 1.0) < 0.1 True
[ "Generates", "an", "RDD", "comprised", "of", "i", ".", "i", ".", "d", ".", "samples", "from", "the", "standard", "normal", "distribution", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/random.py#L81-L106
apache/spark
python/pyspark/mllib/random.py
RandomRDDs.logNormalRDD
def logNormalRDD(sc, mean, std, size, numPartitions=None, seed=None): """ Generates an RDD comprised of i.i.d. samples from the log normal distribution with the input mean and standard distribution. :param sc: SparkContext used to create the RDD. :param mean: mean for the log Normal distribution :param std: std for the log Normal distribution :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ log N(mean, std). >>> from math import sqrt, exp >>> mean = 0.0 >>> std = 1.0 >>> expMean = exp(mean + 0.5 * std * std) >>> expStd = sqrt((exp(std * std) - 1.0) * exp(2.0 * mean + std * std)) >>> x = RandomRDDs.logNormalRDD(sc, mean, std, 1000, seed=2) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - expMean) < 0.5 True >>> from math import sqrt >>> abs(stats.stdev() - expStd) < 0.5 True """ return callMLlibFunc("logNormalRDD", sc._jsc, float(mean), float(std), size, numPartitions, seed)
python
def logNormalRDD(sc, mean, std, size, numPartitions=None, seed=None): """ Generates an RDD comprised of i.i.d. samples from the log normal distribution with the input mean and standard distribution. :param sc: SparkContext used to create the RDD. :param mean: mean for the log Normal distribution :param std: std for the log Normal distribution :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ log N(mean, std). >>> from math import sqrt, exp >>> mean = 0.0 >>> std = 1.0 >>> expMean = exp(mean + 0.5 * std * std) >>> expStd = sqrt((exp(std * std) - 1.0) * exp(2.0 * mean + std * std)) >>> x = RandomRDDs.logNormalRDD(sc, mean, std, 1000, seed=2) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - expMean) < 0.5 True >>> from math import sqrt >>> abs(stats.stdev() - expStd) < 0.5 True """ return callMLlibFunc("logNormalRDD", sc._jsc, float(mean), float(std), size, numPartitions, seed)
[ "def", "logNormalRDD", "(", "sc", ",", "mean", ",", "std", ",", "size", ",", "numPartitions", "=", "None", ",", "seed", "=", "None", ")", ":", "return", "callMLlibFunc", "(", "\"logNormalRDD\"", ",", "sc", ".", "_jsc", ",", "float", "(", "mean", ")", ",", "float", "(", "std", ")", ",", "size", ",", "numPartitions", ",", "seed", ")" ]
Generates an RDD comprised of i.i.d. samples from the log normal distribution with the input mean and standard distribution. :param sc: SparkContext used to create the RDD. :param mean: mean for the log Normal distribution :param std: std for the log Normal distribution :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ log N(mean, std). >>> from math import sqrt, exp >>> mean = 0.0 >>> std = 1.0 >>> expMean = exp(mean + 0.5 * std * std) >>> expStd = sqrt((exp(std * std) - 1.0) * exp(2.0 * mean + std * std)) >>> x = RandomRDDs.logNormalRDD(sc, mean, std, 1000, seed=2) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - expMean) < 0.5 True >>> from math import sqrt >>> abs(stats.stdev() - expStd) < 0.5 True
[ "Generates", "an", "RDD", "comprised", "of", "i", ".", "i", ".", "d", ".", "samples", "from", "the", "log", "normal", "distribution", "with", "the", "input", "mean", "and", "standard", "distribution", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/random.py#L110-L139
apache/spark
python/pyspark/mllib/random.py
RandomRDDs.exponentialRDD
def exponentialRDD(sc, mean, size, numPartitions=None, seed=None): """ Generates an RDD comprised of i.i.d. samples from the Exponential distribution with the input mean. :param sc: SparkContext used to create the RDD. :param mean: Mean, or 1 / lambda, for the Exponential distribution. :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ Exp(mean). >>> mean = 2.0 >>> x = RandomRDDs.exponentialRDD(sc, mean, 1000, seed=2) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - mean) < 0.5 True >>> from math import sqrt >>> abs(stats.stdev() - sqrt(mean)) < 0.5 True """ return callMLlibFunc("exponentialRDD", sc._jsc, float(mean), size, numPartitions, seed)
python
def exponentialRDD(sc, mean, size, numPartitions=None, seed=None): """ Generates an RDD comprised of i.i.d. samples from the Exponential distribution with the input mean. :param sc: SparkContext used to create the RDD. :param mean: Mean, or 1 / lambda, for the Exponential distribution. :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ Exp(mean). >>> mean = 2.0 >>> x = RandomRDDs.exponentialRDD(sc, mean, 1000, seed=2) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - mean) < 0.5 True >>> from math import sqrt >>> abs(stats.stdev() - sqrt(mean)) < 0.5 True """ return callMLlibFunc("exponentialRDD", sc._jsc, float(mean), size, numPartitions, seed)
[ "def", "exponentialRDD", "(", "sc", ",", "mean", ",", "size", ",", "numPartitions", "=", "None", ",", "seed", "=", "None", ")", ":", "return", "callMLlibFunc", "(", "\"exponentialRDD\"", ",", "sc", ".", "_jsc", ",", "float", "(", "mean", ")", ",", "size", ",", "numPartitions", ",", "seed", ")" ]
Generates an RDD comprised of i.i.d. samples from the Exponential distribution with the input mean. :param sc: SparkContext used to create the RDD. :param mean: Mean, or 1 / lambda, for the Exponential distribution. :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ Exp(mean). >>> mean = 2.0 >>> x = RandomRDDs.exponentialRDD(sc, mean, 1000, seed=2) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - mean) < 0.5 True >>> from math import sqrt >>> abs(stats.stdev() - sqrt(mean)) < 0.5 True
[ "Generates", "an", "RDD", "comprised", "of", "i", ".", "i", ".", "d", ".", "samples", "from", "the", "Exponential", "distribution", "with", "the", "input", "mean", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/random.py#L170-L193
apache/spark
python/pyspark/mllib/random.py
RandomRDDs.gammaRDD
def gammaRDD(sc, shape, scale, size, numPartitions=None, seed=None): """ Generates an RDD comprised of i.i.d. samples from the Gamma distribution with the input shape and scale. :param sc: SparkContext used to create the RDD. :param shape: shape (> 0) parameter for the Gamma distribution :param scale: scale (> 0) parameter for the Gamma distribution :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ Gamma(shape, scale). >>> from math import sqrt >>> shape = 1.0 >>> scale = 2.0 >>> expMean = shape * scale >>> expStd = sqrt(shape * scale * scale) >>> x = RandomRDDs.gammaRDD(sc, shape, scale, 1000, seed=2) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - expMean) < 0.5 True >>> abs(stats.stdev() - expStd) < 0.5 True """ return callMLlibFunc("gammaRDD", sc._jsc, float(shape), float(scale), size, numPartitions, seed)
python
def gammaRDD(sc, shape, scale, size, numPartitions=None, seed=None): """ Generates an RDD comprised of i.i.d. samples from the Gamma distribution with the input shape and scale. :param sc: SparkContext used to create the RDD. :param shape: shape (> 0) parameter for the Gamma distribution :param scale: scale (> 0) parameter for the Gamma distribution :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ Gamma(shape, scale). >>> from math import sqrt >>> shape = 1.0 >>> scale = 2.0 >>> expMean = shape * scale >>> expStd = sqrt(shape * scale * scale) >>> x = RandomRDDs.gammaRDD(sc, shape, scale, 1000, seed=2) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - expMean) < 0.5 True >>> abs(stats.stdev() - expStd) < 0.5 True """ return callMLlibFunc("gammaRDD", sc._jsc, float(shape), float(scale), size, numPartitions, seed)
[ "def", "gammaRDD", "(", "sc", ",", "shape", ",", "scale", ",", "size", ",", "numPartitions", "=", "None", ",", "seed", "=", "None", ")", ":", "return", "callMLlibFunc", "(", "\"gammaRDD\"", ",", "sc", ".", "_jsc", ",", "float", "(", "shape", ")", ",", "float", "(", "scale", ")", ",", "size", ",", "numPartitions", ",", "seed", ")" ]
Generates an RDD comprised of i.i.d. samples from the Gamma distribution with the input shape and scale. :param sc: SparkContext used to create the RDD. :param shape: shape (> 0) parameter for the Gamma distribution :param scale: scale (> 0) parameter for the Gamma distribution :param size: Size of the RDD. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of float comprised of i.i.d. samples ~ Gamma(shape, scale). >>> from math import sqrt >>> shape = 1.0 >>> scale = 2.0 >>> expMean = shape * scale >>> expStd = sqrt(shape * scale * scale) >>> x = RandomRDDs.gammaRDD(sc, shape, scale, 1000, seed=2) >>> stats = x.stats() >>> stats.count() 1000 >>> abs(stats.mean() - expMean) < 0.5 True >>> abs(stats.stdev() - expStd) < 0.5 True
[ "Generates", "an", "RDD", "comprised", "of", "i", ".", "i", ".", "d", ".", "samples", "from", "the", "Gamma", "distribution", "with", "the", "input", "shape", "and", "scale", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/random.py#L197-L225
apache/spark
python/pyspark/mllib/random.py
RandomRDDs.uniformVectorRDD
def uniformVectorRDD(sc, numRows, numCols, numPartitions=None, seed=None): """ Generates an RDD comprised of vectors containing i.i.d. samples drawn from the uniform distribution U(0.0, 1.0). :param sc: SparkContext used to create the RDD. :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD. :param seed: Seed for the RNG that generates the seed for the generator in each partition. :return: RDD of Vector with vectors containing i.i.d samples ~ `U(0.0, 1.0)`. >>> import numpy as np >>> mat = np.matrix(RandomRDDs.uniformVectorRDD(sc, 10, 10).collect()) >>> mat.shape (10, 10) >>> mat.max() <= 1.0 and mat.min() >= 0.0 True >>> RandomRDDs.uniformVectorRDD(sc, 10, 10, 4).getNumPartitions() 4 """ return callMLlibFunc("uniformVectorRDD", sc._jsc, numRows, numCols, numPartitions, seed)
python
def uniformVectorRDD(sc, numRows, numCols, numPartitions=None, seed=None): """ Generates an RDD comprised of vectors containing i.i.d. samples drawn from the uniform distribution U(0.0, 1.0). :param sc: SparkContext used to create the RDD. :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD. :param seed: Seed for the RNG that generates the seed for the generator in each partition. :return: RDD of Vector with vectors containing i.i.d samples ~ `U(0.0, 1.0)`. >>> import numpy as np >>> mat = np.matrix(RandomRDDs.uniformVectorRDD(sc, 10, 10).collect()) >>> mat.shape (10, 10) >>> mat.max() <= 1.0 and mat.min() >= 0.0 True >>> RandomRDDs.uniformVectorRDD(sc, 10, 10, 4).getNumPartitions() 4 """ return callMLlibFunc("uniformVectorRDD", sc._jsc, numRows, numCols, numPartitions, seed)
[ "def", "uniformVectorRDD", "(", "sc", ",", "numRows", ",", "numCols", ",", "numPartitions", "=", "None", ",", "seed", "=", "None", ")", ":", "return", "callMLlibFunc", "(", "\"uniformVectorRDD\"", ",", "sc", ".", "_jsc", ",", "numRows", ",", "numCols", ",", "numPartitions", ",", "seed", ")" ]
Generates an RDD comprised of vectors containing i.i.d. samples drawn from the uniform distribution U(0.0, 1.0). :param sc: SparkContext used to create the RDD. :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD. :param seed: Seed for the RNG that generates the seed for the generator in each partition. :return: RDD of Vector with vectors containing i.i.d samples ~ `U(0.0, 1.0)`. >>> import numpy as np >>> mat = np.matrix(RandomRDDs.uniformVectorRDD(sc, 10, 10).collect()) >>> mat.shape (10, 10) >>> mat.max() <= 1.0 and mat.min() >= 0.0 True >>> RandomRDDs.uniformVectorRDD(sc, 10, 10, 4).getNumPartitions() 4
[ "Generates", "an", "RDD", "comprised", "of", "vectors", "containing", "i", ".", "i", ".", "d", ".", "samples", "drawn", "from", "the", "uniform", "distribution", "U", "(", "0", ".", "0", "1", ".", "0", ")", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/random.py#L230-L251
apache/spark
python/pyspark/mllib/random.py
RandomRDDs.normalVectorRDD
def normalVectorRDD(sc, numRows, numCols, numPartitions=None, seed=None): """ Generates an RDD comprised of vectors containing i.i.d. samples drawn from the standard normal distribution. :param sc: SparkContext used to create the RDD. :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ `N(0.0, 1.0)`. >>> import numpy as np >>> mat = np.matrix(RandomRDDs.normalVectorRDD(sc, 100, 100, seed=1).collect()) >>> mat.shape (100, 100) >>> abs(mat.mean() - 0.0) < 0.1 True >>> abs(mat.std() - 1.0) < 0.1 True """ return callMLlibFunc("normalVectorRDD", sc._jsc, numRows, numCols, numPartitions, seed)
python
def normalVectorRDD(sc, numRows, numCols, numPartitions=None, seed=None): """ Generates an RDD comprised of vectors containing i.i.d. samples drawn from the standard normal distribution. :param sc: SparkContext used to create the RDD. :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ `N(0.0, 1.0)`. >>> import numpy as np >>> mat = np.matrix(RandomRDDs.normalVectorRDD(sc, 100, 100, seed=1).collect()) >>> mat.shape (100, 100) >>> abs(mat.mean() - 0.0) < 0.1 True >>> abs(mat.std() - 1.0) < 0.1 True """ return callMLlibFunc("normalVectorRDD", sc._jsc, numRows, numCols, numPartitions, seed)
[ "def", "normalVectorRDD", "(", "sc", ",", "numRows", ",", "numCols", ",", "numPartitions", "=", "None", ",", "seed", "=", "None", ")", ":", "return", "callMLlibFunc", "(", "\"normalVectorRDD\"", ",", "sc", ".", "_jsc", ",", "numRows", ",", "numCols", ",", "numPartitions", ",", "seed", ")" ]
Generates an RDD comprised of vectors containing i.i.d. samples drawn from the standard normal distribution. :param sc: SparkContext used to create the RDD. :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ `N(0.0, 1.0)`. >>> import numpy as np >>> mat = np.matrix(RandomRDDs.normalVectorRDD(sc, 100, 100, seed=1).collect()) >>> mat.shape (100, 100) >>> abs(mat.mean() - 0.0) < 0.1 True >>> abs(mat.std() - 1.0) < 0.1 True
[ "Generates", "an", "RDD", "comprised", "of", "vectors", "containing", "i", ".", "i", ".", "d", ".", "samples", "drawn", "from", "the", "standard", "normal", "distribution", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/random.py#L256-L277
apache/spark
python/pyspark/mllib/random.py
RandomRDDs.logNormalVectorRDD
def logNormalVectorRDD(sc, mean, std, numRows, numCols, numPartitions=None, seed=None): """ Generates an RDD comprised of vectors containing i.i.d. samples drawn from the log normal distribution. :param sc: SparkContext used to create the RDD. :param mean: Mean of the log normal distribution :param std: Standard Deviation of the log normal distribution :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ log `N(mean, std)`. >>> import numpy as np >>> from math import sqrt, exp >>> mean = 0.0 >>> std = 1.0 >>> expMean = exp(mean + 0.5 * std * std) >>> expStd = sqrt((exp(std * std) - 1.0) * exp(2.0 * mean + std * std)) >>> m = RandomRDDs.logNormalVectorRDD(sc, mean, std, 100, 100, seed=1).collect() >>> mat = np.matrix(m) >>> mat.shape (100, 100) >>> abs(mat.mean() - expMean) < 0.1 True >>> abs(mat.std() - expStd) < 0.1 True """ return callMLlibFunc("logNormalVectorRDD", sc._jsc, float(mean), float(std), numRows, numCols, numPartitions, seed)
python
def logNormalVectorRDD(sc, mean, std, numRows, numCols, numPartitions=None, seed=None): """ Generates an RDD comprised of vectors containing i.i.d. samples drawn from the log normal distribution. :param sc: SparkContext used to create the RDD. :param mean: Mean of the log normal distribution :param std: Standard Deviation of the log normal distribution :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ log `N(mean, std)`. >>> import numpy as np >>> from math import sqrt, exp >>> mean = 0.0 >>> std = 1.0 >>> expMean = exp(mean + 0.5 * std * std) >>> expStd = sqrt((exp(std * std) - 1.0) * exp(2.0 * mean + std * std)) >>> m = RandomRDDs.logNormalVectorRDD(sc, mean, std, 100, 100, seed=1).collect() >>> mat = np.matrix(m) >>> mat.shape (100, 100) >>> abs(mat.mean() - expMean) < 0.1 True >>> abs(mat.std() - expStd) < 0.1 True """ return callMLlibFunc("logNormalVectorRDD", sc._jsc, float(mean), float(std), numRows, numCols, numPartitions, seed)
[ "def", "logNormalVectorRDD", "(", "sc", ",", "mean", ",", "std", ",", "numRows", ",", "numCols", ",", "numPartitions", "=", "None", ",", "seed", "=", "None", ")", ":", "return", "callMLlibFunc", "(", "\"logNormalVectorRDD\"", ",", "sc", ".", "_jsc", ",", "float", "(", "mean", ")", ",", "float", "(", "std", ")", ",", "numRows", ",", "numCols", ",", "numPartitions", ",", "seed", ")" ]
Generates an RDD comprised of vectors containing i.i.d. samples drawn from the log normal distribution. :param sc: SparkContext used to create the RDD. :param mean: Mean of the log normal distribution :param std: Standard Deviation of the log normal distribution :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ log `N(mean, std)`. >>> import numpy as np >>> from math import sqrt, exp >>> mean = 0.0 >>> std = 1.0 >>> expMean = exp(mean + 0.5 * std * std) >>> expStd = sqrt((exp(std * std) - 1.0) * exp(2.0 * mean + std * std)) >>> m = RandomRDDs.logNormalVectorRDD(sc, mean, std, 100, 100, seed=1).collect() >>> mat = np.matrix(m) >>> mat.shape (100, 100) >>> abs(mat.mean() - expMean) < 0.1 True >>> abs(mat.std() - expStd) < 0.1 True
[ "Generates", "an", "RDD", "comprised", "of", "vectors", "containing", "i", ".", "i", ".", "d", ".", "samples", "drawn", "from", "the", "log", "normal", "distribution", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/random.py#L282-L312
apache/spark
python/pyspark/mllib/random.py
RandomRDDs.poissonVectorRDD
def poissonVectorRDD(sc, mean, numRows, numCols, numPartitions=None, seed=None): """ Generates an RDD comprised of vectors containing i.i.d. samples drawn from the Poisson distribution with the input mean. :param sc: SparkContext used to create the RDD. :param mean: Mean, or lambda, for the Poisson distribution. :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`) :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ Pois(mean). >>> import numpy as np >>> mean = 100.0 >>> rdd = RandomRDDs.poissonVectorRDD(sc, mean, 100, 100, seed=1) >>> mat = np.mat(rdd.collect()) >>> mat.shape (100, 100) >>> abs(mat.mean() - mean) < 0.5 True >>> from math import sqrt >>> abs(mat.std() - sqrt(mean)) < 0.5 True """ return callMLlibFunc("poissonVectorRDD", sc._jsc, float(mean), numRows, numCols, numPartitions, seed)
python
def poissonVectorRDD(sc, mean, numRows, numCols, numPartitions=None, seed=None): """ Generates an RDD comprised of vectors containing i.i.d. samples drawn from the Poisson distribution with the input mean. :param sc: SparkContext used to create the RDD. :param mean: Mean, or lambda, for the Poisson distribution. :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`) :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ Pois(mean). >>> import numpy as np >>> mean = 100.0 >>> rdd = RandomRDDs.poissonVectorRDD(sc, mean, 100, 100, seed=1) >>> mat = np.mat(rdd.collect()) >>> mat.shape (100, 100) >>> abs(mat.mean() - mean) < 0.5 True >>> from math import sqrt >>> abs(mat.std() - sqrt(mean)) < 0.5 True """ return callMLlibFunc("poissonVectorRDD", sc._jsc, float(mean), numRows, numCols, numPartitions, seed)
[ "def", "poissonVectorRDD", "(", "sc", ",", "mean", ",", "numRows", ",", "numCols", ",", "numPartitions", "=", "None", ",", "seed", "=", "None", ")", ":", "return", "callMLlibFunc", "(", "\"poissonVectorRDD\"", ",", "sc", ".", "_jsc", ",", "float", "(", "mean", ")", ",", "numRows", ",", "numCols", ",", "numPartitions", ",", "seed", ")" ]
Generates an RDD comprised of vectors containing i.i.d. samples drawn from the Poisson distribution with the input mean. :param sc: SparkContext used to create the RDD. :param mean: Mean, or lambda, for the Poisson distribution. :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`) :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ Pois(mean). >>> import numpy as np >>> mean = 100.0 >>> rdd = RandomRDDs.poissonVectorRDD(sc, mean, 100, 100, seed=1) >>> mat = np.mat(rdd.collect()) >>> mat.shape (100, 100) >>> abs(mat.mean() - mean) < 0.5 True >>> from math import sqrt >>> abs(mat.std() - sqrt(mean)) < 0.5 True
[ "Generates", "an", "RDD", "comprised", "of", "vectors", "containing", "i", ".", "i", ".", "d", ".", "samples", "drawn", "from", "the", "Poisson", "distribution", "with", "the", "input", "mean", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/random.py#L317-L343
apache/spark
python/pyspark/mllib/random.py
RandomRDDs.gammaVectorRDD
def gammaVectorRDD(sc, shape, scale, numRows, numCols, numPartitions=None, seed=None): """ Generates an RDD comprised of vectors containing i.i.d. samples drawn from the Gamma distribution. :param sc: SparkContext used to create the RDD. :param shape: Shape (> 0) of the Gamma distribution :param scale: Scale (> 0) of the Gamma distribution :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ Gamma(shape, scale). >>> import numpy as np >>> from math import sqrt >>> shape = 1.0 >>> scale = 2.0 >>> expMean = shape * scale >>> expStd = sqrt(shape * scale * scale) >>> mat = np.matrix(RandomRDDs.gammaVectorRDD(sc, shape, scale, 100, 100, seed=1).collect()) >>> mat.shape (100, 100) >>> abs(mat.mean() - expMean) < 0.1 True >>> abs(mat.std() - expStd) < 0.1 True """ return callMLlibFunc("gammaVectorRDD", sc._jsc, float(shape), float(scale), numRows, numCols, numPartitions, seed)
python
def gammaVectorRDD(sc, shape, scale, numRows, numCols, numPartitions=None, seed=None): """ Generates an RDD comprised of vectors containing i.i.d. samples drawn from the Gamma distribution. :param sc: SparkContext used to create the RDD. :param shape: Shape (> 0) of the Gamma distribution :param scale: Scale (> 0) of the Gamma distribution :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ Gamma(shape, scale). >>> import numpy as np >>> from math import sqrt >>> shape = 1.0 >>> scale = 2.0 >>> expMean = shape * scale >>> expStd = sqrt(shape * scale * scale) >>> mat = np.matrix(RandomRDDs.gammaVectorRDD(sc, shape, scale, 100, 100, seed=1).collect()) >>> mat.shape (100, 100) >>> abs(mat.mean() - expMean) < 0.1 True >>> abs(mat.std() - expStd) < 0.1 True """ return callMLlibFunc("gammaVectorRDD", sc._jsc, float(shape), float(scale), numRows, numCols, numPartitions, seed)
[ "def", "gammaVectorRDD", "(", "sc", ",", "shape", ",", "scale", ",", "numRows", ",", "numCols", ",", "numPartitions", "=", "None", ",", "seed", "=", "None", ")", ":", "return", "callMLlibFunc", "(", "\"gammaVectorRDD\"", ",", "sc", ".", "_jsc", ",", "float", "(", "shape", ")", ",", "float", "(", "scale", ")", ",", "numRows", ",", "numCols", ",", "numPartitions", ",", "seed", ")" ]
Generates an RDD comprised of vectors containing i.i.d. samples drawn from the Gamma distribution. :param sc: SparkContext used to create the RDD. :param shape: Shape (> 0) of the Gamma distribution :param scale: Scale (> 0) of the Gamma distribution :param numRows: Number of Vectors in the RDD. :param numCols: Number of elements in each Vector. :param numPartitions: Number of partitions in the RDD (default: `sc.defaultParallelism`). :param seed: Random seed (default: a random long integer). :return: RDD of Vector with vectors containing i.i.d. samples ~ Gamma(shape, scale). >>> import numpy as np >>> from math import sqrt >>> shape = 1.0 >>> scale = 2.0 >>> expMean = shape * scale >>> expStd = sqrt(shape * scale * scale) >>> mat = np.matrix(RandomRDDs.gammaVectorRDD(sc, shape, scale, 100, 100, seed=1).collect()) >>> mat.shape (100, 100) >>> abs(mat.mean() - expMean) < 0.1 True >>> abs(mat.std() - expStd) < 0.1 True
[ "Generates", "an", "RDD", "comprised", "of", "vectors", "containing", "i", ".", "i", ".", "d", ".", "samples", "drawn", "from", "the", "Gamma", "distribution", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/mllib/random.py#L379-L408
apache/spark
python/pyspark/sql/session.py
SparkSession.getActiveSession
def getActiveSession(cls): """ Returns the active SparkSession for the current thread, returned by the builder. >>> s = SparkSession.getActiveSession() >>> l = [('Alice', 1)] >>> rdd = s.sparkContext.parallelize(l) >>> df = s.createDataFrame(rdd, ['name', 'age']) >>> df.select("age").collect() [Row(age=1)] """ from pyspark import SparkContext sc = SparkContext._active_spark_context if sc is None: return None else: if sc._jvm.SparkSession.getActiveSession().isDefined(): SparkSession(sc, sc._jvm.SparkSession.getActiveSession().get()) return SparkSession._activeSession else: return None
python
def getActiveSession(cls): """ Returns the active SparkSession for the current thread, returned by the builder. >>> s = SparkSession.getActiveSession() >>> l = [('Alice', 1)] >>> rdd = s.sparkContext.parallelize(l) >>> df = s.createDataFrame(rdd, ['name', 'age']) >>> df.select("age").collect() [Row(age=1)] """ from pyspark import SparkContext sc = SparkContext._active_spark_context if sc is None: return None else: if sc._jvm.SparkSession.getActiveSession().isDefined(): SparkSession(sc, sc._jvm.SparkSession.getActiveSession().get()) return SparkSession._activeSession else: return None
[ "def", "getActiveSession", "(", "cls", ")", ":", "from", "pyspark", "import", "SparkContext", "sc", "=", "SparkContext", ".", "_active_spark_context", "if", "sc", "is", "None", ":", "return", "None", "else", ":", "if", "sc", ".", "_jvm", ".", "SparkSession", ".", "getActiveSession", "(", ")", ".", "isDefined", "(", ")", ":", "SparkSession", "(", "sc", ",", "sc", ".", "_jvm", ".", "SparkSession", ".", "getActiveSession", "(", ")", ".", "get", "(", ")", ")", "return", "SparkSession", ".", "_activeSession", "else", ":", "return", "None" ]
Returns the active SparkSession for the current thread, returned by the builder. >>> s = SparkSession.getActiveSession() >>> l = [('Alice', 1)] >>> rdd = s.sparkContext.parallelize(l) >>> df = s.createDataFrame(rdd, ['name', 'age']) >>> df.select("age").collect() [Row(age=1)]
[ "Returns", "the", "active", "SparkSession", "for", "the", "current", "thread", "returned", "by", "the", "builder", ".", ">>>", "s", "=", "SparkSession", ".", "getActiveSession", "()", ">>>", "l", "=", "[", "(", "Alice", "1", ")", "]", ">>>", "rdd", "=", "s", ".", "sparkContext", ".", "parallelize", "(", "l", ")", ">>>", "df", "=", "s", ".", "createDataFrame", "(", "rdd", "[", "name", "age", "]", ")", ">>>", "df", ".", "select", "(", "age", ")", ".", "collect", "()", "[", "Row", "(", "age", "=", "1", ")", "]" ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/session.py#L263-L282
apache/spark
python/pyspark/sql/session.py
SparkSession.conf
def conf(self): """Runtime configuration interface for Spark. This is the interface through which the user can get and set all Spark and Hadoop configurations that are relevant to Spark SQL. When getting the value of a config, this defaults to the value set in the underlying :class:`SparkContext`, if any. """ if not hasattr(self, "_conf"): self._conf = RuntimeConfig(self._jsparkSession.conf()) return self._conf
python
def conf(self): """Runtime configuration interface for Spark. This is the interface through which the user can get and set all Spark and Hadoop configurations that are relevant to Spark SQL. When getting the value of a config, this defaults to the value set in the underlying :class:`SparkContext`, if any. """ if not hasattr(self, "_conf"): self._conf = RuntimeConfig(self._jsparkSession.conf()) return self._conf
[ "def", "conf", "(", "self", ")", ":", "if", "not", "hasattr", "(", "self", ",", "\"_conf\"", ")", ":", "self", ".", "_conf", "=", "RuntimeConfig", "(", "self", ".", "_jsparkSession", ".", "conf", "(", ")", ")", "return", "self", ".", "_conf" ]
Runtime configuration interface for Spark. This is the interface through which the user can get and set all Spark and Hadoop configurations that are relevant to Spark SQL. When getting the value of a config, this defaults to the value set in the underlying :class:`SparkContext`, if any.
[ "Runtime", "configuration", "interface", "for", "Spark", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/session.py#L298-L307
apache/spark
python/pyspark/sql/session.py
SparkSession.catalog
def catalog(self): """Interface through which the user may create, drop, alter or query underlying databases, tables, functions etc. :return: :class:`Catalog` """ from pyspark.sql.catalog import Catalog if not hasattr(self, "_catalog"): self._catalog = Catalog(self) return self._catalog
python
def catalog(self): """Interface through which the user may create, drop, alter or query underlying databases, tables, functions etc. :return: :class:`Catalog` """ from pyspark.sql.catalog import Catalog if not hasattr(self, "_catalog"): self._catalog = Catalog(self) return self._catalog
[ "def", "catalog", "(", "self", ")", ":", "from", "pyspark", ".", "sql", ".", "catalog", "import", "Catalog", "if", "not", "hasattr", "(", "self", ",", "\"_catalog\"", ")", ":", "self", ".", "_catalog", "=", "Catalog", "(", "self", ")", "return", "self", ".", "_catalog" ]
Interface through which the user may create, drop, alter or query underlying databases, tables, functions etc. :return: :class:`Catalog`
[ "Interface", "through", "which", "the", "user", "may", "create", "drop", "alter", "or", "query", "underlying", "databases", "tables", "functions", "etc", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/session.py#L311-L320
apache/spark
python/pyspark/sql/session.py
SparkSession.range
def range(self, start, end=None, step=1, numPartitions=None): """ Create a :class:`DataFrame` with single :class:`pyspark.sql.types.LongType` column named ``id``, containing elements in a range from ``start`` to ``end`` (exclusive) with step value ``step``. :param start: the start value :param end: the end value (exclusive) :param step: the incremental step (default: 1) :param numPartitions: the number of partitions of the DataFrame :return: :class:`DataFrame` >>> spark.range(1, 7, 2).collect() [Row(id=1), Row(id=3), Row(id=5)] If only one argument is specified, it will be used as the end value. >>> spark.range(3).collect() [Row(id=0), Row(id=1), Row(id=2)] """ if numPartitions is None: numPartitions = self._sc.defaultParallelism if end is None: jdf = self._jsparkSession.range(0, int(start), int(step), int(numPartitions)) else: jdf = self._jsparkSession.range(int(start), int(end), int(step), int(numPartitions)) return DataFrame(jdf, self._wrapped)
python
def range(self, start, end=None, step=1, numPartitions=None): """ Create a :class:`DataFrame` with single :class:`pyspark.sql.types.LongType` column named ``id``, containing elements in a range from ``start`` to ``end`` (exclusive) with step value ``step``. :param start: the start value :param end: the end value (exclusive) :param step: the incremental step (default: 1) :param numPartitions: the number of partitions of the DataFrame :return: :class:`DataFrame` >>> spark.range(1, 7, 2).collect() [Row(id=1), Row(id=3), Row(id=5)] If only one argument is specified, it will be used as the end value. >>> spark.range(3).collect() [Row(id=0), Row(id=1), Row(id=2)] """ if numPartitions is None: numPartitions = self._sc.defaultParallelism if end is None: jdf = self._jsparkSession.range(0, int(start), int(step), int(numPartitions)) else: jdf = self._jsparkSession.range(int(start), int(end), int(step), int(numPartitions)) return DataFrame(jdf, self._wrapped)
[ "def", "range", "(", "self", ",", "start", ",", "end", "=", "None", ",", "step", "=", "1", ",", "numPartitions", "=", "None", ")", ":", "if", "numPartitions", "is", "None", ":", "numPartitions", "=", "self", ".", "_sc", ".", "defaultParallelism", "if", "end", "is", "None", ":", "jdf", "=", "self", ".", "_jsparkSession", ".", "range", "(", "0", ",", "int", "(", "start", ")", ",", "int", "(", "step", ")", ",", "int", "(", "numPartitions", ")", ")", "else", ":", "jdf", "=", "self", ".", "_jsparkSession", ".", "range", "(", "int", "(", "start", ")", ",", "int", "(", "end", ")", ",", "int", "(", "step", ")", ",", "int", "(", "numPartitions", ")", ")", "return", "DataFrame", "(", "jdf", ",", "self", ".", "_wrapped", ")" ]
Create a :class:`DataFrame` with single :class:`pyspark.sql.types.LongType` column named ``id``, containing elements in a range from ``start`` to ``end`` (exclusive) with step value ``step``. :param start: the start value :param end: the end value (exclusive) :param step: the incremental step (default: 1) :param numPartitions: the number of partitions of the DataFrame :return: :class:`DataFrame` >>> spark.range(1, 7, 2).collect() [Row(id=1), Row(id=3), Row(id=5)] If only one argument is specified, it will be used as the end value. >>> spark.range(3).collect() [Row(id=0), Row(id=1), Row(id=2)]
[ "Create", "a", ":", "class", ":", "DataFrame", "with", "single", ":", "class", ":", "pyspark", ".", "sql", ".", "types", ".", "LongType", "column", "named", "id", "containing", "elements", "in", "a", "range", "from", "start", "to", "end", "(", "exclusive", ")", "with", "step", "value", "step", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/session.py#L333-L361
apache/spark
python/pyspark/sql/session.py
SparkSession._inferSchemaFromList
def _inferSchemaFromList(self, data, names=None): """ Infer schema from list of Row or tuple. :param data: list of Row or tuple :param names: list of column names :return: :class:`pyspark.sql.types.StructType` """ if not data: raise ValueError("can not infer schema from empty dataset") first = data[0] if type(first) is dict: warnings.warn("inferring schema from dict is deprecated," "please use pyspark.sql.Row instead") schema = reduce(_merge_type, (_infer_schema(row, names) for row in data)) if _has_nulltype(schema): raise ValueError("Some of types cannot be determined after inferring") return schema
python
def _inferSchemaFromList(self, data, names=None): """ Infer schema from list of Row or tuple. :param data: list of Row or tuple :param names: list of column names :return: :class:`pyspark.sql.types.StructType` """ if not data: raise ValueError("can not infer schema from empty dataset") first = data[0] if type(first) is dict: warnings.warn("inferring schema from dict is deprecated," "please use pyspark.sql.Row instead") schema = reduce(_merge_type, (_infer_schema(row, names) for row in data)) if _has_nulltype(schema): raise ValueError("Some of types cannot be determined after inferring") return schema
[ "def", "_inferSchemaFromList", "(", "self", ",", "data", ",", "names", "=", "None", ")", ":", "if", "not", "data", ":", "raise", "ValueError", "(", "\"can not infer schema from empty dataset\"", ")", "first", "=", "data", "[", "0", "]", "if", "type", "(", "first", ")", "is", "dict", ":", "warnings", ".", "warn", "(", "\"inferring schema from dict is deprecated,\"", "\"please use pyspark.sql.Row instead\"", ")", "schema", "=", "reduce", "(", "_merge_type", ",", "(", "_infer_schema", "(", "row", ",", "names", ")", "for", "row", "in", "data", ")", ")", "if", "_has_nulltype", "(", "schema", ")", ":", "raise", "ValueError", "(", "\"Some of types cannot be determined after inferring\"", ")", "return", "schema" ]
Infer schema from list of Row or tuple. :param data: list of Row or tuple :param names: list of column names :return: :class:`pyspark.sql.types.StructType`
[ "Infer", "schema", "from", "list", "of", "Row", "or", "tuple", "." ]
train
https://github.com/apache/spark/blob/618d6bff71073c8c93501ab7392c3cc579730f0b/python/pyspark/sql/session.py#L363-L380