Skip to content

Commit b20d145

Browse files
author
Ben Guseman
committed
Cleanup and build changes for pyrasterframes
Signed-off-by: Ben Guseman <[email protected]>
1 parent 4b180e6 commit b20d145

File tree

9 files changed

+103
-23
lines changed

9 files changed

+103
-23
lines changed

pyrasterframes/build.sbt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import scala.sys.process.Process
2+
import sbt.Keys.`package`
23

34
enablePlugins(SparkPackagePlugin, AssemblyPlugin)
45

6+
lazy val pyZip = taskKey[File]("Create a minimal pyrasterframes zip distribution.")
7+
58
val pysparkCmd = taskKey[Unit]("Builds pyspark package and emits command string for running pyspark with package")
69

710
lazy val pyTest = taskKey[Unit]("Run pyrasterframes tests.")
@@ -61,6 +64,18 @@ spPublishLocal := {
6164
spPublishLocal.value
6265
}
6366

67+
pyZip := {
68+
val jar = (`package` in Compile).value
69+
val license = baseDirectory.value / "python" / "LICENSE.md"
70+
val pyDir = baseDirectory.value / "python" / "pyrasterframes"
71+
val files = (IO.listFiles(pyDir, GlobFilter("*.py") | GlobFilter("*.rst")) ++ Seq(jar, license))
72+
.map(f => (f, "pyrasterframes/" + f.getName))
73+
val zipFile = target.value / "python-dist" / "pyrasterframes.zip"
74+
IO.zip(files, zipFile)
75+
76+
zipFile
77+
}
78+
6479
pysparkCmd := {
6580
val _ = spPublishLocal.value
6681
val id = (projectID in spPublishLocal).value
File renamed without changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
PyRasterFrames
2+
--------------
3+
4+
PyRasterFrames provides a Python API for RasterFrames!
5+
6+
To initialize PyRasterFrames:
7+
8+
>>> from pyrasterframes import *
9+
>>> spark = SparkSession.builder \
10+
... .master("local[*]") \
11+
... .appName("Using RasterFrames") \
12+
... .config("spark.some.config.option", "some-value") \
13+
... .getOrCreate() \
14+
... .withRasterFrames()
15+

pyrasterframes/python/pyrasterframes/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def _rf_init(spark_session):
2424
if not hasattr(spark_session, "rasterframes"):
2525
spark_session.rasterframes = RFContext(spark_session)
2626
spark_session.sparkContext._rf_context = spark_session.rasterframes
27+
2728
return spark_session
2829

2930

@@ -45,7 +46,9 @@ def _convertDF(df, sp_key = None, metadata = None):
4546

4647

4748
_prevFJ = UserDefinedType.fromJson
48-
def _fromJson(json_val):
49+
# this classmethod annotation is needed for 2.7
50+
@classmethod
51+
def _fromJson(cls, json_val):
4952
if str(json_val['class']).startswith('org.apache.spark.sql.jts'):
5053
json_val['pyClass'] = 'pyrasterframes.GeometryUDT'
5154

@@ -66,3 +69,5 @@ def _fromJson(json_val):
6669
# If you don't have Python support, you will get it anyway
6770
UserDefinedType.fromJson = _fromJson
6871

72+
73+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
This module contains access to the jvm SparkContext with RasterFrame support.
3+
"""
4+
5+
from pyspark import SparkContext
6+
7+
def _checked_context():
8+
""" Get the active SparkContext and throw an error if it is not enabled for RasterFrames."""
9+
sc = SparkContext._active_spark_context
10+
if not hasattr(sc, '_rf_context'):
11+
raise AttributeError(
12+
"RasterFrames have not been enabled for the active session. Call 'SparkSession.withRasterFrames()'.")
13+
return sc._rf_context._jrfctx

pyrasterframes/python/pyrasterframes/rasterfunctions.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,9 @@
66

77

88
from __future__ import absolute_import
9-
from pyspark import SparkContext
109
from pyspark.sql.types import *
1110
from pyspark.sql.column import Column, _to_java_column
12-
13-
14-
def _checked_context():
15-
""" Get the active SparkContext and throw an error if it is not enabled for RasterFrames."""
16-
sc = SparkContext._active_spark_context
17-
if not hasattr(sc, '_rf_context'):
18-
print(vars(sc))
19-
raise AttributeError(
20-
"RasterFrames have not been enabled for the active session. Call 'SparkSession.withRasterFrames()'.")
21-
return sc._rf_context._jrfctx
11+
from pyrasterframes.context import _checked_context
2212

2313

2414
def _celltype(cellTypeStr):

pyrasterframes/python/pyrasterframes/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class here provides the PyRasterFrames entry point.
1111
from pyspark.sql.types import *
1212
from pyspark.ml.wrapper import JavaTransformer
1313
from pyspark.ml.util import JavaMLReadable, JavaMLWritable
14-
from pyrasterframes.rasterfunctions import _checked_context
14+
from pyrasterframes.context import _checked_context
1515

1616
__all__ = ['RFContext', 'RasterFrame', 'TileUDT', 'GeometryUDT', 'TileExploder']
1717

pyrasterframes/python/setup.py

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
"""
2+
The operations in this file are designed for development and testing only.
3+
"""
4+
5+
16
from setuptools import setup, find_packages
27
import distutils.log
38
import importlib
4-
from pathlib import Path
59

610

711
def _extract_module(mod):
@@ -24,19 +28,21 @@ class ExampleCommand(distutils.cmd.Command):
2428
]
2529

2630
def initialize_options(self):
31+
from pathlib import Path
2732
"""Set default values for options."""
2833
# Each user option must be listed here with their default value.
2934
self.examples = filter(lambda x: not x.name.startswith('_'),
3035
list(Path('./examples').resolve().glob('*.py')))
3136

3237
def _check_ex_path(self, ex):
38+
from pathlib import Path
3339
file = Path(ex)
3440
if not file.suffix:
3541
file = file.with_suffix('.py')
3642
file = (Path('./examples') / file).resolve()
3743

3844
assert file.is_file(), ('Invalid example %s' % file)
39-
return file.with_suffix('')
45+
return file
4046

4147
def finalize_options(self):
4248
"""Post-process options."""
@@ -59,6 +65,40 @@ def run(self):
5965
print(traceback.format_exc())
6066

6167

68+
class ZipCommand(distutils.cmd.Command):
69+
"""A custom command to create a minimal zip distribution."""
70+
71+
description = 'create a minimal pyrasterframes source zip file'
72+
user_options = []
73+
74+
def initialize_options(self):
75+
pass
76+
77+
def finalize_options(self):
78+
pass
79+
80+
def run(self):
81+
"""Create the zip."""
82+
import zipfile
83+
from pathlib import Path
84+
import os
85+
zfile = 'pyrasterframes.zip'
86+
87+
if os.path.isfile(zfile):
88+
os.remove(zfile)
89+
with zipfile.ZipFile(zfile, 'w') as przip:
90+
przip.write('pyrasterframes')
91+
# Bring in source files and readme
92+
patterns = ['*.py', '*.rst', '*.jar']
93+
root = Path('.').resolve()
94+
for pattern in patterns:
95+
for file in list(root.glob('pyrasterframes/' + pattern)):
96+
przip.write(str(file.relative_to(root)))
97+
# Put a copy of the license in the zip
98+
przip.write('LICENSE.md', 'pyrasterframes/LICENSE.md')
99+
100+
101+
62102

63103

64104
with open('README.rst') as f:
@@ -75,31 +115,33 @@ def run(self):
75115
author='Simeon H.K. Fitch',
76116
author_email='[email protected]',
77117
license='Apache 2',
78-
setup_requires=['pytest-runner', pyspark_ver],
118+
setup_requires=['pytest-runner', pyspark_ver, 'pathlib'],
79119
install_requires=[
80-
pyspark_ver,
120+
# pyspark_ver,
121+
# 'pathlib'
81122
],
82123
tests_require=[
83124
pyspark_ver,
84125
'pytest==3.4.2'
85126
],
86127
test_suite="pytest-runner",
87-
packages=['.'] + find_packages(exclude=['tests']),
128+
packages=find_packages(exclude=['tests', 'examples']),
88129
include_package_data=True,
89-
package_data={'.':['LICENSE', 'static/*']},
90-
exclude_package_data={'.':['setup.cfg']},
130+
package_data={'.':['LICENSE.md'], 'pyrasterframes':['*.jar']},
131+
exclude_package_data={'.':['setup.*', 'README.*']},
91132
classifiers=[
92133
'Development Status :: 3 - Alpha',
93134
'Environment :: Other Environment',
94135
'License :: OSI Approved :: Apache Software License',
95136
'Natural Language :: English',
96137
'Operating System :: Unix',
97-
'Programming Language :: Python :: 3 :: Only',
138+
'Programming Language :: Python',
98139
'Topic :: Software Development :: Libraries'
99140
],
100141
zip_safe=False,
101142
cmdclass={
102-
'examples': ExampleCommand
143+
'examples': ExampleCommand,
144+
'minzip': ZipCommand
103145
}
104146
# entry_points={
105147
# "console_scripts": ['pyrasterframes=pyrasterframes:console']

pyrasterframes/src/main/scala/astraea/spark/rasterframes/py/PyRFContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class PyRFContext(implicit sparkSession: SparkSession) extends RasterFunctions {
6262
/** DESERIALIZATION **/
6363

6464
def generateTile(cellType: String, cols: Int, rows: Int, bytes: Array[Byte]): ArrayTile = {
65-
ArrayTile.fromBytes(bytes, PyRFContext.this.cellType(cellType), cols, rows)
65+
ArrayTile.fromBytes(bytes, this.cellType(cellType), cols, rows)
6666
}
6767

6868
def generateGeometry(obj: Array[Byte]): Geometry = {

0 commit comments

Comments
 (0)