-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from deepskies/re/rename
Re/rename
- Loading branch information
Showing
25 changed files
with
1,112 additions
and
1,354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
Oops, something went wrong.