def test_singles(self): self.assertEqual(singles(self.counts), 3) self.assertEqual(singles(np.array([0, 3, 4])), 0) self.assertEqual(singles(np.array([1])), 1) self.assertEqual(singles(np.array([0, 0])), 0)
# 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') output.to_file(args['output'], encoding='utf-8') # Print status
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!')