Coverage for csv_manager/XV_CSVManager.py: 33%
36 statements
« prev ^ index » next coverage.py v7.0.4, created at 2023-01-10 09:27 -0600
« prev ^ index » next coverage.py v7.0.4, created at 2023-01-10 09:27 -0600
1"""
2Copyright 1999 Illinois Institute of Technology
4Permission is hereby granted, free of charge, to any person obtaining
5a copy of this software and associated documentation files (the
6"Software"), to deal in the Software without restriction, including
7without limitation the rights to use, copy, modify, merge, publish,
8distribute, sublicense, and/or sell copies of the Software, and to
9permit persons to whom the Software is furnished to do so, subject to
10the following conditions:
12The above copyright notice and this permission notice shall be
13included in all copies or substantial portions of the Software.
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18IN NO EVENT SHALL ILLINOIS INSTITUTE OF TECHNOLOGY BE LIABLE FOR ANY
19CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23Except as contained in this notice, the name of Illinois Institute
24of Technology shall not be used in advertising or otherwise to promote
25the sale, use or other dealings in this Software without prior written
26authorization from Illinois Institute of Technology.
27"""
29from os import makedirs
30from os.path import exists
31import pandas as pd
32try:
33 from ..utils.file_manager import fullPath
34except: # for coverage
35 from utils.file_manager import fullPath
37class XV_CSVManager:
38 """
39 A class taking care of writing results including csv file and failedcases file
40 """
41 def __init__(self, dir_path):
42 """
43 init with directory path
44 :param dir_path:
45 """
46 self.dataframe = None
47 result_path = fullPath(dir_path, "xv_results")
48 if not exists(result_path):
49 makedirs(result_path)
50 self.filename = fullPath(result_path, 'summary.csv')
51 self.colnames = ['Filename', 'Histogram', 'Comment']
52 self.loadSummary()
54 def loadSummary(self):
55 """
56 Load summary.csv file and keep data in self.dataframe
57 :return:
58 """
59 if not exists(self.filename):
60 self.dataframe = pd.DataFrame(columns = self.colnames)
61 else:
62 self.dataframe = pd.read_csv(self.filename)
64 def writeNewData(self, xrayViewer):
65 """
66 Add new data to dataframe, then re-write summary.csv
67 :param xrayViewer: QuadrantFolder object with results in its info dict
68 :return: -
69 """
70 img_name = xrayViewer.img_name
71 self.removeData(img_name)
72 data = {}
74 # If there is no result
75 if xrayViewer.hist == []:
76 for k in self.dataframe.columns:
77 data[k] = '-'
78 data['Filename'] = img_name
79 data['comment'] = "No slice or box selected"
80 else:
81 # Get all needed infos
82 data['Filename'] = img_name
83 data['Histogram'] = xrayViewer.hist
85 self.dataframe = pd.concat([self.dataframe, pd.DataFrame.from_records([data])])
86 # self.dataframe = self.dataframe.append(data, ignore_index=True) # Future warning deprecated
87 self.dataframe.reset_index()
88 self.dataframe.to_csv(self.filename, index=False, columns=self.colnames) # Write to csv file
90 def removeData(self, img_name):
91 """
92 Remove data from dataframe
93 :param img_name: (str)
94 :return:
95 """
96 self.dataframe = self.dataframe[self.dataframe["Filename"] != img_name]