flekspy 0.3.2__tar.gz → 0.3.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flekspy
3
- Version: 0.3.2
3
+ Version: 0.3.4
4
4
  Summary: Python utilities for processing FLEKS data
5
5
  Author-email: Yuxi Chen <yuxichen@umich.edu>, Hongyang Zhou <hyzhou@umich.edu>
6
6
  License-File: LICENSE.md
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "flekspy"
3
- version = "0.3.2"
3
+ version = "0.3.4"
4
4
  description = "Python utilities for processing FLEKS data"
5
5
  authors = [
6
6
  {name = "Yuxi Chen", email = "yuxichen@umich.edu"},
@@ -212,14 +212,14 @@ class IDLData(object):
212
212
  with open(saveName, "w") as f:
213
213
  f.write(self.unit + "\n")
214
214
  f.write(
215
- "{:d}\t{:e}\t{:d}\t{:d}\t{:d}\n".format(
215
+ "{0:d}\t{1:e}\t{2:d}\t{3:d}\t{4:d}\n".format(
216
216
  self.iter, self.time, self.ndim, self.nparam, self.nvar
217
217
  )
218
218
  )
219
- [f.write("{:d}\t".format(i)) for i in self.grid]
219
+ [f.write("{0:d}\t".format(i)) for i in self.grid]
220
220
  f.write("\n")
221
221
  if self.nparam > 0:
222
- [f.write("{:e}\t".format(i)) for i in self.para]
222
+ [f.write("{0:e}\t".format(i)) for i in self.para]
223
223
  f.write("\n")
224
224
  [f.write(i + " ") for i in self.variables]
225
225
  f.write("\n")
@@ -231,7 +231,7 @@ class IDLData(object):
231
231
  for jj in range(nj):
232
232
  for ii in range(ni):
233
233
  [
234
- f.write("{:e}\t".format(i))
234
+ f.write("{0:e}\t".format(i))
235
235
  for i in self.data.array[:, ii, jj, kk]
236
236
  ]
237
237
  f.write("\n")
@@ -424,30 +424,30 @@ class IDLData(object):
424
424
  def read_binary_instance(self, infile):
425
425
  self.get_file_head(infile)
426
426
  nrow = self.ndim + self.nvar
427
- self.data.array = np.zeros((nrow, self.npoints), dtype=np.float32)
428
427
 
429
- # Get the grid points...
428
+ if self.pformat == "f":
429
+ dtype = np.float32
430
+ else:
431
+ dtype = np.float64
432
+
433
+ self.data.array = np.empty((nrow, self.npoints), dtype=dtype)
434
+ dtype_str = f"{self.end_char}{self.pformat}"
435
+
436
+ # Get the grid points
430
437
  (old_len, record_len) = struct.unpack(self.end_char + "2l", infile.read(8))
431
- for i in range(0, self.ndim):
432
- # Read the data into a temporary grid
433
- tempgrid = np.array(
434
- struct.unpack(
435
- "{0}{1}{2}".format(self.end_char, self.npoints, self.pformat),
436
- infile.read(int(record_len // self.ndim)),
437
- )
438
- )
439
- self.data.array[i, :] = tempgrid
438
+ buffer = infile.read(record_len)
439
+ grid_data = np.frombuffer(
440
+ buffer, dtype=dtype_str, count=self.npoints * self.ndim
441
+ )
442
+ self.data.array[0 : self.ndim, :] = grid_data.reshape((self.ndim, self.npoints))
440
443
 
441
444
  # Get the actual data and sort
442
445
  for i in range(self.ndim, self.nvar + self.ndim):
443
446
  (old_len, record_len) = struct.unpack(self.end_char + "2l", infile.read(8))
444
- tmp = np.array(
445
- struct.unpack(
446
- "{0}{1}{2}".format(self.end_char, self.npoints, self.pformat),
447
- infile.read(record_len),
448
- )
447
+ buffer = infile.read(record_len)
448
+ self.data.array[i, :] = np.frombuffer(
449
+ buffer, dtype=dtype_str, count=self.npoints
449
450
  )
450
- self.data.array[i, :] = tmp
451
451
  # Consume the last record length
452
452
  infile.read(4)
453
453
 
@@ -119,19 +119,27 @@ class DataContainer(object):
119
119
  expression (str): Python codes to be executed
120
120
  Example: expression = "np.log({rhos0}+{rhos1})"
121
121
  """
122
+ import re
122
123
 
123
- if expression.find("{") < 0:
124
+ if "{" not in expression:
124
125
  return self.get_variable(expression, unit)
125
126
 
126
- exp1 = expression.replace("{", "self.get_variable('")
127
+ # A dictionary to store the variables for eval.
128
+ eval_context = {"np": np}
127
129
 
128
- exp2 = exp1.replace("}", "',unit)")
129
- exp2 = r"result=" + exp2
130
+ def repl(match):
131
+ var_name = match.group(1)
132
+ # Add the variable to the context for eval.
133
+ # Use a name that is valid for a python identifier.
134
+ eval_context[var_name] = self.get_variable(var_name, unit)
135
+ return var_name
130
136
 
131
- print(exp2)
132
- exec(exp2)
137
+ # Replace `{var}` with `var` and populate `eval_context`.
138
+ expression_for_eval = re.sub(r"\{(.*?)\}", repl, expression)
133
139
 
134
- return locals()["result"]
140
+ # Evaluate the expression in the prepared context.
141
+ # We provide an empty `__builtins__` to restrict access to other built-in functions for security.
142
+ return eval(expression_for_eval, {"__builtins__": {}}, eval_context)
135
143
 
136
144
  def add_variable(self, name, val):
137
145
  r"""
@@ -161,6 +169,7 @@ class DataContainer(object):
161
169
  Return: YTArray
162
170
  """
163
171
 
172
+ ytarr = None
164
173
  if var in self.data.keys():
165
174
  varUnit = get_unit(var, unit)
166
175
  ytarr = self.data[var]
@@ -202,6 +211,9 @@ class DataContainer(object):
202
211
  varUnit = "dimensionless"
203
212
  ytarr = yt.YTArray(ytarr, varUnit)
204
213
 
214
+ if ytarr is None:
215
+ raise KeyError(f"Variable '{var}' not found in dataset.")
216
+
205
217
  return ytarr if str(ytarr.units) == "dimensionless" else ytarr.in_units(varUnit)
206
218
 
207
219
 
@@ -323,8 +335,8 @@ class DataContainer2D(DataContainer):
323
335
  nlevels: int = 200,
324
336
  cmap: str = "turbo",
325
337
  figsize=(10, 6),
326
- f = None,
327
- axes = None,
338
+ f=None,
339
+ axes=None,
328
340
  pcolor=False,
329
341
  logscale=False,
330
342
  addgrid=False,
@@ -152,7 +152,7 @@ def download_testfile(url: str, target_path="."):
152
152
  target_dir.mkdir(parents=True, exist_ok=True)
153
153
 
154
154
  with tarfile.open(temp_file, "r:gz") as tar:
155
- tar.extractall(target_dir)
155
+ tar.extractall(target_dir, filter="data")
156
156
 
157
157
  except requests.exceptions.RequestException as e:
158
158
  print(f"Error downloading file: {e}")
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes