def cos_angle(y_reco, y_true): zep, zet, azp, azt = y_reco[:, 1], y_true[:, 1], y_reco[:, 2], y_true[:, 2] # cosalpha=abs(sin(zep))*cos(azp)*sin(zet)*cos(azt)+abs(sin(zep))*sin(azp)*sin(zet)*sin(azt)+cos(zep)*cos(zet) cosalpha = abs(sin(zep)) * abs(sin(zet)) * cos(azp - azt) + cos(zep) * cos( zet) #check for double absolutes cosalpha -= tf.math.sign(cosalpha) * eps return cosalpha
def PDF_gen_Si_func(costk, costl , phi, Fl, S3, S4, S5, AFB, S7, S8, S9): ##sint2k: square of sin(thetak); sin2tk: sin(2thetak); sintk: sin(thetak); cost2k = costk * costk sint2k = 1 - cost2k sintk = sqrt(sint2k) sin2tk = 2 * sintk * costk cost2l = costl * costl sint2l = 1 - cost2l sintl = sqrt(sint2l) sin2tl = 2 * sintl * costl cos2tl = 2 * cost2l - 1 sinphi = sin(phi) cosphi = cos(phi) cos2phi = cos(2*phi) sin2phi = sin(2*phi) dcrate = (0.75 * (1 - Fl) * sint2k + Fl * cost2k + 0.25 * (1 - Fl) * sint2k * cos2tl - Fl * cost2k * cos2tl + S3 * sint2k * sint2l * cos2phi + S4 * sin2tk * sin2tl * cosphi + S5 * sin2tk * sintl * cosphi + 4/3 * AFB * sint2k * costl + S7 * sin2tk * sintl * sinphi + S8 * sin2tk * sin2tl * sinphi + S9 * sint2k * sint2l * sin2phi) return 9 / (32 * 3.14159265) * dcrate
def alpha_from_angle(y_reco, y_true): zep, zet, azp, azt = y_reco[:, 1], y_true[:, 1], y_reco[:, 2], y_true[:, 2] cosalpha = abs(sin(zep)) * cos(azp) * sin(zet) * cos(azt) + abs( sin(zep)) * sin(azp) * sin(zet) * sin(azt) + cos(zep) * cos(zet) cosalpha -= tf.math.sign(cosalpha) * eps alpha = acos(cosalpha) return alpha
def PDF_gen_Pi_func(costk, costl, phi, Fl, P1, P2, P3, P4p, P5p, P6p, P8p): ##sint2k: square of sin(thetak); sin2tk: sin(2thetak); sintk: sin(thetak); cost2k = costk * costk sint2k = 1 - cost2k sintk = sqrt(sint2k) sin2tk = 2 * sintk * costk cost2l = costl * costl sint2l = 1 - cost2l sintl = sqrt(sint2l) sin2tl = 2 * sintl * costl cos2tl = 2 * cost2l - 1 sinphi = sin(phi) cosphi = cos(phi) cos2phi = cos(2 * phi) sin2phi = sin(2 * phi) Ft = 1 - Fl dcrate = (0.75 * Ft * sint2k + Fl * cost2k + 0.25 * Ft * sint2k * cos2tl - Fl * cost2k * cos2tl + 0.5 * P1 * Ft * sint2k * sint2l * cos2phi + sqrt(Fl * Ft) * 0.5 * P4p * sin2tk * sin2tl * cosphi + sqrt(Fl * Ft) * P5p * sin2tk * sintl * cosphi - sqrt(Fl * Ft) * P6p * sin2tk * sintl * sinphi + 0.5 * sqrt(Fl * Ft) * P8p * sin2tk * sin2tl * sinphi + 2 * P2 * Ft * sint2k * costl - P3 * Ft * sint2k * sint2l * sin2phi) return 9 / (32 * 3.14159265) * dcrate
def sqr_vonMises23D_angle(y_reco, y_true, re=False): #energy loss_energy = reduce_mean( tf.math.squared_difference(y_reco[:, 0], y_true[:, 0])) #mae again polar_k = abs(y_reco[:, 3]) + eps zenth_k = abs(y_reco[:, 4]) + eps cos_azi = cos(subtract(y_true[:, 2], y_reco[:, 2])) cos_zenth = cos(subtract(y_true[:, 1], y_reco[:, 1])) lnI0_azi = polar_k + tf.math.log( 1 + tf.math.exp(-2 * polar_k) ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(polar_k)) + tf.math.log( 1 + 0.24273 * tf.square(polar_k)) - tf.math.log(1 + 0.43023 * tf.square(polar_k)) lnI0_zenth = zenth_k + tf.math.log( 1 + tf.math.exp(-2 * zenth_k) ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(zenth_k)) + tf.math.log( 1 + 0.24273 * tf.square(zenth_k)) - tf.math.log(1 + 0.43023 * tf.square(zenth_k)) llh_azi = polar_k * cos_azi - lnI0_azi llh_zenith = zenth_k * cos_zenth - lnI0_zenth loss_azi = reduce_mean(-llh_azi) loss_zenith = reduce_mean(-llh_zenith) kappa = tf.math.abs(y_reco[:, 5]) + eps cos_alpha = cos_angle(y_reco, y_true) # tf.debugging.assert_less_equal(tf.math.abs(cos_alpha), 1, message='cos_alpha problem', summarize=None, name=None) tf.debugging.assert_all_finite(tf.math.abs(cos_alpha), message='cos_alpha problem infinite/nan', name=None) nlogC = -tf.math.log(kappa) + kappa + tf.math.log(1 - tf.math.exp(-2 * kappa)) tf.debugging.assert_all_finite(nlogC, 'log kappa problem', name=None) loss_angle = tf.reduce_mean(-kappa * cos_alpha + nlogC) if not re: return loss_azi + loss_zenith + loss_energy + loss_angle if re: return float(loss_azi + loss_zenith + loss_energy + loss_angle), [ float(loss_energy), float(loss_zenith), float(loss_azi), float(loss_angle) ]
def loss_funcxpos2(y_reco, y_true, re=False): from tensorflow.math import sin, cos, acos, abs, reduce_mean, subtract, square # Energy loss loss_energy = reduce_mean(abs(subtract(y_reco[:,0], y_true[:,0]))) #this works well but could maybe be improved zeni = [cos(y_true[:,1]) - y_reco[:,1] , sin(y_true[:,1]) - y_reco[:,2]] azi = [cos(y_true[:,2]) - y_reco[:,3] , sin(y_true[:,2]) - y_reco[:,4]] loss_angle = reduce_mean(square(azi[0]))+reduce_mean(square(azi[1]))+reduce_mean(square(zeni[0]))+reduce_mean(square(zeni[1])) if not re: return loss_energy+loss_angle else: return float(loss_energy+loss_angle), [float(loss_energy), float(loss_angle)]
def integralPi_costk_costl(x, limits, norm_range, params, model): phi = x.unstack_x() Fl = params['Fl'] P1 = params['P1'] P3 = params['P3'] return 9. / (32 * 3.14159265) * ( 4.0 / 9.0 + (1 - Fl) / 18.0 * P1 * cos(2 * phi) * 2 * 2 + (Fl - 1) / 9.0 * P3 * sin(2 * phi) * 2 * 2) * (2 * 2)
def integralPi_costl(x, limits, norm_range, params, model): costk, phi = x.unstack_x() Fl = params['Fl'] P1 = params['P1'] P3 = params['P3'] P5p = params['P5p'] P6p = params['P6p'] K00 = assoc_legendre_tf(0, 0, costk) K20 = assoc_legendre_tf(2, 0, costk) K21 = assoc_legendre_tf(2, 1, costk) K22 = assoc_legendre_tf(2, 2, costk) return 9. / (32 * 3.14159265) * ( (4.0 / 9.0) * K00 + (4.0 * Fl / 3.0 - 4.0 / 9.0) * K20 + (1 - Fl) / 18.0 * P1 * K22 * cos(2 * phi) * 2 + (Fl - 1) / 9.0 * P3 * K22 * sin(2 * phi) * 2 + (2.0 / 3.0) * sqrt(Fl - Fl * Fl) * P5p * K21 * cos(phi) * pi / 4 + (-2.0 / 3.0) * sqrt(Fl - Fl * Fl) * P6p * K21 * sin(phi) * pi / 4) * ( 2)
def abs_vM2D_KDE_weak(y_reco, y_true, kdet, re=False): #energy loss_energy = reduce_mean(abs(subtract(y_reco[:, 0], y_true[:, 0]))) #mae again polar_k = abs(y_reco[:, 3]) + eps zenth_k = abs(y_reco[:, 4]) + eps cos_azi = cos(subtract(y_true[:, 2], y_reco[:, 2])) cos_zenth = cos(subtract(y_true[:, 1], y_reco[:, 1])) lnI0_azi = polar_k + tf.math.log( 1 + tf.math.exp(-2 * polar_k) ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(polar_k)) + tf.math.log( 1 + 0.24273 * tf.square(polar_k)) - tf.math.log(1 + 0.43023 * tf.square(polar_k)) lnI0_zenth = zenth_k + tf.math.log( 1 + tf.math.exp(-2 * zenth_k) ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(zenth_k)) + tf.math.log( 1 + 0.24273 * tf.square(zenth_k)) - tf.math.log(1 + 0.43023 * tf.square(zenth_k)) llh_azi = polar_k * cos_azi - lnI0_azi llh_zenith = zenth_k * cos_zenth - lnI0_zenth loss_azi = reduce_mean(-llh_azi) loss_zenith = reduce_mean(-llh_zenith) kder = kde(tf.cast(y_reco[:, 1], tf.float32)) kdeloss = tf.reduce_mean( tf.math.abs(kdet.log_prob(xkde) - kder.log_prob(xkde))) / 10 if not re: return loss_azi + loss_zenith + loss_energy + tf.cast( kdeloss, tf.float32) if re: return float(loss_azi + loss_zenith + loss_energy + kdeloss), [ float(loss_energy), float(loss_zenith), float(loss_azi), float(kdeloss) ]
def angle_loss_fn_batch(y_true, y_pred): x_p = math.sin(y_pred[:, 0]) * math.cos(y_pred[:, 1]) y_p = math.sin(y_pred[:, 0]) * math.sin(y_pred[:, 1]) z_p = math.cos(y_pred[:, 0]) x_t = math.sin(y_true[:, 0]) * math.cos(y_true[:, 1]) y_t = math.sin(y_true[:, 0]) * math.sin(y_true[:, 1]) z_t = math.cos(y_true[:, 0]) norm_p = math.sqrt(x_p * x_p + y_p * y_p + z_p * z_p) norm_t = math.sqrt(x_t * x_t + y_t * y_t + z_t * z_t) dot_pt = x_p * x_t + y_p * y_t + z_p * z_t angle_value = dot_pt / (norm_p * norm_t) angle_value = tf.clip_by_value(angle_value, -0.99999, 0.99999) loss_val = (math.acos(angle_value)) return loss_val
def _convert_to_pxyz(self, x): from numpy import cos, sin, sinh, exp, clip, stack pt = exp(clip(x[:, 0], -7., 7.)) - 0.1 pt *= self._pt_scale eta = x[:, 1] phi = x[:, 2] px = pt * cos(phi) py = pt * sin(phi) pz = pt * sinh(clip(eta, -5, 5)) return stack([px, py, pz], axis=1)
def _convert_to_pxyz(self, x): from tensorflow.math import cos, sin, sinh, exp from tensorflow import clip_by_value, stack pt = exp(clip_by_value(x[:, 0], -7., 7.)) - 0.1 pt *= self._pt_scale eta = x[:, 1] phi = x[:, 2] px = pt * cos(phi) py = pt * sin(phi) pz = pt * sinh(clip_by_value(eta, -5, 5)) return stack([px, py, pz], axis=1)
def angle_loss_fn(self, y_true, y_pred): x_p = math.sin(y_pred[:, 0]) * math.cos(y_pred[:, 1]) y_p = math.sin(y_pred[:, 0]) * math.sin(y_pred[:, 1]) z_p = math.cos(y_pred[:, 0]) x_t = math.sin(y_true[:, 0]) * math.cos(y_true[:, 1]) y_t = math.sin(y_true[:, 0]) * math.sin(y_true[:, 1]) z_t = math.cos(y_true[:, 0]) norm_p = math.sqrt(x_p * x_p + y_p * y_p + z_p * z_p) norm_t = math.sqrt(x_t * x_t + y_t * y_t + z_t * z_t) dot_pt = x_p * x_t + y_p * y_t + z_p * z_t angle_value = dot_pt / (norm_p * norm_t) angle_value = tf.clip_by_value(angle_value, -0.99999, 0.99999) loss_val = (math.acos(angle_value)) # tf.debugging.check_numerics( # loss_val, "Vse propalo", name=None # ) # print(loss_val.shape) return loss_val
def sqr_vonMises2D_angle(y_reco, y_true, re=False): #energy loss_energy = reduce_mean( tf.math.squared_difference(y_reco[:, 0], y_true[:, 0])) #mae again polar_k = abs(y_reco[:, 3]) + eps zenth_k = abs(y_reco[:, 4]) + eps cos_azi = cos(subtract(y_true[:, 2], y_reco[:, 2])) cos_zenth = cos(subtract(y_true[:, 1], y_reco[:, 1])) lnI0_azi = polar_k + tf.math.log( 1 + tf.math.exp(-2 * polar_k) ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(polar_k)) + tf.math.log( 1 + 0.24273 * tf.square(polar_k)) - tf.math.log(1 + 0.43023 * tf.square(polar_k)) lnI0_zenth = zenth_k + tf.math.log( 1 + tf.math.exp(-2 * zenth_k) ) - 0.25 * tf.math.log(1 + 0.25 * tf.square(zenth_k)) + tf.math.log( 1 + 0.24273 * tf.square(zenth_k)) - tf.math.log(1 + 0.43023 * tf.square(zenth_k)) llh_azi = polar_k * cos_azi - lnI0_azi llh_zenith = zenth_k * cos_zenth - lnI0_zenth loss_azi = reduce_mean(-llh_azi) loss_zenith = reduce_mean(-llh_zenith) if not re: return loss_azi + loss_zenith + loss_energy if re: return float(loss_azi + loss_zenith + loss_energy), [ float(loss_energy), float(loss_zenith), float(loss_azi) ]
def integralPi_costk(x, limits, norm_range, params, model): costl, phi = x.unstack_x() Fl = params['Fl'] P2 = params['P2'] P1 = params['P1'] P3 = params['P3'] L00 = assoc_legendre_tf(0, 0, costl) L10 = assoc_legendre_tf(1, 0, costl) L20 = assoc_legendre_tf(2, 0, costl) L22 = assoc_legendre_tf(2, 2, costl) return 9. / (32 * 3.14159265) * ( (4.0 / 9.0) * L00 + (2.0 / 9.0 - 2.0 * Fl / 3.0) * L20 + (4.0 / 3.0) * P2 * (1 - Fl) * L10 + (1 - Fl) / 18.0 * P1 * L22 * cos(2 * phi) * 2 + (Fl - 1) / 9.0 * P3 * L22 * sin(2 * phi) * 2) * (2)
def rotate_tensor_by_angle_xyz(input_data, graph, angle_x=0, angle_y=0, angle_z=0): """ Rotate the point cloud along up direction with certain angle. Rotate in the order of x, y and then z. Input: input_data: Nx3 tensor, original batch of point clouds angle_x: tf constant angle_y: tf constant angle_z: tf constant Return: Nx3 tensor, rotated batch of point clouds """ with graph.as_default(): # print('angle_x', angle_x) if angle_x == 0: angle_x = tflt(0) if angle_y == 0: angle_y = tflt(0) if angle_z == 0: angle_z = tflt(0) input_data = tf.cast(input_data, tf.float32) rotation_matrix1 = tf.stack([1, 0, 0, 0, tm.cos(angle_x), -tm.sin(angle_x), 0, tm.sin(angle_x), tm.cos(angle_x)]) rotation_matrix1 = tf.reshape(rotation_matrix1, (3,3)) rotation_matrix1 = tf.cast(rotation_matrix1, tf.float32) shape_pc = tf.matmul(input_data, rotation_matrix1) rotation_matrix2 = tf.stack([tm.cos(angle_y), 0, tm.sin(angle_y), 0, 1, 0, -tm.sin(angle_y), 0, tm.cos(angle_y)]) rotation_matrix2 = tf.reshape(rotation_matrix2, (3,3)) rotation_matrix2 = tf.cast(rotation_matrix2, tf.float32) shape_pc = tf.matmul(shape_pc, rotation_matrix2) rotation_matrix3 = tf.stack([tm.cos(angle_z), -tm.sin(angle_z), 0, tm.sin(angle_z), tm.cos(angle_z), 0, 0, 0, 1]) rotation_matrix3 = tf.reshape(rotation_matrix3, (3,3)) rotation_matrix3 = tf.cast(rotation_matrix3, tf.float32) shape_pc = tf.matmul(shape_pc, rotation_matrix3) return shape_pc
def integralSi_costk(x, limits, norm_range, params, model): costl, phi = x.unstack_x() Fl = params['Fl'] S3 = params['S3'] S9 = params['S9'] AFB = params['AFB'] L00 = assoc_legendre_tf(0,0,costl) L10 = assoc_legendre_tf(1,0,costl) L20 = assoc_legendre_tf(2,0,costl) L22 = assoc_legendre_tf(2,2,costl) return 9./(32 * 3.14159265) * ( (4.0/9.0)*L00 + (2.0/9.0-2.0*Fl/3.0)*L20 + (4.0/3.0)*(3.0/2.0)*AFB*L10 + 1/18.0*2*S3*L22*cos(2*phi)*2 + 1/9.0*S9*L22*sin(2*phi)*2) * (2)
def integralSi_costl(x, limits, norm_range, params, model): costk, phi = x.unstack_x() Fl = params['Fl'] S3 = params['S3'] S9 = params['S9'] S5 = params['S5'] S7 = params['S7'] K00 = assoc_legendre_tf(0,0,costk) K20 = assoc_legendre_tf(2,0,costk) K21 = assoc_legendre_tf(2,1,costk) K22 = assoc_legendre_tf(2,2,costk) return 9./(32 * 3.14159265) * ( (4.0/9.0)*K00 + (4.0*Fl/3.0-4.0/9.0)*K20 + 1/18.0*2*S3*K22*cos(2*phi)*2 + 1/9.0*S9*K22*sin(2*phi)*2 + (2.0/3.0)*S5*K21*cos(phi)*pi/4 + (-2.0/3.0)*S7*K21*sin(phi)*pi/4 ) * (2)
def integralSi_costk_costl(x, limits, norm_range , params , model): phi = x.unstack_x() S3 = params['S3'] S9 = params['S9'] return 9./(32 * 3.14159265) * ( 4.0/9.0 + 1/18.0*2*S3*cos(2*phi)*2*2 + 1/9.0*S9*sin(2*phi)*2*2 ) * (2 * 2)