Exemplo n.º 1
0
 def test_menhinick(self):
     # observed_otus = 9, total # of individuals = 22
     self.assertEqual(menhinick(self.counts), 9 / np.sqrt(22))
Exemplo n.º 2
0
# Dissolving
print('[INFO] - Dissolving results')
dissolved = joined.dissolve(by='id', aggfunc=lambda x: list(x))

# Getting output length
dis_len = len(dissolved)

# Counting language dominance, menhinick diversity and simpson index
print('[INFO] - Calculating variables..')
for i, row in dissolved.iterrows():
    print("[INFO] - Calculating grid cell {}/{}...".format(i, dis_len))
    lang_counts = list(Counter(
        row[args['language']]).values())  # occurence counts
    lang_counts = np.asarray(lang_counts)  # cast as numpy array for skbio
    dissolved.at[i, 'dominance'] = sk.dominance(lang_counts)
    dissolved.at[i, 'menhinick'] = sk.menhinick(lang_counts)
    dissolved.at[i, 'simpson'] = sk.simpson(lang_counts)
    dissolved.at[i, 'berger'] = sk.berger_parker_d(lang_counts)
    dissolved.at[i, 'singles'] = sk.singles(lang_counts)
    dissolved.at[i, 'shannon'] = np.exp(sk.shannon(lang_counts, base=np.e))
    dissolved.at[i, 'unique'] = sk.observed_otus(lang_counts)

# Select columns for output
cols = [
    'geometry', 'dominance', 'menhinick', 'simpson', 'berger', 'singles',
    'shannon', 'unique'
]
output = dissolved[cols]

# Save the output to pickle
print('[INFO] - Saving to shapefile')
Exemplo n.º 3
0
 def test_menhinick(self):
     # observed_otus = 9, total # of individuals = 22
     self.assertEqual(menhinick(self.counts), 9 / np.sqrt(22))
Exemplo n.º 4
0
joined = gpd.sjoin(grid, points, how='inner', op='contains')

# Dissolving
print('[INFO] - Dissolving results')
dissolved = joined.dissolve(by='id', aggfunc=lambda x: list(x))

# Getting output length
dis_len = len(dissolved)

# Counting topic dominance, menhinick diversity and simpson index
print('[INFO] - Calculating variables..')
for i, row in dissolved.iterrows():
    print("[INFO] Processing row {}/{}...".format(i, dis_len))
    topic_counts = list(Counter(
        row[args['topic']]).values())  # occurence counts
    topic_counts = np.asarray(topic_counts)  # cast as numpy array for skbio
    dissolved.at[i, 'dominance'] = sk.dominance(topic_counts)
    dissolved.at[i, 'menhinick'] = sk.menhinick(topic_counts)
    dissolved.at[i, 'simpson'] = sk.simpson(topic_counts)

# Select columns for output
cols = ['geometry', 'dominance', 'menhinick', 'simpson']
output = dissolved[cols]

# Save the output to pickle
print('[INFO] - Saving to shapefile')
output.to_file(args['output'], encoding='utf-8')

# Print status
print("[INFO] ... Done.")
        areas.at[i, colname4] = (int(lposts) / int(lpostsum)) * 100

# get dominant language from selected columns
areas['propmax'] = areas[['fi_prop','en_prop','et_prop','ru_prop','sv_prop','es_prop','ja_prop','fr_prop','pt_prop','de_prop']].idxmax(axis=1)
areas['mean_propmax'] = areas[['fi_mean_prop','en_mean_prop','et_mean_prop','ru_mean_prop','sv_mean_prop','es_mean_prop','ja_mean_prop','fr_mean_prop','pt_mean_prop','de_mean_prop']].idxmax(axis=1)
areas['sum_propmax'] = areas[['fi_sum_prop','en_sum_prop','et_sum_prop','ru_sum_prop','sv_sum_prop','es_sum_prop','ja_sum_prop','fr_sum_prop','pt_sum_prop','de_sum_prop']].idxmax(axis=1)

# get all language column names
cols = list(areas[langlist].columns)

# loop over areas
print('[INFO] - Calculating diversity metrics per area..')
for i, row in areas.iterrows():
    # get counts of languages
    otus = list(row[cols])
    # drop zeros
    otus = [i for i in otus if i != 0]
    # calculate diversity metrics
    areas.at[i, 'dominance'] = sk.dominance(otus)
    areas.at[i, 'berger'] = sk.berger_parker_d(otus)
    areas.at[i, 'menhinick'] = sk.menhinick(otus)
    areas.at[i, 'singletons'] = sk.singles(otus)
    areas.at[i, 'shannon'] = np.exp(sk.shannon(otus, base=np.e))
    areas.at[i, 'unique'] = sk.observed_otus(otus)

# save to file
print('[INFO] - Saving output geopackage...')
areas.to_file(args['output'], driver='GPKG')

print('[INFO] - ... done!')