from myria import *
import numpy

# Load Myria extensions
%load_ext myria

# Create Jupyter Connection
%connect http://demo.myria.cs.washington.edu:8753

# Alternatively, create an ordinary Python connection to the Myria demo cluster
connection = MyriaConnection(rest_url='http://demo.myria.cs.washington.edu:8753')
# Use this as the default connection
MyriaRelation.DefaultConnection = connection

"""
Explanation: Myria Python & Jupyter
<img src="https://raw.githubusercontent.com/uwescience/myria-python/master/ipnb%20examples/overview.png" style="height: 300px"/>
To install Myria-Python:
pip install myria-python
Or:
git clone https://github.com/uwescience/myria-python
cd myria-python
sudo python setup.py install
1. Connecting to Myria
End of explanation
"""


# How many datasets are there on the server?
print len(connection.datasets())

# Let's look at the first dataset...
dataset = connection.datasets()[0]
print dataset['relationKey']['relationName']
print dataset['created']

# View data stored in this relation
MyriaRelation(dataset['relationKey'])

"""
Explanation: 2. Myria: Connections, Relations, and Queries (and Schemas and Plans)
End of explanation
"""


%%query

-- Load from S3
florida = load("https://s3-us-west-2.amazonaws.com/myria-demo-data/fl_insurance_sample_2.csv",
csv(schema(
            id:int,
            geo:string,
            granularity:int,
            deductable:float,
            policyID:int, 
            construction:string,
            line:string,
            county:string,
            state:string,
            longitude:float,
            latitude:float,
            fl_site_deductible:float,
            hu_site_deductible:float,
            eq_site_deductible:float,
            tiv_2012:float,
            tiv_2011:float,
            fr_site_limit:float,
            fl_site_limit:float,
            hu_site_limit:float,
            eq_site_limit:float), skip=1));


clay_county = [from florida where county = 'CLAY COUNTY' emit *];

store(clay_county, insurance);

# Alternatively, you can upload directly from a Python string
name = {'userName': 'Brandon', 'programName': 'Demo', 'relationName': 'Books'}
schema = { "columnNames" : ["name", "pages"],
           "columnTypes" : ["STRING_TYPE","LONG_TYPE"] }

data = """Brave New World,288
Nineteen Eighty-Four,376
We,256"""

result = connection.upload_file(
    name, schema, data, delimiter=',', overwrite=True)

MyriaRelation(result['relationKey'], connection=connection)

#Or, load using the myria_upload command-line utility
!wget https://s3-us-west-2.amazonaws.com/myria-demo-data/books.csv
!myria_upload --hostname demo.myria.cs.washington.edu --port 8753 --no-ssl --user Brandon --program Demo --relation Demo --overwrite books.csv

"""
Explanation: Uploading data
End of explanation
"""


# Using the previously-stored insurance relation
MyriaRelation("insurance")

# View details about this relation
relation = MyriaRelation("insurance")
print len(relation)
print relation.created_date
print relation.schema.names

"""
Explanation: Working with relations
End of explanation
"""


# 1: Download as a Python dictionary
d = MyriaRelation("insurance").to_dict()
print 'First entry returned: %s' % d[0]['county']

# 2: Download as a Pandas DataFrame
df = MyriaRelation("insurance").to_dataframe()
print '%d entries with nonzero deductable' % len(df[df.eq_site_deductible > 0]) 

# 3: Download as a DataFrame and convert to a numpy array
array = MyriaRelation("insurance").to_dataframe().as_matrix()
print 'Mean site limit = %d' % array[:,4].mean()

"""
Explanation: Working Locally with Relations
End of explanation
"""


%%query --Embed MyriaL in Jupyter notebook by using the "%%query" prefix 

insurance = scan(insurance);

descriptives = [from insurance emit min(eq_site_deductible) as min_deductible, 
                                    max(eq_site_deductible) as max_deductible, 
                                    avg(eq_site_deductible) as mean_deductible, 
                                    stdev(eq_site_deductible) as stdev_deductible];

store(descriptives, descriptives);

# Grab the results of the most recent execution
query = _
or_this_works_too = _45

query

"""
Explanation: Working with queries
End of explanation
"""


query = %datalog Just500(column0, 500) :- TwitterK(column0, 500)%
print query.status
query

"""
Explanation: Single-line queries may be treated like Python expressions
End of explanation
"""


low, high, destination = 543, 550, 'BoundRelation'

"""
Explanation: 5. Variable Binding
End of explanation
"""


%%query
T1 = scan(TwitterK);
T2 = [from T1 where $0 > @low and $0 < @high emit $1 as x];
store(T2, @destination);

"""
Explanation: The tokens @low, @high, and @destination are bound to their values:
End of explanation
"""


!myria-cluster create my-cluster

"""
Explanation: Deploying Myria in an Amazon Cluster!
1. Installing the Myria CLI
```
From the command line, execute:
sudo pip install myria-cluster
```
2. Launching Clusters
End of explanation
"""


# Substitute your MyriaX REST URL here!
%connect http://ec2-52-1-38-182.compute-1.amazonaws.com:8753 

"""
Explanation: 3. Connecting to the Cluster via Python
You can connect to the new cluster by using the MyriaX REST endpoint URL.  In the example above, this is listed as http://ec2-50-112-33-121.us-west-2.compute.amazonaws.com:8753.
End of explanation
"""


