Coverage for src/galsbi/sham_ot/utils.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-09 18:05 +0000

1# Copyright (C) 2025 ETH Zurich 

2# Institute for Particle Physics and Astrophysics 

3# Author: Silvan Fischbacher 

4# created: Thu Mar 06 2025 

5 

6import re 

7 

8import numpy as np 

9from scipy.special import erf 

10 

11 

12def determine_skyarea_of_pinocchio_sim(pinocchio_output_filename): 

13 """ 

14 Determines the sky area of a Pinocchio simulation from the output log file. 

15 

16 :param pinocchio_output_filename: Filename of the Pinocchio output log file 

17 :return: Sky area in square radians 

18 """ 

19 with open(pinocchio_output_filename) as f: 

20 text = f.read() 

21 

22 min_aperture_search = re.search(r"PLCApertureMin\s+(.+)\n", text) 

23 if min_aperture_search is not None: 

24 min_aperture = float(min_aperture_search.groups()[0]) 

25 else: 

26 min_aperture = 0 

27 

28 max_aperture = float(re.search(r"PLCAperture\s+(.+)\n", text).groups()[0]) 

29 

30 skyarea = opening_to_skyarea(max_aperture) - opening_to_skyarea(min_aperture) 

31 

32 return skyarea 

33 

34 

35def opening_to_skyarea(opening_degrees): 

36 """ 

37 For a given lightcone opening angle in degrees, 

38 return the corresponding sky area in radians^2. 

39 Opening = 90 degrees corresponds to half-sky, i.e. area = 2 * np.pi. 

40 Opening = 180 degrees corresponds to full-sky, i.e. area = 4 * np.pi. 

41 

42 :param opening_degrees: Opening angle in degrees 

43 :return: Sky area in square radians 

44 """ 

45 return ((1 - np.cos(np.radians(opening_degrees))) / 2) * 4 * np.pi 

46 

47 

48def adapted_err_function(x, mu, sigma, min_val=-1, max_val=1): 

49 """ 

50 Adapted error function that adapts mean, width, and limits of the classical error 

51 function. 

52 

53 The error function erf(x/sqrt(2)) is the probability that a normally distributed 

54 random variable with mean 0 and standard deviation 1 is less than x. 

55 The adapted error function has additinional parameters mu and sigma to adapt the 

56 mean and standard deviation of the distribution. 

57 

58 The range of the error function is normally [-1, 1], but can be adapted to a 

59 different range by setting min_val and max_val. If min_val and max_val are not set, 

60 the range is [-1, 1]. 

61 

62 :param x: input value 

63 :param mu: x coordinate of the center of the error function 

64 :param sigma: width of the error function 

65 :param min_val: minimum value of the error function 

66 :param max_val: maximum value of the error function 

67 :return: adapted error function value 

68 """ 

69 if sigma <= 0: 

70 raise ValueError("Sigma must be positive.") 

71 

72 # Calculate the standard normal variable 

73 z = (x - mu) / (sigma * np.sqrt(2)) 

74 

75 # Calculate the classical error function 

76 erf_value = erf(z) 

77 

78 # Scale and shift to the desired range 

79 return min_val + (max_val - min_val) * (erf_value + 1) / 2