Skip to content

Commit

Permalink
Moved the covariance bookkeeping functions into the slics module
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Tersenov committed Sep 26, 2023
1 parent 7c153a2 commit cc764e9
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 125 deletions.
3 changes: 0 additions & 3 deletions example/cosmoSLICS_bookkeeping.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@
"metadata": {},
"outputs": [],
"source": [
"import sp_peaks\n",
"from sp_peaks import slics\n",
"\n",
"# Read the file paths from master_file.txt\n",
"filename = \".././input/master_file.txt\"\n",
"with open(filename, 'r') as file:\n",
Expand Down
149 changes: 28 additions & 121 deletions example/covariance_bookkeeping.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import os\n",
"import random\n",
"from sp_peaks import slics\n",
"from sp_peaks import mapping\n",
"from sp_peaks import summary_statistics"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -9,12 +23,10 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"# Define the root directory\n",
"root_directory = \"/n17data/tersenov/Cov_DES_SLICS\"\n",
"\n",
Expand All @@ -34,118 +46,48 @@
" master_file.write(file_path + \"\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def parse_cov_SLICS_filenames(file_paths):\n",
" # Make empty recarray to store the data\n",
" data = np.recarray(len(file_paths), dtype=[('bin', int), ('LOS', int), ('tile', int)])\n",
"\n",
" # Iterate over the file paths and process each file\n",
" for i, file_path in enumerate(file_paths):\n",
" # Extract the file name from the file path\n",
" file_name = file_path.split(\"/\")[-1]\n",
" \n",
" # Split file name into parts\n",
" file_parts = file_name.split(\"_\")\n",
" \n",
" # Extract relevant information\n",
" bin = int(file_parts[4][3:]) # Extract the number after \"Bin\"\n",
" LOS = int(file_parts[5][3:]) # Extract the number after \"LOS\"\n",
" tile = int(file_parts[6][1:-4]) # Extract the number after \"R\"\n",
"\n",
" # Assign the extracted data to the corresponding fields in the recarray\n",
" data[i]['bin'] = bin\n",
" data[i]['LOS'] = LOS\n",
" data[i]['tile'] = tile\n",
"\n",
" return data"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Define the path to the \"master_file_cov.txt\"\n",
"master_file_path = \".././input/master_file_cov.txt\"\n",
"\n",
"# Read the file paths from the \"master_file_cov.txt\"\n",
"with open(master_file_path, \"r\") as file:\n",
" file_paths = file.readlines()\n",
" file_paths = [path.strip() for path in file_paths]\n",
"\n",
"# Parse these file paths\n",
"parsed_cov_data = parse_cov_SLICS_filenames(file_paths)"
"master_file_path = \".././input/master_file_cov.txt\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Reconstruct 124 realisations of the survey by picking each tile from a random LOS."
"Parse the filenames"
]
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"los_numbers = np.unique(parsed_cov_data['LOS']) # List of all LOS numbers\n",
"num_realizations = 124 # Number of realizations\n",
"num_tiles_per_realization = 19 # Number of tiles to select for each realization\n",
"\n",
"num_bins = 4\n",
"bin_number = 2\n",
"\n",
"# Create an empty list to store the collections of selected files for this bin\n",
"collections_of_files = []\n",
"\n",
"# Iterate through realizations\n",
"for realization in range(num_realizations):\n",
" # Create an empty list to store the selected files for this realization\n",
" selected_tiles = []\n",
"\n",
" # Create a list of available LOS numbers for this realization\n",
" available_los_numbers = list(los_numbers)\n",
"\n",
" # Iterate through tiles\n",
" for tile_number in range(1, num_tiles_per_realization + 1):\n",
" # Randomly select a LOS from the available options\n",
" selected_los = random.choice(available_los_numbers)\n",
"\n",
" # Generate the filename pattern for the selected LOS, bin, and tile\n",
" filename_pattern = f\"Bin{bin_number}_LOS{selected_los}_R{tile_number}.\"\n",
" \n",
" # Find the matching file in the list of file paths\n",
" matching_files = [filename for filename in file_paths if filename_pattern in filename]\n",
"\n",
" selected_tiles.append(matching_files[0])\n",
"# Read the file paths from the \"master_file_cov.txt\"\n",
"with open(master_file_path, \"r\") as file:\n",
" file_paths = file.readlines()\n",
" file_paths = [path.strip() for path in file_paths]\n",
"\n",
" # Remove the selected LOS from the list of available LOS options\n",
" available_los_numbers.remove(selected_los)\n",
" \n",
" # Append the list of selected files for this realization to the collections_of_files\n",
" collections_of_files.append(selected_tiles)"
"# Parse these file paths\n",
"parsed_cov_data = slics.parse_cov_SLICS_filenames(file_paths)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The same as the previous cell, but ensuring that each file is only included once."
"Reconstruct 124 realisations of the survey by picking each tile from a random LOS, ensuring that each file is only included once."
]
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -156,43 +98,8 @@
"num_bins = 4\n",
"bin_number = 2\n",
"\n",
"# Create an empty list to store the collections of selected files for this bin\n",
"collections_of_files = []\n",
"\n",
"# Create a set to keep track of selected filenames\n",
"selected_filenames = set()\n",
"\n",
"# Iterate through realizations\n",
"for realization in range(num_realizations):\n",
" # Create an empty list to store the selected files for this realization\n",
" selected_tiles = []\n",
"\n",
" # Create a list of available LOS numbers for this realization\n",
" available_los_numbers = list(los_numbers)\n",
"\n",
" # Iterate through tiles\n",
" for tile_number in range(1, num_tiles_per_realization + 1):\n",
" # Initialize selected_file as None\n",
" selected_file = None\n",
"\n",
" # Continue trying different LOS options until a matching file is found\n",
" while not selected_file:\n",
" # Randomly select a LOS from the available options\n",
" selected_los = random.choice(available_los_numbers)\n",
"\n",
" # Generate the filename pattern for the selected LOS, bin, and tile\n",
" filename_pattern = f\"Bin{bin_number}_LOS{selected_los}_R{tile_number}.\"\n",
"\n",
" # Find the matching file in the list of file paths that has not been selected before\n",
" matching_files = [filename for filename in file_paths if filename_pattern in filename and filename not in selected_filenames]\n",
"\n",
" if matching_files:\n",
" selected_file = matching_files[0]\n",
" selected_tiles.append(selected_file)\n",
" selected_filenames.add(selected_file)\n",
"\n",
" # Append the list of selected files for this realization to the collections_of_files\n",
" collections_of_files.append(selected_tiles)"
"# Reconstruct 124 realisations of the survey by picking each tile from a random LOS, ensuring that each file is only included once.\n",
"collections_of_files = slics.survey_realizations_reconstruction(num_realizations, num_tiles_per_realization, bin_number, parsed_cov_data['LOS'], file_paths)"
]
}
],
Expand Down
70 changes: 69 additions & 1 deletion sp_peaks/slics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
from astropy.io import ascii
import numpy as np
import random



Expand Down Expand Up @@ -176,4 +177,71 @@ def map_cosmo_params_to_data(data, dat_file_path):
else:
print(f"No parameters found for ID {id}")

return mapped_params
return mapped_params


def parse_cov_SLICS_filenames(file_paths):
# Make empty recarray to store the data
data = np.recarray(len(file_paths), dtype=[('bin', int), ('LOS', int), ('tile', int)])

# Iterate over the file paths and process each file
for i, file_path in enumerate(file_paths):
# Extract the file name from the file path
file_name = file_path.split("/")[-1]

# Split file name into parts
file_parts = file_name.split("_")

# Extract relevant information
bin = int(file_parts[4][3:]) # Extract the number after "Bin"
LOS = int(file_parts[5][3:]) # Extract the number after "LOS"
tile = int(file_parts[6][1:-4]) # Extract the number after "R"

# Assign the extracted data to the corresponding fields in the recarray
data[i]['bin'] = bin
data[i]['LOS'] = LOS
data[i]['tile'] = tile

return data

def survey_realizations_reconstruction(num_realizations, num_tiles_per_realization, bin_number, los_numbers, file_paths):

# Create an empty list to store the collections of selected files for this bin
collections_of_files = []

# Create a set to keep track of selected filenames
selected_filenames = set()

# Iterate through realizations
for realization in range(num_realizations):
# Create an empty list to store the selected files for this realization
selected_tiles = []

# Create a list of available LOS numbers for this realization
available_los_numbers = list(los_numbers)

# Iterate through tiles
for tile_number in range(1, num_tiles_per_realization + 1):
# Initialize selected_file as None
selected_file = None

# Continue trying different LOS options until a matching file is found
while not selected_file:
# Randomly select a LOS from the available options
selected_los = random.choice(available_los_numbers)

# Generate the filename pattern for the selected LOS, bin, and tile
filename_pattern = f"Bin{bin_number}_LOS{selected_los}_R{tile_number}."

# Find the matching file in the list of file paths that has not been selected before
matching_files = [filename for filename in file_paths if filename_pattern in filename and filename not in selected_filenames]

if matching_files:
selected_file = matching_files[0]
selected_tiles.append(selected_file)
selected_filenames.add(selected_file)

# Append the list of selected files for this realization to the collections_of_files
collections_of_files.append(selected_tiles)

return collections_of_files

0 comments on commit cc764e9

Please sign in to comment.