Skip to content

Instantly share code, notes, and snippets.

@dalguji
Last active August 29, 2015 13:59

Revisions

  1. @hongsi hongsi revised this gist Jun 18, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Haiti.py
    Original file line number Diff line number Diff line change
    @@ -70,7 +70,7 @@ def basic_haiti_map(ax=None, lllat=17.25, urlat=20.25, lllon=-75, urlon=-71):
    cat_data = data[data['category_%s' % code] == 1]

    # compute map proj coordinates.
    x, y = m(cat_data.LONGITUDE, cat_data.LATITUDE)
    x, y = m(cat_data.LONGITUDE.values, cat_data.LATITUDE.values)

    m.plot(x, y, 'k.', alpha=0.5)
    ax.set_title('%s: %s' % (code, english_mapping[code]))
  2. @hongsi hongsi revised this gist Jun 18, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Haiti.py
    Original file line number Diff line number Diff line change
    @@ -70,7 +70,6 @@ def basic_haiti_map(ax=None, lllat=17.25, urlat=20.25, lllon=-75, urlon=-71):
    cat_data = data[data['category_%s' % code] == 1]

    # compute map proj coordinates.
    print cat_data.LONGITUDE, cat_data.LATITUDE
    x, y = m(cat_data.LONGITUDE, cat_data.LATITUDE)

    m.plot(x, y, 'k.', alpha=0.5)
  3. @hongsi hongsi revised this gist Jun 18, 2014. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions Haiti.py
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,8 @@ def basic_haiti_map(ax=None, lllat=17.25, urlat=20.25, lllon=-75, urlon=-71):
    llcrnrlat=lllat, urcrnrlat=urlat,
    llcrnrlon=lllon, urcrnrlon=urlon,
    resolution='f')
    # draw coastlines, state and country boundaries, edge of map. m.drawcoastlines()
    # draw coastlines, state and country boundaries, edge of map.
    m.drawcoastlines()
    m.drawstates()
    m.drawcountries()
    return m
    @@ -73,4 +74,6 @@ def basic_haiti_map(ax=None, lllat=17.25, urlat=20.25, lllon=-75, urlon=-71):
    x, y = m(cat_data.LONGITUDE, cat_data.LATITUDE)

    m.plot(x, y, 'k.', alpha=0.5)
    ax.set_title('%s: %s' % (code, english_mapping[code]))
    ax.set_title('%s: %s' % (code, english_mapping[code]))

    plt.show()
  4. @hongsi hongsi revised this gist Apr 17, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Haiti.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    # coding: utf-8
    import pandas as pd
    import numpy as np
    from pandas import DataFrame
  5. @hongsi hongsi revised this gist Apr 17, 2014. 1 changed file with 51 additions and 39 deletions.
    90 changes: 51 additions & 39 deletions Haiti.py
    Original file line number Diff line number Diff line change
    @@ -1,65 +1,77 @@
    # coding: utf-8
    import pandas as pd
    import numpy as np
    from pandas import DataFrame, Series
    import matplotlib.pyplot as plt
    from mpl_toolkits.basemap import Basemap
    from pandas import DataFrame

    data = pd.read_csv('Haiti.csv')
    data = data[(data.LATITUDE > 18) & (data.LATITUDE < 20) & (data.LONGITUDE > -75) & (data.LONGITUDE < -70) & data.CATEGORY.notnull()]

    data = data[(data.LATITUDE > 18) & (data.LATITUDE < 20) &
    (data.LONGITUDE > -75) & (data.LONGITUDE < -70)
    & data.CATEGORY.notnull()]

    def to_cat_list(catstr):
    stripped = (x.strip() for x in catstr.split(','))
    return [x for x in stripped if x]
    stripped = (x.strip() for x in catstr.split(','))
    return [x for x in stripped if x]

    def get_all_categories(cat_series):
    cat_sets = (set(to_cat_list(x)) for x in cat_series)
    return sorted(set.union(*cat_sets))
    cat_sets = (set(to_cat_list(x)) for x in cat_series)
    return sorted(set.union(*cat_sets))

    def get_english(cat):
    code, names = cat.split('.')
    if '|' in names:
    names = names.split('|')[1]
    return code, names.strip()
    code, names = cat.split('.')
    if '|' in names:
    names = names.split(' | ')[1]
    return code, names.strip()

    all_cats = get_all_categories(data.CATEGORY)
    english_mapping = dict(get_english(x) for x in all_cats)

    def get_code(seq):
    return [x.split('.')[0] for x in seq if x]
    return [x.split('.')[0] for x in seq if x]

    all_codes = get_code(all_cats)
    code_index = pd.Index(np.unique(all_codes))
    dummy_frame = DataFrame(np.zeros((len(data), len(code_index))),
    index=data.index, columns=code_index)
    index=data.index, columns=code_index)

    for row, cat in zip(data.index, data.CATEGORY):
    codes = get_code(to_cat_list(cat))
    dummy_frame.ix[row, codes] = 1

    for row, cat in zip(data.index, data.CATEGORY):
    codes = get_code(to_cat_list(cat))
    dummy_frame.ix[row, codes] = 1

    data = data.join(dummy_frame.add_prefix('category_'))

    def basic_haiti_map(ax=None,lllat=17.25,urlat=20.25,lllon=-75,urlon=-71):
    m = Basemap(ax=ax,projection='stere',
    lon_0 = (urlon + lllon) / 2,
    lat_0 = (urlat + lllat) / 2,
    llcrnrlat = lllat, urcrnrlat=urlat,
    llcrnrlon = lllon, urcrnrlon=urlon,
    resolution='f')
    m.drawcoastlines()
    m.drawstates()
    m.drawcountries()
    return m

    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12,10))
    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt

    def basic_haiti_map(ax=None, lllat=17.25, urlat=20.25, lllon=-75, urlon=-71):
    # create polar stereographic Basemap instance.
    m = Basemap(ax=ax, projection='stere',
    lon_0=(urlon + lllon) / 2,
    lat_0=(urlat + lllat) / 2,
    llcrnrlat=lllat, urcrnrlat=urlat,
    llcrnrlon=lllon, urcrnrlon=urlon,
    resolution='f')
    # draw coastlines, state and country boundaries, edge of map. m.drawcoastlines()
    m.drawstates()
    m.drawcountries()
    return m

    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12, 10))
    fig.subplots_adjust(hspace=0.05, wspace=0.05)
    to_plot = ['2a','1','3c','7a']

    to_plot = ['2a', '1', '3c', '7a']

    lllat=17.25; urlat=20.25; lllon=-75; urlon=-71

    for code, ax in zip(to_plot, axes.flat):
    m = basic_haiti_map(ax, lllat=lllat, urlat=urlat,
    lllon=lllon, urlon=urlon)
    cat_data = data[data['category_%s' % code] == 1]
    x,y = m(cat_data.LONGITUDE, cat_data.LATITUDE)
    m.plot(x,y,'k.',alpha=0.5)

    ax.set_title('%s: %s' % (code, english_mapping[code]))
    m = basic_haiti_map(ax, lllat=lllat, urlat=urlat,
    lllon=lllon, urlon=urlon)

    cat_data = data[data['category_%s' % code] == 1]

    # compute map proj coordinates.
    print cat_data.LONGITUDE, cat_data.LATITUDE
    x, y = m(cat_data.LONGITUDE, cat_data.LATITUDE)

    m.plot(x, y, 'k.', alpha=0.5)
    ax.set_title('%s: %s' % (code, english_mapping[code]))
  6. @hongsi hongsi revised this gist Apr 17, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Haiti.py
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,6 @@
    import matplotlib.pyplot as plt
    from mpl_toolkits.basemap import Basemap

    pd.read_csv('Haiti.csv')
    data = pd.read_csv('Haiti.csv')
    data = data[(data.LATITUDE > 18) & (data.LATITUDE < 20) & (data.LONGITUDE > -75) & (data.LONGITUDE < -70) & data.CATEGORY.notnull()]

  7. @hongsi hongsi created this gist Apr 17, 2014.
    66 changes: 66 additions & 0 deletions Haiti.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    import pandas as pd
    import numpy as np
    from pandas import DataFrame, Series
    import matplotlib.pyplot as plt
    from mpl_toolkits.basemap import Basemap

    pd.read_csv('Haiti.csv')
    data = pd.read_csv('Haiti.csv')
    data = data[(data.LATITUDE > 18) & (data.LATITUDE < 20) & (data.LONGITUDE > -75) & (data.LONGITUDE < -70) & data.CATEGORY.notnull()]

    def to_cat_list(catstr):
    stripped = (x.strip() for x in catstr.split(','))
    return [x for x in stripped if x]

    def get_all_categories(cat_series):
    cat_sets = (set(to_cat_list(x)) for x in cat_series)
    return sorted(set.union(*cat_sets))

    def get_english(cat):
    code, names = cat.split('.')
    if '|' in names:
    names = names.split('|')[1]
    return code, names.strip()

    all_cats = get_all_categories(data.CATEGORY)
    english_mapping = dict(get_english(x) for x in all_cats)

    def get_code(seq):
    return [x.split('.')[0] for x in seq if x]

    all_codes = get_code(all_cats)
    code_index = pd.Index(np.unique(all_codes))
    dummy_frame = DataFrame(np.zeros((len(data), len(code_index))),
    index=data.index, columns=code_index)

    for row, cat in zip(data.index, data.CATEGORY):
    codes = get_code(to_cat_list(cat))
    dummy_frame.ix[row, codes] = 1

    data = data.join(dummy_frame.add_prefix('category_'))

    def basic_haiti_map(ax=None,lllat=17.25,urlat=20.25,lllon=-75,urlon=-71):
    m = Basemap(ax=ax,projection='stere',
    lon_0 = (urlon + lllon) / 2,
    lat_0 = (urlat + lllat) / 2,
    llcrnrlat = lllat, urcrnrlat=urlat,
    llcrnrlon = lllon, urcrnrlon=urlon,
    resolution='f')
    m.drawcoastlines()
    m.drawstates()
    m.drawcountries()
    return m

    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(12,10))
    fig.subplots_adjust(hspace=0.05, wspace=0.05)
    to_plot = ['2a','1','3c','7a']
    lllat=17.25; urlat=20.25; lllon=-75; urlon=-71

    for code, ax in zip(to_plot, axes.flat):
    m = basic_haiti_map(ax, lllat=lllat, urlat=urlat,
    lllon=lllon, urlon=urlon)
    cat_data = data[data['category_%s' % code] == 1]
    x,y = m(cat_data.LONGITUDE, cat_data.LATITUDE)
    m.plot(x,y,'k.',alpha=0.5)

    ax.set_title('%s: %s' % (code, english_mapping[code]))