Skip to content

Commit 4c81654

Browse files
author
Ivan Cao-Berg
committed
Refactor square function. Use math.pow instead of multiplication.
1 parent 9143b59 commit 4c81654

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

braininventory/get.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import squarify
2121

2222

23-
def __get_general_modality_plot(df):
23+
def __create_general_modality_plot(df):
2424
modality_counts = df["generalmodality"].value_counts()
2525

2626
plt.figure(figsize=(10, 6))
@@ -70,11 +70,23 @@ def __get_lable_dict(name_lst):
7070
}
7171

7272

73-
def __get_general_modality_treemap(df):
73+
def __create_general_modality_treemap(df):
7474
"""
75-
input: dataframe
76-
output: tree map that displays the frequencies of "generalmodality" that occur in dataframe
75+
Create a treemap visualization for the general modality data.
76+
77+
This function takes a pandas DataFrame as input and generates a treemap visualization based on
78+
the counts of different modalities in the 'generalmodality' column of the DataFrame. The function
79+
utilizes the Squarify library to create the treemap.
80+
81+
Parameters:
82+
df (pandas.DataFrame): The input DataFrame containing the 'generalmodality' column.
83+
84+
Returns:
85+
None: The function generates a treemap and saves it as an image file, but it does not return any value.
86+
The treemap is saved with a filename in the format 'treemap-general-modality-YYYYMMDD.png', where
87+
'YYYYMMDD' represents the current date in year-month-day format.
7788
"""
89+
7890
modality_counts = df["generalmodality"].value_counts().to_dict()
7991
plt.figure(figsize=(14, 10))
8092
values = list(modality_counts.values())
@@ -94,7 +106,9 @@ def __get_general_modality_treemap(df):
94106
plt.legend(
95107
legend_patches, name, loc="upper left", bbox_to_anchor=(1, 1), fontsize="medium"
96108
)
97-
plt.show()
109+
110+
filename = f'treemap-general-modality-{datetime.now().strftime("%Y%m%d")}.png'
111+
plt.savefig(filename)
98112

99113

100114
def __get_pretty_size_statistics(df):
@@ -184,21 +198,24 @@ def get_date(df):
184198
day = dateList[2] # get day
185199
return f"{yr}-{day}-{mnt}" # format in year-day-month
186200

201+
187202
import geoip2.database
188203
from geopy.geocoders import Nominatim
189204
import folium
190205
import math
191206
import urllib.request
192207

193-
"""print(c.get_country_cities(country_code_iso="DE"))"""
208+
"""print(c.get_country_cities(country_code_iso="DE"))"""
194209
"""
195210
Geopy: input: University #correct some data (do later) Output: Address, lat, lon
196211
folium or Ivan's
197212
Must choose module to make the map
198213
"""
199214

215+
200216
def __get_affiliations(df):
201-
return df['affiliation'].value_counts().keys()
217+
return df["affiliation"].value_counts().keys()
218+
202219

203220
def __get_coordin(university):
204221
geolocator = Nominatim(user_agent="my_geocoding_app")
@@ -207,20 +224,22 @@ def __get_coordin(university):
207224
if location:
208225
return location.latitude, location.longitude
209226
else:
210-
return (0,0)
227+
return (0, 0)
211228
except:
212229
print(f"Geocoding service is unavailable for {university}")
213230
return 0, 0
214231

232+
215233
def get_zero_coords(affiliation_coordinates):
216234
total = 0
217235
zeros = 0
218236
for university in affiliation_coordinates:
219-
if affiliation_coordinates[university] == (0 ,0):
220-
zeros +=1
221-
print(affiliation_coordinates[university],university,"ain't working")
237+
if affiliation_coordinates[university] == (0, 0):
238+
zeros += 1
239+
print(affiliation_coordinates[university], university, "ain't working")
222240
total += 1
223-
return zeros, total, math.ceil(zeros/total*100)/100
241+
return zeros, total, math.ceil(zeros / total * 100) / 100
242+
224243

225244
def __get_affilation_coordinates(df):
226245
affiliations_dict = {}
@@ -230,6 +249,7 @@ def __get_affilation_coordinates(df):
230249
affiliations_dict[university] = (latitude, longitude)
231250
return affiliations_dict
232251

252+
233253
if __name__ == "__main__":
234254
affiliation_coordinates = __get_affilation_coordinates(df)
235255
print(affiliation_coordinates)
@@ -1033,16 +1053,18 @@ def report():
10331053

10341054
return report
10351055

1056+
10361057
import pandas as pd
10371058
import urllib.request
10381059
import geoip2.database
10391060
from geopy.geocoders import Nominatim
10401061
import folium
1041-
'''
1062+
1063+
"""
10421064
Import modules that will be used to create the world map, find coordinates of affiliations, and
1043-
'''
1065+
"""
10441066

1045-
url = 'https://download.brainimagelibrary.org/inventory/daily/reports/today.json'
1067+
url = "https://download.brainimagelibrary.org/inventory/daily/reports/today.json"
10461068
file_path, _ = urllib.request.urlretrieve(url)
10471069
df = pd.read_json(file_path)
10481070
df
@@ -1056,8 +1078,8 @@ def report():
10561078
from tqdm import tqdm
10571079

10581080
for index, row in tqdm(df.iterrows()):
1059-
city = row['city']
1060-
lat = row['lat']
1061-
lon = row['lng']
1062-
folium.Marker([lat, lon], popup=city).add_to(map)
1063-
map.save('project_map.html')
1081+
city = row["city"]
1082+
lat = row["lat"]
1083+
lon = row["lng"]
1084+
folium.Marker([lat, lon], popup=city).add_to(map)
1085+
map.save("project_map.html")

0 commit comments

Comments
 (0)