Skip to content

Commit

Permalink
[DRAFT] default log folder on initialize (#205)
Browse files Browse the repository at this point in the history
* set log directory as mandatory in job_builder

* add prepare path method to create folders outside of initialize

* add Spectrogram initializer from csv metadata file

* build spectrogram paths after csv instantiation

* format with black
  • Loading branch information
Gautzilla authored Oct 11, 2024
1 parent 5103437 commit c8dd94e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
28 changes: 25 additions & 3 deletions src/OSmOSE/Spectrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@ def __init__(

self.__build_path(dry=True)

@classmethod
def from_csv(
cls, dataset_path: Path, metadata_csv_path: Path
): # I don't want to use the dataset_path, but for now I have to
df = pd.read_csv(metadata_csv_path)
instance = cls(dataset_path=dataset_path)

for attribute in df:
instance.__setattr__(attribute, df[attribute].values[0])

instance.prepare_paths()

return instance

@property
def dataset_sr(self):
"""int: The sampling frequency of the dataset."""
Expand Down Expand Up @@ -427,6 +441,10 @@ def frequency_resolution(self) -> float:
"""Frequency resolution of the spectrogram, calculated by dividing the samplerate by nfft."""
return self.dataset_sr / self.nfft

@frequency_resolution.setter
def frequency_resolution(self, value: float):
self._frequency_resolution = value # I don't know why this value is stored in the .csv, as it directly depends on dataset_sr and nfft

@property
def time_resolution(self):
return self.__time_resolution
Expand Down Expand Up @@ -547,6 +565,10 @@ def check_spectro_size(self):
"(Hz)",
)

def prepare_paths(self, force_init: bool = False):
# create some internal paths
self.__build_path(force_init=force_init)

def initialize(
self,
*,
Expand Down Expand Up @@ -598,8 +620,7 @@ def initialize(
mode=DPDEFAULT,
)

# create some internal paths
self.__build_path(force_init=force_init)
self.prepare_paths(force_init=force_init)

if not (
self.path
Expand Down Expand Up @@ -907,7 +928,7 @@ def initialize(
metadata.to_csv(new_meta_path, index=False)
os.chmod(new_meta_path, mode=FPDEFAULT)

def save_spectro_metadata(self, adjust_bool: bool):
def save_spectro_metadata(self, adjust_bool: bool) -> Path:
temporal_resolution, frequency_resolution, Nbwin = self.extract_spectro_params()

data = {
Expand Down Expand Up @@ -960,6 +981,7 @@ def save_spectro_metadata(self, adjust_bool: bool):

analysis_sheet.to_csv(meta_path, index=False)
os.chmod(meta_path, mode=FPDEFAULT)
return meta_path

def audio_file_list_csv(self) -> Path:
list_audio = get_all_audio_files(self.path_input_audio_file)
Expand Down
15 changes: 4 additions & 11 deletions src/OSmOSE/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def build_job_file(
*,
script_path: str,
script_args: str,
logdir: Path,
jobname: str = None,
preset: Literal["low", "medium", "high"] = None,
job_scheduler: Literal["Torque", "Slurm"] = None,
Expand All @@ -223,7 +224,6 @@ def build_job_file(
mem: str = None,
outfile: str = None,
errfile: str = None,
logdir: Path = None,
) -> str:
"""Build a job file corresponding to your job scheduler.
Expand Down Expand Up @@ -288,14 +288,7 @@ def build_job_file(

pwd = Path(__file__).parent

if logdir is None:
logdir = pwd.joinpath("log_job")
jobdir = pwd.joinpath("ongoing_jobs")
logdir.mkdir(mode=DPDEFAULT, exist_ok=True)
jobdir.mkdir(mode=DPDEFAULT, exist_ok=True)
else:
logdir.mkdir(mode=DPDEFAULT, exist_ok=True)
jobdir = logdir
logdir.mkdir(mode=DPDEFAULT, exist_ok=True)

job_file = ["#!/bin/bash"]

Expand Down Expand Up @@ -403,10 +396,10 @@ def build_job_file(

#! FOOTER
outfilename = (
f"{jobname}_{date_id}_{job_scheduler}_{len(os.listdir(jobdir))}.pbs"
f"{jobname}_{date_id}_{job_scheduler}_{len(os.listdir(logdir))}.pbs"
)

job_file_path = jobdir.joinpath(outfilename)
job_file_path = logdir.joinpath(outfilename)
job_file.append(f"\nrm {job_file_path}\n")

#! BUILD DONE => WRITING
Expand Down

0 comments on commit c8dd94e

Please sign in to comment.