11
11
import matplotlib .pyplot as plt
12
12
13
13
14
+ def get_random_sample (df ):
15
+ """
16
+ Returns a random sample from the dataframe from a dataset with non-zero score.
17
+
18
+ Input: dataframe
19
+ Output:open the json file that was located in datasets Brain Image Library dataframe
20
+ """
21
+
22
+ isNotZero = df [df ["score" ] != 0.0 ] # only have files with the correct data
23
+ randomRow = isNotZero .iloc [
24
+ random .randint (0 , len (isNotZero ))
25
+ ] # select a random row of random index
26
+ jsonFileLink = randomRow .json_file .replace (
27
+ "/bil/data" , "https://download.brainimagelibrary.org" , 1
28
+ ) # create the link
29
+ result = requests .get (jsonFileLink )
30
+
31
+ return result .json ()
32
+
33
+
14
34
def today ():
15
35
"""
16
36
Get today's snapshot of Brain Image Library.
17
37
"""
18
38
39
+ # if file can be found locally, then load from disk
40
+ directory = "/bil/data/inventory/daily/reports/"
41
+ if Path (directory ).exists ():
42
+ data = json .loads (f"{ directory } /today.json" )
43
+ data = pd .DataFrame (data )
44
+ return data
45
+
46
+ # else get file from the web
19
47
server = "https://download.brainimagelibrary.org/inventory/daily/reports/"
20
48
filename = "today.json"
21
49
@@ -27,7 +55,6 @@ def today():
27
55
data = json .loads (response .text )
28
56
data = pd .DataFrame (data )
29
57
return data
30
-
31
58
else :
32
59
print ("Error: Failed to fetch JSON data" )
33
60
return pd .DataFrame ()
0 commit comments