def raw2ML(cfg, typ, hyper, x=None, z=None, inverse=False):
    """
    transform raw values to ML-friendly values
    inverse == False: raw --> ML
    inverse == True: ML --> raw
    """
    def normalize(x, mu, std, inverse):
        if inverse:  # ML --> raw
            x = std * x + mu
        else:  # raw --> ML
            x = (x - mu) / std
        return x

    if x is not None:
        if x.dim() != 3:  # [b,*,n_x]
            raise ValueError(x.shape)
        if typ == "setup":
            x[:, :, 0] = normalize(x[:, :, 0], hyper["mu"]["N"], hyper["std"]["N"], inverse)
            x[:, :, 1] = normalize(x[:, :, 1], hyper["mu"]["mass_agg"], hyper["std"]["mass_agg"], inverse)
            x[:, :, 2] = normalize(x[:, :, 2], hyper["mu"]["gamma"], hyper["std"]["gamma"], inverse)
            x[:, :, 3:3+4] = normalize(x[:, :, 3:3+4], hyper["mu"]["fraction_mat"], hyper["std"]["fraction_mat"], inverse)
            for dim in range(3):  # sx,sy,sz
                x[:, :, dim + 7] = normalize(x[:, :, dim + 7], hyper["mu"]["spin"], hyper["std"]["spin"], inverse)  # proj
                x[:, :, dim + 10] = normalize(x[:, :, dim + 10], hyper["mu"]["spin"], hyper["std"]["spin"], inverse)  # targ
            for dim in range(6):  # x,y,z,vx,vy,vz
                x[:, :, dim + 13] = normalize(x[:, :, dim + 13], hyper["mu"]["pos_vel"][dim], hyper["std"]["pos_vel"][dim], inverse)  # proj
                x[:, :, dim + 19] = normalize(x[:, :, dim + 19], hyper["mu"]["pos_vel"][dim], hyper["std"]["pos_vel"][dim], inverse)  # targ
        elif typ == "sph":
            raise NotImplementedError()

    if z is not None:
        if z.dim() != 3:  # [t,*,n_z]
            raise ValueError(z.shape)
        if typ == "agg":
            z[:, :, 0] = normalize(z[:, :, 0], hyper["mu"]["mass_agg"], hyper["std"]["mass_agg"], inverse)
            z[:, :, 3] = normalize(z[:, :, 3], hyper["mu"]["mass_agg"], hyper["std"]["mass_agg"], inverse)
            z[:, :, 6] = normalize(z[:, :, 6], hyper["mu"]["mass_agg"], hyper["std"]["mass_agg"], inverse)
            z[:, :, 1] = normalize(z[:, :, 1], hyper["mu"]["fraction_mat"], hyper["std"]["fraction_mat"], inverse)
            z[:, :, 2] = normalize(z[:, :, 2], hyper["mu"]["fraction_mat"], hyper["std"]["fraction_mat"], inverse)
            z[:, :, 4] = normalize(z[:, :, 4], hyper["mu"]["fraction_mat"], hyper["std"]["fraction_mat"], inverse)
            z[:, :, 5] = normalize(z[:, :, 5], hyper["mu"]["fraction_mat"], hyper["std"]["fraction_mat"], inverse)
            z[:, :, 7] = normalize(z[:, :, 7], hyper["mu"]["fraction_mat"], hyper["std"]["fraction_mat"], inverse)
            z[:, :, 8] = normalize(z[:, :, 8], hyper["mu"]["fraction_mat"], hyper["std"]["fraction_mat"], inverse)
            for dim in range(6):  # x,y,z,vx,vy,vz
                z[:, :, dim + 9] = normalize(z[:, :, dim + 9], hyper["mu"]["pos_vel"][dim], hyper["std"]["pos_vel"][dim], inverse)  # largest
                z[:, :, dim + 15] = normalize(z[:, :, dim + 15], hyper["mu"]["pos_vel"][dim], hyper["std"]["pos_vel"][dim], inverse)  # 2nd largest
                z[:, :, dim + 21] = normalize(z[:, :, dim + 21], hyper["mu"]["pos_vel"][dim], hyper["std"]["pos_vel"][dim], inverse)  # rest
        else:
            raise NotImplementedError()
        
    return x, z