Skip to content

Commit 4eef895

Browse files
authored
Merge pull request #79 from brain-image-library/78-prettyfy-get_size_statistics
Update get.py
2 parents 343f53f + 8bb7995 commit 4eef895

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

braininventory/get.py

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import requests
2-
import pandas as pd
31
import json
42
from datetime import date
5-
import pandas as pd
3+
4+
import humanize
65
import matplotlib.pyplot as plt
6+
import pandas as pd
7+
import requests
8+
import seaborn as sb
9+
import squarify
710
from pandarallel import pandarallel
811

912
pandarallel.initialize(nb_workers=8, progress_bar=True)
10-
import squarify
1113
import matplotlib.pyplot as plt
14+
import squarify
1215

1316

1417
def get_random_sample(df):
@@ -30,27 +33,25 @@ def get_random_sample(df):
3033

3134
return result.json()
3235

33-
import urllib.request
34-
import matplotlib.pyplot as plt
35-
import seaborn as sb
36-
import squarify
3736

3837
def __get_lable_dict(name_lst):
3938
"""
40-
input: a list of University names
41-
output: a dictionary with the names as keys and abbreviations that include the first letter of each University name
39+
input: a list of University names
40+
output: a dictionary with the names as keys and abbreviations that include the first letter of each University name
4241
"""
43-
return {uni_name: ''.join(word[0].upper() for word in uni_name.split()) for uni_name in name_lst}
42+
return {
43+
uni_name: "".join(word[0].upper() for word in uni_name.split())
44+
for uni_name in name_lst
45+
}
4446

4547

4648
def __get_general_modality_treemap(df):
4749
"""
48-
input: dataframe
49-
output: tree map that displays the frequencies of "generalmodality" that occur in dataframe
50+
input: dataframe
51+
output: tree map that displays the frequencies of "generalmodality" that occur in dataframe
5052
"""
51-
modality_counts = df['generalmodality'].value_counts().to_dict()
52-
53-
plt.figure(figsize=(14,10))
53+
modality_counts = df["generalmodality"].value_counts().to_dict()
54+
plt.figure(figsize=(14, 10))
5455
values = list(modality_counts.values())
5556
name = list(modality_counts.keys())
5657
abbrName = __get_lable_dict(name)
@@ -60,30 +61,50 @@ def __get_general_modality_treemap(df):
6061
print(num_labels)
6162

6263
ax = squarify.plot(sizes=values, color=colors, label=abbrName.values(), alpha=0.8)
63-
ax.axis('off')
64+
ax.axis("off")
6465
ax.invert_xaxis()
65-
ax.set_aspect('equal')
66+
ax.set_aspect("equal")
6667

6768
legend_patches = [plt.Rectangle((0, 0), 1, 1, fc=color) for color in colors]
68-
plt.legend(legend_patches, name, loc='upper left', bbox_to_anchor=(1, 1), fontsize='medium')\
69-
69+
plt.legend(
70+
legend_patches, name, loc="upper left", bbox_to_anchor=(1, 1), fontsize="medium"
71+
)
7072
plt.show()
7173

74+
75+
def __get_pretty_size_statistics(df):
76+
"""
77+
Pretty version of __get_size_statistics
78+
79+
Input: dataframe
80+
Output: list of strings
81+
"""
82+
size_stats = __get_size_statistics(df)
83+
84+
return [
85+
humanize.naturalsize(size_stats[0]),
86+
humanize.naturalsize(size_stats[1]),
87+
humanize.naturalsize(size_stats[2]),
88+
humanize.naturalsize(size_stats[3]),
89+
]
90+
91+
7292
def __get_size_statistics(df):
73-
'''
74-
Helper method that returns size statistics from size column.
93+
"""
94+
Helper method that returns size statistics from size column.
95+
96+
Input: dataframe
97+
Output: list of numbers
98+
"""
99+
100+
min = df["size"].min()
101+
max = df["size"].max()
102+
average = df["size"].mean()
103+
std = df["size"].std()
75104

76-
Input: dataframe
77-
Output: list of numbers
78-
'''
105+
return [min, max, average, std]
79106

80-
min = df['size'].min()
81-
max = df['size'].max()
82-
average = df['size'].mean()
83-
std = df['size'].std()
84107

85-
return[min, max, average, std]
86-
87108
def today():
88109
"""
89110
Get today's snapshot of Brain Image Library.

0 commit comments

Comments
 (0)