Skip to content

Commit 29043bb

Browse files
refactor: engineering-forward README, package structure, CI/CD, tests
1 parent 3a4767f commit 29043bb

14 files changed

Lines changed: 888 additions & 131 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
concurrency:
10+
group: ci-${{ github.ref }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
test:
15+
runs-on: ubuntu-latest
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
python-version: ["3.10", "3.11", "3.12"]
20+
21+
steps:
22+
- uses: actions/checkout@v4
23+
24+
- name: Set up Python ${{ matrix.python-version }}
25+
uses: actions/setup-python@v5
26+
with:
27+
python-version: ${{ matrix.python-version }}
28+
cache: 'pip'
29+
30+
- name: Install dependencies
31+
run: |
32+
python -m pip install --upgrade pip
33+
pip install -e ".[dev]"
34+
35+
- name: Lint with ruff
36+
run: ruff check cosmic_string_signals/ --output-format=github
37+
38+
- name: Run tests
39+
run: python -m pytest tests/ -v

‎.gitignore‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.egg-info/
6+
*.egg
7+
dist/
8+
build/
9+
*.whl
10+
11+
# Testing
12+
.pytest_cache/
13+
.coverage
14+
htmlcov/
15+
16+
# IDE
17+
.vscode/
18+
.idea/
19+
*.swp
20+
*.swo
21+
22+
# OS
23+
.DS_Store
24+
Thumbs.db

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Jeffrey Morais
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md‎

Lines changed: 176 additions & 131 deletions
Large diffs are not rendered by default.

‎cosmic_string_signals/__init__.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""Cosmic String Signals: synthetic 3D signal detection in noisy fields."""
2+
3+
__version__ = "0.1.0"

‎cosmic_string_signals/filtering.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Matched filtering and signal extraction utilities."""
2+
3+
import numpy as np
4+
from scipy import signal
5+
6+
7+
def unfold(grid: np.ndarray, orientation: str = "horizontal") -> list[float]:
8+
"""Unfold a 2D array into a 1D sequence for correlation analysis.
9+
10+
Parameters
11+
----------
12+
grid : np.ndarray
13+
2D array to unfold.
14+
orientation : str
15+
Either 'horizontal' (row-major) or 'vertical' (column-major).
16+
17+
Returns
18+
-------
19+
list of float
20+
Flattened values in the specified traversal order.
21+
"""
22+
copy = np.copy(grid)
23+
if orientation == "vertical":
24+
copy = copy.T
25+
return copy.ravel().tolist()
26+
27+
28+
def matched_filter_1d(template: np.ndarray, data: np.ndarray) -> np.ndarray:
29+
"""Compute 1D cross-correlation between a template and data signal.
30+
31+
Parameters
32+
----------
33+
template : np.ndarray
34+
1D reference signal (the expected wake profile).
35+
data : np.ndarray
36+
1D observed signal (may contain noise).
37+
38+
Returns
39+
-------
40+
np.ndarray
41+
Full cross-correlation output.
42+
"""
43+
return np.correlate(template, data, mode="full")
44+
45+
46+
def matched_filter_2d(template: np.ndarray, data: np.ndarray) -> np.ndarray:
47+
"""Compute 2D convolution between a template and data map.
48+
49+
Parameters
50+
----------
51+
template : np.ndarray
52+
2D reference map.
53+
data : np.ndarray
54+
2D observed map.
55+
56+
Returns
57+
-------
58+
np.ndarray
59+
Full 2D convolution output.
60+
"""
61+
return signal.convolve2d(template, data, mode="full")

‎cosmic_string_signals/geometry.py‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"""Wake geometry construction and SO(3) coordinate transforms."""
2+
3+
import numpy as np
4+
from scipy.spatial import ConvexHull
5+
6+
7+
def rotation_x(theta: float) -> np.ndarray:
8+
"""Rotation matrix about the x-axis by angle theta."""
9+
return np.array([
10+
[1, 0, 0],
11+
[0, np.cos(theta), -np.sin(theta)],
12+
[0, np.sin(theta), np.cos(theta)],
13+
])
14+
15+
16+
def rotation_y(theta: float) -> np.ndarray:
17+
"""Rotation matrix about the y-axis by angle theta."""
18+
return np.array([
19+
[np.cos(theta), 0, np.sin(theta)],
20+
[0, 1, 0],
21+
[-np.sin(theta), 0, np.cos(theta)],
22+
])
23+
24+
25+
def rotation_z(theta: float) -> np.ndarray:
26+
"""Rotation matrix about the z-axis by angle theta."""
27+
return np.array([
28+
[np.cos(theta), -np.sin(theta), 0],
29+
[np.sin(theta), np.cos(theta), 0],
30+
[0, 0, 1],
31+
])
32+
33+
34+
def apply_rotation(wedge: np.ndarray, angles: tuple[float, float, float]) -> np.ndarray:
35+
"""Apply SO(3) rotation (Euler angles) to a set of 3D points.
36+
37+
Parameters
38+
----------
39+
wedge : np.ndarray
40+
Array of shape (N, 3) representing points in R^3.
41+
angles : tuple of float
42+
Euler angles (alpha, beta, gamma) for rotations about x, y, z axes.
43+
44+
Returns
45+
-------
46+
np.ndarray
47+
Rotated points with the same shape as input.
48+
"""
49+
center = np.mean(wedge, axis=0)
50+
shifted = wedge - center
51+
rotated = shifted @ rotation_x(angles[0]).T
52+
rotated = rotated @ rotation_y(angles[1]).T
53+
rotated = rotated @ rotation_z(angles[2]).T
54+
return rotated + center
55+
56+
57+
def build_wake_wedge(
58+
tip: np.ndarray,
59+
deficit_angle: float,
60+
wake_depth: float,
61+
wake_length: float,
62+
) -> np.ndarray:
63+
"""Construct a 6-vertex wake wedge geometry in physical space.
64+
65+
Parameters
66+
----------
67+
tip : np.ndarray
68+
3D coordinates of the wake tip point.
69+
deficit_angle : float
70+
Opening angle of the wake wedge.
71+
wake_depth : float
72+
Radial depth of the wake in Mpc.
73+
wake_length : float
74+
Length of the wake along the string direction in Mpc.
75+
76+
Returns
77+
-------
78+
np.ndarray
79+
Array of shape (6, 3) defining the wake wedge vertices.
80+
"""
81+
end_points = np.array([
82+
[tip[0] + wake_depth * np.cos(deficit_angle / 2),
83+
tip[1] + wake_depth * np.sin(deficit_angle / 2),
84+
tip[2]],
85+
[tip[0] + wake_depth * np.cos(deficit_angle / 2),
86+
tip[1] - wake_depth * np.sin(deficit_angle / 2),
87+
tip[2]],
88+
])
89+
projected = np.array([
90+
tip + [0, 0, wake_length],
91+
end_points[0] + [0, 0, wake_length],
92+
end_points[1] + [0, 0, wake_length],
93+
])
94+
return np.array([tip, end_points[0], end_points[1],
95+
projected[0], projected[1], projected[2]])
96+
97+
98+
def point_in_hull(hull_points: np.ndarray, test_points: np.ndarray) -> bool:
99+
"""Check whether test points lie inside the convex hull of hull_points.
100+
101+
Uses the vertex-stability method: if adding the test points does not
102+
change the hull vertices, all test points are interior.
103+
104+
Parameters
105+
----------
106+
hull_points : np.ndarray
107+
Array of shape (M, 3) defining the convex hull.
108+
test_points : np.ndarray
109+
Array of shape (K, 3) of points to test.
110+
111+
Returns
112+
-------
113+
bool
114+
True if all test points are inside the hull.
115+
"""
116+
hull = ConvexHull(hull_points)
117+
combined = np.append(hull_points, test_points, axis=0)
118+
new_hull = ConvexHull(combined)
119+
return list(hull.vertices) == list(new_hull.vertices)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""Brightness temperature models for cosmic string wake signals."""
2+
3+
import numpy as np
4+
5+
6+
def photon_cmb_temperature(z: float) -> float:
7+
"""CMB photon temperature at redshift z.
8+
9+
Parameters
10+
----------
11+
z : float
12+
Cosmological redshift.
13+
14+
Returns
15+
-------
16+
float
17+
Temperature in Kelvin.
18+
"""
19+
return 2.72548 * (1 + z)
20+
21+
22+
def kinetic_temperature(z: float, g_mu: float, gamma_v: float) -> float:
23+
"""Kinetic temperature inside the wake at redshift z.
24+
25+
Parameters
26+
----------
27+
z : float
28+
Cosmological redshift.
29+
g_mu : float
30+
Dimensionless string tension.
31+
gamma_v : float
32+
Product of Lorentz factor and string speed (gamma * v_s).
33+
34+
Returns
35+
-------
36+
float
37+
Kinetic temperature in Kelvin.
38+
"""
39+
return 20 * (g_mu * 1e6) ** 2 * gamma_v ** 2 * (1100 + 1) / (z + 1)
40+
41+
42+
def gas_temperature(z: float) -> float:
43+
"""Gas temperature scaling with redshift.
44+
45+
Parameters
46+
----------
47+
z : float
48+
Cosmological redshift.
49+
50+
Returns
51+
-------
52+
float
53+
Gas temperature in Kelvin.
54+
"""
55+
return 0.02 * (z + 1) ** 2
56+
57+
58+
def brightness_temperature(z: float, g_mu: float, gamma_v: float) -> float:
59+
"""21cm brightness temperature fluctuation at redshift z.
60+
61+
Computes the differential brightness temperature induced by a cosmic
62+
string wake, following the model in Brandenberger et al.
63+
64+
Parameters
65+
----------
66+
z : float
67+
Cosmological redshift.
68+
g_mu : float
69+
Dimensionless string tension.
70+
gamma_v : float
71+
Product of Lorentz factor and string speed.
72+
73+
Returns
74+
-------
75+
float
76+
Brightness temperature fluctuation in Kelvin.
77+
"""
78+
deexcitation_cross_section = 0.16
79+
t_kin = kinetic_temperature(z, g_mu, gamma_v)
80+
t_gas = gas_temperature(z)
81+
82+
u = t_kin if t_kin > 3 * t_gas else 3 * t_gas
83+
c = 4 if t_kin > 3 * t_gas else 1 + t_kin / t_gas
84+
85+
t_gamma = photon_cmb_temperature(z)
86+
result = (
87+
0.017
88+
* deexcitation_cross_section
89+
/ (1 + deexcitation_cross_section)
90+
* (1 - t_gamma / u)
91+
* np.sqrt(1 + z)
92+
* c
93+
)
94+
return result

0 commit comments

Comments
 (0)