Skip to content

Instantly share code, notes, and snippets.

@joefutrelle
Last active December 12, 2019 17:34
Show Gist options
  • Save joefutrelle/275f90133e29e8d6723a44773377455f to your computer and use it in GitHub Desktop.
Save joefutrelle/275f90133e29e8d6723a44773377455f to your computer and use it in GitHub Desktop.
save/load IFCB class scores
import h5py as h5
import numpy as np
from scipy.io import savemat
def save_class_scores(path, bin_id, scores, roi_numbers, class_labels):
assert scores.shape[0] == len(roi_numbers), 'wrong number of ROI numbers'
assert scores.shape[1] == len(class_labels), 'wrong number of class labels'
with h5.File(path,'w') as f:
ds = f.create_dataset('scores', data=scores, compression='gzip', dtype='f4')
ds.attrs['bin_id'] = bin_id
ds.attrs['class_labels'] = [l.encode('ascii') for l in class_labels]
f.create_dataset('roi_numbers', data=roi_numbers, compression='gzip', dtype='i2')
def save_class_scores_v1(out_path, bin_id, scores, roi_numbers, class_labels):
d = {
'version': 2,
'roinum': roi_numbers,
'TBscores': scores.astype('f4'),
'class2useTB': np.asarray(class_labels + ['unclassified'], dtype='object')
}
savemat(out_path, d, do_compression=True)
def load_class_scores(path):
with h5.File(path) as f:
ds = f['scores']
scores = ds[:]
bin_id = ds.attrs['bin_id']
class_labels = [l.decode('ascii') for l in ds.attrs['class_labels']]
roi_numbers = f['roi_numbers'][:]
return bin_id, scores, roi_numbers, class_labels
function [bin_id, scores, roi_numbers, class_labels] = load_class_scores(path)
scores = h5read(path,'/scores')';
bin_id = h5readatt(path,'/scores','bin_id');
roi_numbers = h5read(path,'/roi_numbers');
class_labels = h5readatt(path,'/scores','class_labels');
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment