Example #1
0
 def get_or_create_window(self, name: str, index: Optional[int] = None):
     """Returns a window with specified name and index
     
     In case if window exists with the right name and index, returns it.
     If its index is not equal to the passed one, tries to move it.
     Creates a new window if none has the specified name.
     
     Args:
         name: window name
         index: desired window index
         
     Returns:
         libtmux.Window object
     """
     windows = self._session.windows
     for window in windows:
         if window.name == name:
             if index is not None and window.index != str(index):
                 window.move_window(index)
             return window
     report(
         "tmux",
         f"Creating window [!tmux_name]{name}[/] in session [!tmux_name]{self.name}[/]"
     )
     return self._session.new_window(window_name=name,
                                     window_index=index,
                                     attach=False)
Example #2
0
def on_stop_print(data, verbose=True, **k):
    if not verbose:
        return
    report(
        "prof",
        f"Step [!step]{data.name}[/] finished, took [!time]{data.timing:.5f}s[/]"
    )
Example #3
0
 def get_or_create_session(self, name: str):
     session = self.server.find_where({"session_name": name})
     created = False
     if session is None:
         report("tmux", f"Creating session [!tmux_name]{name}[/]")
         session = self.server.new_session(name)
         created = True
     return session, created
Example #4
0
 def run(self, cmd: str):
     """Runs command in window
     
     Args:
         cmd: command
     """
     report(
         "tmux",
         f"[!tmux_name]({self.session_name}/{self.window_name}) $[/] {cmd}")
     self._window.attached_pane.send_keys(cmd)
Example #5
0
 def move(self, index: int):
     """Moves window to index
     
     Args:
         index: desired index
     """
     report(
         "tmux",
         f"Moving window [!tmux_name]{self.window_name}[/] from index [!number]{self._window.index}[/] to [!number]{index}[/] in session [!tmux_name]{self.session_name}[/]"
     )
     self._window.move_window(index)
     self.window_index = index
Example #6
0
 def switch_workdir(self, path: Union[Path, str], create: bool = False):
     path = self._convert_resolve_path(path)
     report(
         "ctx",
         f"Switch workdir: [!path]{shorten_path(self.workdir)}[/] -> [!path]{shorten_path(path)}[/]"
     )
     if create:
         path.mkdir(parents=True, exist_ok=True)
     if not path.exists():
         raise OSError(
             f"{path} does not exist. Use ctx.switch_workdir(..., create=True) to create it."
         )
     os.chdir(path)
Example #7
0
 def log_image(self,
               log_name,
               x,
               y=None,
               image_name=None,
               description=None,
               timestamp=None):
     if self.verbose:
         x_shape = x.shape if hasattr(x, 'shape') else None
         y_shape = y.shape if hasattr(y, 'shape') else None
         report("logger", f"log_image({log_name}, {x_shape}, {y_shape})")
     self.experiment.log_image(log_name, x, y, image_name, description,
                               timestamp)
Example #8
0
 def shift_all_windows(self, index_delta: int):
     """Shifts indices of all windows in the session by index_delta
     
     Args:
         index_delta: shift length
     """
     for window in self._session.windows:
         old_index = int(window.index)
         new_index = old_index + index_delta
         report(
             "tmux",
             f"Moving window [!tmux_name]{window.name}[/] from index [!number]{old_index}[/] to [!number]{new_index}[/] in session [!tmux_name]{self.name}[/]"
         )
         window.move_window(new_index)
Example #9
0
 def close_windows(self, leave_session: bool = True):
     """Closes all windows in the session
     
     Args:
         leave_session: if set to True, prevents session from closing
     """
     sentinel = "tmux-keep"
     if leave_session:
         self.get_or_create_window(name=sentinel, index=100)
     for window in self._session.windows:
         if leave_session and window.name == sentinel:
             continue
         report(
             "tmux",
             f"[!alert]Killing[/] window [!tmux_name]{window.name}[/] in session [!tmux_name]{self.name}[/]"
         )
         window.kill_window()
Example #10
0
    def copy_sources(
        self,
        filenames: Optional[List[str]] = None,
        src_dir: Optional[Union[str, Path]] = None,
        dest_dir: Optional[Union[str, Path]] = None,
    ):
        filenames = filenames or self.tracked_filenames
        src_dir = src_dir or self.src_dir
        dest_dir = dest_dir or self.workdir

        for filename in filenames:
            src_file = src_dir / filename
            dest_file = dest_dir / filename
            report(
                "ctx",
                f"Copy file: [!path]{shorten_path(src_file)}[/] -> [!path]{shorten_path(dest_file)}[/]"
            )
            shutil.copy(src_file, dest_file)
Example #11
0
def load_config(filename, postload_hooks=True):
    global _CONFIG_FILENAME
    _CONFIG_FILENAME = filename
    loaded_config = Box.from_yaml(
        filename=filename, 
        box_dots=True,
        Loader=yaml.SafeLoader
    )
    report("config", f"Loaded config from [!path]{shorten_path(filename)}[/]")

    config.merge_update(loaded_config)
    if "hparams" in loaded_config:
        hparams.merge_update(loaded_config.hparams)

    if postload_hooks:
        for hook in _CONFIG_POSTLOAD_HOOKS:
            hook()

    _CONFIG_FILENAME = None
Example #12
0
def on_start_print(data, verbose=True, **k):
    if not verbose:
        return
    report("prof", f"Step [!step]{data.name}[/] started")
Example #13
0
 def log_artifact(self, artifact, destination=None):
     if self.verbose:
         report("logger", f"log_artifact()")
     self.experiment.log_artifact(artifact, destination)
Example #14
0
 def log_text(self, log_name, x, y=None, timestamp=None):
     if self.verbose:
         report("logger", f"log_text({log_name}, {x}, {y})")
     self.experiment.log_text(log_name, x, y, timestamp)