Skip to content

Commit 6422a77

Browse files
authored
Merge pull request #46 from brain-image-library/45-get-jsonfile
#45 get jsonfile
2 parents 332823d + e4b09be commit 6422a77

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

braininventory/get.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,39 @@
1111
import matplotlib.pyplot as plt
1212

1313

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+
1434
def today():
1535
"""
1636
Get today's snapshot of Brain Image Library.
1737
"""
1838

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
1947
server = "https://download.brainimagelibrary.org/inventory/daily/reports/"
2048
filename = "today.json"
2149

@@ -27,7 +55,6 @@ def today():
2755
data = json.loads(response.text)
2856
data = pd.DataFrame(data)
2957
return data
30-
3158
else:
3259
print("Error: Failed to fetch JSON data")
3360
return pd.DataFrame()

0 commit comments

Comments
 (0)