{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/56547627","id":56547627,"diff_hunk":"@@ -65,6 +72,106 @@ def __eq__(self, other):\n             return False\n \n \n+class TypeConverters(object):\n+    \"\"\"\n+    .. note:: DeveloperApi\n+\n+    Factory methods for common type conversion functions for `Param.typeConverter`.\n+\n+    .. versionadded:: 2.0.0\n+    \"\"\"\n+\n+    @staticmethod\n+    def _is_numeric(value):\n+        vtype = type(value)\n+        return vtype == int or vtype == float or vtype == np.float64 \\\n+            or vtype == np.int64 or vtype.__name__ == 'long'\n+\n+    @staticmethod\n+    def _can_convert_to_list(value):\n+        vtype = type(value)\n+        return vtype == list or vtype == np.ndarray or isinstance(value, Vector)\n+\n+    @staticmethod\n+    def identity(value):\n+        \"\"\"\n+        Dummy converter that just returns value.\n+        \"\"\"\n+        return value\n+\n+    @staticmethod\n+    def convertToList(value):\n+        \"\"\"\n+        Convert a value to a list, if possible.\n+        \"\"\"\n+        if type(value) == list:\n+            return value\n+        elif type(value) == np.ndarray:\n+            return list(value)\n+        elif isinstance(value, Vector):\n+            return value.toArray()\n+        else:\n+            raise TypeError(\"Could not convert %s to list\" % value)\n+\n+    @staticmethod\n+    def convertToListFloat(value):\n+        \"\"\"\n+        Convert a value to list of floats, if possible.\n+        \"\"\"\n+        if TypeConverters._can_convert_to_list(value) and \\\n+                all(map(lambda v: TypeConverters._is_numeric(v), value)):\n+            value = TypeConverters.convertToList(value)\n+            return list(map(lambda v: float(v), value))\n+        else:\n+            raise TypeError(\"Could not convert %s to list of floats\" % value)\n+\n+    @staticmethod\n+    def convertToListInt(value):\n+        \"\"\"\n+        Convert a value to list of ints, if possible.\n+        \"\"\"\n+        if TypeConverters._can_convert_to_list(value) and \\\n+                all(map(lambda v: TypeConverters._is_numeric(v), value)):","path":"python/pyspark/ml/param/__init__.py","position":null,"original_position":99,"commit_id":"c483da8a6b1c305a96ce2a4a25ac8a0a1f98e653","original_commit_id":"40b00dd2e3209fcf757ec3e3759ce4270096087e","user":{"login":"sethah","id":7275795,"avatar_url":"https://avatars.githubusercontent.com/u/7275795?v=3","gravatar_id":"","url":"https://api.github.com/users/sethah","html_url":"https://github.com/sethah","followers_url":"https://api.github.com/users/sethah/followers","following_url":"https://api.github.com/users/sethah/following{/other_user}","gists_url":"https://api.github.com/users/sethah/gists{/gist_id}","starred_url":"https://api.github.com/users/sethah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sethah/subscriptions","organizations_url":"https://api.github.com/users/sethah/orgs","repos_url":"https://api.github.com/users/sethah/repos","events_url":"https://api.github.com/users/sethah/events{/privacy}","received_events_url":"https://api.github.com/users/sethah/received_events","type":"User","site_admin":false},"body":"Done, added an `_is_integer()` check to make sure values are whole numbers.","created_at":"2016-03-17T17:47:54Z","updated_at":"2016-03-17T17:47:54Z","html_url":"https://github.com/apache/spark/pull/11663#discussion_r56547627","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/11663","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/56547627"},"html":{"href":"https://github.com/apache/spark/pull/11663#discussion_r56547627"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/11663"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/11663","id":62616912,"html_url":"https://github.com/apache/spark/pull/11663","diff_url":"https://github.com/apache/spark/pull/11663.diff","patch_url":"https://github.com/apache/spark/pull/11663.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/11663","number":11663,"state":"open","locked":false,"title":"[SPARK-13068][PYSPARK][ML] Type conversion for Pyspark params","user":{"login":"sethah","id":7275795,"avatar_url":"https://avatars.githubusercontent.com/u/7275795?v=3","gravatar_id":"","url":"https://api.github.com/users/sethah","html_url":"https://github.com/sethah","followers_url":"https://api.github.com/users/sethah/followers","following_url":"https://api.github.com/users/sethah/following{/other_user}","gists_url":"https://api.github.com/users/sethah/gists{/gist_id}","starred_url":"https://api.github.com/users/sethah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sethah/subscriptions","organizations_url":"https://api.github.com/users/sethah/orgs","repos_url":"https://api.github.com/users/sethah/repos","events_url":"https://api.github.com/users/sethah/events{/privacy}","received_events_url":"https://api.github.com/users/sethah/received_events","type":"User","site_admin":false},"body":"## What changes were proposed in this pull request?\r\n\r\nThis patch adds type conversion functionality for parameters in Pyspark. A `typeConverter` field is added to the constructor of `Param` class. This argument is a function which converts values passed to this param to the appropriate type if possible. This is beneficial so that the params can fail at set time if they are given inappropriate values, but even more so because coherent error messages are now provided when Py4J cannot cast the python type to the appropriate Java type. \r\n\r\nThis patch also adds a `TypeConverters` class with factory methods for common type conversions. Most of the changes involve adding these factory type converters to existing params. The previous solution to this issue, `expectedType`, is deprecated and can be removed in 2.1.0 as discussed on the Jira.\r\n\r\n\r\n## How was this patch tested?\r\n\r\nUnit tests were added in python/pyspark/ml/tests.py to test parameter type conversion. These tests check that values that should be convertible are converted correctly, and that the appropriate errors are thrown when invalid values are provided.\r\n\r\n","created_at":"2016-03-11T23:15:29Z","updated_at":"2016-03-17T17:47:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"800c08beadc84233bb2160db53bc6877df0f58be","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/11663/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/11663/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/11663/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/c483da8a6b1c305a96ce2a4a25ac8a0a1f98e653","head":{"label":"sethah:SPARK-13068-tc","ref":"SPARK-13068-tc","sha":"c483da8a6b1c305a96ce2a4a25ac8a0a1f98e653","user":{"login":"sethah","id":7275795,"avatar_url":"https://avatars.githubusercontent.com/u/7275795?v=3","gravatar_id":"","url":"https://api.github.com/users/sethah","html_url":"https://github.com/sethah","followers_url":"https://api.github.com/users/sethah/followers","following_url":"https://api.github.com/users/sethah/following{/other_user}","gists_url":"https://api.github.com/users/sethah/gists{/gist_id}","starred_url":"https://api.github.com/users/sethah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sethah/subscriptions","organizations_url":"https://api.github.com/users/sethah/orgs","repos_url":"https://api.github.com/users/sethah/repos","events_url":"https://api.github.com/users/sethah/events{/privacy}","received_events_url":"https://api.github.com/users/sethah/received_events","type":"User","site_admin":false},"repo":{"id":38016887,"name":"spark","full_name":"sethah/spark","owner":{"login":"sethah","id":7275795,"avatar_url":"https://avatars.githubusercontent.com/u/7275795?v=3","gravatar_id":"","url":"https://api.github.com/users/sethah","html_url":"https://github.com/sethah","followers_url":"https://api.github.com/users/sethah/followers","following_url":"https://api.github.com/users/sethah/following{/other_user}","gists_url":"https://api.github.com/users/sethah/gists{/gist_id}","starred_url":"https://api.github.com/users/sethah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sethah/subscriptions","organizations_url":"https://api.github.com/users/sethah/orgs","repos_url":"https://api.github.com/users/sethah/repos","events_url":"https://api.github.com/users/sethah/events{/privacy}","received_events_url":"https://api.github.com/users/sethah/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/sethah/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/sethah/spark","forks_url":"https://api.github.com/repos/sethah/spark/forks","keys_url":"https://api.github.com/repos/sethah/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sethah/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sethah/spark/teams","hooks_url":"https://api.github.com/repos/sethah/spark/hooks","issue_events_url":"https://api.github.com/repos/sethah/spark/issues/events{/number}","events_url":"https://api.github.com/repos/sethah/spark/events","assignees_url":"https://api.github.com/repos/sethah/spark/assignees{/user}","branches_url":"https://api.github.com/repos/sethah/spark/branches{/branch}","tags_url":"https://api.github.com/repos/sethah/spark/tags","blobs_url":"https://api.github.com/repos/sethah/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sethah/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sethah/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/sethah/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sethah/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/sethah/spark/languages","stargazers_url":"https://api.github.com/repos/sethah/spark/stargazers","contributors_url":"https://api.github.com/repos/sethah/spark/contributors","subscribers_url":"https://api.github.com/repos/sethah/spark/subscribers","subscription_url":"https://api.github.com/repos/sethah/spark/subscription","commits_url":"https://api.github.com/repos/sethah/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/sethah/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/sethah/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/sethah/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/sethah/spark/contents/{+path}","compare_url":"https://api.github.com/repos/sethah/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sethah/spark/merges","archive_url":"https://api.github.com/repos/sethah/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sethah/spark/downloads","issues_url":"https://api.github.com/repos/sethah/spark/issues{/number}","pulls_url":"https://api.github.com/repos/sethah/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/sethah/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/sethah/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sethah/spark/labels{/name}","releases_url":"https://api.github.com/repos/sethah/spark/releases{/id}","deployments_url":"https://api.github.com/repos/sethah/spark/deployments","created_at":"2015-06-24T23:36:53Z","updated_at":"2016-01-14T16:52:52Z","pushed_at":"2016-03-17T17:41:36Z","git_url":"git://github.com/sethah/spark.git","ssh_url":"git@github.com:sethah/spark.git","clone_url":"https://github.com/sethah/spark.git","svn_url":"https://github.com/sethah/spark","homepage":null,"size":150629,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"250832c733602bcca034dd1fea6e325c55ccd1cd","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":17165658,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","deployments_url":"https://api.github.com/repos/apache/spark/deployments","created_at":"2014-02-25T08:00:08Z","updated_at":"2016-03-17T16:26:54Z","pushed_at":"2016-03-17T17:45:58Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":446140,"stargazers_count":7830,"watchers_count":7830,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":7187,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":386,"forks":7187,"open_issues":386,"watchers":7830,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/11663"},"html":{"href":"https://github.com/apache/spark/pull/11663"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/11663"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/11663/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/11663/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/11663/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/c483da8a6b1c305a96ce2a4a25ac8a0a1f98e653"}}}}