I am working on the file "GRU/GRU.ipynb".
The first task is:
GRU  class and functions
Can you write Python code for it?

class RNN_cell(object):

    """
    RNN cell object which takes 3 arguments for initialization.
    input_size = Input Vector size
    hidden_layer_size = Hidden layer size
    target_size = Output vector size

    """

    def __init__(self, input_size, hidden_layer_size, target_size):

        #Initialization of given values
        self.input_size = input_size
        self.hidden_layer_size = hidden_layer_size
        self.target_size = target_size

        # Weights for input and hidden tensor
        self.Wx = tf.Variable(tf.zeros([self.input_size,self.hidden_layer_size]))
        self.Wr = tf.Variable(tf.zeros([self.input_size,self.hidden_layer_size]))
        self.Wz = tf.Variable(tf.zeros([self.input_size,self.hidden_layer_size]))

        self.br = tf.Variable(tf.truncated_normal([self.hidden_layer_size],mean=1))
        self.bz = tf.Variable(tf.truncated_normal([self.hidden_layer_size],mean=1))

        self.Wh = tf.Variable(tf.zeros([self.hidden_layer_size,self.hidden_layer_size]))

        #Weights for output layer
        self.Wo = tf.Variable(tf.truncated_normal([self.hidden_layer_size,self.target_size],mean=1,stddev=.01))
        self.bo = tf.Variable(tf.truncated_normal([self.target_size],mean=1,stddev=.01))
        # Placeholder for input vector with shape[batch, seq, embeddings]
        self._inputs = tf.placeholder(tf.float32,
                                      shape=[None, None, self.input_size],
                                      name='inputs')

        # Processing inputs to work with scan function
        self.processed_input = process_batch_input_for_RNN(self._inputs)

        '''
        Initial hidden state's shape is [1,self.hidden_layer_size]
        In First time stamp, we are doing dot product with weights to
        get the shape of [batch_size, self.hidden_layer_size].
        For this dot product tensorflow use broadcasting. But during
        Back propagation a low level error occurs.
        So to solve the problem it was needed to...


I am working on the file "GRU/GRU.ipynb".
The first task is:
Models
Can you write Python code for it?

#Initializing rnn object
rnn=RNN_cell( input_size, hidden_layer_size, target_size)

#Getting all outputs from rnn
outputs = rnn.get_outputs()

#Getting final output through indexing after reversing
last_output = outputs[-1]

#As rnn model output the final layer through Relu activation softmax is used for final output.
output=tf.nn.softmax(last_output)

#Computing the Cross Entropy loss
cross_entropy = -tf.reduce_sum(y * tf.log(output))

# Trainning with Adadelta Optimizer
train_step = tf.train.AdamOptimizer().minimize(cross_entropy)

#Calculatio of correct prediction and accuracy
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(output,1))
accuracy = (tf.reduce_mean(tf.cast(correct_prediction, tf.float32)))*100


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:

Image Display

Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:

Rescaling

The -scale switch can be used to rescale the data range of a given image. Explicit control of the input and output ranges is also available. The gdalinfo -mm switch can be used to see pixel min/max values.
The output will display a computed Min/Max value for each band in the raster imput (3 band in our case)
Can you write Python code for it?

!gdalinfo -mm {DATADIR}/HYP_50M_SR_W_small.tif | grep Min/Max


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:

Resizing

The -outsize switch can be used to set the size of the output file.
Can you write Python code for it?

!gdal_translate -outsize 50% 50% {DATADIR}/HYP_50M_SR_W.tif  {DATADIR}/HYP_50M_SR_W_small.tif


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:

gdal_translate

Format translation

Translations are accomplished with the gdal_translate command. The default output format is GeoTIFF. The -of flag is used to select an output format and the -co flag is used to specify a creation option:
The web browser is not happy with tif files so to have a quick look at the image we convert it to a web friendly format (JPG) using gdal_translate (this requires few seconds)
Can you write Python code for it?

!gdal_translate -of JPEG -co QUALITY=10 {DATADIR}/HYP_50M_SR_W.tif {DATADIR}/HYP_50M_SR_W.jpg


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:

gdalinfo

get information about the raster data

Can you write Python code for it?

!gdalinfo --help-general

!gdalinfo {DATADIR}/HYP_50M_SR_W.tif


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:

gdaltindex

Raster tileindex with gdaltindex

You can build a shapefile as a raster tileindex. For every image a polygon is generated with the bounds of the extent of the polygon and the path to the file.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
Check the results with gdalinfo :
Can you write Python code for it?

!gdalinfo -mm {DATADIR}/HYP_50M_SR_W_scaled.tif | grep Min/Max


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
Convert the countries to GeoJson.

ogr2ogr

Can you write Python code for it?

!rm -rf {DATADIR}/countries.json
!ogr2ogr -f GeoJSON {DATADIR}/countries.json {DATADIR}/ne_10m_admin_0_countries.shp

#unfinished - need to add ogr2ogr features yet, I'm considering to split this notebook in 2 parts (one for gdal and one for ogr2ogr) i'll study the gdal tutorial released recently


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
Here we reproject to the Ortho projection.
Can you write Python code for it?

!gdalwarp -t_srs '+proj=ortho +datum=WGS84' {DATADIR}/HYP_50M_SR_W.tif {DATADIR}/ortho.tif 2>/dev/null

!gdal_translate -of JPEG -co QUALITY=40 {DATADIR}/ortho.tif {DATADIR}/ortho.png

Image(DATADIR+'/ortho.png')


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
If you run ogrinfo without a parameter you will get a summary about your data and afterwards a section for every dataset. You can forward the result from ogrinfo to grep to filter and get only the attribute COUNTRY.
Can you write Python code for it?

!ogrinfo -ro {DATADIR}/ne_10m_admin_0_countries.shp ne_10m_admin_0_countries | grep 'admin '


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
Let's convert the scaled output to JPG for a quick display, notice the color rearrangement.
Can you write Python code for it?

!gdal_translate -of JPEG -co QUALITY=40 {DATADIR}/HYP_50M_SR_W_scaled.tif {DATADIR}/HYP_50M_SR_W_scaled.jpg

Image(DATADIR+'/HYP_50M_SR_W_scaled.jpg')


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
Set the PATH to the natural earth dataset used in this notebook
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
The command above just created a new ESRI Shape File (default vector format), we will see how to work on vector files later on in the OGR section.

Mosaicking

gdal_merge.py is a python script that can be used for simple mosaicking tasks. Mosaic the east.tif and west.tif into a single file:
Can you write Python code for it?

!/home/main/anaconda/envs/python3/bin/gdal_merge.py  {DATADIR}/east.tif {DATADIR}/west.tif -o {DATADIR}/merged.tif


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
The same task can be accomplished with gdalwarp. gdalwarp has a variety of advantages over gdal_merge, but can be slow to merge many files:
gdalwarp east.tif west.tif warpmerged.tif

Get to know OGR

ogrinfo

Like we did with raster using gdalinfo, is possible to retrieve descriptive information from a vector datasource using the command line too ogrinfo.
Let's run ogrinfo on the previously generated shapefile index_natural_earth.shp:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
The shape file we just created can not be rendered directly in the notebook. Later in the python tutorial we'll learn how to use libraries like shapely to render directly vector data.
For now to display the results of our processing in the notebook we'll use the shp2img command line tool (provided by mapserver ).
shp2img require a mapserver mapfile as input to generate a rendered image.
Below we'll write a mapfile with 3 layers :
* west.tif
* east.tif
* index_natural_earth.shp
Note: the shapefile color has been classified based on the shapefile attribute location with a translarency to show the 2 raster images underneat.
Can you write Python code for it?

index_natural_earth="""
MAP
  EXTENT -180 -89.999999999982 179.999999999964 90
  IMAGETYPE "png"
  NAME "simplepolygon"
  SIZE 600 600
  STATUS ON
  UNITS DD

  OUTPUTFORMAT
    NAME "png"
    MIMETYPE "image/png"
    DRIVER "AGG/PNG"
    EXTENSION "png"
    IMAGEMODE RGB
    TRANSPARENT TRUE
  END # OUTPUTFORMAT

  PROJECTION
    "proj=longlat"
    "datum=WGS84"
    "no_defs"
  END # PROJECTION

    LAYER
    DATA "{DATADIR}/west.tif"
    EXTENT -180 -89.999999999982 -1.79596892913025e-11 90
    METADATA
      "ows_title"	"west"
    END # METADATA
    NAME "west"
    PROJECTION
      "proj=longlat"
      "datum=WGS84"
      "no_defs"
    END # PROJECTION
    STATUS ON
    TILEITEM "location"
    TYPE RASTER
    UNITS METERS
  END # LAYER

  LAYER
    DATA "{DATADIR}/east.tif"
    EXTENT -1.79596892913025e-11 -89.999999999982 179.999999999964 90
    METADATA
      "ows_title"	"east"
    END # METADATA
    NAME "east"
    PROJECTION
      "proj=longlat"
      "datum=WGS84"
      "no_defs"
    END # PROJECTION
    STATUS ON
    TILEITEM "location"
    TYPE RASTER
    UNITS METERS
  END # LAYER

  LAYER
    DATA "{DATADIR}/index_natural_earth.shp"
    EXTENT -180 -89.999999999982 179.999999999964 90
    NAME "index_natural_earth"
    PROJECTION
      "proj=longlat"
      "datum=WGS84"
      "no_defs"
    END # PROJECTION
    STATUS ON
    TILEITEM "location"
    TYPE POLYGON
    UNITS METERS
    CLASS
      NAME "{DATADIR}/east.tif"
      EXPRESSION ("[location]" ="{DATADIR}/east.tif")
      STYLE
        OPACITY 50
        COLOR 218 57 57
      END # STYLE
      STYLE
        OUTLINECOLOR 0 0 0
        WIDTH 0.26
      END # STYLE
    END # CLASS
    CLASS
      NAME "{DATADIR}/west.tif"
      EXPRESSION ("[location]" ="{DATADIR}/west.tif")
      STYLE
        OPACITY 50
        COLOR 18 211 14
      END # STYLE
      STYLE
        OUTLINECOLOR 0 0 0
        WIDTH 0.26
      END # STYLE
    END # CLASS
  END # LAYER
END #...


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
To rescale a specific band we can use the "-scale_bn" syntax where bn is a band number (e.g. "-scale_2" for the 2nd band of the output dataset), in the example below we will rescale the 3 bands of the HYP_50M_SR_W GeoTiff to be in the range 0-255 :
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
Use gdalinfo to verify data type.
Can you write Python code for it?

!gdalinfo {DATADIR}/HYP_50M_SR_W.tif | grep Band

!gdalinfo {DATADIR}/HYP_50M_SR_W_Int16.tif | grep Band


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR Quickstart.ipynb".
The first task is:
Use gdalinfo to verify the size.
Can you write Python code for it?

!gdalinfo {DATADIR}/HYP_50M_SR_W.tif | grep Size

!gdalinfo {DATADIR}/HYP_50M_SR_W_small.tif | grep Size


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR with Python.ipynb".
The first task is:
Now compute a list of points along the geodesic curve with a fixed distance of 100000m
Can you write Python code for it?

gc = [Geodesic.WGS84.Direct(lat1, lon1, g['azi1'], i) for i in range(0,int(g['s12']),100000)]


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR with Python.ipynb".
The first task is:
Now let's start with OGR!
We will first create a geometry collection with:
Can you write Python code for it?

from osgeo import ogr
geomcol =  ogr.Geometry(ogr.wkbGeometryCollection)


I am working on the file "GSOC/notebooks/Access to Geospatial data/GDAL-OGR with Python.ipynb".
The first task is:
To save on file the new dataset we will echo the geojson string to a file and then use the gist utility to upload the data online:
Can you write Python code for it?

!echo '{data}' > /tmp/geojson.geojson

!gist -p /tmp/geojson.geojson

from CesiumWidget import CesiumWidget

cesiumExample = CesiumWidget(width="100%",geojson=data, enable_lighting=True)

cesiumExample


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
0.3048006096 meters per foot
ossim-info --ft2mtrs-us-survey <feet>
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
0.3048006096 meters per foot
ossim-info --mtrs2ft-us-survey  <meters>
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
Projections
Prints projections list
ossim-info --projections
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
Utility functions
radians from degrees
ossim-info --deg2rad <degrees>
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
ossim-chipper
Can you write Python code for it?

!ossim-chipper --color 255 255 255 \
              --azimuth 270 \
              --elevation 45 \
              --exaggeration 2.0 \
              --op hillshade \
              --color-table {DATADIR}/ossim-dem-color-table-template.kwl \
              --input-dem {DATADIR}/SRTM_fB03_p011r031.tif \
              hillshade.jpg -P /usr/local/share/ossim/ossim_preference

!ossim-info {DATADIR}/SRTM_fB03_p011r031.tif -P /usr/local/share/ossim/ossim_preference

!gdalinfo {DATADIR}/SRTM_fB03_p011r031.tif

Image('hillshade.jpg')


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
ossim-cmm
Can you write Python code for it?

!ossim-cmm {DATADIR}/p011r031_7t19990918_z19_nn10.tif -P /usr/local/share/ossim/ossim_preference

!ossim-cmm {DATADIR}/p011r031_7t19990918_z19_nn20.tif -P /usr/local/share/ossim/ossim_preference

!ossim-cmm {DATADIR}/p011r031_7t19990918_z19_nn30.tif -P /usr/local/share/ossim/ossim_preference

!cat /home/main/notebooks/data/landsat/p011r031_7t19990918_z19_nn10.omd


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
ossim-img2md
Usage: ossim-img2md [options] <metadata_writer> <input_file> <output_file>

Valid metadata writer types:
envi_header
ers_header
ossim_fgdc
ossim_geometry
ossim_readme
tiff_world_file
jpeg_world_file
Can you write Python code for it?

!ossim-img2md -P /usr/local/share/ossim/ossim_preference tiff_world_file {DATADIR}/p011r031_7t19990918_z19_nn10.tif {DATADIR}/p011r031_7t19990918_z19_nn10.tfw

!cat /home/main/notebooks/data/landsat//p011r031_7t19990918_z19_nn10.tfw


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
ossim-info
top
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
top
Explore raster data
ground center
```ossim-info --cg filename```
Can you write Python code for it?

!ossim-info --cg {DATADIR}/p011r031_7t19990918_z19_nn10.tif -P /usr/local/share/ossim/ossim_preference


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
top
Meters per degrees
Gives meters per degree and meters per minute for a given latitude.
ossim-info --mtrsPerDeg <latitude>
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
top
ground and image center
```ossim-info -c filename```
Can you write Python code for it?

!ossim-info  -c {DATADIR}/p011r031_7t19990918_z19_nn10.tif -P /usr/local/share/ossim/ossim_preference


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
top
height
return the MSL and ellipoid height given a latitude longitude position
ossim-info --height <latitude-in-degrees> <longitude-in-degrees>
Can you write Python code for it?

# note we pass the path to the ossim_preference file to tell where the geoid file is
!ossim-info --height 47.54 157.40 -P /usr/local/share/ossim/ossim_preference


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
top
image projection information
ossim-info -p filename
Can you write Python code for it?

!ossim-info  -p {DATADIR}/p011r031_7t19990918_z19_nn10.tif -P /usr/local/share/ossim/ossim_preference


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
top
image rectangle
ossim-info -r filename
Can you write Python code for it?

!ossim-info  -r {DATADIR}/p011r031_7t19990918_z19_nn10.tif -P /usr/local/share/ossim/ossim_preference


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
top
meta data image information
ossim-info -m filename
Can you write Python code for it?

!ossim-info  -m {DATADIR}/p011r031_7t19990918_z19_nn10.tif -P /usr/local/share/ossim/ossim_preference


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
top
meters from feet

0.3048 meters per foot
ossim-info --ft2mtrs <feet>
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
top
ossim-icp
ossim-icp [options] <output_type> <input_file> <output_file>
Can you write Python code for it?

# A complete list of ossim writers (driver) is given by:
!ossim-info --writers -P /usr/local/share/ossim/ossim_preference

#convert a geotiff to a geopdf
!ossim-icp ossim_pdf rgb_histmm.jpeg rgb_histmm.pdf

#view the results in a pdf viewer
#nohup evince lsat7_2002_30.pdf &


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
top
ossim-igen
Execute image chains specified in a spec file.
In the folowing example the spec file rgb.spec has been generated using  imagelinker, from an example session: /home/user/ossim/ossim-rgb.prj
Can you write Python code for it?

#ossim-igen /home/user/ossim/rgb.spec

#display < /home/user/rgb.jpg


I am working on the file "GSOC/notebooks/Access to Geospatial data/OSSIM Quickstart.ipynb".
The first task is:
top
ossim-img2rr
Can you write Python code for it?

!ossim-img2rr {DATADIR}/p011r031_7t19990918_z19_nn10.tif -P /usr/local/share/ossim/ossim_preference

!ossim-img2rr {DATADIR}/p011r031_7t19990918_z19_nn20.tif -P /usr/local/share/ossim/ossim_preference

!ossim-img2rr {DATADIR}/p011r031_7t19990918_z19_nn30.tif -P /usr/local/share/ossim/ossim_preference


I am working on the file "GSOC/notebooks/Numerical Cartography/The Geodesic Problem.ipynb".
The first task is:
Extract Latitude and Longitude from and add the destination point which is missed in the previous list
Can you write Python code for it?

lat = [i['lat2'] for i in gc]
lat.append(lat2)
lon = [i['lon2'] for i in gc]
lon.append(lon2)


I am working on the file "GSOC/notebooks/Numerical Cartography/The Geodesic Problem.ipynb".
The first task is:
Geodetic curve
Example:
Compute a list of points along the $geodesic \ curve$ with a fixed distance of $100000m$
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Numerical Cartography/The Geodesic Problem.ipynb".
The first task is:
Note:
* pyproj takes as input longitude first then latitude
* the output of geographiclib and pyproj for the backward azimuth are complementary of 180^{\circ}
The direct geodesic problem:
Example:
Compute the midpoint on the path from 1 to 2 starting at 1
Can you write Python code for it?

h = Geodesic.WGS84.Direct(lat1, lon1, g['azi1'], g['s12']/2)
print(h['lat2'], h['lon2']);


I am working on the file "GSOC/notebooks/Numerical Cartography/The Geodesic Problem.ipynb".
The first task is:
Set two location for which we want compute the measurments, in this example $P_1$
Can you write Python code for it?

lat1,lon1 = (40.7143528, -74.0059731)  # New York, NY
lat2,lon2 = (1.359, 103.989)   # Delhi, India
az12,az21,dist = g.inv(lon1,lat1,lon2,lat2)
az12,az21,dist

# using geograhiclib:
# Compute path from 1 to 2
from geographiclib.geodesic import Geodesic
g = Geodesic.WGS84.Inverse(lat1, lon1, lat2, lon2)
g


I am working on the file "GSOC/notebooks/Numerical Cartography/The Geodesic Problem.ipynb".
The first task is:
Surface's area on the ellipsoid
The Geodesic class in geographiclib can be also used to compute the area on an ellipsoidal surface, see example:
Can you write Python code for it?

# Computing the area of a geodesic polygon
def p(lat,lon): return {'lat': lat, 'lon': lon}
Geodesic.WGS84.Area([p(0, 0), p(0, 90), p(90, 0)])


I am working on the file "GSOC/notebooks/Numerical Cartography/The Geodesic Problem.ipynb".
The first task is:
We can plot the resulting geodesic with:
Can you write Python code for it?

%matplotlib inline

import matplotlib.pyplot as plt
plt.plot(lon,lat,'-');


I am working on the file "GSOC/notebooks/Numerical Cartography/geometric-transformation.ipynb".
The first task is:
Helmert 7 Parameters
Can you write Python code for it?

from transform import helmert

res_helmert = helmert(gcp1='d1s.csv', gcp2='d2s.csv', inputf='d.csv', output='helmert.txt')
res_helmert

d2[11:][['x','y']]

delta_conforme = (d2[11:]['x'].values - res_conforme[:,0], d2[11:]['y'].values - res_conforme[:,1])
delta_conforme

delta_affine = (d2[11:]['x'].values - res_affine[:,0], d2[11:]['y'].values - res_affine[:,1])
delta_affine

delta_helmert = (d2[11:]['x'].values - res_helmert[:,0], d2[11:]['y'].values - res_helmert[:,1])
delta_helmert


I am working on the file "GSOC/notebooks/Numerical Cartography/geometric-transformation.ipynb".
The first task is:
Random points in DATUM 1
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/CARTOPY/00 Using cartopy with matplotlib.ipynb".
The first task is:
A list of the available projections to be used with matplotlib can be found on the Cartopy projection list notebook.
The line plt.axes(projection=ccrs.PlateCarree()) sets up a GeoAxes instance which exposes a variety of other map related methods, in the case of the previous example, we used the coastlines() method to add coastlines to the map.
Lets create another map in a different projection, and make use of the stock_img() method to add an underlay image to the map:
Can you write Python code for it?

import cartopy.crs as ccrs
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.Mollweide())
ax.stock_img();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/00 Using cartopy with matplotlib.ipynb".
The first task is:
Adding data to the map
Once you have the map just the way you want it, data can be added to it in exactly the same way as with normal matplotlib axes. By default, the coordinate system of any data added to a GeoAxes is the same as the coordinate system of the GeoAxes itself, to control which coordinate system that the given data is in, you can add the transform keyword with an appropriate cartopy.crs.CRS instance:
Can you write Python code for it?

%matplotlib inline
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()

ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61

plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
         color='blue', linewidth=2, marker='o',
         transform=ccrs.Geodetic(),
         )

plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
         color='gray', linestyle='--',
         transform=ccrs.PlateCarree(),
         )

plt.text(ny_lon - 3, ny_lat - 12, 'New York',
         horizontalalignment='right',
         transform=ccrs.Geodetic())

plt.text(delhi_lon + 3, delhi_lat - 12, 'Delhi',
         horizontalalignment='left',
         transform=ccrs.Geodetic());


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
EuroPP
projection list
class cartopy.crs.EuroPP
UTM Zone 32 projection for EuroPP domain.
Ellipsoid is International 1924, Datum is ED50.
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.EuroPP())
ax.coastlines(resolution='110m')
ax.stock_img()
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
Geostationary
projection list
class cartopy.crs.Geostationary(central_longitude=0.0, satellite_height=35785831, false_easting=0, false_northing=0, globe=None)
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.Geostationary())
ax.coastlines(resolution='110m')
ax.stock_img()
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
Gnomonic
projection list
class cartopy.crs.Gnomonic(central_latitude=0.0, globe=None)
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.Gnomonic())
ax.coastlines(resolution='110m')
ax.stock_img()
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
LambertCylindrical
projection list
class cartopy.crs.LambertCylindrical(central_longitude=0.0)
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.LambertCylindrical())
ax.coastlines(resolution='110m')
ax.stock_img()
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
NorthPolarStereo
projection list
class cartopy.crs.NorthPolarStereo(central_longitude=0.0, globe=None)
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.NorthPolarStereo())
ax.coastlines(resolution='110m')
ax.stock_img()
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
OSGB
projection list
class cartopy.crs.OSGB
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.OSGB())
ax.coastlines(resolution='110m')
ax.stock_img()
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
Orthographic
projection list
class cartopy.crs.Orthographic(central_longitude=0.0, central_latitude=0.0, globe=None)
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.Orthographic())
ax.coastlines(resolution='110m')
ax.stock_img()
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
Robinson
projection list
class cartopy.crs.Robinson(central_longitude=0, globe=None)
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.Robinson())
ax.coastlines(resolution='110m')
ax.stock_img()
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
Stereographic
projection list
class cartopy.crs.Stereographic(central_latitude=0.0, central_longitude=0.0, false_easting=0.0, false_northing=0.0, true_scale_latitude=None, globe=None)
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.Stereographic())
ax.coastlines(resolution='110m')
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
TransverseMercator
projection list
class cartopy.crs.TransverseMercator(central_longitude=0.0, central_latitude=0.0, false_easting=0.0, false_northing=0.0, scale_factor=1.0, globe=None)
A Transverse Mercator projection.
Kwargs:

central_longitude - The true longitude of the central meridian in
    degrees. Defaults to 0.

central_latitude - The true latitude of the planar origin in
    degrees. Defaults to 0.

false_easting - X offset from the planar origin in metres.
    Defaults to 0.

false_northing - Y offset from the planar origin in metres.
    Defaults to 0.

scale_factor - Scale factor at the central meridian. Defaults
    to 1.

globe - An instance of cartopy.crs.Globe. If omitted, a
    default globe is created.
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.TransverseMercator())
ax.coastlines(resolution='110m')
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
UTM
projection list
class cartopy.crs.UTM(zone, southern_hemisphere=False, globe=None)
Universal Transverse Mercator projection.
Kwargs:

zone - the numeric zone of the UTM required.

globe - An instance of cartopy.crs.Globe. If omitted, a
    default globe is created.

southern_hemisphere - set to True if the zone is in the southern
    hemisphere, defaults to False.

InterruptedGoodeHomolosine
projection list
class cartopy.crs.InterruptedGoodeHomolosine(central_longitude=0, globe=None)
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.InterruptedGoodeHomolosine())
ax.coastlines(resolution='110m')
ax.stock_img()
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CARTOPY/01 CARTOPY-projections.ipynb".
The first task is:
set the central longitude to $180^\circ$
Can you write Python code for it?

plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
ax.coastlines(resolution='110m')
ax.stock_img()
ax.gridlines();


I am working on the file "GSOC/notebooks/Projects/CESIUM/CesiumWidget Example.ipynb".
The first task is:
A more complex CZML example
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/CESIUM/CesiumWidget Example.ipynb".
The first task is:
Add some data to the viewer

A simple czml
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/CESIUM/CesiumWidget Example.ipynb".
The first task is:
Display the widget:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/CESIUM/CesiumWidget Example.ipynb".
The first task is:
The code:
from czml_example import simple_czml, complex_czml
Simply import some CZML data for the viewer to display.
Create widget object
Can you write Python code for it?

cesiumExample = CesiumWidget(width="100%",czml=simple_czml, enable_lighting=True)


I am working on the file "GSOC/notebooks/Projects/CESIUM/CesiumWidget Example.ipynb".
The first task is:
store the CZML objet in a dictionary and use their name as keys
define a function to switch between CZML
bind the IPython intercat class to the function
Can you write Python code for it?

myczml = {'simple_czml':simple_czml, 'complex_czml':complex_czml}

myplace = {'Eboli, IT':'', 'Woods Hole, MA':'', 'Durham, NH':''}

import geocoder
import time
for i in myplace.keys():
    g = geocoder.google(i)
    print(g.latlng)
    myplace[i]=g.latlng

myplace

def f(CZML):
    cesiumExample.czml = myczml[CZML]

def z(Location,z=(0,20000000)):
    cesiumExample.zoom_to(myplace[Location][1],myplace[Location][0],z)

interact(f, CZML=('simple_czml','complex_czml')), interact(z, Location=('Eboli, IT','Woods Hole, MA','Durham, NH'));

cesiumExample


I am working on the file "GSOC/notebooks/Projects/FIONA/fiona-ipynb-interop.ipynb".
The first task is:
Task: quickly examine the bounding areas of a directory of shapefiles

use fiona.open() to read data files on disk
save the filename and bounding box into a python dict

.
Can you write Python code for it?

td = {}

for shp in shps:
    with fiona.open( shp, 'r') as inp:
        td[ inp.name ] = inp.bounds

## Fiona inp.bounds => ( lower_lng, lower_lat, upper_lng, upper_lat)

## Create shapely geometry from the coords
## shapely/geometry/geo.py
##   box(minx, miny, maxx, maxy, ccw=True)

nboxes = []
for k,v in iter(td.iteritems()):
    nboxes.append( box( v[0], v[1], v[2], v[3]) )

print 'Found BBOXs: ',len(nboxes)

## create a single cascaded UNION too
dst_poly = cascaded_union(nboxes)
dst_poly.bounds

## Draw every BBOX for all files, transparently
## use matplotlib.Polygon to draw; let autoscale calculate the area

fig, ax = plt.subplots(figsize=(12,12))
for polygon in nboxes:
    mpl_poly = Polygon(np.array(polygon.exterior), facecolor="y", alpha=0.02)
    ax.add_patch(mpl_poly)

## Indicate the exterior of the study area with a heavy line
ax.add_patch( Polygon(np.array(dst_poly.exterior), fill=False, lw=4, ec="b", alpha=0.9) )

ax.relim()
ax.autoscale()


I am working on the file "GSOC/notebooks/Projects/FIONA/fiona-ipynb-interop.ipynb".
The first task is:
Terminal Commands
Shell script can be executed with results stored into python variables
Can you write Python code for it?

shps = !ls /home/user/data/north_carolina/shape/*shp


I am working on the file "GSOC/notebooks/Projects/GRASS/Introduction to GRASS GIS/igrass/Command_parsing.ipynb".
The first task is:
Use of the g.mapset
* Set the GRASS WORKSPACE to :
    - LOCATION  : spearfish
    - MAPSET : user1
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/Introduction to GRASS GIS/igrass/Command_parsing.ipynb".
The first task is:
print info for a raster layer with r.info
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/Introduction to GRASS GIS/igrass/Command_parsing.ipynb".
The first task is:
print projection info with g.proj
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/Introduction to GRASS GIS/igrass/Command_parsing.ipynb".
The first task is:
use the getLayerList function to store the g.list output in a python list
Can you write Python code for it?

rasterlist = getLayerList(type='rast')
vectorlist = getLayerList(type='vect')

rasterlist

vectorlist


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb".
The first task is:
Before running any GRASS raster modules, you need to set the computational region using g.region. In this example, we set the computational extent and resolution to the raster layer elevation.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb".
The first task is:
Calling GRASS GIS modules with textual input or output
Textual output from modules can be captured using the read_command() function.
Can you write Python code for it?

print(gscript.read_command('g.region', flags='p'))

print(gscript.read_command('r.univar', map='elev_smoothed', flags='g'))


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb".
The first task is:
Certain modules require the text input be in a file or provided as standard input. Using the write_command() function we can conveniently pass the string to the module. Here, we are creating a new vector with one point with v.in.ascii.
Note that stdin parameter is not used as a module parameter, but its content is passed as standard input to the subprocess.
Can you write Python code for it?

gscript.write_command('v.in.ascii', input='-', stdin='%s|%s' % (635818.8, 221342.4), output='view_point')


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb".
The first task is:
Convenient wrapper functions
Some modules have wrapper functions to simplify frequent tasks.
We can obtain the information about the vector layer which we just created with the v.info wrapper.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb".
The first task is:
For comparison, below is the same example, but using the read_command() function.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb".
The first task is:
Here is an example of a different g.list wrapper which structures the output as list of pairs (name, mapset).
We obtain current mapset with g.gisenv wrapper.
Can you write Python code for it?

current_mapset = gscript.gisenv()['MAPSET']
gscript.list_pairs('raster', mapset=current_mapset)


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb".
The first task is:
It is also possible to retrieve the raster layer history (r.support) and layer information (r.info) or to query (r.what) raster layer pixel values.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb".
The first task is:
Specifics of interactive interpreters
When using the GUI, we can look at the map right away. In scripts, we rarely render a map. In IPython Notebook we are able to show a map as a result. For this we use function view() from a custom Python module render supplied with these notebooks. (This function might be part of GRASS GIS Python API for IPython in the future.)
Can you write Python code for it?

from render import view
view(rasters=['elev_smoothed'])


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb".
The first task is:
We can list data stored in a GRASS GIS location with g.list wrappers. With this function, the map layers are grouped by mapsets (in this example, raster layers):
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/01_scripting_library.ipynb".
The first task is:
When an unrecoverable error occurs (due to incorrect parameter use or other issues), the GRASS GIS functions usually print the error message and end the program execution (by calling the exit() function). However, when working in an interactive environment such as IPython, the behavior can be changed using set_raise_on_error() function. The following call will cause GRASS GIS Python functions to raise an exception instead of calling exit().
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Add the above two points to the new vector map with:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Create an instance of the vector map, with:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Create some vector geometry features, like two points:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Extra parameters of the Module class
run_
The run_ parameter defines if the module must be execute or not, the default value is True.
finish_
The finish_ parameter defines if the module has to wait the end of the process, default is True
env_
The env_ parameter defines the environmental variable that will be set and used by the process.
stdin_, stdout_, stderr_
The parameters stdin_, stdout_ and stderr_ are used to pass or capture the input or the output of the GRASS GIS modules.
Example
Can you write Python code for it?

import subprocess as sub

# Another way to use the Module class objects is through the shortcuts
# import the shortcuts for the general modules
from grass.pygrass.modules.shortcuts import general as g


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Finally close the vector map, with:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Note: we don't need to close the vector map because it is already closed by the context manager.
Reading an existing vector map
Can you write Python code for it?

with VectorTopo('view_points', mode='r') as points:
    for point in points:
        print(point, point.attrs['name'])


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Now do the same thing using the context manager syntax provided by with, and creating and setting also the attribute table, with:
Can you write Python code for it?

# Define the columns of the new vector map
cols = [(u'cat',       'INTEGER PRIMARY KEY'),
        (u'name',      'TEXT')]

with VectorTopo('view_points', mode='w', tab_cols=cols, overwrite=True) as view_points:
    # save the point and the attribute
    view_points.write(point1, ('pub', ))
    view_points.write(point2, ('restaurant', ))
    # save the changes to the database
    view_points.table.conn.commit()


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Now we can call and capture the stdout of a GRASS GIS modules, with:
Can you write Python code for it?

greg = g.region(flags='p', stdout_=sub.PIPE)
print(greg.outputs.stdout)


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Now we have row by row access to the whole raster map. Below we show an example to access the first row:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Open the map in write mode, with:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Or re-execute the module passing only the parameter(s) that need(s) to be different:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Reading an existing raster map
Instanziate a raster class with:
Can you write Python code for it?

elev = RasterRow('elevation', mapset='PERMANENT')


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
To convert the whole raster map to a numpy array, and then back to a GRASS GIS raster map, we can define two functions like:
Can you write Python code for it?

import numpy as np

from grass.pygrass.raster.buffer import Buffer
from grass.pygrass.gis.region import Region

def raster2numpy(rastname, mapset=''):
    """Return a numpy array from a raster map"""
    with RasterRow(rastname, mapset=mapset, mode='r') as rast:
        return np.array(rast)

def numpy2raster(array, mtype, rastname, overwrite=False):
    """Save a numpy array to a raster map"""
    reg = Region()
    if (reg.rows, reg.cols) != array.shape:
        msg = "Region and array are different: %r != %r"
        raise TypeError(msg % ((reg.rows, reg.cols), array.shape))
    with RasterRow(rastname, mode='w', mtype=mtype, overwrite=overwrite) as new:
        newrow = Buffer((array.shape[1],), mtype=mtype)
        for row in array:
            newrow[:] = row[:]
            new.put_row(newrow)

elev = raster2numpy('elevation', mapset='PERMANENT')

elev

numpy2raster(elev * 2, mtype='FCELL', rastname='doubled_elev', overwrite=True)

# import a function to render the results
from render import view

from grass.pygrass.modules.shortcuts import raster as r

# set the colors of the input and output maps to a predefined elevation color table
r.colors(map='elevation@PERMANENT,doubled_elev', color='elevation')

view(['elevation@PERMANENT'])

view(['doubled_elev'])


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/02_pygrass_library.ipynb".
The first task is:
Working with raster data
A class with similar behaviour has been created for raster maps, import this class with:
Can you write Python code for it?

from grass.pygrass.raster import RasterRow


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/03_create_script.ipynb".
The first task is:
As a first property of the viewshed, compute its area. The area is computed with r.univar as the number of non-null cells times the raster cell size. The result is in reported map units squared (i.e., square meters in this case).
Can you write Python code for it?

cells = gscript.parse_command('r.univar', map=viewshed_name,
                              flags='g')['n']
print(cells)
print gscript.region()

area = float(cells) * gscript.region()['nsres'] * gscript.region()['nsres']
print(area)


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/03_create_script.ipynb".
The first task is:
The last viewshed property we want to compute is the distance from the current point to the closest visible point.
Since v.distance requires vector input, we first save the current point as a separate vector layer using v.in.ascii.
Can you write Python code for it?

tmp_point = 'tmp_current_point'
if float(n_points_visible) >= 1:
    gscript.write_command('v.in.ascii', input='-', stdin='%s|%s' % (point.x, point.y),
                          output=tmp_point)
    distance = gscript.read_command('v.distance', from_=tmp_point, from_type='point', flags='p',
                                    to=visible_points, to_type='point', upload='dist', dmin=1).strip()

    distance = float(distance.splitlines()[1].split('|')[1])
else:
    distance = 0
print(distance)


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/03_create_script.ipynb".
The first task is:
We can now get the number of points using vector_info_topo, a wrapper function around v.info.
Can you write Python code for it?

n_points_visible = gscript.vector_info_topo(visible_points)['points']
print n_points_visible


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/03_create_script.ipynb".
The first task is:
We first generate some random input points within a specified region using v.random (we use a fixed seed here for reproducibility):
Can you write Python code for it?

import grass.script as gscript

gscript.run_command('g.region', n=225200, s=222500, w=637500, e=640000, raster='elevation')
gscript.run_command('v.random', output='input_points', npoints=20, seed=2, quiet=True)


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/03_create_script.ipynb".
The first task is:
We now compute the viewshed from this point using r.viewshed and then change all invisible cells to null values (no data) and all visible cells to value 1 using raster algebra r.mapcalc. This is needed for converting it to the vector data model further on (vectorization).
Can you write Python code for it?

elevation = 'elevation'
input_points = 'input_points'
viewshed_name = 'viewshed'
tmp_viewshed_name = 'tmp_viewshed'
viewshed_id = 1

gscript.run_command('r.viewshed', input=elevation, observer_elevation=3,
                    output=tmp_viewshed_name, coordinates=point.coords())
gscript.mapcalc(exp="{viewshed} = if({tmp}, {vid}, null())".format(viewshed=viewshed_name,
                                                                   tmp=tmp_viewshed_name,
                                                                   vid=viewshed_id))


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/03_create_script.ipynb".
The first task is:
Workflow for multiple points
Now we will put the previous snippets together and compute the viewshed properties in a loop for all given input points.
Notice that we subtract 1 from the number of visible points since the current point is one of the visible ones.
Can you write Python code for it?

from grass.pygrass.vector import Vector
import grass.script as gscript

elevation = 'elevation'
input_points = 'input_points'

tmp_viewshed_name = 'tmp_viewshed'
tmp_visible_points = 'tmp_points'
tmp_point = 'tmp_current_point'

with Vector(input_points, mode='r') as points:
    for point in points:
        viewshed_id = str(point.cat)
        viewshed_name = 'viewshed_' + viewshed_id
        gscript.run_command('r.viewshed', input=elevation, observer_elevation=3,
                            output=tmp_viewshed_name, coordinates=point.coords())
        gscript.mapcalc(exp="{viewshed} = if({tmp}, {vid}, null())".format(viewshed=viewshed_name,
                                                                           tmp=tmp_viewshed_name,
                                                                           vid=viewshed_id))

        # viewshed size
        cells = gscript.parse_command('r.univar', map=viewshed_name,
                                      flags='g')['n']
        area = float(cells) * gscript.region()['nsres'] * gscript.region()['nsres']

        # visible points
        gscript.run_command('r.to.vect', input=viewshed_name, output=viewshed_name,
                            type='area')
        gscript.run_command('v.select', ainput=input_points, atype='point',
                            binput=viewshed_name, btype='area',
                            operator='overlap', flags='t', output=tmp_visible_points)
        n_points_visible = gscript.vector_info_topo(tmp_visible_points)['points'] - 1

        # distance to closest visible point
        if float(n_points_visible) >= 1:
            gscript.write_command('v.in.ascii', input='-', stdin='%s|%s' % (point.x, point.y),
                                  output=tmp_point)
            distance = gscript.read_command('v.distance', from_=tmp_point, from_type='point', flags='p',
                                            to=tmp_visible_points, to_type='point', upload='dist', dmin=1).strip()

            distance =...


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/04_script_to_grass_module.ipynb".
The first task is:
Adding parameters as custom options
When using standard options, all items are predefined by the parser but we can modify e.g. key or other items to our needs. However, for other cases, we have to define the options completely from scratch. In our example, we introduce a non-standard key with name max_distance which has its type set to double. We use additionally description to document the meaning of this option. In case we would need a longer description we may use the label key for a short and concise description and the description key for more lengthy and wordy description. In addition to the description we add also key_desc which is a short hint for a proper syntax. Again, the existing GRASS GIS Python scripts may help to quickly understand the best practice.
Our max_distance parameter option has as default value -1 which stands for infinity in the case of viewsheds. The user is not required to provide this option, so we set required to no. Also, there can be only one value provided for this option, so we set multiple to no as well.
Can you write Python code for it?

%%file r.viewshed.points.py
#!/usr/bin/env python

#%module
#% description: Compute and analyze viewsheds
#% keyword: raster
#% keyword: viewshed
#%end
#%option G_OPT_R_INPUT
#% key: elevation
#%end
#%option G_OPT_V_INPUT
#% key: points
#%end
#%option
#% key: max_distance
#% key_desc: value
#% type: double
#% description: Maximum visibility radius. By default infinity (-1)
#% answer: -1
#% multiple: no
#% required: no
#%end

import grass.script as gscript

def main():
    options, flags = gscript.parser()
    elevation = options['elevation']
    points = options['points']
    max_distance = float(options['max_distance'])
    print(elevation, points, max_distance)

if __name__ == '__main__':
    main()

!./r.viewshed.points.py elevation=elevation points=bridges max_distance=50


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/04_script_to_grass_module.ipynb".
The first task is:
Adding parameters as flags
Can you write Python code for it?

%%file r.viewshed.points.py
#!/usr/bin/env python

#%module
#% description: Compute and analyze viewsheds
#% keyword: raster
#% keyword: viewshed
#%end
#%option G_OPT_R_INPUT
#% key: elevation
#%end
#%option G_OPT_V_INPUT
#% key: points
#%end
#%option
#% key: max_distance
#% key_desc: value
#% type: double
#% description: Maximum visibility radius. By default infinity (-1)
#% answer: -1
#% multiple: no
#% required: no
#%end
#%flag
#% key: c
#% description: Consider the curvature of the earth (current ellipsoid)
#%end

import grass.script as gscript

def main():
    options, flags = gscript.parser()
    elevation = options['elevation']
    points = options['points']
    max_distance = float(options['max_distance'])
    use_curvature = flags['c']
    print(elevation, points, max_distance)
    if use_curvature:
        print(use_curvature, "is true")
    else:
        print(use_curvature, "is false")

if __name__ == '__main__':
    main()

!./r.viewshed.points.py -c elevation=elevation points=bridges max_distance=50


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/04_script_to_grass_module.ipynb".
The first task is:
Adding parameters as standard options
Now we will add parameters to our module. Let's say, we want to pass the name of an elevation raster map and of a vector points map. In the interface definition comment we hence add a section option for each of these maps and define respectively standard (or predefined) options. For the elevation raster map we use G_OPT_R_INPUT and for the points vector map G_OPT_V_INPUT. Both these standard options are named input by default, so we have to give them unique names. Here we will use elevation for the raster and points for the vector map. The option's name is defined using a key named key.
Can you write Python code for it?

%%file r.viewshed.points.py
#!/usr/bin/env python

#%module
#% description: Compute and analyze viewsheds
#% keyword: raster
#% keyword: viewshed
#%end
#%option G_OPT_R_INPUT
#% key: elevation
#%end
#%option G_OPT_V_INPUT
#% key: points
#%end

import grass.script as gscript

def main():
    options, flags = gscript.parser()
    elevation = options['elevation']
    points = options['points']
    print(elevation, points)

if __name__ == '__main__':
    main()

!./r.viewshed.points.py --help

!./r.viewshed.points.py elevation=elevation points=bridges


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/04_script_to_grass_module.ipynb".
The first task is:
All GRASS GIS modules must be distributed under GNU GPL license (version 2 or later).
There is a specified way how the first comment in module's main file should look like.
Here is how it can look like for our module:
"""
MODULE:    r.viewshed.points

AUTHOR(S): John Smith <email at some domain>

PURPOSE:   Computes viewshed for multiple points

COPYRIGHT: (C) 2015 John Smith

           This program is free software under the GNU General Public
           License (>=v2). Read the file COPYING that comes with GRASS
           for details.
"""

Although Python is not a compiled language like C, we need to 'compile' also the Python based GRASS GIS module in order to include it in our GRASS installation, and to create HTML documentation and GUI. For this a Makefile needs to be written which follows a standard template as well. The included 'Script.make' takes care of processing everything, given that the Python script, the HTML documentation and optional screenshot(s) in PNG format are present in the same directory.
Can you write Python code for it?

%%file Makefile
MODULE_TOPDIR = ../..

PGM = r.viewshed.points

include $(MODULE_TOPDIR)/include/Make/Script.make

default: script


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/04_script_to_grass_module.ipynb".
The first task is:
As shown in this example,
the region set in the script doesn't affect the current region set in the previous step.
Can you write Python code for it?

import grass.script as gscript

def main():
    gscript.run_command('g.region', n=100, s=0, w=0, e=100, res=1)
    print gscript.region()

if __name__ == '__main__':
    gscript.use_temp_region()
    main()

print gscript.region()


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/04_script_to_grass_module.ipynb".
The first task is:
However in case of error, the cleanup function won't be called:
Can you write Python code for it?

import os
import grass.script as gscript

TMP_MAPS = []

def main():
    for i in range(5):
        name = 'tmp_raster_' + str(os.getpid()) + str(i)
        TMP_MAPS.append(name)
        gscript.mapcalc("{name} = rand(0, 10)".format(name=name), seed=1)
    # we raise intentionally error here:
    raise TypeError
    cleanup()

def cleanup():
    gscript.run_command('g.remove', type='raster', name=','.join(TMP_MAPS), flags='f')

if __name__ == '__main__':
    main()

gscript.list_pairs(type='raster', pattern='tmp_raster_*')


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/04_script_to_grass_module.ipynb".
The first task is:
In these two command, we are using 'backticks' syntax to include output of command into another command.
Handling temporary maps and region
In scripts we have to often create temporary maps, which needs to be removed after the script is finished, or if there is an error.
Similarly, when we need to change computational region in a script, we use temporary region, so that we don't affect the current region settings. This also allows running multiple scripts in parallel, each with its own region settings.
We will show you how to handle such cases.
Temporary maps
We will show you how to deal with temporary maps on a simple example script. We will generate temporary random maps, each with unique name. Unique name can be achieved by using process ID in combination with some other prefix:
Can you write Python code for it?

%tb
import os
import grass.script as gscript

def main():
    for i in range(5):
        name = 'tmp_raster_' + str(os.getpid()) + str(i)
        gscript.mapcalc("{name} = rand(0, 10)".format(name=name), seed=1)

if __name__ == '__main__':
    main()


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/04_script_to_grass_module.ipynb".
The first task is:
Temporary region
Using temporary region is simple - library function use_temp_region is called in the beginning of the script and we can then call g.region anywhere in the script. First, we set the region to match a raster map.
Can you write Python code for it?

import grass.script as gscript
gscript.run_command('g.region', raster='elevation')
print gscript.region()


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/04_script_to_grass_module.ipynb".
The first task is:
The construction if __name__ == '__main__': is a standard way in Python of marking code which should be executed when the file is used as a script. It is a best practice to use the above mentioned template.
GRASS GIS parser
Every GRASS GIS module must use the GRASS parser mechanism. This very advanced parser helps to check the user interput, format the help text and optionally create a graphical user interface for the new module.
In Python, this means calling the parser() function from grass.script package. This function parses the special comments written at the beginning of the Python file (below the 'shebang'), processes the parameters provided in command line when using the module and provides these data accordingly within the module. These special comments start with #% and can be referred to as interface definition comments.
Minimal template
The interface definition comment should contain at least the description of the module and two keywords as shown below.  Existing GRASS GIS Python scripts may help to understand the best practice. These values are defined in section module which contains the description and keyword keys.
Can you write Python code for it?

%%file r.viewshed.points.py
#!/usr/bin/env python

#%module
#% description: Compute and analyze viewsheds
#% keyword: raster
#% keyword: viewshed
#%end

import grass.script as gscript

def main():
    gscript.parser()

if __name__ == '__main__':
    main()


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/04_script_to_grass_module.ipynb".
The first task is:
Then we can compile:
Can you write Python code for it?

!make MODULE_TOPDIR=`grass70 --config path`


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/04_script_to_grass_module.ipynb".
The first task is:
Therefore we will use Python atexit module to ensure the cleanup functions is called everytime, even when the script ends with error.
Can you write Python code for it?

import os
import atexit
import grass.script as gscript

TMP_MAPS = []

def main():
    for i in range(5):
        name = 'tmp_raster_' + str(os.getpid()) + str(i)
        TMP_MAPS.append(name)
        gscript.mapcalc("{name} = rand(0, 10)".format(name=name), seed=1)
    # we raise intentionally error here:
    raise TypeError

def cleanup():
    gscript.run_command('g.remove', type='raster', name=','.join(TMP_MAPS), flags='f')

if __name__ == '__main__':
    atexit.register(cleanup)
    main()


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/06_temporal_framework.ipynb".
The first task is:
Deleting a temporal dataset
Removing a space time datasets from temporal database (again, the contained maps remain in GRASS GIS) can be done directly from the same object using the delete function:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/06_temporal_framework.ipynb".
The first task is:
Get useful info (as name, starting time) about registered maps and query the data using the GRASS GIS command r.what:
Can you write Python code for it?

from collections import OrderedDict
infos = OrderedDict()
for row in rows:
    name = row["name"] + "@" + row["mapset"]
    values = gscript.read_command('r.what', map=name, coordinates=coors).strip().split('|')
    infos[name] = {'date': row["start_time"], 'value': values[3]}
print infos


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/06_temporal_framework.ipynb".
The first task is:
Next we update the information of temporal dataset and print its metadata:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/06_temporal_framework.ipynb".
The first task is:
Now it is possible to obtain all the registered maps:
Can you write Python code for it?

rows = strds.get_registered_maps(columns="name,mapset,start_time,end_time",
                                 where=None, order="start_time")


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/06_temporal_framework.ipynb".
The first task is:
Now we register the created maps within the temporal dataset:
Can you write Python code for it?

tgis.register_maps_in_space_time_dataset(type='raster', name=dataset_name, maps=','.join(maps), start='2014-01-01',
                                         increment="1 month", interval=True, update_cmd_list=True)


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/06_temporal_framework.ipynb".
The first task is:
Now, for each record read from the temporal dataset, the procedure stores the respective record in the CSV file:
Can you write Python code for it?

with open(fil.name, 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=';',
                            quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Map_name', 'Date', 'Temp'])

    for mapp, vals in infos.iteritems():
        spamwriter.writerow([mapp, vals['date'].strftime('%Y-%m-%d'), vals['value']])
fil.close()


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/06_temporal_framework.ipynb".
The first task is:
Query an existing temporal dataset
During this session you will learn how to extract values for a point from a temporal dataset.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/06_temporal_framework.ipynb".
The first task is:
Register maps into temporal dataset
Create a set of random maps of temperature using min and max values
Can you write Python code for it?

# monthly mean Raleigh temperature
nc_temp_data = {1:[30, 51], 2: [32, 54], 3: [40, 63], 4: [48, 72],
                5:[57, 80], 6: [66, 87], 7: [70, 90], 8: [69, 88],
                9:[62, 82], 10:[50, 73], 11:[41, 64], 12:[32, 54]}
# list of maps to add into temporal dataset
maps = []
gscript.run_command('g.region', raster='elevation')
for month, values in nc_temp_data.iteritems():
    map_name = "temp_{mon}".format(mon=month)
    gscript.run_command('r.random.surface', output=map_name, seed=values, high=values[1], overwrite=True)
    maps.append(map_name)
print maps


I am working on the file "GSOC/notebooks/Projects/GRASS/python-grass-addons/06_temporal_framework.ipynb".
The first task is:
Some other tips
Unregistering maps
To remove maps from a temporal dataset (they will not be truly deleted but just unregistered from the temporal dataset), first a map object should be created. Then, using this new dataset object, we remove the selected map(s) using the unregister_map function:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
An IPython quick reference card:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
And the last three have shorthands for convenience:
Can you write Python code for it?

print('last output:', _)
print('next one   :', __)
print('and next   :', ___)

In[11]

_i

_ii

print('last input:', _i)
print('next one  :', _ii)
print('and next  :', _iii)

%history -n 1-5


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
Another interesting cell magic: create any file you want locally from the notebook:
Can you write Python code for it?

%%writefile test.txt
This is a test file!
It can contain anything I want...

And more...

!cat test.txt


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
Exercise
Write a cell that executes in Bash and prints your current working directory as well as the date.
Apologies to Windows users who may not have Bash available, not sure how to obtain the equivalent result with cmd.exe or Powershell.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
Getting help:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
Line magics can be used even inside code blocks:
Can you write Python code for it?

for i in range(5):
    size = i*100
    print('size:',size)
    %timeit range(size)


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
Line vs cell magics:
Can you write Python code for it?

%timeit range(10)

%%timeit
range(10)
range(100)


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
Magics can do anything they want with their input, so it doesn't have to be valid Python:
Can you write Python code for it?

%%bash
echo "My shell is:" $SHELL
echo "My memory status is:"
free


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
Raw Input in the notebook
Since 1.0 the IPython notebook web application support raw_input which for example allow us to invoke the %debug magic in the notebook:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
Running normal Python code: execution and errors
Not only can you input normal Python code, you can even paste straight from a Python or IPython shell session:
Can you write Python code for it?

>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
...     print(b)
...     a, b = b, a+b

In [1]: for i in range(10):
   ...:     print(i)
   ...:


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
Tab completion
Tab completion, especially for attributes, is a convenient way to explore the structure of any object you’re dealing with. Simply type object_name.<TAB> to view the object’s attributes. Besides Python objects and keywords, tab completion also works on file and directory names.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
The default %xmode is "context", which shows additional context but not all local variables.  Let's restore that one for the rest of our session.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
The output is stored in _N and Out[N] variables:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
Typing object_name? will print all sorts of details about any object, including docstrings, function definition lines (for call arguments) and constructor details for classes.
Can you write Python code for it?

import collections
collections.namedtuple?

collections.Counter??

*int*?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Beyond Plain Python.ipynb".
The first task is:
We can connect automatically a Qt Console to the currently running kernel with the %qtconsole magic, or by typing ipython console --existing <kernel-UUID> in any terminal:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
A NumPy array can be auralized automatically. The Audio class normalizes and encodes the data and embeds the resulting audio in the Notebook.
For instance, when two sine waves with almost the same frequency are superimposed a phenomena known as beats occur. This can be auralised as follows:
Can you write Python code for it?

import numpy as np
max_time = 3
f1 = 220.0
f2 = 224.0
rate = 8000.0
L = 3
times = np.linspace(0,L,rate*L)
signal = np.sin(2*np.pi*f1*times) + np.sin(2*np.pi*f2*times)

Audio(data=signal, rate=rate)


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
An image can also be displayed from raw data or a URL.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
Audio
IPython makes it easy to work with sounds interactively. The Audio display class allows you to create an audio control that is embedded in the Notebook. The interface is analogous to the interface of the Image display class. All audio formats supported by the browser can be used. Note that no single format is presently supported in all browsers.
Can you write Python code for it?

from IPython.display import Audio
Audio(url="http://www.nch.com.au/acm/8k16bitpcm.wav")


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
Embedded vs non-embedded Images
By default, image data is embedded in the notebook document so that the images can be viewed offline. However it is also possible to tell the Image class to only store a link to the image. Let's see how this works using a webcam at Berkeley.
Can you write Python code for it?

from IPython.display import Image
img_url = 'http://www.lawrencehallofscience.org/static/scienceview/scienceview.berkeley.edu/html/view/view_assets/images/newview.jpg'

# by default Image data are embedded
Embed      = Image(img_url)

# if kwarg `url` is given, the embedding is assumed to be false
SoftLinked = Image(url=img_url)

# In each case, embed can be specified explicitly with the `embed` kwarg
# ForceEmbed = Image(url=img_url, embed=True)


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
External sites
You can even embed an entire page from another site in an iframe; for example this is today's Wikipedia
page for mobile users:
Can you write Python code for it?

from IPython.display import IFrame
IFrame('http://ipython.org', width='100%', height=350)


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
Here is a more complicated example that loads d3.js from a CDN, uses the %%html magic to load CSS styles onto the page and then runs ones of the d3.js examples.
Can you write Python code for it?

Javascript(
    """$.getScript('//cdnjs.cloudflare.com/ajax/libs/d3/3.2.2/d3.v3.min.js')"""
)

%%html
<style type="text/css">

circle {
  fill: rgb(31, 119, 180);
  fill-opacity: .25;
  stroke: rgb(31, 119, 180);
  stroke-width: 1px;
}

.leaf circle {
  fill: #ff7f0e;
  fill-opacity: 1;
}

text {
  font: 10px sans-serif;
}

</style>

%%javascript

// element is the jQuery element we will append to
var e = element.get(0);

var diameter = 600,
    format = d3.format(",d");

var pack = d3.layout.pack()
    .size([diameter - 4, diameter - 4])
    .value(function(d) { return d.size; });

var svg = d3.select(e).append("svg")
    .attr("width", diameter)
    .attr("height", diameter)
  .append("g")
    .attr("transform", "translate(2,2)");

d3.json("data/flare.json", function(error, root) {
  var node = svg.datum(root).selectAll(".node")
      .data(pack.nodes)
    .enter().append("g")
      .attr("class", function(d) { return d.children ? "node" : "leaf node"; })
      .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

  node.append("title")
      .text(function(d) { return d.name + (d.children ? "" : ": " + format(d.size)); });

  node.append("circle")
      .attr("r", function(d) { return d.r; });

  node.filter(function(d) { return !d.children; }).append("text")
      .attr("dy", ".3em")
      .style("text-anchor", "middle")
      .text(function(d) { return d.name.substring(0, d.r / 3); });
});

d3.select(self.frameElement).style("height", diameter + "px");


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
Images
To work with images (JPEG, PNG) use the Image class.
Can you write Python code for it?

from IPython.display import Image

i = Image(filename='../images/ipython_logo.png')


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
JavaScript
The Notebook also enables objects to declare a JavaScript representation. At first, this may seem odd as  output is inherently visual and JavaScript is a programming language. However, this opens the door for rich output that leverages the full power of JavaScript and associated libraries such as d3.js for output.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
LaTeX
The IPython display system also has builtin support for the display of mathematical expressions typeset in LaTeX, which is rendered in the browser using MathJax.
You can pass raw LaTeX test as a string to the Math object:
Can you write Python code for it?

from IPython.display import Math
Math(r'F(k) = \int_{-\infty}^{\infty} f(x) e^{2\pi i k} dx')


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
Links to local files
IPython provides builtin display classes for generating links to local files. Create a link to a single file using the FileLink object:
Can you write Python code for it?

from IPython.display import FileLink, FileLinks
FileLink('Cell Magics.ipynb')


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
Returning an Image object from an expression will automatically display it:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
SVG images are also supported out of the box.
Can you write Python code for it?

from IPython.display import SVG
SVG(filename='../images/python_logo.svg')


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
The display machinery can also be used by libraries. Tabipy is a library for constructing tables:
Can you write Python code for it?

!pip install Tabipy

from tabipy import Table, TableHeaderRow, TableCell

t = Table(TableHeaderRow('divisions', 'result'))
num = 55
for x in range(7):
    if num < 1:
        resultcell = TableCell(num, bg_colour='DarkBlue', text_colour='white')
    else:
        resultcell = TableCell(num)
    t.append_row((x, resultcell))
    num /= 3
t


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
The same thing can be accomplished using the %%javascript cell magic:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
Using the nascent video capabilities of modern browsers, you may also be able to display local
videos.  At the moment this doesn't work very well in all browsers, so it may or may not work for you;
we will continue testing this and looking for ways to make it more robust.
The following cell loads a local file called  animation.m4v, encodes the raw video as base64 for http
transport, and uses the HTML5 video tag to load it. On Chrome 15 it works correctly, displaying a control bar at the bottom with a play/pause button and a location slider.
Can you write Python code for it?

from IPython.display import HTML
from base64 import b64encode
video = open("../images/animation.m4v", "rb").read()
video_encoded = b64encode(video).decode('ascii')
video_tag = '<video controls alt="test" src="data:video/x-m4v;base64,{0}">'.format(video_encoded)
HTML(data=video_tag)


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
Video
More exotic objects can also be displayed, as long as their representation supports the IPython display protocol.  For example, videos hosted externally on YouTube are easy to load:
Can you write Python code for it?

from IPython.display import YouTubeVideo
YouTubeVideo('sjfsUzECqK0')


I am working on the file "GSOC/notebooks/ipython/examples/IPython Kernel/Rich Output.ipynb".
The first task is:
You can also use the %%html cell magic to accomplish the same thing.
Can you write Python code for it?

%%html

Header 1
Header 2

row 1, cell 1
row 1, cell 2

row 2, cell 1
row 2, cell 2


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/File Upload Widget.ipynb".
The first task is:
The following shows how the file widget can be used.
Can you write Python code for it?

file_widget = FileWidget()

# Register an event to echo the filename when it has been changed.
def file_loading():
    print("Loading %s" % file_widget.filename)
file_widget.on_trait_change(file_loading, 'filename')

# Register an event to echo the filename and contents when a file
# has been uploaded.
def file_loaded():
    print("Loaded, file contents: %s" % file_widget.value)
file_widget.on_trait_change(file_loaded, 'value')

# Register an event to print an error message when a file could not
# be opened.  Since the error messages are not handled through
# traitlets but instead handled through custom msgs, the registration
# of the handler is different than the two examples above.  Instead
# the API provided by the CallbackDispatcher must be used.
def file_failed():
    print("Could not load file contents of %s" % file_widget.filename)
file_widget.errors.register_callback(file_failed)

file_widget


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Nonblocking Console.ipynb".
The first task is:
Create the console widgets without displaying them.
Can you write Python code for it?

console_container = widgets.VBox(visible=False)
console_container.padding = '10px'

output_box = widgets.Textarea()
output_box.height = '400px'
output_box.font_family = 'monospace'
output_box.color = '#AAAAAA'
output_box.background_color = 'black'
output_box.width = '800px'

input_box = widgets.Text()
input_box.font_family = 'monospace'
input_box.color = '#AAAAAA'
input_box.background_color = 'black'
input_box.width = '800px'

console_container.children = [output_box, input_box]


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Nonblocking Console.ipynb".
The first task is:
Define function to run a process without blocking the input.
Can you write Python code for it?

def read_process(process, append_output):
    """ Try to read the stdout and stderr of a process and render it using
    the append_output method provided

    Parameters
    ----------
    process: Popen handle
    append_output: method handle
        Callback to render output.  Signature of
        append_output(output, [prefix=])"""

    try:
        stdout = process.stdout.read()
        if stdout is not None and len(stdout) > 0:
            append_output(stdout, prefix='    ')
    except:
        pass

    try:
        stderr = process.stderr.read()
        if stderr is not None and len(stderr) > 0:
            append_output(stderr, prefix='ERR ')
    except:
        pass

def set_pipe_nonblocking(pipe):
    """Set a pipe as non-blocking"""
    try:
        import fcntl
        fl = fcntl.fcntl(pipe, fcntl.F_GETFL)
        fcntl.fcntl(pipe, fcntl.F_SETFL, fl | os.O_NONBLOCK)
    except:
        pass

kernel = get_ipython().kernel
def run_command(command, append_output, has_user_exited=None):
    """Run a command asyncronously

    Parameters
    ----------
    command: str
        Shell command to launch a process with.
    append_output: method handle
        Callback to render output.  Signature of
        append_output(output, [prefix=])
    has_user_exited: method handle
        Check to see if the user wants to stop the command.
        Must return a boolean."""

    # Echo input.
    append_output(command, prefix='>>> ')

    # Create the process.  Make sure the pipes are set as non-blocking.
    process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
    set_pipe_nonblocking(process.stdout)
    set_pipe_nonblocking(process.stderr)

    # Only continue to read from the command
    while (has_user_exited is None or not has_user_exited()) and process.poll() is None:
        read_process(process, append_output)
        kernel.do_one_iteration() # Run IPython iteration.  This is the code that
                                  # makes this operation...


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Nonblocking Console.ipynb".
The first task is:
Hook the process execution methods up to our console widgets.
Can you write Python code for it?

def append_output(output, prefix):
    if isinstance(output, string_types):
        output_str = output
    else:
        output_str = bytes_to_str(output)
    output_lines = output_str.split('\n')
    formatted_output = '\n'.join([prefix + line for line in output_lines if len(line) > 0]) + '\n'
    output_box.value += formatted_output
    output_box.scroll_to_bottom()

def has_user_exited():
    return not console_container.visible

def handle_input(sender):
    sender.disabled = True
    try:
        command = sender.value
        sender.value = ''
        run_command(command, append_output=append_output, has_user_exited=has_user_exited)
    finally:
        sender.disabled = False

input_box.on_submit(handle_input)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Events.ipynb".
The first task is:
Example
Since button clicks are stateless, they are transmitted from the front-end to the back-end using custom messages.  By using the on_click method, a button that prints a message when it has been clicked is shown below.
Can you write Python code for it?

from IPython.display import display
button = widgets.Button(description="Click Me!")
display(button)

def on_button_clicked(b):
    print("Button clicked.")

button.on_click(on_button_clicked)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Events.ipynb".
The first task is:
Function traitlets.link returns a Link object. The link can be broken by calling the unlink method.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Events.ipynb".
The first task is:
Function widgets.jslink returns a Link widget. The link can be broken by calling the unlink method.
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Events.ipynb".
The first task is:
Linking Widgets
Often, you may want to simply link widget attributes together. Synchronization of attributes can be done in a simpler way than by using bare traitlets events.
The first method is to use the link and directional_link functions from the traitlets module.
Linking traitlets attributes from the server side
Can you write Python code for it?

from IPython.utils import traitlets

caption = widgets.Latex(value = 'The values of slider1, slider2 and slider3 are synchronized')
sliders1, slider2, slider3 = widgets.IntSlider(description='Slider 1'),\
                             widgets.IntSlider(description='Slider 2'),\
                             widgets.IntSlider(description='Slider 3')
l = traitlets.link((sliders1, 'value'), (slider2, 'value'), (slider3, 'value'))
display(caption, sliders1, slider2, slider3)

caption = widgets.Latex(value = 'Changes in source values are reflected in target1 and target2')
source, target1, target2 = widgets.IntSlider(description='Source'),\
                           widgets.IntSlider(description='Target 1'),\
                           widgets.IntSlider(description='Target 2')
traitlets.dlink((source, 'value'), (target1, 'value'), (target2, 'value'))
display(caption, source, target1, target2)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Events.ipynb".
The first task is:
Linking widgets attributes from the client side
When synchronizing traitlets attributes, you may experience a lag because of the latency dues to the rountrip to the server side. You can also directly link widgets attributes, either in a unidirectional or a bidirectional fashion using the link widgets.
Can you write Python code for it?

caption = widgets.Latex(value = 'The values of range1, range2 and range3 are synchronized')
range1, range2, range3 = widgets.IntSlider(description='Range 1'),\
                         widgets.IntSlider(description='Range 2'),\
                         widgets.IntSlider(description='Range 3')
l = widgets.jslink((range1, 'value'), (range2, 'value'), (range3, 'value'))
display(caption, range1, range2, range3)

caption = widgets.Latex(value = 'Changes in source_range values are reflected in target_range1 and target_range2')
source_range, target_range1, target_range2 = widgets.IntSlider(description='Source range'),\
                                             widgets.IntSlider(description='Target range 1'),\
                                             widgets.IntSlider(description='Target range 2')
widgets.jsdlink((source_range, 'value'), (target_range1, 'value'), (target_range2, 'value'))
display(caption, source_range, target_range1, target_range2)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Events.ipynb".
The first task is:
The Button is not used to represent a data type.  Instead the button widget is used to handle mouse clicks.  The on_click method of the Button can be used to register function to be called when the button is clicked.  The doc string of the on_click can be seen below.
Can you write Python code for it?

from IPython.html import widgets
print(widgets.Button.on_click.__doc__)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Events.ipynb".
The first task is:
on_sumbit
The Text also has a special on_submit event.  The on_submit event fires when the user hits return.
Can you write Python code for it?

text = widgets.Text()
display(text)

def handle_submit(sender):
    print(text.value)

text.on_submit(handle_submit)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget List.ipynb".
The first task is:
BoundedFloatText
Can you write Python code for it?

widgets.BoundedFloatText(
    value=7.5,
    min=5.0,
    max=10.0,
    description='Text:',
)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget List.ipynb".
The first task is:
Button
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget List.ipynb".
The first task is:
FloatText
Can you write Python code for it?

widgets.FloatText(
    value=7.5,
    description='Any:',
)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget List.ipynb".
The first task is:
Numeric widgets
There are 8 widgets distributed with IPython that are designed to display numeric values.  Widgets exist for displaying integers and floats, both bounded and unbounded.  The integer widgets share a similar naming scheme to their floating point counterparts.  By replacing Float with Int in the widget name, you can find the Integer equivalent.
FloatSlider
Can you write Python code for it?

widgets.FloatSlider(
    value=7.5,
    min=5.0,
    max=10.0,
    step=0.1,
    description='Test:',
)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget List.ipynb".
The first task is:
RadioButtons
Can you write Python code for it?

widgets.RadioButtons(
    description='Pizza topping:',
    options=['pepperoni', 'pineapple', 'anchovies'],
)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget List.ipynb".
The first task is:
SelectMultiple
Multiple values can be selected with shift and ctrl pressed and mouse clicks or arrow keys.
Can you write Python code for it?

w = widgets.SelectMultiple(
    description="Fruits",
    options=['Apples', 'Oranges', 'Pears']
)
display(w)

w.value


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget List.ipynb".
The first task is:
Selection widgets
There are four widgets that can be used to display single selection lists, and one that can be used to display multiple selection lists.  All inherit from the same base class.  You can specify the enumeration of selectable options by passing a list.  You can also specify the enumeration as a dictionary, in which case the keys will be used as the item displayed in the list and the corresponding value will be returned when an item is selected.
Dropdown
Can you write Python code for it?

from IPython.display import display
w = widgets.Dropdown(
    options=['1', '2', '3'],
    value='2',
    description='Number:',
)
display(w)

w.value


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget List.ipynb".
The first task is:
Sliders can also be displayed vertically.
Can you write Python code for it?

widgets.FloatSlider(
    value=7.5,
    min=5.0,
    max=10.0,
    step=0.1,
    description='Test',
    orientation='vertical',
)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget List.ipynb".
The first task is:
String widgets
There are 4 widgets that can be used to display a string value.  Of those, the Text and Textarea widgets accept input.  The Latex and HTML widgets display the string as either Latex or HTML respectively, but do not accept input.
Text
Can you write Python code for it?

widgets.Text(
    description='String:',
    value='Hello World',
)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget List.ipynb".
The first task is:
The following is also valid:
Can you write Python code for it?

w = widgets.Dropdown(
    options={'One': 1, 'Two': 2, 'Three': 3},
    value=2,
    description='Number:',
)
display(w)

w.value


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget List.ipynb".
The first task is:
ToggleButtons
Can you write Python code for it?

widgets.ToggleButtons(
    description='Speed:',
    options=['Slow', 'Regular', 'Fast'],
)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Styling.ipynb".
The first task is:
Fancy boxes
If you need to display a more complicated set of widgets, there are specialized containers that you can use.  To display multiple sets of widgets, you can use an Accordion or a Tab in combination with one Box per set of widgets (as seen below).  The "pages" of these widgets are their children.  To set the titles of the pages, one can call set_title.
Accordion
Can you write Python code for it?

name1 = widgets.Text(description='Location:')
zip1 = widgets.BoundedIntText(description='Zip:', min=0, max=99999)
page1 = widgets.Box(children=[name1, zip1])

name2 = widgets.Text(description='Location:')
zip2 = widgets.BoundedIntText(description='Zip:', min=0, max=99999)
page2 = widgets.Box(children=[name2, zip2])

accord = widgets.Accordion(children=[page1, page2])
display(accord)

accord.set_title(0, 'From')
accord.set_title(1, 'To')


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Styling.ipynb".
The first task is:
Flex boxes
Widgets can be aligned using the FlexBox, HBox, and VBox widgets.
Application to widgets
Widgets display vertically by default:
Can you write Python code for it?

buttons = [widgets.Button(description=str(i)) for i in range(3)]
display(*buttons)


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Styling.ipynb".
The first task is:
If a description is not set for the widget, the label is not displayed:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Styling.ipynb".
The first task is:
If a label is longer than the minimum width, the widget is shifted to the right:
Can you write Python code for it?


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Styling.ipynb".
The first task is:
Parent/child relationships
To display widget A inside widget B, widget A must be a child of widget B.  Widgets that can contain other widgets have a children attribute.  This attribute can be set via a keyword argument in the widget's constructor or after construction.  Calling display on an object with children automatically displays those children, too.
Can you write Python code for it?

from IPython.display import display

float_range = widgets.FloatSlider()
string = widgets.Text(value='hi')
container = widgets.Box(children=[float_range, string])

container.border_color = 'red'
container.border_style = 'dotted'
container.border_width = 3
display(container) # Displays the `container` and all of it's children.


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Styling.ipynb".
The first task is:
TabWidget
Can you write Python code for it?

name = widgets.Text(description='Name:')
color = widgets.Dropdown(description='Color:', options=['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'])
page1 = widgets.Box(children=[name, color])

age = widgets.IntSlider(description='Age:', min=0, max=120, value=50)
gender = widgets.RadioButtons(description='Gender:', options=['male', 'female'])
page2 = widgets.Box(children=[age, gender])

tabs = widgets.Tab(children=[page1, page2])
display(tabs)

tabs.set_title(0, 'Name')
tabs.set_title(1, 'Details')


I am working on the file "GSOC/notebooks/ipython/examples/Interactive Widgets/Widget Styling.ipynb".
The first task is:
Visibility
Sometimes it is necessary to hide or show widgets in place, without having to re-display the widget.
The visible property of widgets can be used to hide or show widgets that have already been displayed (as seen below).  The visible property can be:
* True - the widget is displayed
* False - the widget is hidden, and the empty space where the widget would be is collapsed
* None - the widget is hidden, and the empty space where the widget would be is shown
Can you write Python code for it?

w1 = widgets.Latex(value="First line")
w2 = widgets.Latex(value="Second line")
w3 = widgets.Latex(value="Third line")
display(w1, w2, w3)

w2.visible=None

w2.visible=False

w2.visible=True


I am working on the file "GSOC/notebooks/ipython/examples/Notebook/Running Code.ipynb".
The first task is:
Beyond a certain point, output will scroll automatically:
Can you write Python code for it?

for i in range(500):
    print(2**i - 1)