Skip to content

Commit 8d7a8fd

Browse files
committed
Update MAD binaries to version 1.1.6 and adjust tests to use py_name parameter in MAD context manager
1 parent 377691d commit 8d7a8fd

File tree

6 files changed

+41
-25
lines changed

6 files changed

+41
-25
lines changed

.github/workflows/python-publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ jobs:
3636
pip install build
3737
- name: Get MAD Binaries
3838
run: |
39-
mkdir src/pymadng/bin
40-
curl https://madx.web.cern.ch/releases/madng/1.1/mad-linux-1.1.3 -o src/pymadng/bin/mad_Linux
41-
curl https://madx.web.cern.ch/releases/madng/1.1/mad-macos-1.1.3 -o src/pymadng/bin/mad_Darwin
39+
mkdir src/pymadng/bin
40+
curl https://madx.web.cern.ch/releases/madng/1.1/mad-linux-1.1.6 -o src/pymadng/bin/mad_Linux
41+
curl https://madx.web.cern.ch/releases/madng/1.1/mad-macos-1.1.6 -o src/pymadng/bin/mad_Darwin
4242
chmod +x src/pymadng/bin/mad_Linux src/pymadng/bin/mad_Darwin
4343
- name: Build package
4444
run: python -m build

.github/workflows/test-pymadng.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
name: Test pymadng
55

6-
on:
6+
on:
77
push:
88
branches:
99
- main
@@ -32,9 +32,9 @@ jobs:
3232
python-version: ${{ matrix.python-version }}
3333
- name: Get MAD Binaries
3434
run: |
35-
mkdir ./src/pymadng/bin
36-
curl https://madx.web.cern.ch/releases/madng/1.1/mad-linux-1.1.3 -o ./src/pymadng/bin/mad_Linux
37-
curl https://madx.web.cern.ch/releases/madng/1.1/mad-macos-1.1.3 -o ./src/pymadng/bin/mad_Darwin
35+
mkdir ./src/pymadng/bin
36+
curl https://madx.web.cern.ch/releases/madng/1.1/mad-linux-1.1.6 -o ./src/pymadng/bin/mad_Linux
37+
curl https://madx.web.cern.ch/releases/madng/1.1/mad-macos-1.1.6 -o ./src/pymadng/bin/mad_Darwin
3838
chmod +x ./src/pymadng/bin/mad_Linux ./src/pymadng/bin/mad_Darwin
3939
- name: Install dependencies
4040
run: |

docs/source/reference.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ PyMAD-NG Module contents
1212
Useful functions for MAD References
1313
-----------------------------------
1414

15-
.. autofunction:: pymadng.madp_classes.high_level_mad_object.convert_to_dataframe
16-
:no-index:
15+
.. autofunction:: pymadng.madp_classes.MadObject.convert_to_dataframe
16+
:no-index:
1717

18-
.. autofunction:: pymadng.madp_classes.high_level_mad_object.to_df
18+
.. autofunction:: pymadng.madp_classes.MadObject.to_df
1919
:no-index:

src/pymadng/madp_pymad.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ def __init__(
7979

8080
# Create a chunk of code to start the process
8181
lua_debug_flag = "true" if debug else "false"
82-
startup_chunk = f"MAD.pymad '{py_name}' {{_dbg = {lua_debug_flag}}} :__ini({mad_write})"
82+
startup_chunk = (
83+
f"MAD.pymad '{py_name}' {{_dbg = {lua_debug_flag}}} :__ini({mad_write})"
84+
)
8385

8486
if threading.current_thread() is threading.main_thread():
8587
self._setup_signal_handler()
@@ -125,7 +127,9 @@ def __init__(
125127
if not startup_status_checker[0] or mad_rtrn != "started":
126128
self.close()
127129
if mad_rtrn == "started":
128-
raise OSError(f"Could not establish communication with {mad_path} process")
130+
raise OSError(
131+
f"Could not establish communication with {mad_path} process"
132+
)
129133
raise OSError(f"Could not start {mad_path} process, received: {mad_rtrn}")
130134

131135
# Set the error handler to be on by default
@@ -175,7 +179,9 @@ def send_cpx_tpsa(self, monos: np.ndarray, coefficients: np.ndarray) -> None:
175179
self.mad_input_stream.write(b"ctpa")
176180
send_generic_tpsa(self, monos, coefficients, send_cpx)
177181

178-
def send(self, data: str | int | float | np.ndarray | bool | list | dict) -> MadProcess:
182+
def send(
183+
self, data: str | int | float | np.ndarray | bool | list | dict
184+
) -> MadProcess:
179185
"""Send data to the MAD-NG process.
180186
181187
Accepts several types (str, int, float, ndarray, bool, list, dict) and sends them using the appropriate serialization.
@@ -199,9 +205,13 @@ def protected_send(self, string: str) -> MadProcess:
199205
if self.raise_on_madng_error:
200206
# If the user has specified that they want to raise an error always, skip the error handling on and off
201207
return self.send(string)
202-
return self.send(f"{self.py_name}:__err(true); {string}; {self.py_name}:__err(false);")
208+
return self.send(
209+
f"{self.py_name}:__err(true); {string}; {self.py_name}:__err(false);"
210+
)
203211

204-
def protected_variable_retrieval(self, name: str, shallow_copy: bool = False) -> Any:
212+
def protected_variable_retrieval(
213+
self, name: str, shallow_copy: bool = False
214+
) -> Any:
205215
"""Safely retrieve a variable from MAD-NG.
206216
207217
Enables temporary error handling while retrieving a variable.
@@ -213,7 +223,7 @@ def protected_variable_retrieval(self, name: str, shallow_copy: bool = False) ->
213223
"""
214224
shallow_copy = str(shallow_copy).lower()
215225
if self.raise_on_madng_error:
216-
return self.send(f"py:send({name}, {shallow_copy})").recv(name)
226+
return self.send(f"{self.py_name}:send({name}, {shallow_copy})").recv(name)
217227
self.send(
218228
f"{self.py_name}:__err(true):send({name}, {shallow_copy}):__err(false)"
219229
) # Enable error handling, ask for the variable, and disable error handling
@@ -297,7 +307,9 @@ def recv_vars(self, *names, shallow_copy: bool = False) -> Any:
297307
raise ValueError("Cannot retrieve private variables from MAD-NG")
298308
if len(names) == 1:
299309
return self.protected_variable_retrieval(names[0], shallow_copy)
300-
return tuple(self.protected_variable_retrieval(name, shallow_copy) for name in names)
310+
return tuple(
311+
self.protected_variable_retrieval(name, shallow_copy) for name in names
312+
)
301313

302314
# -------------------------------------------------------------------------- #
303315

@@ -683,7 +695,9 @@ def recv_generic_matrix(self: MadProcess, dtype: np.dtype) -> str:
683695
str: A string representation of the matrix (reshaped numpy array).
684696
"""
685697
shape = read_data_stream(self, 8, np.int32)
686-
return read_data_stream(self, shape[0] * shape[1] * dtype.itemsize, dtype).reshape(shape)
698+
return read_data_stream(self, shape[0] * shape[1] * dtype.itemsize, dtype).reshape(
699+
shape
700+
)
687701

688702

689703
def recv_matrix(self: MadProcess) -> np.ndarray:
@@ -759,7 +773,9 @@ def send_generic_tpsa(
759773
assert len(monos) == len(coefficients), (
760774
"The number of monomials must be equal to the number of coefficients"
761775
)
762-
assert monos.dtype == np.uint8, "The monomials must be of type 8-bit unsigned integer "
776+
assert monos.dtype == np.uint8, (
777+
"The monomials must be of type 8-bit unsigned integer "
778+
)
763779
write_serial_data(self, "ii", len(monos), len(monos[0]))
764780
for mono in monos:
765781
self.mad_input_stream.write(mono.tobytes())

tests/inputs/example.log

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ match = MAD.match
2727
***pymad.recv: [py:__err(true):send(MAD['env']['version'], false):__err(false)] 62 bytes
2828
***pymad.send: [str_] 4 bytes
2929
***pymad.send: binary data 4 bytes
30-
***pymad.send: [1.1.3] 5 bytes
30+
***pymad.send: [1.1.6] 5 bytes
3131
***pymad.recv: binary data 4 bytes
3232
***pymad.recv: [
3333
function __mklast__ (a, b, ...)

tests/test_object_wrapping.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def test_create_deferred_expression(self):
114114

115115
class TestObjFun(unittest.TestCase):
116116
def test_call_obj(self):
117-
with MAD() as mad:
117+
with MAD(py_name="python") as mad:
118118
mad.load("element", "quadrupole", "sextupole")
119119
qd = mad.quadrupole(knl=[0, 0.25], l=1)
120120
sd = mad.sextupole(knl=[0, 0.25, 0.5], l=1)
@@ -157,7 +157,7 @@ def test_call_fail(self):
157157
self.assertRaises(RuntimeError, lambda: mad.mtable.read("'abad.tfs'").eval())
158158

159159
def test_call_func(self):
160-
with MAD() as mad:
160+
with MAD(py_name="python") as mad:
161161
mad.load("element", "quadrupole")
162162
mad["qd"] = mad.quadrupole(knl=[0, 0.25], l=1)
163163
mad.qd.select()
@@ -237,7 +237,7 @@ def test_evaluate_in_madx_environment(self):
237237

238238
class TestOps(unittest.TestCase):
239239
def test_matrix(self):
240-
with MAD() as mad:
240+
with MAD(py_name="python") as mad:
241241
mad.load("MAD", "matrix")
242242
py_mat = np.arange(1, 101).reshape((10, 10))
243243

@@ -311,7 +311,7 @@ def test_kwargs(self):
311311

312312
class TestDir(unittest.TestCase):
313313
def test_dir(self):
314-
with MAD() as mad:
314+
with MAD(py_name="python") as mad:
315315
mad.load("MAD", "gfunc", "element", "object")
316316
mad.load("element", "quadrupole")
317317
mad.load("gfunc", "functor")
@@ -330,7 +330,7 @@ def test_dir(self):
330330
) # Adding to the instance should change the dir
331331

332332
def test_dir_on_mad_object(self):
333-
with MAD() as mad:
333+
with MAD(py_name="python") as mad:
334334
mad.load("MAD", "object")
335335
mad.send("my_obj = object {a = 1, b = 2, c = 3}")
336336
expected_dir = sorted(["a", "b", "c"] + dir(mad.object))

0 commit comments

Comments
 (0)