Skip to content

Commit 8bb7995

Browse files
committed
Refactored code for better readability. Fixed bug causing null pointer exception. Added new function for improved functionality.
1 parent 6df8d3e commit 8bb7995

File tree

1 file changed

+49
-35
lines changed

1 file changed

+49
-35
lines changed

braininventory/get.py

Lines changed: 49 additions & 35 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,37 +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

72-
import humanize
7374

7475
def __get_pretty_size_statistics(df):
75-
size_stats = __get_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+
7691

77-
return [humanize.naturalsize(size_stats[0]), humanize.naturalsize(size_stats[1]), humanize.naturalsize(size_stats[2]), humanize.naturalsize(size_stats[3])]
78-
7992
def __get_size_statistics(df):
80-
'''
81-
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()
82104

83-
Input: dataframe
84-
Output: list of numbers
85-
'''
105+
return [min, max, average, std]
86106

87-
min = df['size'].min()
88-
max = df['size'].max()
89-
average = df['size'].mean()
90-
std = df['size'].std()
91107

92-
return[min, max, average, std]
93-
94108
def today():
95109
"""
96110
Get today's snapshot of Brain Image Library.

0 commit comments

Comments
 (0)