Skip to content

Commit

Permalink
Merge pull request #44 from deepskies/re/rename
Browse files Browse the repository at this point in the history
Re/rename
  • Loading branch information
voetberg authored Oct 3, 2023
2 parents e6b8519 + a087d57 commit 7e2c363
Show file tree
Hide file tree
Showing 25 changed files with 1,112 additions and 1,354 deletions.
2 changes: 2 additions & 0 deletions DeepSurveySim/IO/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from DeepSurveySim.IO.read_config import ReadConfig
from DeepSurveySim.IO.save_simulation import SaveSimulation
59 changes: 59 additions & 0 deletions DeepSurveySim/IO/read_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from typing import Union
import yaml
import os


class ReadConfig:
"""
Read a .yaml file to a dictionary, and adds defaults
Args:
observator_configuration (Union[None, str], optional): Path to configuration file to read. Defaults to None.
survey (bool, optional): Read a survey configuration, filling in those defaults. Defaults to False.
Examples:
>>> observatory_config = IO.ReadConfig(observatory_config_path)()
survey_config = IO.ReadConfig(survey_config_path, survey=True)()
"""

def __init__(
self, observator_configuration: Union[None, str] = None, survey: bool = False
) -> None:

self.survey = survey
if observator_configuration is not None:
config = ReadConfig.load_yaml(observator_configuration)

else:
config = {}

self.config = self._add_defaults(config)

@staticmethod
def load_yaml(config_path: str):
"""Read a yaml file from a path
Args:
config_path (str): path to file, .yaml or .yml.
Returns:
dict: Read contents of the config
"""
with open(config_path, "r") as f:
return yaml.safe_load(f)

def _add_defaults(self, current_config: dict):
if not self.survey:
default_config_path = (
f"{os.path.dirname(__file__).rstrip('/')}/../settings/SEO.yaml"
)
else:
default_config_path = f"{os.path.dirname(__file__).rstrip('/')}/../settings/equatorial_survey.yaml"

default_config = ReadConfig.load_yaml(default_config_path)
full_config = {**default_config, **current_config}

return full_config

def __call__(self):
return self.config
85 changes: 85 additions & 0 deletions DeepSurveySim/IO/save_simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import yaml
import json
import pandas as pd
import os

import numpy as np
import datetime as dt


class SaveSimulation:
"""
Save a run survey to a json (survey results) and the config file used to generate it (yaml)
Args:
survey_instance (Survey.Survey): Survey used to generate the simulation
survey_results (dict): Run survey results - format of <mjd>:{variable:[value]}
Examples:
>>> survey = Survey.Survey(obseravtory_config, survey_config)
survey_results = survey()
IO.SaveSimulation(survey, survey_results)()
"""

def __init__(self, survey_instance, survey_results) -> None:
assert survey_instance.save_config is not None
self.survey_instance = survey_instance
self.survey_results = survey_results

save_id = SaveSimulation._generate_run_id()
self.save_path = f"{os.path.abspath(survey_instance.save_config.rstrip('/'))}/survey_{save_id}"

os.makedirs(self.save_path)

def save_results(self):
"""
Save results to the path of "/survey_{id}/survey_results.json"
"""
result_path = f"{self.save_path}/survey_results.json"

format_result = {
index: {
key: self.survey_results[index][key]
.reshape(
self.survey_results[index][key].size,
)
.tolist()
for key in self.survey_results[index].keys()
}
for index in self.survey_results.keys()
}
print(format_result)
with open(result_path, "w") as f:
json.dump(format_result, f)

def save_config(self):
"""
Save config of run to the path of "/survey_{id}/run_config.yaml"
"""
formated_config = {
**self.survey_instance.survey_config,
**self.survey_instance.telescope_config,
}
formated_config["run_id"] = self.save_path.split("/")[0]

config_path = f"{self.save_path}/run_config.yaml"
with open(config_path, "w") as f:
yaml.safe_dump(formated_config, f)

@staticmethod
def _generate_run_id(random_digits=4):
_rint = np.random.randint(10**random_digits)
date_string = (
str(dt.datetime.now())
.split(".")[0]
.replace(" ", "_")
.replace("-", "_")
.replace(":", "_")
)
return f"{date_string}_{str(_rint).zfill(random_digits)}"

def __call__(self):
self.save_results()
self.save_config()
4 changes: 4 additions & 0 deletions DeepSurveySim/Survey/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from DeepSurveySim.Survey.survey import Survey
from DeepSurveySim.Survey.observation_variables import (
ObservationVariables,
)
Loading

0 comments on commit 7e2c363

Please sign in to comment.