Repository: incubator-systemml Updated Branches: refs/heads/master a11374b89 -> 398c75e15
[SYSTEMML-1220] dmlFromResource in Python MLContext Add dmlFromResource/pydmlFromResource capability to Python MLContext. Closes #373. Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/398c75e1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/398c75e1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/398c75e1 Branch: refs/heads/master Commit: 398c75e1500da34a033abacc39bb27b60171aec7 Parents: a11374b Author: Deron Eriksson <[email protected]> Authored: Mon Feb 6 20:49:11 2017 -0800 Committer: Deron Eriksson <[email protected]> Committed: Mon Feb 6 20:49:11 2017 -0800 ---------------------------------------------------------------------- src/main/python/systemml/mlcontext.py | 49 ++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/398c75e1/src/main/python/systemml/mlcontext.py ---------------------------------------------------------------------- diff --git a/src/main/python/systemml/mlcontext.py b/src/main/python/systemml/mlcontext.py index babfe5b..ecf4e5c 100644 --- a/src/main/python/systemml/mlcontext.py +++ b/src/main/python/systemml/mlcontext.py @@ -19,7 +19,7 @@ # #------------------------------------------------------------- -__all__ = ['MLResults', 'MLContext', 'Script', 'dml', 'pydml', '_java2py', 'Matrix'] +__all__ = ['MLResults', 'MLContext', 'Script', 'dml', 'pydml', 'dmlFromResource', 'pydmlFromResource', '_java2py', 'Matrix'] import os @@ -52,6 +52,24 @@ def dml(scriptString): raise ValueError("scriptString should be a string, got %s" % type(scriptString)) return Script(scriptString, scriptType="dml") +def dmlFromResource(resourcePath): + """ + Create a dml script object based on a resource path. + + Parameters + ---------- + resourcePath: string + Path to a dml script on the classpath. + + Returns + ------- + script: Script instance + Instance of a script object. + """ + if not isinstance(resourcePath, str): + raise ValueError("resourcePath should be a string, got %s" % type(resourcePath)) + return Script(resourcePath, scriptType="dml", isResource=True) + def pydml(scriptString): """ @@ -71,6 +89,23 @@ def pydml(scriptString): raise ValueError("scriptString should be a string, got %s" % type(scriptString)) return Script(scriptString, scriptType="pydml") +def pydmlFromResource(resourcePath): + """ + Create a pydml script object based on a resource path. + + Parameters + ---------- + resourcePath: string + Path to a pydml script on the classpath. + + Returns + ------- + script: Script instance + Instance of a script object. + """ + if not isinstance(resourcePath, str): + raise ValueError("resourcePath should be a string, got %s" % type(resourcePath)) + return Script(resourcePath, scriptType="pydml", isResource=True) def _java2py(sc, obj): """ Convert Java object to Python. """ @@ -186,10 +221,14 @@ class Script(object): scriptType: string Script language, either "dml" for DML (R-like) or "pydml" for PyDML (Python-like). + + isResource: boolean + If true, scriptString is a path to a resource on the classpath """ - def __init__(self, scriptString, scriptType="dml"): + def __init__(self, scriptString, scriptType="dml", isResource=False): self.scriptString = scriptString self.scriptType = scriptType + self.isResource = isResource self._input = {} self._output = [] @@ -240,7 +279,7 @@ class MLContext(object): raise ValueError("Expected sc to be a SparkContext, got " % sc) self._sc = sc self._ml = createJavaObject(sc, 'mlcontext') - + def __repr__(self): return "MLContext" @@ -267,6 +306,8 @@ class MLContext(object): script_java = self._sc._jvm.org.apache.sysml.api.mlcontext.ScriptFactory.dmlFromUrl(scriptString) elif os.path.exists(scriptString): script_java = self._sc._jvm.org.apache.sysml.api.mlcontext.ScriptFactory.dmlFromFile(scriptString) + elif script.isResource == True: + script_java = self._sc._jvm.org.apache.sysml.api.mlcontext.ScriptFactory.dmlFromResource(scriptString) else: raise ValueError("path: %s does not exist" % scriptString) else: @@ -277,6 +318,8 @@ class MLContext(object): script_java = self._sc._jvm.org.apache.sysml.api.mlcontext.ScriptFactory.pydmlFromUrl(scriptString) elif os.path.exists(scriptString): script_java = self._sc._jvm.org.apache.sysml.api.mlcontext.ScriptFactory.pydmlFromFile(scriptString) + elif script.isResource == True: + script_java = self._sc._jvm.org.apache.sysml.api.mlcontext.ScriptFactory.pydmlFromResource(scriptString) else: raise ValueError("path: %s does not exist" % scriptString) else:
