Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyaml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import logging.config
import os
from pyaml.exception import PyAMLException
from pyaml.configuration.config_exception import PyAMLConfigException
from pyaml.common.exception import PyAMLException
from pyaml.common.exception import PyAMLConfigException

__all__ = [__version__, PyAMLException, PyAMLConfigException]

Expand Down
16 changes: 8 additions & 8 deletions pyaml/bpm/bpm.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from pyaml.lattice.element import Element, ElementConfigModel
from pyaml.lattice.abstract_impl import RBpmArray, RWBpmOffsetArray, RWBpmTiltScalar
from ..control.deviceaccess import DeviceAccess
from ..common import abstract
from ..lattice.element import Element, ElementConfigModel
from ..lattice.abstract_impl import RBpmArray, RWBpmOffsetArray, RWBpmTiltScalar
from ..bpm.bpm_model import BPMModel
from ..common.exception import PyAMLException

from typing import Self
from pyaml.bpm.bpm_model import BPMModel

PYAMLCLASS = "BPM"

Expand Down Expand Up @@ -48,19 +48,19 @@ def model(self) -> BPMModel:
@property
def positions(self) -> RBpmArray:
if self.__positions is None:
raise Exception(f"{str(self)} has no attached positions")
raise PyAMLException(f"{str(self)} has no attached positions")
return self.__positions

@property
def offset(self) -> RWBpmOffsetArray:
if self.__offset is None:
raise Exception(f"{str(self)} has no attached offset")
raise PyAMLException(f"{str(self)} has no attached offset")
return self.__offset

@property
def tilt(self) -> RWBpmTiltScalar:
if self.__tilt is None:
raise Exception(f"{str(self)} has no attached tilt")
raise PyAMLException(f"{str(self)} has no attached tilt")
return self.__tilt

def attach(self, positions: RBpmArray , offset: RWBpmOffsetArray,
Expand Down
41 changes: 21 additions & 20 deletions pyaml/common/element_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ..rf.rf_transmitter import RFTransmitter
from ..arrays.magnet_array import MagnetArray
from ..arrays.bpm_array import BPMArray
from ..common.exception import PyAMLException

class ElementHolder(object):
"""
Expand All @@ -28,28 +29,34 @@ def __init__(self):
def fill_device(self,elements:list[Element]):
raise "ElementHolder.fill_device() is not subclassed"

# Magnets

def fill_magnet_array(self,arrayName:str,elementNames:list[str]):
def fill_array(self,arrayName:str,elementNames:list[str],get_func,constructor,ARR:dict):
a = []
for name in elementNames:
try:
a.append(self.get_magnet(name))
m = get_func(name)
except Exception as err:
raise Exception(f"MagnetArray {arrayName} : {err}")
self.__MAGNET_ARRAYS[arrayName] = MagnetArray(arrayName,a,self)
raise PyAMLException(f"{constructor.__name__} {arrayName} : {err} @index {len(a)}") from None
if m in a:
raise PyAMLException(f"{constructor.__name__} {arrayName} : duplicate name {name} @index {len(a)}") from None
a.append(m)
ARR[arrayName] = constructor(arrayName,a,self)

# Magnets

def fill_magnet_array(self,arrayName:str,elementNames:list[str]):
self.fill_array(arrayName,elementNames,self.get_magnet,MagnetArray,self.__MAGNET_ARRAYS)

def get_magnet(self,name:str) -> Magnet:
if name not in self.__MAGNETS:
raise Exception(f"Magnet {name} not defined")
raise PyAMLException(f"Magnet {name} not defined")
return self.__MAGNETS[name]

def add_magnet(self,name:str,m:Magnet):
self.__MAGNETS[name] = m

def get_magnets(self,name:str) -> MagnetArray:
if name not in self.__MAGNET_ARRAYS:
raise Exception(f"Magnet array {name} not defined")
raise PyAMLException(f"Magnet array {name} not defined")
return self.__MAGNET_ARRAYS[name]

def get_all_magnets(self) -> dict:
Expand All @@ -58,48 +65,42 @@ def get_all_magnets(self) -> dict:
# BPMs

def fill_bpm_array(self,arrayName:str,elementNames:list[str]):
a = []
for name in elementNames:
try:
a.append(self.get_bpm(name))
except Exception as err:
raise Exception(f"BpmArray {arrayName} : {err}")
self.__BPM_ARRAYS[arrayName] = BPMArray(arrayName,a,self)
self.fill_array(arrayName,elementNames,self.get_bpm,BPMArray,self.__BPM_ARRAYS)

def get_bpm(self,name:str) -> Element:
if name not in self.__BPMS:
raise Exception(f"BPM {name} not defined")
raise PyAMLException(f"BPM {name} not defined")
return self.__BPMS[name]

def add_bpm(self,name:str,bpm:Element):
self.__BPMS[name] = bpm

def get_bpms(self,name:str) -> BPMArray:
if name not in self.__BPM_ARRAYS:
raise Exception(f"BPM array {name} not defined")
raise PyAMLException(f"BPM array {name} not defined")
return self.__BPM_ARRAYS[name]

# RF

def get_rf_plant(self,name:str) -> RFPlant:
if name not in self.__RFPLANT:
raise Exception(f"RFPlant {name} not defined")
raise PyAMLException(f"RFPlant {name} not defined")
return self.__RFPLANT[name]

def add_rf_plant(self,name:str,rf:RFPlant):
self.__RFPLANT[name] = rf

def get_rf_plant(self,name:str) -> RFPlant:
if name not in self.__RFPLANT:
raise Exception(f"RFPlant {name} not defined")
raise PyAMLException(f"RFPlant {name} not defined")
return self.__RFPLANT[name]

def add_rf_transnmitter(self,name:str,rf:RFTransmitter):
self.__RFTRANSMITTER[name] = rf

def get_rf_trasnmitter(self,name:str) -> RFTransmitter:
if name not in self.__RFTRANSMITTER:
raise Exception(f"RFTransmitter {name} not defined")
raise PyAMLException(f"RFTransmitter {name} not defined")
return self.__RFTRANSMITTER[name]


Expand Down
20 changes: 20 additions & 0 deletions pyaml/common/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class PyAMLException(Exception):
"""Exception raised for PyAML error scenarios.

Attributes:
message -- explanation of the error
"""
def __init__(self, message):
super().__init__(message)
self.message = message


class PyAMLConfigException(Exception):
"""Exception raised for PyAML configuration error scenarios.

Attributes:
message -- explanation of the error
"""
def __init__(self, message):
super().__init__(message)
self.message = message
54 changes: 0 additions & 54 deletions pyaml/configuration/config_exception.py

This file was deleted.

21 changes: 13 additions & 8 deletions pyaml/configuration/csvcurve.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""
Class for load CSV (x,y) curve
"""
from pydantic import BaseModel,ConfigDict
from ..configuration import get_root_folder
from pathlib import Path
from ..common.exception import PyAMLException
from .curve import Curve

from pathlib import Path
from pydantic import BaseModel,ConfigDict
import numpy as np

from .curve import Curve

# Define the main class name for this module
PYAMLCLASS = "CSVCurve"
Expand All @@ -20,16 +18,23 @@ class ConfigModel(BaseModel):
"""CSV file that contains the curve (n rows,2 columns)"""

class CSVCurve(Curve):
"""
Class for load CSV (x,y) curve
"""

def __init__(self, cfg: ConfigModel):
self._cfg = cfg

# Load CSV curve
path:Path = get_root_folder() / cfg.file
self._curve = np.genfromtxt(path, delimiter=",", dtype=float)
try:
self._curve = np.genfromtxt(path, delimiter=",", dtype=float, loose=False)
except ValueError as e:
raise PyAMLException(f"CSVCurve(file='{cfg.file}',dtype=float): {str(e)}") from None

_s = np.shape(self._curve)
if len(_s) != 2 or _s[1] != 2:
raise Exception(cfg.file + " wrong dimension")
raise PyAMLException(f"CSVCurve(file='{cfg.file}',dtype=float): wrong shape (2,2) expected but got {str(_s)}")

def get_curve(self) -> np.array:
return self._curve
18 changes: 11 additions & 7 deletions pyaml/configuration/csvmatrix.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""
Class for load CSV matrix
"""
from pydantic import BaseModel,ConfigDict
from ..configuration import get_root_folder
from pathlib import Path
from .matrix import Matrix
from ..common.exception import PyAMLException

from pydantic import BaseModel,ConfigDict
from pathlib import Path
import numpy as np

from .matrix import Matrix

# Define the main class name for this module
PYAMLCLASS = "CSVMatrix"
Expand All @@ -20,12 +18,18 @@ class ConfigModel(BaseModel):
"""CSV file that contains the matrix"""

class CSVMatrix(Matrix):
"""
Class for loading CSV matrix
"""

def __init__(self, cfg: ConfigModel):
self._cfg = cfg
# Load CSV matrix
path:Path = get_root_folder() / cfg.file
self._mat = np.genfromtxt(path, delimiter=",", dtype=float)
try:
self._mat = np.genfromtxt(path, delimiter=",", dtype=float, loose=False)
except ValueError as e:
raise PyAMLException(f"CSVMatrix(file='{cfg.file}',dtype=float): {str(e)}") from None

def get_matrix(self) -> np.array:
return self._mat
Loading
Loading