From 27f10e7f479ae6208cb6b12fd3a447db39f6a56e Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Tue, 2 May 2023 16:26:04 -0400 Subject: [PATCH] Make pytest error on warnings --- src/nidm/experiment/CDE.py | 8 +- src/nidm/experiment/Query.py | 18 +- src/nidm/experiment/Utils.py | 4 +- src/nidm/experiment/tools/bidsmri2nidm.py | 7 +- src/nidm/experiment/tools/nidm2bids.py | 11 +- .../tools/nidm_affinity_propagation.py | 49 +++-- .../tools/nidm_agglomerative_clustering.py | 49 +++-- src/nidm/experiment/tools/nidm_gmm.py | 50 +++-- src/nidm/experiment/tools/nidm_kmeans.py | 45 ++--- src/nidm/experiment/tools/nidm_linreg.py | 189 ++++++++---------- tox.ini | 8 + 11 files changed, 210 insertions(+), 228 deletions(-) diff --git a/src/nidm/experiment/CDE.py b/src/nidm/experiment/CDE.py index 1e987e0f..e809f098 100644 --- a/src/nidm/experiment/CDE.py +++ b/src/nidm/experiment/CDE.py @@ -28,7 +28,8 @@ def getCDEs(file_list=None): cache_file_name = tempfile.gettempdir() + "/cde_graph.{}.pickle".format(h) if path.isfile(cache_file_name): - rdf_graph = pickle.load(open(cache_file_name, "rb")) + with open(cache_file_name, "rb") as fp: + rdf_graph = pickle.load(fp) getCDEs.cache = rdf_graph return rdf_graph @@ -58,9 +59,8 @@ def getCDEs(file_list=None): cde_graph = nidm.experiment.Query.OpenGraph(fname) rdf_graph = rdf_graph + cde_graph - cache_file = open(cache_file_name, "wb") - pickle.dump(rdf_graph, cache_file) - cache_file.close() + with open(cache_file_name, "wb") as cache_file: + pickle.dump(rdf_graph, cache_file) getCDEs.cache = rdf_graph return rdf_graph diff --git a/src/nidm/experiment/Query.py b/src/nidm/experiment/Query.py index 0bbc92a5..e8f4062d 100644 --- a/src/nidm/experiment/Query.py +++ b/src/nidm/experiment/Query.py @@ -1407,8 +1407,8 @@ def OpenGraph(file): # If we have a Blazegraph instance, load the data then do the rest if "BLAZEGRAPH_URL" in environ.keys(): try: - f = open(file) - data = f.read() + with open(file) as f: + data = f.read() logging.debug("Sending {} to blazegraph".format(file)) requests.post( url=environ["BLAZEGRAPH_URL"], @@ -1429,11 +1429,13 @@ def OpenGraph(file): pickle_file = "{}/rdf_graph.{}.pickle".format(tempfile.gettempdir(), digest) if path.isfile(pickle_file): - return pickle.load(open(pickle_file, "rb")) + with open(pickle_file, "rb") as fp: + return pickle.load(fp) rdf_graph = Graph() rdf_graph.parse(file, format=util.guess_format(file)) - pickle.dump(rdf_graph, open(pickle_file, "wb")) + with open(pickle_file, "wb") as fp: + pickle.dump(rdf_graph, fp) # new graph, so to be safe clear out all cached entries memory.clear(warn=False) @@ -1512,7 +1514,8 @@ def getCDEs(file_list=None): cache_file_name = tempfile.gettempdir() + "/cde_graph.{}.pickle".format(h) if path.isfile(cache_file_name): - rdf_graph = pickle.load(open(cache_file_name, "rb")) + with open(cache_file_name, "rb") as fp: + rdf_graph = pickle.load(fp) getCDEs.cache = rdf_graph return rdf_graph @@ -1543,9 +1546,8 @@ def getCDEs(file_list=None): cde_graph = OpenGraph(fname) rdf_graph = rdf_graph + cde_graph - cache_file = open(cache_file_name, "wb") - pickle.dump(rdf_graph, cache_file) - cache_file.close() + with open(cache_file_name, "wb") as cache_file: + pickle.dump(rdf_graph, cache_file) getCDEs.cache = rdf_graph return rdf_graph diff --git a/src/nidm/experiment/Utils.py b/src/nidm/experiment/Utils.py index 492f66b7..56d01729 100644 --- a/src/nidm/experiment/Utils.py +++ b/src/nidm/experiment/Utils.py @@ -966,8 +966,8 @@ def load_nidm_terms_concepts(): concept_url = "https://raw.githubusercontent.com/NIDM-Terms/terms/master/terms/NIDM_Concepts.jsonld" try: - response = urlopen(concept_url) - concept_graph = json.loads(response.read().decode("utf-8")) + with urlopen(concept_url) as response: + concept_graph = json.loads(response.read().decode("utf-8")) except Exception: logging.info("Error opening %s used concepts file..continuing" % concept_url) return None diff --git a/src/nidm/experiment/tools/bidsmri2nidm.py b/src/nidm/experiment/tools/bidsmri2nidm.py index 904283a4..8c891516 100755 --- a/src/nidm/experiment/tools/bidsmri2nidm.py +++ b/src/nidm/experiment/tools/bidsmri2nidm.py @@ -242,9 +242,10 @@ def addbidsignore(directory, filename_to_add): with open(os.path.join(directory, ".bidsignore"), "w") as text_file: text_file.write("%s\n" % filename_to_add) else: - if filename_to_add not in open(os.path.join(directory, ".bidsignore")).read(): - with open(os.path.join(directory, ".bidsignore"), "a") as text_file: - text_file.write("%s\n" % filename_to_add) + with open(os.path.join(directory, ".bidsignore")) as fp: + if filename_to_add not in fp.read(): + with open(os.path.join(directory, ".bidsignore"), "a") as text_file: + text_file.write("%s\n" % filename_to_add) def addimagingsessions( diff --git a/src/nidm/experiment/tools/nidm2bids.py b/src/nidm/experiment/tools/nidm2bids.py index a011a77e..ea1fa2f6 100644 --- a/src/nidm/experiment/tools/nidm2bids.py +++ b/src/nidm/experiment/tools/nidm2bids.py @@ -145,12 +145,11 @@ def GetImageFromURL(url): # try to open the url and get the pointed to file try: # open url and get file - opener = ur.urlopen(url) - # write temporary file to disk and use for stats - temp = tempfile.NamedTemporaryFile(delete=False) - temp.write(opener.read()) - temp.close() - return temp.name + with ur.urlopen(url) as opener: + # write temporary file to disk and use for stats + with tempfile.NamedTemporaryFile(delete=False) as temp: + temp.write(opener.read()) + return temp.name except Exception: print("ERROR! Can't open url: %s" % url) return -1 diff --git a/src/nidm/experiment/tools/nidm_affinity_propagation.py b/src/nidm/experiment/tools/nidm_affinity_propagation.py index ea3b96f0..ea053b75 100644 --- a/src/nidm/experiment/tools/nidm_affinity_propagation.py +++ b/src/nidm/experiment/tools/nidm_affinity_propagation.py @@ -56,9 +56,8 @@ def data_aggregation(): # all data from all the files is collected print("Your command was: " + command) if o is not None: - f = open(o, "w") - f.write("Your command was " + command) - f.close() + with open(o, "w") as f: + f.write("Your command was " + command) verbosity = 0 restParser = RestParser(verbosity_level=int(verbosity)) restParser.setOutputFormat(RestParser.OBJECT_FORMAT) @@ -118,9 +117,10 @@ def data_aggregation(): # all data from all the files is collected ) as temp: # turns the dataframe into a temporary csv df.to_csv(temp.name + ".csv") temp.close() - data = list( - csv.reader(open(temp.name + ".csv")) - ) # makes the csv a 2D list to make it easier to call the contents of certain cells + with open(temp.name + ".csv") as fp: + data = list( + csv.reader(fp) + ) # makes the csv a 2D list to make it easier to call the contents of certain cells numcols = (len(data) - 1) // ( len(model_list) ) # Finds the number of columns in the original dataframe @@ -255,20 +255,18 @@ def data_aggregation(): # all data from all the files is collected + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." ) if o is not None: - f = open(o, "a") - f.write("Your variables were " + v) - f.write( - "The following variables were not found in " - + nidm_file - + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." - ) - f.close() + with open(o, "a") as f: + f.write("Your variables were " + v) + f.write( + "The following variables were not found in " + + nidm_file + + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." + ) for i in range(0, len(not_found_list)): print(str(i + 1) + ". " + not_found_list[i]) if o is not None: - f = open(o, "a") - f.write(str(i + 1) + ". " + not_found_list[i]) - f.close() + with open(o, "a") as f: + f.write(str(i + 1) + ". " + not_found_list[i]) for j in range(len(not_found_list) - 1, 0, -1): not_found_list.pop(j) 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 ) print() if o is not None: - f = open(o, "a") - f.write(df_final.to_string(header=True, index=True)) - f.write( - "\n\n***********************************************************************************************************" - ) - f.write("\n\nModel Results: ") - f.close() + with open(o, "a") as f: + f.write(df_final.to_string(header=True, index=True)) + f.write( + "\n\n***********************************************************************************************************" + ) + f.write("\n\nModel Results: ") def ap(): @@ -398,8 +395,8 @@ def ap(): plt.title(title) plt.show() if o is not None: - f = open(o, "a") - f.close() + with open(o, "a"): + pass def opencsv(data): diff --git a/src/nidm/experiment/tools/nidm_agglomerative_clustering.py b/src/nidm/experiment/tools/nidm_agglomerative_clustering.py index e7b89305..996565eb 100644 --- a/src/nidm/experiment/tools/nidm_agglomerative_clustering.py +++ b/src/nidm/experiment/tools/nidm_agglomerative_clustering.py @@ -56,9 +56,8 @@ def data_aggregation(): # all data from all the files is collected print("Your command was: " + command) if o is not None: - f = open(o, "w") - f.write("Your command was " + command) - f.close() + with open(o, "w") as f: + f.write("Your command was " + command) verbosity = 0 restParser = RestParser(verbosity_level=int(verbosity)) restParser.setOutputFormat(RestParser.OBJECT_FORMAT) @@ -118,9 +117,10 @@ def data_aggregation(): # all data from all the files is collected ) as temp: # turns the dataframe into a temporary csv df.to_csv(temp.name + ".csv") temp.close() - data = list( - csv.reader(open(temp.name + ".csv")) - ) # makes the csv a 2D list to make it easier to call the contents of certain cells + with open(temp.name + ".csv") as fp: + data = list( + csv.reader(fp) + ) # makes the csv a 2D list to make it easier to call the contents of certain cells numcols = (len(data) - 1) // ( len(model_list) ) # Finds the number of columns in the original dataframe @@ -255,20 +255,18 @@ def data_aggregation(): # all data from all the files is collected + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." ) if o is not None: - f = open(o, "a") - f.write("Your variables were " + v) - f.write( - "The following variables were not found in " - + nidm_file - + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." - ) - f.close() + with open(o, "a") as f: + f.write("Your variables were " + v) + f.write( + "The following variables were not found in " + + nidm_file + + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." + ) for i in range(0, len(not_found_list)): print(str(i + 1) + ". " + not_found_list[i]) if o is not None: - f = open(o, "a") - f.write(str(i + 1) + ". " + not_found_list[i]) - f.close() + with open(o, "a") as f: + f.write(str(i + 1) + ". " + not_found_list[i]) for j in range(len(not_found_list) - 1, 0, -1): not_found_list.pop(j) 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 ) print() if o is not None: - f = open(o, "a") - f.write(df_final.to_string(header=True, index=True)) - f.write( - "\n\n***********************************************************************************************************" - ) - f.write("\n\nModel Results: ") - f.close() + with open(o, "a") as f: + f.write(df_final.to_string(header=True, index=True)) + f.write( + "\n\n***********************************************************************************************************" + ) + f.write("\n\nModel Results: ") def ac(): @@ -386,8 +383,8 @@ def ac(): plt.show() if o is not None: - f = open(o, "a") - f.close() + with open(o, "a"): + pass def opencsv(data): diff --git a/src/nidm/experiment/tools/nidm_gmm.py b/src/nidm/experiment/tools/nidm_gmm.py index d835f5d3..29a5e851 100644 --- a/src/nidm/experiment/tools/nidm_gmm.py +++ b/src/nidm/experiment/tools/nidm_gmm.py @@ -85,9 +85,8 @@ def data_aggregation(): # all data from all the files is collected print("Your command was: " + command) if o is not None: - f = open(o, "w") - f.write("Your command was " + command) - f.close() + with open(o, "w") as f: + f.write("Your command was " + command) verbosity = 0 restParser = RestParser(verbosity_level=int(verbosity)) restParser.setOutputFormat(RestParser.OBJECT_FORMAT) @@ -149,9 +148,11 @@ def data_aggregation(): # all data from all the files is collected ) as temp: # turns the dataframe into a temporary csv df.to_csv(temp.name + ".csv") temp.close() - data = list( - csv.reader(open(temp.name + ".csv")) - ) # makes the csv a 2D list to make it easier to call the contents of certain cells + + with open(temp.name + ".csv") as fp: + data = list( + csv.reader(fp) + ) # makes the csv a 2D list to make it easier to call the contents of certain cells var_list = variables.split(",") # makes a list of the independent variables numcols = (len(data) - 1) // ( @@ -293,20 +294,18 @@ def data_aggregation(): # all data from all the files is collected + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." ) if o is not None: - f = open(o, "a") - f.write("Your variables were " + v) - f.write( - "The following variables were not found in " - + nidm_file - + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." - ) - f.close() + with open(o, "a") as f: + f.write("Your variables were " + v) + f.write( + "The following variables were not found in " + + nidm_file + + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." + ) for i in range(0, len(not_found_list)): print(str(i + 1) + ". " + not_found_list[i]) if o is not None: - f = open(o, "a") - f.write(str(i + 1) + ". " + not_found_list[i]) - f.close() + with open(o, "a") as f: + f.write(str(i + 1) + ". " + not_found_list[i]) for j in range(len(not_found_list) - 1, 0, -1): not_found_list.pop(j) 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 ) print() if o is not None: - f = open(o, "a") - f.write(df_final.to_string(header=True, index=True)) - f.write( - "\n\n***********************************************************************************************************" - ) - f.write("\n\nModel Results: ") - f.close() + with open(o, "a") as f: + f.write(df_final.to_string(header=True, index=True)) + f.write( + "\n\n***********************************************************************************************************" + ) + f.write("\n\nModel Results: ") def cluster_number(): @@ -562,8 +560,8 @@ def cluster_number(): plt.show() if o is not None: - f = open(o, "a") - f.close() + with open(o, "a"): + pass def opencsv(data): diff --git a/src/nidm/experiment/tools/nidm_kmeans.py b/src/nidm/experiment/tools/nidm_kmeans.py index 74c16c6e..542db866 100644 --- a/src/nidm/experiment/tools/nidm_kmeans.py +++ b/src/nidm/experiment/tools/nidm_kmeans.py @@ -91,9 +91,8 @@ def data_aggregation(): # all data from all the files is collected print("Your command was: " + command) if o is not None: - f = open(o, "w") - f.write("Your command was " + command) - f.close() + with open(o, "w") as f: + f.write("Your command was " + command) verbosity = 0 restParser = RestParser(verbosity_level=int(verbosity)) restParser.setOutputFormat(RestParser.OBJECT_FORMAT) @@ -155,9 +154,10 @@ def data_aggregation(): # all data from all the files is collected ) as temp: # turns the dataframe into a temporary csv df.to_csv(temp.name + ".csv") temp.close() - data = list( - csv.reader(open(temp.name + ".csv")) - ) # makes the csv a 2D list to make it easier to call the contents of certain cells + with open(temp.name + ".csv") as fp: + data = list( + csv.reader(fp) + ) # makes the csv a 2D list to make it easier to call the contents of certain cells var_list = variables.split(",") # makes a list of the independent variables numcols = (len(data) - 1) // ( @@ -299,20 +299,18 @@ def data_aggregation(): # all data from all the files is collected + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." ) if o is not None: - f = open(o, "a") - f.write("Your variables were " + v) - f.write( - "The following variables were not found in " - + nidm_file - + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." - ) - f.close() + with open(o, "a") as f: + f.write("Your variables were " + v) + f.write( + "The following variables were not found in " + + nidm_file + + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." + ) for i in range(0, len(not_found_list)): print(str(i + 1) + ". " + not_found_list[i]) if o is not None: - f = open(o, "a") - f.write(str(i + 1) + ". " + not_found_list[i]) - f.close() + with open(o, "a") as f: + f.write(str(i + 1) + ". " + not_found_list[i]) for j in range(len(not_found_list) - 1, 0, -1): not_found_list.pop(j) not_found_count = not_found_count + 1 @@ -394,13 +392,12 @@ def dataparsing(): # The data is changed to a format that is usable by the line ) print() if o is not None: - f = open(o, "a") - f.write(df_final.to_string(header=True, index=True)) - f.write( - "\n\n***********************************************************************************************************" - ) - f.write("\n\nModel Results: ") - f.close() + with open(o, "a") as f: + f.write(df_final.to_string(header=True, index=True)) + f.write( + "\n\n***********************************************************************************************************" + ) + f.write("\n\nModel Results: ") def cluster_number(): diff --git a/src/nidm/experiment/tools/nidm_linreg.py b/src/nidm/experiment/tools/nidm_linreg.py index 5e6b8ab8..2a6096ab 100644 --- a/src/nidm/experiment/tools/nidm_linreg.py +++ b/src/nidm/experiment/tools/nidm_linreg.py @@ -125,9 +125,8 @@ def data_aggregation(): # all data from all the files is collected command = command + "-r " + r + " " print("Your command was: " + command) if o is not None: - f = open(o, "w") - f.write("Your command was " + command) - f.close() + with open(o, "w") as f: + f.write("Your command was " + command) verbosity = 0 restParser = RestParser(verbosity_level=int(verbosity)) restParser.setOutputFormat(RestParser.OBJECT_FORMAT) @@ -194,16 +193,15 @@ def data_aggregation(): # all data from all the files is collected + '" from either the right or the left side of the equation.\n\n' ) if o is not None: - f = open(o, "a") - f.write( - "\n\nAn independent variable cannot be the same as the dependent variable. This prevents the model from running accurately." - ) - f.write( - 'Please try a different model removing "' - + dep_var - + '" from either the right or the left side of the equation.' - ) - f.close() + with open(o, "a") as f: + f.write( + "\n\nAn independent variable cannot be the same as the dependent variable. This prevents the model from running accurately." + ) + f.write( + 'Please try a different model removing "' + + dep_var + + '" from either the right or the left side of the equation.' + ) exit(1) else: ind_vars = ind_vars + model_list[i] + "," @@ -225,9 +223,10 @@ def data_aggregation(): # all data from all the files is collected ) as temp: # turns the dataframe into a temporary csv df.to_csv(temp.name + ".csv") temp.close() - data = list( - csv.reader(open(temp.name + ".csv")) - ) # makes the csv a 2D list to make it easier to call the contents of certain cells + with open(temp.name + ".csv") as fp: + data = list( + csv.reader(fp) + ) # makes the csv a 2D list to make it easier to call the contents of certain cells global independentvariables # used in linreg independentvariables = ind_vars.split( @@ -390,20 +389,18 @@ def data_aggregation(): # all data from all the files is collected + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." ) if o is not None: - f = open(o, "a") - f.write("Your model was " + m) - f.write( - "The following variables were not found in " - + nidm_file - + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." - ) - f.close() + with open(o, "a") as f: + f.write("Your model was " + m) + f.write( + "The following variables were not found in " + + nidm_file + + ". The model cannot run because this will skew the data. Try checking your spelling or use nidm_query.py to see other possible variables." + ) for i in range(0, len(not_found_list)): print(str(i + 1) + ". " + not_found_list[i]) if o is not None: - f = open(o, "a") - f.write(str(i + 1) + ". " + not_found_list[i]) - f.close() + with open(o, "a") as f: + f.write(str(i + 1) + ". " + not_found_list[i]) for j in range(len(not_found_list) - 1, 0, -1): not_found_list.pop(j) not_found_count = not_found_count + 1 @@ -446,21 +443,19 @@ def dataparsing(): # The data is changed to a format that is usable by the line warnings.filterwarnings("ignore") answer = input("Continue anyways? Y or N: ") if o is not None: - f = open(o, "a") - f.write("Your model was " + m) - f.write( - "\n\nThere was a lack of data (<20 points) in your model, which may result in inaccuracies. In addition, a regularization cannot and will not be performed.\n" - ) - f.close() + with open(o, "a") as f: + f.write("Your model was " + m) + f.write( + "\n\nThere was a lack of data (<20 points) in your model, which may result in inaccuracies. In addition, a regularization cannot and will not be performed.\n" + ) if "n" in answer.lower(): print("\nModel halted.") if o is not None: - f = open(o, "a") - f.write("Your model was " + m) - f.write( - "Due to a lack of data (<20 points), you stopped the model because the results may have been inaccurate." - ) - f.close() + with open(o, "a") as f: + f.write("Your model was " + m) + f.write( + "Due to a lack of data (<20 points), you stopped the model because the results may have been inaccurate." + ) exit(1) x = pd.read_csv( opencsv(condensed_data) @@ -514,13 +509,12 @@ def dataparsing(): # The data is changed to a format that is usable by the line ) print() if o is not None: - f = open(o, "a") - f.write(df_final.to_string(header=True, index=True)) - f.write( - "\n\n***********************************************************************************************************" - ) - f.write("\n\nModel Results: ") - f.close() + with open(o, "a") as f: + f.write(df_final.to_string(header=True, index=True)) + f.write( + "\n\n***********************************************************************************************************" + ) + f.write("\n\nModel Results: ") def linreg(): # actual linear regression @@ -658,18 +652,17 @@ def contrasting(): print(res.summary()) if o is not None: # concatenate data frames - f = open(o, "a") - f.write("\n" + full_model) - f.write( - "\n\n***********************************************************************************************************" - ) + with open(o, "a") as f: + f.write("\n" + full_model) + f.write( + "\n\n***********************************************************************************************************" + ) - f.write( - "\n\n\n\nTreatment (Dummy) Coding: Dummy coding compares each level of the categorical variable to a base reference level. The base reference level is the value of the intercept." - ) - f.write("With contrast (treatment coding)") - f.write(res.summary().as_text()) - f.close() + f.write( + "\n\n\n\nTreatment (Dummy) Coding: Dummy coding compares each level of the categorical variable to a base reference level. The base reference level is the value of the intercept." + ) + f.write("With contrast (treatment coding)") + f.write(res.summary().as_text()) # Defining the Simple class def _name_levels(prefix, levels): @@ -709,12 +702,11 @@ def code_without_intercept(self, levels): print(res.summary()) if o is not None: # concatenate data frames - f = open(o, "a") - f.write( - "\n\n\nSimple Coding: Like Treatment Coding, Simple Coding compares each level to a fixed reference level. However, with simple coding, the intercept is the grand mean of all the levels of the factors." - ) - f.write(res.summary().as_text()) - f.close() + with open(o, "a") as f: + f.write( + "\n\n\nSimple Coding: Like Treatment Coding, Simple Coding compares each level to a fixed reference level. However, with simple coding, the intercept is the grand mean of all the levels of the factors." + ) + f.write(res.summary().as_text()) # With contrast (sum/deviation coding) Sum().code_without_intercept(levels) @@ -734,12 +726,11 @@ def code_without_intercept(self, levels): print(res.summary()) if o is not None: # concatenate data frames - f = open(o, "a") - f.write( - "\n\n\nSum (Deviation) Coding: Sum coding compares the mean of the dependent variable for a given level to the overall mean of the dependent variable over all the levels." - ) - f.write(res.summary().as_text()) - f.close() + with open(o, "a") as f: + f.write( + "\n\n\nSum (Deviation) Coding: Sum coding compares the mean of the dependent variable for a given level to the overall mean of the dependent variable over all the levels." + ) + f.write(res.summary().as_text()) # With contrast (backward difference coding) Diff().code_without_intercept(levels) @@ -759,12 +750,11 @@ def code_without_intercept(self, levels): print(res.summary()) if o is not None: # concatenate data frames - f = open(o, "a") - f.write( - "\n\n\nBackward Difference Coding: In backward difference coding, the mean of the dependent variable for a level is compared with the mean of the dependent variable for the prior level." - ) - f.write(res.summary().as_text()) - f.close() + with open(o, "a") as f: + f.write( + "\n\n\nBackward Difference Coding: In backward difference coding, the mean of the dependent variable for a level is compared with the mean of the dependent variable for the prior level." + ) + f.write(res.summary().as_text()) # With contrast (Helmert coding) Helmert().code_without_intercept(levels) @@ -784,12 +774,11 @@ def code_without_intercept(self, levels): print(res.summary()) if o is not None: # concatenate data frames - f = open(o, "a") - f.write( - "\n\n\nHelmert Coding: Our version of Helmert coding is sometimes referred to as Reverse Helmert Coding. The mean of the dependent variable for a level is compared to the mean of the dependent variable over all previous levels. Hence, the name ‘reverse’ being sometimes applied to differentiate from forward Helmert coding." - ) - f.write(res.summary().as_text()) - f.close() + with open(o, "a") as f: + f.write( + "\n\n\nHelmert Coding: Our version of Helmert coding is sometimes referred to as Reverse Helmert Coding. The mean of the dependent variable for a level is compared to the mean of the dependent variable over all previous levels. Hence, the name ‘reverse’ being sometimes applied to differentiate from forward Helmert coding." + ) + f.write(res.summary().as_text()) def regularizing(): @@ -824,27 +813,24 @@ def regularizing(): print("\nCoefficients:") if o is not None: # concatenate data frames - f = open(o, "a") - f.write("\n\nLasso regression model:") - f.write( - "\nAlpha with maximum likelihood (range: 1 to %d) = %f" - % (MAX_ALPHA, max_cross_val_alpha) - ) - f.write("\nCurrent Model Score = %f" % (lassoModelChosen.score(X, y))) - f.write("\n\nCoefficients:") - f.close() + with open(o, "a") as f: + f.write("\n\nLasso regression model:") + f.write( + "\nAlpha with maximum likelihood (range: 1 to %d) = %f" + % (MAX_ALPHA, max_cross_val_alpha) + ) + f.write("\nCurrent Model Score = %f" % (lassoModelChosen.score(X, y))) + f.write("\n\nCoefficients:") for var in full_model_variable_list: print("%s \t %f" % (var, lassoModelChosen.coef_[index])) if o is not None: with open(o, "a") as f: f.write("\n%s \t %f" % (var, lassoModelChosen.coef_[index])) - f.close() index = index + 1 print("Intercept: %f" % (lassoModelChosen.intercept_)) if o is not None: with open(o, "a") as f: f.write("\nIntercept: %f" % (lassoModelChosen.intercept_)) - f.close() print() if r == ("L2" or "Ridge" or "l2" or "Ridge") and not ( @@ -884,15 +870,14 @@ def regularizing(): numpy_conversion = True if o is not None: # concatenate data frames - f = open(o, "a") - f.write("\n\nRidge regression model:") - f.write( - "\nAlpha with maximum likelihood (range: 1 to %d) = %f" - % (MAX_ALPHA, max_cross_val_alpha) - ) - f.write("\nCurrent Model Score = %f" % (ridgeModelChosen.score(X, y))) - f.write("\n\nCoefficients:") - f.close() + with open(o, "a") as f: + f.write("\n\nRidge regression model:") + f.write( + "\nAlpha with maximum likelihood (range: 1 to %d) = %f" + % (MAX_ALPHA, max_cross_val_alpha) + ) + f.write("\nCurrent Model Score = %f" % (ridgeModelChosen.score(X, y))) + f.write("\n\nCoefficients:") print("\nCoefficients:") if numpy_conversion: coeff_list = ridgeModelChosen.coef_[index].tolist() @@ -907,7 +892,6 @@ def regularizing(): if o is not None: with open(o, "a") as f: f.write("\nIntercept: %f" % (ridgeModelChosen.intercept_)) - f.close() print() else: for var in full_model_variable_list: @@ -920,7 +904,6 @@ def regularizing(): if o is not None: with open(o, "a") as f: f.write("\nIntercept: %f" % (ridgeModelChosen.intercept_)) - f.close() print() diff --git a/tox.ini b/tox.ini index 276ac64f..ac605496 100644 --- a/tox.ini +++ b/tox.ini @@ -18,6 +18,14 @@ deps = commands = flake8 src tests +[pytest] +filterwarnings = + error + # + ignore:.*pkg_resources:DeprecationWarning + # + ignore:the imp module is deprecated:DeprecationWarning + [flake8] doctests = True exclude = .*/,build/,dist/,test/data,venv/