diff --git a/utils.py b/utils.py index 962aa74b..48eb248f 100644 --- a/utils.py +++ b/utils.py @@ -94,16 +94,15 @@ def getMMposeDirectory(isDocker=False): return mmposeDirectory def loadCameraParameters(filename): - open_file = open(filename, "rb") - cameraParams = pickle.load(open_file) - - open_file.close() + with open(filename, "rb") as open_file: + cameraParams = pickle.load(open_file) + return cameraParams def importMetadata(filePath): - myYamlFile = open(filePath) - parsedYamlFile = yaml.load(myYamlFile, Loader=yaml.FullLoader) - + with open(filePath) as myYamlFile: + parsedYamlFile = yaml.load(myYamlFile, Loader=yaml.FullLoader) + return parsedYamlFile def download_file(url, file_name): @@ -191,7 +190,8 @@ def _loadDepthFrames(metadata_path): raise ValueError("Unsupported depth frame byte count.") depth_path = os.path.join(os.path.dirname(metadata_path), meta.get("file", "depth.bin")) - blob = open(depth_path, "rb").read() + with open(depth_path, "rb") as f: + blob = f.read() if meta.get("frame_layout") == "length_prefixed": frames = [] @@ -499,9 +499,8 @@ def postCalibrationOptions(session_path,session_id,overwrite=False): if trial['meta'] is None or overwrite == True: calibOptionsJsonPath = os.path.join(session_path,'Videos','calibOptionSelections.json') - f = open(calibOptionsJsonPath) - calibOptionsJson = json.load(f) - f.close() + with open(calibOptionsJsonPath) as f: + calibOptionsJson = json.load(f) data = { "meta":json.dumps({'calibration':calibOptionsJson}) } @@ -1478,23 +1477,21 @@ def numpy2storage(labels, data, storage_file): assert data.shape[1] == len(labels), "# labels doesn't match columns" assert labels[0] == "time" - f = open(storage_file, 'w') - f.write('name %s\n' %storage_file) - f.write('datacolumns %d\n' %data.shape[1]) - f.write('datarows %d\n' %data.shape[0]) - f.write('range %f %f\n' %(np.min(data[:, 0]), np.max(data[:, 0]))) - f.write('endheader \n') - - for i in range(len(labels)): - f.write('%s\t' %labels[i]) - f.write('\n') - - for i in range(data.shape[0]): - for j in range(data.shape[1]): - f.write('%20.8f\t' %data[i, j]) + with open(storage_file, 'w') as f: + f.write('name %s\n' %storage_file) + f.write('datacolumns %d\n' %data.shape[1]) + f.write('datarows %d\n' %data.shape[0]) + f.write('range %f %f\n' %(np.min(data[:, 0]), np.max(data[:, 0]))) + f.write('endheader \n') + + for i in range(len(labels)): + f.write('%s\t' %labels[i]) f.write('\n') - - f.close() + + for i in range(data.shape[0]): + for j in range(data.shape[1]): + f.write('%20.8f\t' %data[i, j]) + f.write('\n') def lowpassFilter(inputData, filtFreq, order=4): @@ -1612,17 +1609,15 @@ def storage2numpy(storage_file, excess_header_entries=0): >>> data['ground_force_vy'] """ # What's the line number of the line containing 'endheader'? - f = open(storage_file, 'r') - - header_line = False - for i, line in enumerate(f): - if header_line: - column_names = line.split() - break - if line.count('endheader') != 0: - line_number_of_line_containing_endheader = i + 1 - header_line = True - f.close() + with open(storage_file, 'r') as f: + header_line = False + for i, line in enumerate(f): + if header_line: + column_names = line.split() + break + if line.count('endheader') != 0: + line_number_of_line_containing_endheader = i + 1 + header_line = True # With this information, go get the data. if excess_header_entries == 0: @@ -2121,4 +2116,3 @@ def makeRequestWithRetry(method, url, files=files) response.raise_for_status() return response -