opsml.projects.active_run
1# pylint: disable=invalid-envvar-value,invalid-name 2 3# Copyright (c) Shipt, Inc. 4# This source code is licensed under the MIT license found in the 5# LICENSE file in the root directory of this source tree. 6 7from pathlib import Path 8from typing import Any, Dict, List, Optional, Union, cast 9 10from numpy.typing import NDArray 11 12from opsml.cards.base import ArtifactCard 13from opsml.cards.data import DataCard 14from opsml.cards.model import ModelCard 15from opsml.cards.run import RunCard 16from opsml.helpers.logging import ArtifactLogger 17from opsml.registry.registry import CardRegistries, CardRegistry 18from opsml.registry.semver import VersionType 19from opsml.storage import client 20from opsml.types import ( 21 ArtifactUris, 22 CardInfo, 23 CardType, 24 CommonKwargs, 25 Metrics, 26 Params, 27 SaveName, 28) 29 30logger = ArtifactLogger.get_logger() 31 32 33# dataclass inheritance doesnt handle default vals well for <= py3.9 34class RunInfo: 35 def __init__( 36 self, 37 runcard: RunCard, 38 run_id: str, 39 run_name: Optional[str] = None, 40 ): 41 self.storage_client = client.storage_client 42 self.registries = CardRegistries() 43 self.runcard = runcard 44 self.run_id = run_id 45 self.run_name = run_name 46 47 48class CardHandler: 49 """DRY helper class for ActiveRun and OpsmlProject""" 50 51 @staticmethod 52 def register_card( 53 registries: CardRegistries, 54 card: ArtifactCard, 55 version_type: Union[VersionType, str] = VersionType.MINOR, 56 ) -> None: 57 """Registers and ArtifactCard""" 58 59 registry: CardRegistry = getattr(registries, card.card_type) 60 registry.register_card(card=card, version_type=version_type) 61 62 @staticmethod 63 def load_card(registries: CardRegistries, registry_name: str, info: CardInfo) -> ArtifactCard: 64 """Loads an ArtifactCard""" 65 66 registry: CardRegistry = getattr(registries, registry_name) 67 return registry.load_card(name=info.name, version=info.version, uid=info.uid) 68 69 @staticmethod 70 def update_card(registries: CardRegistries, card: ArtifactCard) -> None: 71 """Updates an ArtifactCard""" 72 registry: CardRegistry = getattr(registries, card.card_type) 73 registry.update_card(card=card) 74 75 76class ActiveRun: 77 def __init__(self, run_info: RunInfo): 78 """ 79 Run object that handles logging artifacts, metrics, cards, and tags for a given run of a Project 80 81 Args: 82 run_info: 83 Run info for a given active run 84 """ 85 self._info = run_info 86 self._active = True # should be active upon instantiation 87 self.runcard = run_info.runcard 88 89 @property 90 def run_id(self) -> str: 91 """Id for current run""" 92 return self._info.run_id 93 94 @property 95 def run_name(self) -> Optional[str]: 96 """Name for current run""" 97 return self._info.run_name 98 99 @property 100 def active(self) -> bool: 101 return self._active 102 103 def _verify_active(self) -> None: 104 if not self.active: 105 raise ValueError("""Run is not active""") 106 107 def add_tag(self, key: str, value: str) -> None: 108 """ 109 Adds a tag to the current run 110 111 Args: 112 key: 113 Name of the tag 114 value: 115 Value to associate with tag 116 """ 117 self.runcard.add_tag(key=key, value=value) 118 119 def add_tags(self, tags: Dict[str, Union[str, float, int]]) -> None: 120 """ 121 Adds a tag to the current run 122 123 Args: 124 tags: 125 Dictionary of key, value tags 126 127 """ 128 for key, value in tags.items(): 129 self.add_tag(key=key, value=cast(str, value)) 130 131 def register_card( 132 self, 133 card: ArtifactCard, 134 version_type: Union[VersionType, str] = VersionType.MINOR, 135 ) -> None: 136 """ 137 Register a given artifact card. 138 139 Args: 140 card: 141 The card to register 142 version_type: 143 Version type for increment. Options are "major", "minor" and 144 "patch". Defaults to "minor". 145 """ 146 self._verify_active() 147 148 # add runuid to card 149 if isinstance(card, (DataCard, ModelCard)): 150 card.metadata.runcard_uid = self.runcard.uid 151 152 CardHandler.register_card( 153 registries=self._info.registries, 154 card=card, 155 version_type=version_type, 156 ) 157 158 tag_key = f"{card.card_type}:{card.name}" 159 self.add_tag(key=tag_key, value=card.version) 160 161 # add uid to RunCard 162 self.runcard.add_card_uid(card_type=card.card_type, uid=str(card.uid)) 163 164 def load_card(self, registry_name: str, info: CardInfo) -> ArtifactCard: 165 """ 166 Loads an ArtifactCard. 167 168 Args: 169 registry_name: 170 Type of card to load (data, model, run, pipeline) 171 info: 172 Card information to retrieve. `uid` takes precedence if it 173 exists. If the optional `version` is specified, that version 174 will be loaded. If it doesn't exist, the most recent version will 175 be loaded. 176 177 Returns 178 `ArtifactCard` 179 """ 180 card_type = CardType(registry_name.lower()).value 181 182 return CardHandler.load_card(registries=self._info.registries, registry_name=card_type, info=info) 183 184 def log_artifact_from_file( 185 self, 186 name: str, 187 local_path: Union[str, Path], 188 artifact_path: Optional[Union[str, Path]] = None, 189 ) -> None: 190 """ 191 Log a local file or directory to the opsml server and associate with the current run. 192 193 Args: 194 name: 195 Name to assign to artifact(s) 196 local_path: 197 Local path to file or directory. Can be string or pathlike object 198 artifact_path: 199 Optional path to store artifact in opsml server. If not provided, 'artifacts' will be used 200 """ 201 202 self._verify_active() 203 204 lpath = Path(local_path) 205 rpath = self.runcard.uri / (artifact_path or SaveName.ARTIFACTS.value) 206 207 if lpath.is_file(): 208 rpath = rpath / lpath.name 209 210 self._info.storage_client.put(lpath, rpath) 211 self.runcard._add_artifact_uri( # pylint: disable=protected-access 212 name=name, 213 local_path=lpath.as_posix(), 214 remote_path=rpath.as_posix(), 215 ) 216 217 def log_metric( 218 self, 219 key: str, 220 value: float, 221 timestamp: Optional[int] = None, 222 step: Optional[int] = None, 223 ) -> None: 224 """ 225 Log a metric for a given run 226 227 Args: 228 key: 229 Metric name 230 value: 231 Metric value 232 timestamp: 233 Optional time indicating metric creation time 234 step: 235 Optional step in training when metric was created 236 237 """ 238 self._verify_active() 239 self.runcard.log_metric( 240 key=key, 241 value=value, 242 timestamp=timestamp, 243 step=step, 244 ) 245 246 def log_metrics( 247 self, 248 metrics: Dict[str, Union[float, int]], 249 step: Optional[int] = None, 250 ) -> None: 251 """Logs a collection of metrics for a run 252 253 Args: 254 metrics: 255 Dictionary of metrics 256 step: 257 step the metrics are associated with 258 259 """ 260 self._verify_active() 261 self.runcard.log_metrics(metrics=metrics, step=step) 262 263 def log_parameter(self, key: str, value: str) -> None: 264 """ 265 Logs a parameter to project run 266 267 Args: 268 key: 269 Parameter name 270 value: 271 Parameter value 272 """ 273 274 self._verify_active() 275 self.runcard.log_parameter(key=key, value=value) 276 277 def log_parameters(self, parameters: Dict[str, Union[float, int, str]]) -> None: 278 """ 279 Logs a collection of parameters for a run 280 281 Args: 282 parameters: 283 Dictionary of parameters 284 """ 285 286 self._verify_active() 287 self.runcard.log_parameters(parameters=parameters) 288 289 def log_graph( 290 self, 291 name: str, 292 x: Union[List[Union[float, int]], NDArray[Any]], 293 y: Union[List[Union[float, int]], NDArray[Any], Dict[str, Union[List[Union[float, int]], NDArray[Any]]]], 294 x_label: str = "x", 295 y_label: str = "y", 296 graph_style: str = "line", 297 ) -> None: 298 """Logs a graph to the RunCard, which will be rendered in the UI as a line graph 299 300 Args: 301 name: 302 Name of graph 303 x: 304 List or numpy array of x values 305 306 x_label: 307 Label for x axis 308 y: 309 Either of the following: 310 (1) a list or numpy array of y values 311 (2) a dictionary of y values where key is the group label and 312 value is a list or numpy array of y values 313 y_label: 314 Label for y axis 315 graph_style: 316 Style of graph. Options are "line" or "scatter" 317 318 example: 319 320 ### single line graph 321 x = np.arange(1, 400, 0.5) 322 y = x * x 323 run.log_graph(name="graph1", x=x, y=y, x_label="x", y_label="y", graph_style="line") 324 325 ### multi line graph 326 x = np.arange(1, 1000, 0.5) 327 y1 = x * x 328 y2 = y1 * 1.1 329 y3 = y2 * 3 330 run.log_graph( 331 name="multiline", 332 x=x, 333 y={"y1": y1, "y2": y2, "y3": y3}, 334 x_label="x", 335 y_label="y", 336 graph_style="line", 337 ) 338 339 """ 340 self.runcard.log_graph(name=name, x=x, x_label=x_label, y=y, y_label=y_label, graph_style=graph_style) 341 342 def create_or_update_runcard(self) -> None: 343 """Creates or updates an active RunCard""" 344 345 self._verify_active() 346 347 if self.runcard.uid is not None and self.runcard.version != CommonKwargs.BASE_VERSION.value: 348 CardHandler.update_card(registries=self._info.registries, card=self.runcard) 349 else: 350 CardHandler.register_card(registries=self._info.registries, card=self.runcard) 351 352 @property 353 def run_data(self) -> Any: 354 raise NotImplementedError 355 356 @property 357 def metrics(self) -> Metrics: 358 return self.runcard.metrics 359 360 @property 361 def parameters(self) -> Params: 362 return self.runcard.parameters 363 364 @property 365 def tags(self) -> dict[str, Union[str, int]]: 366 return self.runcard.tags 367 368 @property 369 def artifact_uris(self) -> ArtifactUris: 370 return self.runcard.artifact_uris
logger =
<builtins.Logger object>
class
RunInfo:
35class RunInfo: 36 def __init__( 37 self, 38 runcard: RunCard, 39 run_id: str, 40 run_name: Optional[str] = None, 41 ): 42 self.storage_client = client.storage_client 43 self.registries = CardRegistries() 44 self.runcard = runcard 45 self.run_id = run_id 46 self.run_name = run_name
RunInfo( runcard: opsml.cards.run.RunCard, run_id: str, run_name: Optional[str] = None)
class
CardHandler:
49class CardHandler: 50 """DRY helper class for ActiveRun and OpsmlProject""" 51 52 @staticmethod 53 def register_card( 54 registries: CardRegistries, 55 card: ArtifactCard, 56 version_type: Union[VersionType, str] = VersionType.MINOR, 57 ) -> None: 58 """Registers and ArtifactCard""" 59 60 registry: CardRegistry = getattr(registries, card.card_type) 61 registry.register_card(card=card, version_type=version_type) 62 63 @staticmethod 64 def load_card(registries: CardRegistries, registry_name: str, info: CardInfo) -> ArtifactCard: 65 """Loads an ArtifactCard""" 66 67 registry: CardRegistry = getattr(registries, registry_name) 68 return registry.load_card(name=info.name, version=info.version, uid=info.uid) 69 70 @staticmethod 71 def update_card(registries: CardRegistries, card: ArtifactCard) -> None: 72 """Updates an ArtifactCard""" 73 registry: CardRegistry = getattr(registries, card.card_type) 74 registry.update_card(card=card)
DRY helper class for ActiveRun and OpsmlProject
@staticmethod
def
register_card( registries: opsml.registry.registry.CardRegistries, card: opsml.cards.base.ArtifactCard, version_type: Union[opsml.registry.semver.VersionType, str] = <VersionType.MINOR: 'minor'>) -> None:
52 @staticmethod 53 def register_card( 54 registries: CardRegistries, 55 card: ArtifactCard, 56 version_type: Union[VersionType, str] = VersionType.MINOR, 57 ) -> None: 58 """Registers and ArtifactCard""" 59 60 registry: CardRegistry = getattr(registries, card.card_type) 61 registry.register_card(card=card, version_type=version_type)
Registers and ArtifactCard
@staticmethod
def
load_card( registries: opsml.registry.registry.CardRegistries, registry_name: str, info: opsml.types.card.CardInfo) -> opsml.cards.base.ArtifactCard:
63 @staticmethod 64 def load_card(registries: CardRegistries, registry_name: str, info: CardInfo) -> ArtifactCard: 65 """Loads an ArtifactCard""" 66 67 registry: CardRegistry = getattr(registries, registry_name) 68 return registry.load_card(name=info.name, version=info.version, uid=info.uid)
Loads an ArtifactCard
@staticmethod
def
update_card( registries: opsml.registry.registry.CardRegistries, card: opsml.cards.base.ArtifactCard) -> None:
70 @staticmethod 71 def update_card(registries: CardRegistries, card: ArtifactCard) -> None: 72 """Updates an ArtifactCard""" 73 registry: CardRegistry = getattr(registries, card.card_type) 74 registry.update_card(card=card)
Updates an ArtifactCard
class
ActiveRun:
77class ActiveRun: 78 def __init__(self, run_info: RunInfo): 79 """ 80 Run object that handles logging artifacts, metrics, cards, and tags for a given run of a Project 81 82 Args: 83 run_info: 84 Run info for a given active run 85 """ 86 self._info = run_info 87 self._active = True # should be active upon instantiation 88 self.runcard = run_info.runcard 89 90 @property 91 def run_id(self) -> str: 92 """Id for current run""" 93 return self._info.run_id 94 95 @property 96 def run_name(self) -> Optional[str]: 97 """Name for current run""" 98 return self._info.run_name 99 100 @property 101 def active(self) -> bool: 102 return self._active 103 104 def _verify_active(self) -> None: 105 if not self.active: 106 raise ValueError("""Run is not active""") 107 108 def add_tag(self, key: str, value: str) -> None: 109 """ 110 Adds a tag to the current run 111 112 Args: 113 key: 114 Name of the tag 115 value: 116 Value to associate with tag 117 """ 118 self.runcard.add_tag(key=key, value=value) 119 120 def add_tags(self, tags: Dict[str, Union[str, float, int]]) -> None: 121 """ 122 Adds a tag to the current run 123 124 Args: 125 tags: 126 Dictionary of key, value tags 127 128 """ 129 for key, value in tags.items(): 130 self.add_tag(key=key, value=cast(str, value)) 131 132 def register_card( 133 self, 134 card: ArtifactCard, 135 version_type: Union[VersionType, str] = VersionType.MINOR, 136 ) -> None: 137 """ 138 Register a given artifact card. 139 140 Args: 141 card: 142 The card to register 143 version_type: 144 Version type for increment. Options are "major", "minor" and 145 "patch". Defaults to "minor". 146 """ 147 self._verify_active() 148 149 # add runuid to card 150 if isinstance(card, (DataCard, ModelCard)): 151 card.metadata.runcard_uid = self.runcard.uid 152 153 CardHandler.register_card( 154 registries=self._info.registries, 155 card=card, 156 version_type=version_type, 157 ) 158 159 tag_key = f"{card.card_type}:{card.name}" 160 self.add_tag(key=tag_key, value=card.version) 161 162 # add uid to RunCard 163 self.runcard.add_card_uid(card_type=card.card_type, uid=str(card.uid)) 164 165 def load_card(self, registry_name: str, info: CardInfo) -> ArtifactCard: 166 """ 167 Loads an ArtifactCard. 168 169 Args: 170 registry_name: 171 Type of card to load (data, model, run, pipeline) 172 info: 173 Card information to retrieve. `uid` takes precedence if it 174 exists. If the optional `version` is specified, that version 175 will be loaded. If it doesn't exist, the most recent version will 176 be loaded. 177 178 Returns 179 `ArtifactCard` 180 """ 181 card_type = CardType(registry_name.lower()).value 182 183 return CardHandler.load_card(registries=self._info.registries, registry_name=card_type, info=info) 184 185 def log_artifact_from_file( 186 self, 187 name: str, 188 local_path: Union[str, Path], 189 artifact_path: Optional[Union[str, Path]] = None, 190 ) -> None: 191 """ 192 Log a local file or directory to the opsml server and associate with the current run. 193 194 Args: 195 name: 196 Name to assign to artifact(s) 197 local_path: 198 Local path to file or directory. Can be string or pathlike object 199 artifact_path: 200 Optional path to store artifact in opsml server. If not provided, 'artifacts' will be used 201 """ 202 203 self._verify_active() 204 205 lpath = Path(local_path) 206 rpath = self.runcard.uri / (artifact_path or SaveName.ARTIFACTS.value) 207 208 if lpath.is_file(): 209 rpath = rpath / lpath.name 210 211 self._info.storage_client.put(lpath, rpath) 212 self.runcard._add_artifact_uri( # pylint: disable=protected-access 213 name=name, 214 local_path=lpath.as_posix(), 215 remote_path=rpath.as_posix(), 216 ) 217 218 def log_metric( 219 self, 220 key: str, 221 value: float, 222 timestamp: Optional[int] = None, 223 step: Optional[int] = None, 224 ) -> None: 225 """ 226 Log a metric for a given run 227 228 Args: 229 key: 230 Metric name 231 value: 232 Metric value 233 timestamp: 234 Optional time indicating metric creation time 235 step: 236 Optional step in training when metric was created 237 238 """ 239 self._verify_active() 240 self.runcard.log_metric( 241 key=key, 242 value=value, 243 timestamp=timestamp, 244 step=step, 245 ) 246 247 def log_metrics( 248 self, 249 metrics: Dict[str, Union[float, int]], 250 step: Optional[int] = None, 251 ) -> None: 252 """Logs a collection of metrics for a run 253 254 Args: 255 metrics: 256 Dictionary of metrics 257 step: 258 step the metrics are associated with 259 260 """ 261 self._verify_active() 262 self.runcard.log_metrics(metrics=metrics, step=step) 263 264 def log_parameter(self, key: str, value: str) -> None: 265 """ 266 Logs a parameter to project run 267 268 Args: 269 key: 270 Parameter name 271 value: 272 Parameter value 273 """ 274 275 self._verify_active() 276 self.runcard.log_parameter(key=key, value=value) 277 278 def log_parameters(self, parameters: Dict[str, Union[float, int, str]]) -> None: 279 """ 280 Logs a collection of parameters for a run 281 282 Args: 283 parameters: 284 Dictionary of parameters 285 """ 286 287 self._verify_active() 288 self.runcard.log_parameters(parameters=parameters) 289 290 def log_graph( 291 self, 292 name: str, 293 x: Union[List[Union[float, int]], NDArray[Any]], 294 y: Union[List[Union[float, int]], NDArray[Any], Dict[str, Union[List[Union[float, int]], NDArray[Any]]]], 295 x_label: str = "x", 296 y_label: str = "y", 297 graph_style: str = "line", 298 ) -> None: 299 """Logs a graph to the RunCard, which will be rendered in the UI as a line graph 300 301 Args: 302 name: 303 Name of graph 304 x: 305 List or numpy array of x values 306 307 x_label: 308 Label for x axis 309 y: 310 Either of the following: 311 (1) a list or numpy array of y values 312 (2) a dictionary of y values where key is the group label and 313 value is a list or numpy array of y values 314 y_label: 315 Label for y axis 316 graph_style: 317 Style of graph. Options are "line" or "scatter" 318 319 example: 320 321 ### single line graph 322 x = np.arange(1, 400, 0.5) 323 y = x * x 324 run.log_graph(name="graph1", x=x, y=y, x_label="x", y_label="y", graph_style="line") 325 326 ### multi line graph 327 x = np.arange(1, 1000, 0.5) 328 y1 = x * x 329 y2 = y1 * 1.1 330 y3 = y2 * 3 331 run.log_graph( 332 name="multiline", 333 x=x, 334 y={"y1": y1, "y2": y2, "y3": y3}, 335 x_label="x", 336 y_label="y", 337 graph_style="line", 338 ) 339 340 """ 341 self.runcard.log_graph(name=name, x=x, x_label=x_label, y=y, y_label=y_label, graph_style=graph_style) 342 343 def create_or_update_runcard(self) -> None: 344 """Creates or updates an active RunCard""" 345 346 self._verify_active() 347 348 if self.runcard.uid is not None and self.runcard.version != CommonKwargs.BASE_VERSION.value: 349 CardHandler.update_card(registries=self._info.registries, card=self.runcard) 350 else: 351 CardHandler.register_card(registries=self._info.registries, card=self.runcard) 352 353 @property 354 def run_data(self) -> Any: 355 raise NotImplementedError 356 357 @property 358 def metrics(self) -> Metrics: 359 return self.runcard.metrics 360 361 @property 362 def parameters(self) -> Params: 363 return self.runcard.parameters 364 365 @property 366 def tags(self) -> dict[str, Union[str, int]]: 367 return self.runcard.tags 368 369 @property 370 def artifact_uris(self) -> ArtifactUris: 371 return self.runcard.artifact_uris
ActiveRun(run_info: RunInfo)
78 def __init__(self, run_info: RunInfo): 79 """ 80 Run object that handles logging artifacts, metrics, cards, and tags for a given run of a Project 81 82 Args: 83 run_info: 84 Run info for a given active run 85 """ 86 self._info = run_info 87 self._active = True # should be active upon instantiation 88 self.runcard = run_info.runcard
Run object that handles logging artifacts, metrics, cards, and tags for a given run of a Project
Arguments:
- run_info: Run info for a given active run
run_name: Optional[str]
95 @property 96 def run_name(self) -> Optional[str]: 97 """Name for current run""" 98 return self._info.run_name
Name for current run
def
add_tag(self, key: str, value: str) -> None:
108 def add_tag(self, key: str, value: str) -> None: 109 """ 110 Adds a tag to the current run 111 112 Args: 113 key: 114 Name of the tag 115 value: 116 Value to associate with tag 117 """ 118 self.runcard.add_tag(key=key, value=value)
Adds a tag to the current run
Arguments:
- key: Name of the tag
- value: Value to associate with tag
def
register_card( self, card: opsml.cards.base.ArtifactCard, version_type: Union[opsml.registry.semver.VersionType, str] = <VersionType.MINOR: 'minor'>) -> None:
132 def register_card( 133 self, 134 card: ArtifactCard, 135 version_type: Union[VersionType, str] = VersionType.MINOR, 136 ) -> None: 137 """ 138 Register a given artifact card. 139 140 Args: 141 card: 142 The card to register 143 version_type: 144 Version type for increment. Options are "major", "minor" and 145 "patch". Defaults to "minor". 146 """ 147 self._verify_active() 148 149 # add runuid to card 150 if isinstance(card, (DataCard, ModelCard)): 151 card.metadata.runcard_uid = self.runcard.uid 152 153 CardHandler.register_card( 154 registries=self._info.registries, 155 card=card, 156 version_type=version_type, 157 ) 158 159 tag_key = f"{card.card_type}:{card.name}" 160 self.add_tag(key=tag_key, value=card.version) 161 162 # add uid to RunCard 163 self.runcard.add_card_uid(card_type=card.card_type, uid=str(card.uid))
Register a given artifact card.
Arguments:
- card: The card to register
- version_type: Version type for increment. Options are "major", "minor" and "patch". Defaults to "minor".
def
load_card( self, registry_name: str, info: opsml.types.card.CardInfo) -> opsml.cards.base.ArtifactCard:
165 def load_card(self, registry_name: str, info: CardInfo) -> ArtifactCard: 166 """ 167 Loads an ArtifactCard. 168 169 Args: 170 registry_name: 171 Type of card to load (data, model, run, pipeline) 172 info: 173 Card information to retrieve. `uid` takes precedence if it 174 exists. If the optional `version` is specified, that version 175 will be loaded. If it doesn't exist, the most recent version will 176 be loaded. 177 178 Returns 179 `ArtifactCard` 180 """ 181 card_type = CardType(registry_name.lower()).value 182 183 return CardHandler.load_card(registries=self._info.registries, registry_name=card_type, info=info)
Loads an ArtifactCard.
Arguments:
- registry_name: Type of card to load (data, model, run, pipeline)
- info: Card information to retrieve.
uid
takes precedence if it exists. If the optionalversion
is specified, that version will be loaded. If it doesn't exist, the most recent version will be loaded.
Returns
ArtifactCard
def
log_artifact_from_file( self, name: str, local_path: Union[str, pathlib.Path], artifact_path: Union[str, pathlib.Path, NoneType] = None) -> None:
185 def log_artifact_from_file( 186 self, 187 name: str, 188 local_path: Union[str, Path], 189 artifact_path: Optional[Union[str, Path]] = None, 190 ) -> None: 191 """ 192 Log a local file or directory to the opsml server and associate with the current run. 193 194 Args: 195 name: 196 Name to assign to artifact(s) 197 local_path: 198 Local path to file or directory. Can be string or pathlike object 199 artifact_path: 200 Optional path to store artifact in opsml server. If not provided, 'artifacts' will be used 201 """ 202 203 self._verify_active() 204 205 lpath = Path(local_path) 206 rpath = self.runcard.uri / (artifact_path or SaveName.ARTIFACTS.value) 207 208 if lpath.is_file(): 209 rpath = rpath / lpath.name 210 211 self._info.storage_client.put(lpath, rpath) 212 self.runcard._add_artifact_uri( # pylint: disable=protected-access 213 name=name, 214 local_path=lpath.as_posix(), 215 remote_path=rpath.as_posix(), 216 )
Log a local file or directory to the opsml server and associate with the current run.
Arguments:
- name: Name to assign to artifact(s)
- local_path: Local path to file or directory. Can be string or pathlike object
- artifact_path: Optional path to store artifact in opsml server. If not provided, 'artifacts' will be used
def
log_metric( self, key: str, value: float, timestamp: Optional[int] = None, step: Optional[int] = None) -> None:
218 def log_metric( 219 self, 220 key: str, 221 value: float, 222 timestamp: Optional[int] = None, 223 step: Optional[int] = None, 224 ) -> None: 225 """ 226 Log a metric for a given run 227 228 Args: 229 key: 230 Metric name 231 value: 232 Metric value 233 timestamp: 234 Optional time indicating metric creation time 235 step: 236 Optional step in training when metric was created 237 238 """ 239 self._verify_active() 240 self.runcard.log_metric( 241 key=key, 242 value=value, 243 timestamp=timestamp, 244 step=step, 245 )
Log a metric for a given run
Arguments:
- key: Metric name
- value: Metric value
- timestamp: Optional time indicating metric creation time
- step: Optional step in training when metric was created
def
log_metrics( self, metrics: Dict[str, Union[float, int]], step: Optional[int] = None) -> None:
247 def log_metrics( 248 self, 249 metrics: Dict[str, Union[float, int]], 250 step: Optional[int] = None, 251 ) -> None: 252 """Logs a collection of metrics for a run 253 254 Args: 255 metrics: 256 Dictionary of metrics 257 step: 258 step the metrics are associated with 259 260 """ 261 self._verify_active() 262 self.runcard.log_metrics(metrics=metrics, step=step)
Logs a collection of metrics for a run
Arguments:
- metrics: Dictionary of metrics
- step: step the metrics are associated with
def
log_parameter(self, key: str, value: str) -> None:
264 def log_parameter(self, key: str, value: str) -> None: 265 """ 266 Logs a parameter to project run 267 268 Args: 269 key: 270 Parameter name 271 value: 272 Parameter value 273 """ 274 275 self._verify_active() 276 self.runcard.log_parameter(key=key, value=value)
Logs a parameter to project run
Arguments:
- key: Parameter name
- value: Parameter value
def
log_parameters(self, parameters: Dict[str, Union[float, int, str]]) -> None:
278 def log_parameters(self, parameters: Dict[str, Union[float, int, str]]) -> None: 279 """ 280 Logs a collection of parameters for a run 281 282 Args: 283 parameters: 284 Dictionary of parameters 285 """ 286 287 self._verify_active() 288 self.runcard.log_parameters(parameters=parameters)
Logs a collection of parameters for a run
Arguments:
- parameters: Dictionary of parameters
def
log_graph( self, name: str, x: Union[List[Union[int, float]], numpy.ndarray[Any, numpy.dtype[Any]]], y: Union[List[Union[int, float]], numpy.ndarray[Any, numpy.dtype[Any]], Dict[str, Union[List[Union[int, float]], numpy.ndarray[Any, numpy.dtype[Any]]]]], x_label: str = 'x', y_label: str = 'y', graph_style: str = 'line') -> None:
290 def log_graph( 291 self, 292 name: str, 293 x: Union[List[Union[float, int]], NDArray[Any]], 294 y: Union[List[Union[float, int]], NDArray[Any], Dict[str, Union[List[Union[float, int]], NDArray[Any]]]], 295 x_label: str = "x", 296 y_label: str = "y", 297 graph_style: str = "line", 298 ) -> None: 299 """Logs a graph to the RunCard, which will be rendered in the UI as a line graph 300 301 Args: 302 name: 303 Name of graph 304 x: 305 List or numpy array of x values 306 307 x_label: 308 Label for x axis 309 y: 310 Either of the following: 311 (1) a list or numpy array of y values 312 (2) a dictionary of y values where key is the group label and 313 value is a list or numpy array of y values 314 y_label: 315 Label for y axis 316 graph_style: 317 Style of graph. Options are "line" or "scatter" 318 319 example: 320 321 ### single line graph 322 x = np.arange(1, 400, 0.5) 323 y = x * x 324 run.log_graph(name="graph1", x=x, y=y, x_label="x", y_label="y", graph_style="line") 325 326 ### multi line graph 327 x = np.arange(1, 1000, 0.5) 328 y1 = x * x 329 y2 = y1 * 1.1 330 y3 = y2 * 3 331 run.log_graph( 332 name="multiline", 333 x=x, 334 y={"y1": y1, "y2": y2, "y3": y3}, 335 x_label="x", 336 y_label="y", 337 graph_style="line", 338 ) 339 340 """ 341 self.runcard.log_graph(name=name, x=x, x_label=x_label, y=y, y_label=y_label, graph_style=graph_style)
Logs a graph to the RunCard, which will be rendered in the UI as a line graph
Arguments:
- name: Name of graph
- x: List or numpy array of x values
- x_label: Label for x axis
- y: Either of the following: (1) a list or numpy array of y values (2) a dictionary of y values where key is the group label and value is a list or numpy array of y values
- y_label: Label for y axis
- graph_style: Style of graph. Options are "line" or "scatter"
example:
### single line graph
x = np.arange(1, 400, 0.5)
y = x * x
run.log_graph(name="graph1", x=x, y=y, x_label="x", y_label="y", graph_style="line")
### multi line graph
x = np.arange(1, 1000, 0.5)
y1 = x * x
y2 = y1 * 1.1
y3 = y2 * 3
run.log_graph(
name="multiline",
x=x,
y={"y1": y1, "y2": y2, "y3": y3},
x_label="x",
y_label="y",
graph_style="line",
)
def
create_or_update_runcard(self) -> None:
343 def create_or_update_runcard(self) -> None: 344 """Creates or updates an active RunCard""" 345 346 self._verify_active() 347 348 if self.runcard.uid is not None and self.runcard.version != CommonKwargs.BASE_VERSION.value: 349 CardHandler.update_card(registries=self._info.registries, card=self.runcard) 350 else: 351 CardHandler.register_card(registries=self._info.registries, card=self.runcard)
Creates or updates an active RunCard