Source code for refann.data_process

# -*- coding: utf-8 -*-

import numpy as np
import torch


[docs]def numpy2torch(data): dtype = torch.FloatTensor data = torch.from_numpy(data).type(dtype) return data
[docs]def numpy2cuda(data, device=None): if device is None: dtype = torch.cuda.FloatTensor data = torch.from_numpy(data).type(dtype) else: data = numpy2torch(data) data = torch2cuda(data, device=device) return data
[docs]def torch2cuda(data, device=None): return data.cuda(device=device)
[docs]def torch2numpy(data): return data.numpy()
[docs]def cuda2torch(data): return data.cpu()
[docs]def cuda2numpy(data): return data.cpu().numpy()
[docs]class Normalize(object): """ Normalize data. """ def __init__(self, x, statistic={}, norm_type='z_score'): self.x = x self.stati = statistic self.norm_type = norm_type
[docs] def minmax(self): """min-max normalization Rescaling the range of features to scale the range in [0, 1] or [a,b] https://en.wikipedia.org/wiki/Feature_scaling """ return (self.x-self.stati['min'])/(self.stati['max']-self.stati['min'])
[docs] def mean(self): """ mean normalization """ return (self.x-self.stati['mean'])/(self.stati['max']-self.stati['min'])
[docs] def z_score(self): """ standardization/z-score/zero-mean normalization """ return (self.x-self.stati['mean'])/self.stati['std']
[docs] def norm(self): return eval('self.%s()'%self.norm_type)
[docs]class InverseNormalize(object): """ Inverse transformation of class :class:`~Normalize`. """ def __init__(self, x1, statistic={}, norm_type='z_score'): self.x = x1 self.stati = statistic self.norm_type = norm_type
[docs] def minmax(self): return self.x * (self.stati['max']-self.stati['min']) + self.stati['min']
[docs] def mean(self): return self.x * (self.stati['max']-self.stati['min']) + self.stati['mean']
[docs] def z_score(self): return self.x * self.stati['std'] + self.stati['mean']
[docs] def inverseNorm(self): return eval('self.%s()'%self.norm_type)
[docs]class Statistic(object): """ Statistics of an array. """ def __init__(self, x): self.x = x @property def mean(self): return np.mean(self.x) @property def xmin(self): return np.min(self.x) @property def xmax(self): return np.max(self.x) @property def std(self): return np.std(self.x)
[docs] def statistic(self): st = {'min' : float(self.xmin), 'max' : float(self.xmax), 'mean': float(self.mean), 'std' : float(self.std), } return st