Skip to content

Commit 47ec0cc

Browse files
authored
Merge pull request #341 from incf-nidash/fail-warn
Make pytest error on warnings
2 parents f73bcf9 + 27f10e7 commit 47ec0cc

11 files changed

+210
-228
lines changed

src/nidm/experiment/CDE.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def getCDEs(file_list=None):
2828
cache_file_name = tempfile.gettempdir() + "/cde_graph.{}.pickle".format(h)
2929

3030
if path.isfile(cache_file_name):
31-
rdf_graph = pickle.load(open(cache_file_name, "rb"))
31+
with open(cache_file_name, "rb") as fp:
32+
rdf_graph = pickle.load(fp)
3233
getCDEs.cache = rdf_graph
3334
return rdf_graph
3435

@@ -58,9 +59,8 @@ def getCDEs(file_list=None):
5859
cde_graph = nidm.experiment.Query.OpenGraph(fname)
5960
rdf_graph = rdf_graph + cde_graph
6061

61-
cache_file = open(cache_file_name, "wb")
62-
pickle.dump(rdf_graph, cache_file)
63-
cache_file.close()
62+
with open(cache_file_name, "wb") as cache_file:
63+
pickle.dump(rdf_graph, cache_file)
6464

6565
getCDEs.cache = rdf_graph
6666
return rdf_graph

src/nidm/experiment/Query.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1407,8 +1407,8 @@ def OpenGraph(file):
14071407
# If we have a Blazegraph instance, load the data then do the rest
14081408
if "BLAZEGRAPH_URL" in environ.keys():
14091409
try:
1410-
f = open(file)
1411-
data = f.read()
1410+
with open(file) as f:
1411+
data = f.read()
14121412
logging.debug("Sending {} to blazegraph".format(file))
14131413
requests.post(
14141414
url=environ["BLAZEGRAPH_URL"],
@@ -1429,11 +1429,13 @@ def OpenGraph(file):
14291429

14301430
pickle_file = "{}/rdf_graph.{}.pickle".format(tempfile.gettempdir(), digest)
14311431
if path.isfile(pickle_file):
1432-
return pickle.load(open(pickle_file, "rb"))
1432+
with open(pickle_file, "rb") as fp:
1433+
return pickle.load(fp)
14331434

14341435
rdf_graph = Graph()
14351436
rdf_graph.parse(file, format=util.guess_format(file))
1436-
pickle.dump(rdf_graph, open(pickle_file, "wb"))
1437+
with open(pickle_file, "wb") as fp:
1438+
pickle.dump(rdf_graph, fp)
14371439

14381440
# new graph, so to be safe clear out all cached entries
14391441
memory.clear(warn=False)
@@ -1512,7 +1514,8 @@ def getCDEs(file_list=None):
15121514
cache_file_name = tempfile.gettempdir() + "/cde_graph.{}.pickle".format(h)
15131515

15141516
if path.isfile(cache_file_name):
1515-
rdf_graph = pickle.load(open(cache_file_name, "rb"))
1517+
with open(cache_file_name, "rb") as fp:
1518+
rdf_graph = pickle.load(fp)
15161519
getCDEs.cache = rdf_graph
15171520
return rdf_graph
15181521

@@ -1543,9 +1546,8 @@ def getCDEs(file_list=None):
15431546
cde_graph = OpenGraph(fname)
15441547
rdf_graph = rdf_graph + cde_graph
15451548

1546-
cache_file = open(cache_file_name, "wb")
1547-
pickle.dump(rdf_graph, cache_file)
1548-
cache_file.close()
1549+
with open(cache_file_name, "wb") as cache_file:
1550+
pickle.dump(rdf_graph, cache_file)
15491551

15501552
getCDEs.cache = rdf_graph
15511553
return rdf_graph

src/nidm/experiment/Utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,8 @@ def load_nidm_terms_concepts():
966966
concept_url = "https://raw.githubusercontent.com/NIDM-Terms/terms/master/terms/NIDM_Concepts.jsonld"
967967

968968
try:
969-
response = urlopen(concept_url)
970-
concept_graph = json.loads(response.read().decode("utf-8"))
969+
with urlopen(concept_url) as response:
970+
concept_graph = json.loads(response.read().decode("utf-8"))
971971
except Exception:
972972
logging.info("Error opening %s used concepts file..continuing" % concept_url)
973973
return None

src/nidm/experiment/tools/bidsmri2nidm.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,10 @@ def addbidsignore(directory, filename_to_add):
242242
with open(os.path.join(directory, ".bidsignore"), "w") as text_file:
243243
text_file.write("%s\n" % filename_to_add)
244244
else:
245-
if filename_to_add not in open(os.path.join(directory, ".bidsignore")).read():
246-
with open(os.path.join(directory, ".bidsignore"), "a") as text_file:
247-
text_file.write("%s\n" % filename_to_add)
245+
with open(os.path.join(directory, ".bidsignore")) as fp:
246+
if filename_to_add not in fp.read():
247+
with open(os.path.join(directory, ".bidsignore"), "a") as text_file:
248+
text_file.write("%s\n" % filename_to_add)
248249

249250

250251
def addimagingsessions(

src/nidm/experiment/tools/nidm2bids.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,11 @@ def GetImageFromURL(url):
145145
# try to open the url and get the pointed to file
146146
try:
147147
# open url and get file
148-
opener = ur.urlopen(url)
149-
# write temporary file to disk and use for stats
150-
temp = tempfile.NamedTemporaryFile(delete=False)
151-
temp.write(opener.read())
152-
temp.close()
153-
return temp.name
148+
with ur.urlopen(url) as opener:
149+
# write temporary file to disk and use for stats
150+
with tempfile.NamedTemporaryFile(delete=False) as temp:
151+
temp.write(opener.read())
152+
return temp.name
154153
except Exception:
155154
print("ERROR! Can't open url: %s" % url)
156155
return -1

src/nidm/experiment/tools/nidm_affinity_propagation.py

+23-26
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ def data_aggregation(): # all data from all the files is collected
5656

5757
print("Your command was: " + command)
5858
if o is not None:
59-
f = open(o, "w")
60-
f.write("Your command was " + command)
61-
f.close()
59+
with open(o, "w") as f:
60+
f.write("Your command was " + command)
6261
verbosity = 0
6362
restParser = RestParser(verbosity_level=int(verbosity))
6463
restParser.setOutputFormat(RestParser.OBJECT_FORMAT)
@@ -118,9 +117,10 @@ def data_aggregation(): # all data from all the files is collected
118117
) as temp: # turns the dataframe into a temporary csv
119118
df.to_csv(temp.name + ".csv")
120119
temp.close()
121-
data = list(
122-
csv.reader(open(temp.name + ".csv"))
123-
) # makes the csv a 2D list to make it easier to call the contents of certain cells
120+
with open(temp.name + ".csv") as fp:
121+
data = list(
122+
csv.reader(fp)
123+
) # makes the csv a 2D list to make it easier to call the contents of certain cells
124124
numcols = (len(data) - 1) // (
125125
len(model_list)
126126
) # Finds the number of columns in the original dataframe
@@ -255,20 +255,18 @@ def data_aggregation(): # all data from all the files is collected
255255
+ ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables."
256256
)
257257
if o is not None:
258-
f = open(o, "a")
259-
f.write("Your variables were " + v)
260-
f.write(
261-
"The following variables were not found in "
262-
+ nidm_file
263-
+ ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables."
264-
)
265-
f.close()
258+
with open(o, "a") as f:
259+
f.write("Your variables were " + v)
260+
f.write(
261+
"The following variables were not found in "
262+
+ nidm_file
263+
+ ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables."
264+
)
266265
for i in range(0, len(not_found_list)):
267266
print(str(i + 1) + ". " + not_found_list[i])
268267
if o is not None:
269-
f = open(o, "a")
270-
f.write(str(i + 1) + ". " + not_found_list[i])
271-
f.close()
268+
with open(o, "a") as f:
269+
f.write(str(i + 1) + ". " + not_found_list[i])
272270
for j in range(len(not_found_list) - 1, 0, -1):
273271
not_found_list.pop(j)
274272
not_found_count = not_found_count + 1
@@ -340,13 +338,12 @@ def dataparsing(): # The data is changed to a format that is usable by the line
340338
)
341339
print()
342340
if o is not None:
343-
f = open(o, "a")
344-
f.write(df_final.to_string(header=True, index=True))
345-
f.write(
346-
"\n\n***********************************************************************************************************"
347-
)
348-
f.write("\n\nModel Results: ")
349-
f.close()
341+
with open(o, "a") as f:
342+
f.write(df_final.to_string(header=True, index=True))
343+
f.write(
344+
"\n\n***********************************************************************************************************"
345+
)
346+
f.write("\n\nModel Results: ")
350347

351348

352349
def ap():
@@ -398,8 +395,8 @@ def ap():
398395
plt.title(title)
399396
plt.show()
400397
if o is not None:
401-
f = open(o, "a")
402-
f.close()
398+
with open(o, "a"):
399+
pass
403400

404401

405402
def opencsv(data):

src/nidm/experiment/tools/nidm_agglomerative_clustering.py

+23-26
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ def data_aggregation(): # all data from all the files is collected
5656

5757
print("Your command was: " + command)
5858
if o is not None:
59-
f = open(o, "w")
60-
f.write("Your command was " + command)
61-
f.close()
59+
with open(o, "w") as f:
60+
f.write("Your command was " + command)
6261
verbosity = 0
6362
restParser = RestParser(verbosity_level=int(verbosity))
6463
restParser.setOutputFormat(RestParser.OBJECT_FORMAT)
@@ -118,9 +117,10 @@ def data_aggregation(): # all data from all the files is collected
118117
) as temp: # turns the dataframe into a temporary csv
119118
df.to_csv(temp.name + ".csv")
120119
temp.close()
121-
data = list(
122-
csv.reader(open(temp.name + ".csv"))
123-
) # makes the csv a 2D list to make it easier to call the contents of certain cells
120+
with open(temp.name + ".csv") as fp:
121+
data = list(
122+
csv.reader(fp)
123+
) # makes the csv a 2D list to make it easier to call the contents of certain cells
124124
numcols = (len(data) - 1) // (
125125
len(model_list)
126126
) # Finds the number of columns in the original dataframe
@@ -255,20 +255,18 @@ def data_aggregation(): # all data from all the files is collected
255255
+ ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables."
256256
)
257257
if o is not None:
258-
f = open(o, "a")
259-
f.write("Your variables were " + v)
260-
f.write(
261-
"The following variables were not found in "
262-
+ nidm_file
263-
+ ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables."
264-
)
265-
f.close()
258+
with open(o, "a") as f:
259+
f.write("Your variables were " + v)
260+
f.write(
261+
"The following variables were not found in "
262+
+ nidm_file
263+
+ ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables."
264+
)
266265
for i in range(0, len(not_found_list)):
267266
print(str(i + 1) + ". " + not_found_list[i])
268267
if o is not None:
269-
f = open(o, "a")
270-
f.write(str(i + 1) + ". " + not_found_list[i])
271-
f.close()
268+
with open(o, "a") as f:
269+
f.write(str(i + 1) + ". " + not_found_list[i])
272270
for j in range(len(not_found_list) - 1, 0, -1):
273271
not_found_list.pop(j)
274272
not_found_count = not_found_count + 1
@@ -340,13 +338,12 @@ def dataparsing(): # The data is changed to a format that is usable by the line
340338
)
341339
print()
342340
if o is not None:
343-
f = open(o, "a")
344-
f.write(df_final.to_string(header=True, index=True))
345-
f.write(
346-
"\n\n***********************************************************************************************************"
347-
)
348-
f.write("\n\nModel Results: ")
349-
f.close()
341+
with open(o, "a") as f:
342+
f.write(df_final.to_string(header=True, index=True))
343+
f.write(
344+
"\n\n***********************************************************************************************************"
345+
)
346+
f.write("\n\nModel Results: ")
350347

351348

352349
def ac():
@@ -386,8 +383,8 @@ def ac():
386383
plt.show()
387384

388385
if o is not None:
389-
f = open(o, "a")
390-
f.close()
386+
with open(o, "a"):
387+
pass
391388

392389

393390
def opencsv(data):

src/nidm/experiment/tools/nidm_gmm.py

+24-26
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ def data_aggregation(): # all data from all the files is collected
8585

8686
print("Your command was: " + command)
8787
if o is not None:
88-
f = open(o, "w")
89-
f.write("Your command was " + command)
90-
f.close()
88+
with open(o, "w") as f:
89+
f.write("Your command was " + command)
9190
verbosity = 0
9291
restParser = RestParser(verbosity_level=int(verbosity))
9392
restParser.setOutputFormat(RestParser.OBJECT_FORMAT)
@@ -149,9 +148,11 @@ def data_aggregation(): # all data from all the files is collected
149148
) as temp: # turns the dataframe into a temporary csv
150149
df.to_csv(temp.name + ".csv")
151150
temp.close()
152-
data = list(
153-
csv.reader(open(temp.name + ".csv"))
154-
) # makes the csv a 2D list to make it easier to call the contents of certain cells
151+
152+
with open(temp.name + ".csv") as fp:
153+
data = list(
154+
csv.reader(fp)
155+
) # makes the csv a 2D list to make it easier to call the contents of certain cells
155156

156157
var_list = variables.split(",") # makes a list of the independent variables
157158
numcols = (len(data) - 1) // (
@@ -293,20 +294,18 @@ def data_aggregation(): # all data from all the files is collected
293294
+ ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables."
294295
)
295296
if o is not None:
296-
f = open(o, "a")
297-
f.write("Your variables were " + v)
298-
f.write(
299-
"The following variables were not found in "
300-
+ nidm_file
301-
+ ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables."
302-
)
303-
f.close()
297+
with open(o, "a") as f:
298+
f.write("Your variables were " + v)
299+
f.write(
300+
"The following variables were not found in "
301+
+ nidm_file
302+
+ ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables."
303+
)
304304
for i in range(0, len(not_found_list)):
305305
print(str(i + 1) + ". " + not_found_list[i])
306306
if o is not None:
307-
f = open(o, "a")
308-
f.write(str(i + 1) + ". " + not_found_list[i])
309-
f.close()
307+
with open(o, "a") as f:
308+
f.write(str(i + 1) + ". " + not_found_list[i])
310309
for j in range(len(not_found_list) - 1, 0, -1):
311310
not_found_list.pop(j)
312311
not_found_count = not_found_count + 1
@@ -388,13 +387,12 @@ def dataparsing(): # The data is changed to a format that is usable by the line
388387
)
389388
print()
390389
if o is not None:
391-
f = open(o, "a")
392-
f.write(df_final.to_string(header=True, index=True))
393-
f.write(
394-
"\n\n***********************************************************************************************************"
395-
)
396-
f.write("\n\nModel Results: ")
397-
f.close()
390+
with open(o, "a") as f:
391+
f.write(df_final.to_string(header=True, index=True))
392+
f.write(
393+
"\n\n***********************************************************************************************************"
394+
)
395+
f.write("\n\nModel Results: ")
398396

399397

400398
def cluster_number():
@@ -562,8 +560,8 @@ def cluster_number():
562560
plt.show()
563561

564562
if o is not None:
565-
f = open(o, "a")
566-
f.close()
563+
with open(o, "a"):
564+
pass
567565

568566

569567
def opencsv(data):

0 commit comments

Comments
 (0)