crtomo-tools 0.2.2.dev0__tar.gz → 0.3.1__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.
Files changed (87) hide show
  1. crtomo_tools-0.3.1/AUTHORS +3 -0
  2. crtomo_tools-0.3.1/LICENSE +21 -0
  3. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/PKG-INFO +4 -4
  4. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/__init__.py +1 -0
  5. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/cfg.py +37 -11
  6. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/configManager.py +2 -2
  7. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/grid.py +223 -43
  8. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/interface.py +12 -8
  9. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/mpl.py +15 -3
  10. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/mpl_setup.py +2 -3
  11. crtomo_tools-0.3.1/lib/crtomo/notebook/__init__.py +0 -0
  12. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/about.html +548 -0
  13. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/data_import.html +548 -0
  14. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/fe_meshes.html +563 -0
  15. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/filtering.html +548 -0
  16. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/index.html +548 -0
  17. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/inv_analysis.html +548 -0
  18. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/inversion_settings.html +548 -0
  19. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/search.json +58 -0
  20. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/bootstrap/bootstrap-icons.css +2078 -0
  21. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/bootstrap/bootstrap-icons.woff +0 -0
  22. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/bootstrap/bootstrap.min.css +12 -0
  23. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/bootstrap/bootstrap.min.js +7 -0
  24. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/clipboard/clipboard.min.js +7 -0
  25. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/quarto-html/anchor.min.js +9 -0
  26. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/quarto-html/popper.min.js +6 -0
  27. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/quarto-html/quarto-syntax-highlighting.css +203 -0
  28. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/quarto-html/quarto.js +899 -0
  29. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/quarto-html/tippy.css +1 -0
  30. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/quarto-html/tippy.umd.min.js +2 -0
  31. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/quarto-nav/headroom.min.js +7 -0
  32. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/quarto-nav/quarto-nav.js +288 -0
  33. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/quarto-search/autocomplete.umd.js +3 -0
  34. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/quarto-search/fuse.min.js +9 -0
  35. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/site_libs/quarto-search/quarto-search.js +1286 -0
  36. crtomo_tools-0.3.1/lib/crtomo/notebook/manual/html/styles.css +1 -0
  37. crtomo_tools-0.3.1/lib/crtomo/notebook/nb.py +1313 -0
  38. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/parManager.py +85 -3
  39. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/plotManager.py +150 -51
  40. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/tdManager.py +489 -51
  41. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/setup.py +6 -3
  42. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/cr_trig_create.py +27 -5
  43. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/cr_trig_parse_gmsh.py +92 -51
  44. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/crtomo_tools.egg-info/PKG-INFO +4 -4
  45. crtomo_tools-0.3.1/src/crtomo_tools.egg-info/SOURCES.txt +84 -0
  46. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/crtomo_tools.egg-info/entry_points.txt +3 -1
  47. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/crtomo_tools.egg-info/top_level.txt +3 -0
  48. crtomo_tools-0.3.1/src/grid_convert_boundary_to_svg.py +56 -0
  49. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/grid_extralines_gen_decouplings.py +171 -57
  50. crtomo_tools-0.3.1/src/grid_parse_svg_to_files.py +244 -0
  51. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/grid_plot_wireframe.py +1 -1
  52. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/grid_translate.py +5 -2
  53. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/td_plot.py +37 -16
  54. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/td_residuals.py +51 -36
  55. crtomo_tools-0.3.1/src/volt_filter_using_residuals.py +117 -0
  56. crtomo_tools-0.2.2.dev0/src/crtomo_tools.egg-info/SOURCES.txt +0 -52
  57. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/analytical_solution.py +0 -0
  58. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/binaries.py +0 -0
  59. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/debug.py +0 -0
  60. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/debug_data/elec_20elecs.dat +0 -0
  61. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/debug_data/elec_40elecs.dat +0 -0
  62. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/debug_data/elem_20elecs.dat +0 -0
  63. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/debug_data/elem_40elecs.dat +0 -0
  64. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/eitManager.py +0 -0
  65. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/nodeManager.py +0 -0
  66. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/lib/crtomo/status.py +0 -0
  67. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/setup.cfg +0 -0
  68. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/cr_get_analytical_solutions.py +0 -0
  69. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/cr_get_modelling_errors.py +0 -0
  70. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/crtomo_tools.egg-info/dependency_links.txt +0 -0
  71. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/crtomo_tools.egg-info/requires.txt +0 -0
  72. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/grid_gen_decouplings.py +0 -0
  73. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/grid_gen_depth_decoupling.py +0 -0
  74. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/grid_gen_surface_files.py +0 -0
  75. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/grid_homogenize.py +0 -0
  76. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/grid_plot_elem_angles.py +0 -0
  77. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/grid_rotate.py +0 -0
  78. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/grid_translate_model.py +0 -0
  79. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/sd_plot.py +0 -0
  80. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/sens_center_plot.py +0 -0
  81. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/td_clean.py +0 -0
  82. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/td_correct_temperature.py +0 -0
  83. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/td_init.py +0 -0
  84. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/td_run_all_local.py +0 -0
  85. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/td_test.py +0 -0
  86. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/volt_correct_temperature.py +0 -0
  87. {crtomo_tools-0.2.2.dev0 → crtomo_tools-0.3.1}/src/volt_histogram.py +0 -0
@@ -0,0 +1,3 @@
1
+ Maximilian Weigand
2
+ Jasmin Budler
3
+ Jannik Fassmer
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 crtomo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,10 +1,10 @@
1
- Metadata-Version: 1.0
1
+ Metadata-Version: 2.1
2
2
  Name: crtomo_tools
3
- Version: 0.2.2.dev0
3
+ Version: 0.3.1
4
4
  Summary: CRTomo Python Toolbox
5
5
  Home-page: https://github.com/geophysics-ubonn/crtomo_tools
6
6
  Author: Maximilian Weigand
7
7
  Author-email: mweigand@geo.uni-bonn.de
8
8
  License: MIT
9
- Description: UNKNOWN
10
- Platform: UNKNOWN
9
+ License-File: LICENSE
10
+ License-File: AUTHORS
@@ -2,6 +2,7 @@ from crtomo.tdManager import tdMan
2
2
  from crtomo.tdManager import noise_model
3
3
  from crtomo.grid import crt_grid
4
4
  from crtomo.plotManager import plotManager as pltMan
5
+ from crtomo.parManager import ParMan as ParMan
5
6
  from crtomo.eitManager import eitMan
6
7
  from crtomo.configManager import ConfigManager
7
8
  from crtomo.status import td_is_finished
@@ -30,6 +30,7 @@ TODO
30
30
  * we could also add help texts here for each parameter
31
31
 
32
32
  """
33
+ import io
33
34
 
34
35
 
35
36
  class crmod_config(dict):
@@ -131,15 +132,26 @@ class crmod_config(dict):
131
132
  Write the configuration to a file. Use the correct order of values.
132
133
  """
133
134
  self._check_and_convert_bools()
134
- fid = open(filename, 'w')
135
+
136
+ if isinstance(filename, io.BytesIO):
137
+ fid = filename
138
+ else:
139
+ fid = open(filename, 'wb')
135
140
 
136
141
  for key in self.key_order:
137
- if(key == -1):
138
- fid.write('\n')
142
+ if (key == -1):
143
+ fid.write(bytes('\n', 'utf-8'))
139
144
  else:
140
- fid.write('{0}\n'.format(self[key]))
145
+ fid.write(
146
+ bytes(
147
+ '{0}\n'.format(self[key]),
148
+ 'utf-8',
149
+ )
150
+ )
141
151
 
142
- fid.close()
152
+ # do not close BytesIO stream
153
+ if not isinstance(filename, io.BytesIO):
154
+ fid.close()
143
155
 
144
156
  def __repr__(self):
145
157
  self._check_and_convert_bools()
@@ -260,15 +272,24 @@ class crtomo_config(dict):
260
272
  def write_to_file(self, filename):
261
273
  """ Write the configuration to a file. Use the correct order of values.
262
274
  """
263
- fid = open(filename, 'w')
275
+ if isinstance(filename, io.BytesIO):
276
+ fid = filename
277
+ else:
278
+ fid = open(filename, 'wb')
264
279
 
265
280
  for key in self.key_order:
266
- if(key == -1):
267
- fid.write('\n')
281
+ if (key == -1):
282
+ fid.write(bytes('\n', 'utf-8'))
268
283
  else:
269
- fid.write('{0}\n'.format(self[key]))
284
+ fid.write(
285
+ bytes(
286
+ '{0}\n'.format(self[key]),
287
+ 'utf-8',
288
+ )
289
+ )
270
290
 
271
- fid.close()
291
+ if not isinstance(filename, io.BytesIO):
292
+ fid.close()
272
293
 
273
294
  def copy(self):
274
295
  return self.__copy__()
@@ -313,7 +334,12 @@ class crtomo_config(dict):
313
334
  filename : str
314
335
  Path to crtomo.cfg file
315
336
  """
316
- lines_raw = [x.strip() for x in open(filename, 'r').readlines()]
337
+ if isinstance(filename, io.BytesIO):
338
+ line_data = filename.readlines()
339
+ else:
340
+ with open(filename, 'r') as fid:
341
+ line_data = fid.readlines()
342
+ lines_raw = [x.strip() for x in line_data]
317
343
  key_index = 0
318
344
  for line in lines_raw:
319
345
  # ignore comments
@@ -211,10 +211,10 @@ class ConfigManager(reda_config_mgr.ConfigManager):
211
211
  Parameters
212
212
  ----------
213
213
  data_source : string|numpy.ndarray
214
- if this is a string, treat it as an input filename. If it is a Nx5
214
+ if this is a string, treat it as an input filename. If it is a Nx6
215
215
  or Nx3 numpy array, use this data directly
216
216
  is_forward_response : bool, optional
217
- If True this indicated volt.dat file created by CRTomo during an
217
+ If True this indicates a volt.dat file created by CRTomo during an
218
218
  inversion run to store forward responses of a given model
219
219
  iteration. In this case the third data column indicates the wdfak
220
220
  parameter, i.e., it indicates if a given data point was excluded
@@ -49,6 +49,7 @@ import tempfile
49
49
  import subprocess
50
50
  import os
51
51
  import time
52
+ import io
52
53
 
53
54
  import numpy as np
54
55
  import scipy.sparse
@@ -109,6 +110,14 @@ class crt_grid(object):
109
110
  self.nr_of_nodes = None
110
111
  self.nr_of_electrodes = None
111
112
 
113
+ # can hold an KDTree of the element centroids
114
+ self.centroid_tree = None
115
+ # can hold an KDTree of the node coordinates
116
+ self.node_tree = None
117
+
118
+ # can hold the distance matrix of the mesh, as a csr_array
119
+ self.distance_matrix = None
120
+
112
121
  # will be used for caching by .get_element_centroids
113
122
  self.centroids = None
114
123
 
@@ -230,10 +239,6 @@ class crt_grid(object):
230
239
  """
231
240
  nodes = {}
232
241
 
233
- # # prepare nodes
234
- # nodes_sorted = np.zeros((number_of_nodes, 3), dtype=float)
235
- # nodes = np.zeros((number_of_nodes, 3), dtype=float)
236
-
237
242
  # read in nodes
238
243
  nodes_raw = np.empty((self.header['nr_nodes'], 3), dtype=float)
239
244
  for nr in range(0, self.header['nr_nodes']):
@@ -248,7 +253,10 @@ class crt_grid(object):
248
253
  # check for CutMcK
249
254
  # The check is based on the first node, but if one node was renumbered,
250
255
  # so were all the others.
251
- if(nodes_raw[:, 0] != list(range(1, nodes_raw.shape[0]))):
256
+ if (
257
+ ~np.all(
258
+ nodes_raw[:, 0] == list(
259
+ range(1, nodes_raw.shape[0] + 1)))):
252
260
  self.header['cutmck'] = True
253
261
  print(
254
262
  'This grid was sorted using CutMcK. The nodes were resorted!')
@@ -256,7 +264,7 @@ class crt_grid(object):
256
264
  self.header['cutmck'] = False
257
265
 
258
266
  # Rearrange nodes when CutMcK was used.
259
- if(self.header['cutmck']):
267
+ if (self.header['cutmck']):
260
268
  nodes_cutmck = np.empty_like(nodes_raw)
261
269
  nodes_cutmck_index = np.zeros(nodes_raw.shape[0], dtype=int)
262
270
  for node in range(0, self.header['nr_nodes']):
@@ -318,7 +326,7 @@ class crt_grid(object):
318
326
  TODO: We want some nice way of not needing to know in the future if we
319
327
  loaded triangles or quadratic elements.
320
328
  """
321
- if(self.header['element_infos'][0, 2] == 3):
329
+ if (self.header['element_infos'][0, 2] == 3):
322
330
  print('Triangular grid found')
323
331
  self.grid_is_rectangular = False
324
332
 
@@ -401,14 +409,21 @@ class crt_grid(object):
401
409
  raise Exception('Not enough neighbors in file')
402
410
 
403
411
  def load_elem_file(self, elem_file):
404
- """
412
+ """Load a CRTomo/CRMod elem.dat mesh file from either a file, or from
413
+ stringIO
405
414
 
406
415
  """
407
- with open(elem_file, 'r') as fid:
408
- self._read_elem_header(fid)
409
- self._read_elem_nodes(fid)
410
- self._read_elem_elements(fid)
411
- self._read_elem_neighbors(fid)
416
+ if isinstance(elem_file, (io.StringIO, io.BytesIO,)):
417
+ # the StringIO object can directly be used
418
+ fid = elem_file
419
+ else:
420
+ fid = open(elem_file, 'r')
421
+ self._read_elem_header(fid)
422
+ self._read_elem_nodes(fid)
423
+ self._read_elem_elements(fid)
424
+ self._read_elem_neighbors(fid)
425
+ fid.close()
426
+
412
427
  self._prepare_grids()
413
428
 
414
429
  def save_elem_file(self, output):
@@ -421,35 +436,51 @@ class crt_grid(object):
421
436
  filename: string
422
437
  output filename
423
438
  """
424
- with open(output, 'wb') as fid:
425
- self._write_elem_header(fid)
426
- self._write_nodes(fid)
427
- self._write_elements(fid)
428
- self._write_neighbors(fid)
439
+ if isinstance(output, (io.StringIO, io.BytesIO,)):
440
+ fid = output
441
+ else:
442
+ fid = open(output, 'wb')
443
+ self._write_elem_header(fid)
444
+ self._write_nodes(fid)
445
+ self._write_elements(fid)
446
+ self._write_neighbors(fid)
447
+ if not isinstance(fid, io.BytesIO):
448
+ fid.close()
429
449
 
430
450
  def save_elec_file(self, filename):
431
- with open(filename, 'wb') as fid:
432
- fid.write(
433
- bytes(
434
- '{0}\n'.format(int(self.electrodes.shape[0])),
435
- 'utf-8',
436
- )
451
+ if isinstance(filename, (io.StringIO, io.BytesIO,)):
452
+ fid = filename
453
+ else:
454
+ fid = open(filename, 'wb')
455
+ fid.write(
456
+ bytes(
457
+ '{0}\n'.format(int(self.electrodes.shape[0])),
458
+ 'utf-8',
437
459
  )
438
- # the + 1 fixes the zero-indexing
439
- np.savetxt(fid, self.electrodes[:, 0].astype(int) + 1, fmt='%i')
460
+ )
461
+ # the + 1 fixes the zero-indexing
462
+ np.savetxt(
463
+ fid, self.electrodes[:, 0].astype(int) + 1, fmt='%i',
464
+ encoding='utf-8',
465
+ )
466
+ if not isinstance(fid, io.BytesIO):
467
+ fid.close()
440
468
 
441
469
  def _write_neighbors(self, fid):
442
470
  for key in (11, 12):
443
471
  if key in self.neighbors:
444
- np.savetxt(fid, self.neighbors[key], fmt='%i')
472
+ np.savetxt(
473
+ fid, self.neighbors[key], fmt='%i', encoding='utf-8'
474
+ )
445
475
 
446
476
  def _write_elements(self, fid):
447
477
  for dtype in self.header['element_infos'][:, 0]:
448
478
  for elm in self.element_data[dtype]:
449
- np.savetxt(fid, np.atleast_2d(elm.nodes), fmt='%i')
479
+ np.savetxt(
480
+ fid, np.atleast_2d(elm.nodes), fmt='%i', encoding='utf-8')
450
481
 
451
482
  def _write_nodes(self, fid):
452
- np.savetxt(fid, self.nodes['raw'], fmt='%i %f %f')
483
+ np.savetxt(fid, self.nodes['raw'], fmt='%i %f %f', encoding='utf-8')
453
484
 
454
485
  def _write_elem_header(self, fid):
455
486
  fid.write(
@@ -461,10 +492,17 @@ class crt_grid(object):
461
492
  'utf-8',
462
493
  )
463
494
  )
464
- np.savetxt(fid, self.header['element_infos'], fmt='%i')
495
+ np.savetxt(
496
+ fid, self.header['element_infos'], fmt='%i', encoding='utf-8')
465
497
 
466
498
  def load_elec_file(self, elec_file):
467
- electrode_nodes_raw = np.loadtxt(elec_file, skiprows=1, dtype=int) - 1
499
+ if isinstance(elec_file, (io.StringIO, io.BytesIO,)):
500
+ # the StringIO object can directly be used
501
+ fid = elec_file
502
+ else:
503
+ fid = open(elec_file, 'r')
504
+ electrode_nodes_raw = np.loadtxt(fid, skiprows=1, dtype=int) - 1
505
+ fid.close()
468
506
  self.electrodes = self.nodes['presort'][electrode_nodes_raw]
469
507
  self.nr_of_electrodes = self.electrodes.shape[0]
470
508
 
@@ -480,7 +518,7 @@ class crt_grid(object):
480
518
  node number as in self.nodes['sorted']
481
519
  """
482
520
  elec_node_raw = int(self.electrodes[electrode - 1][0])
483
- if(self.header['cutmck']):
521
+ if (self.header['cutmck']):
484
522
  elec_node = self.nodes['rev_cutmck_index'][elec_node_raw]
485
523
  else:
486
524
  elec_node = elec_node_raw - 1
@@ -513,6 +551,8 @@ class crt_grid(object):
513
551
  )
514
552
  ax.set_xlim(self.grid['x'].min(), self.grid['x'].max())
515
553
  ax.set_ylim(self.grid['z'].min(), self.grid['z'].max())
554
+ ax.set_xlabel('x [m]')
555
+ ax.set_ylabel('z [m]')
516
556
  # ax.autoscale_view()
517
557
  ax.set_aspect('equal')
518
558
 
@@ -723,6 +763,57 @@ class crt_grid(object):
723
763
 
724
764
  @property
725
765
  def element_neighbors(self):
766
+ if self.element_neighbors_data is not None:
767
+ return self.element_neighbors_data
768
+
769
+ if self.centroid_tree is None:
770
+ centroids = self.get_element_centroids()
771
+ self.centroid_tree = scipy.spatial.cKDTree(centroids)
772
+
773
+ max_nr_edges = self.header['element_infos'][0, 2]
774
+
775
+ A = self.get_element_areas().max()
776
+ # A = pi * r^2
777
+ radius = np.sqrt(A / np.pi)
778
+
779
+ # initialize the neighbor array
780
+ self.element_neighbors_data = []
781
+ self.element_neighbors_edges = []
782
+
783
+ print('Looking for neighbors (V2 with cKDTree)')
784
+ time_start = time.perf_counter()
785
+ for nr, element_nodes in enumerate(self.elements):
786
+ neighbors_edges = []
787
+ neighbors_indices = []
788
+ # r = set(self.centroid_tree.query(centroids[nr], max_nr_edges)[1])
789
+ r = set(
790
+ self.centroid_tree.query_ball_point(
791
+ centroids[nr],
792
+ 2 * radius
793
+ )
794
+ )
795
+ for index in list(r):
796
+ el_test = self.elements[index]
797
+ intersect_nodes = np.intersect1d(element_nodes, el_test)
798
+ if len(intersect_nodes) == 2:
799
+ neighbors_indices += [index]
800
+ neighbors_edges += [intersect_nodes]
801
+ # if len(intersect_nodes) in (0, 1, 3):
802
+ # r.remove(index)
803
+ # neighbors_edges.append(intersect_nodes)
804
+ if len(neighbors_indices) == max_nr_edges:
805
+ break
806
+ self.element_neighbors_edges.append(neighbors_edges)
807
+ self.element_neighbors_data.append(neighbors_indices)
808
+
809
+ # import IPython
810
+ # IPython.embed()
811
+ time_end = time.perf_counter()
812
+ print('elapsed time: {} s'.format(time_end - time_start))
813
+ return self.element_neighbors_data
814
+
815
+ @property
816
+ def element_neighbors_old(self):
726
817
  """Return a list with element numbers (zero indexed) of neighboring
727
818
  elements. Note that the elements are not sorted. No spacial orientation
728
819
  can be inferred from the order of neighbors.
@@ -953,7 +1044,7 @@ class crt_grid(object):
953
1044
 
954
1045
  """
955
1046
  # check if all required information are present
956
- if(electrodes_x is None and
1047
+ if (electrodes_x is None and
957
1048
  (nr_electrodes is None or spacing is None)):
958
1049
  raise Exception(
959
1050
  'You must provide either the parameter "electrodes_" or ' +
@@ -994,11 +1085,11 @@ class crt_grid(object):
994
1085
  minimum_z = np.min(electrodes[:, 1]) - depth
995
1086
  # maximum_z = 0
996
1087
 
997
- boundary_noflow = 11
998
- boundary_mixed = 12
1088
+ boundary_noflow = 12
1089
+ boundary_mixed = 11
999
1090
 
1000
1091
  if force_neumann_only:
1001
- boundary_mixed = 11
1092
+ boundary_mixed = 12
1002
1093
 
1003
1094
  # prepare extra lines
1004
1095
  extra_lines = []
@@ -1085,19 +1176,25 @@ class crt_grid(object):
1085
1176
 
1086
1177
  np.savetxt(
1087
1178
  tempdir + os.sep + 'electrodes.dat', electrodes,
1088
- fmt='%.3f %.3f'
1179
+ fmt='%.3f %.3f',
1180
+ encoding='utf-8',
1181
+ )
1182
+ np.savetxt(
1183
+ tempdir + os.sep + 'boundaries.dat', boundaries,
1184
+ fmt='%.3f %.3f %i',
1185
+ encoding='utf-8',
1089
1186
  )
1090
- np.savetxt(tempdir + os.sep + 'boundaries.dat', boundaries,
1091
- fmt='%.3f %.3f %i')
1092
1187
  np.savetxt(
1093
1188
  tempdir + os.sep + 'char_length.dat',
1094
- np.atleast_1d(char_lengths)
1189
+ np.atleast_1d(char_lengths),
1190
+ encoding='utf-8',
1095
1191
  )
1096
1192
  if extra_lines:
1097
1193
  np.savetxt(
1098
1194
  tempdir + os.sep + 'extra_lines.dat',
1099
1195
  np.atleast_2d(extra_lines),
1100
- fmt='%.3f %.3f %.3f %.3f'
1196
+ fmt='%.3f %.3f %.3f %.3f',
1197
+ encoding='utf-8',
1101
1198
  )
1102
1199
  pwd = os.getcwd()
1103
1200
  os.chdir(tempdir)
@@ -1140,8 +1237,10 @@ class crt_grid(object):
1140
1237
  )
1141
1238
  centroids = self.get_element_centroids()
1142
1239
 
1143
- tree = scipy.spatial.cKDTree(centroids)
1144
- element_indices = tree.query(points)[1]
1240
+ if self.centroid_tree is None:
1241
+ self.centroid_tree = scipy.spatial.cKDTree(centroids)
1242
+
1243
+ element_indices = self.centroid_tree.query(points)[1]
1145
1244
  return element_indices
1146
1245
 
1147
1246
  def get_element_indices_within_rectangle(self, xmin, xmax, zmin, zmax):
@@ -1244,3 +1343,84 @@ class crt_grid(object):
1244
1343
  ][i].zcoords = self.element_data[
1245
1344
  element_type
1246
1345
  ][i].zcoords[::-1]
1346
+
1347
+ def get_distance_matrix(self):
1348
+ if self.distance_matrix is not None:
1349
+ return self.distance_matrix
1350
+
1351
+ N = self.nr_of_nodes
1352
+ self.distance_matrix = scipy.sparse.lil_array((N, N))
1353
+ for element in self.elements:
1354
+ # works only for triangles!
1355
+ for a, b in zip((0, 1, 2), (1, 2, 0)):
1356
+ index_a = element[a]
1357
+ index_b = element[b]
1358
+
1359
+ coords_a = self.nodes['presort'][element[a]][1:3]
1360
+ coords_b = self.nodes['presort'][element[b]][1:3]
1361
+
1362
+ # compute distance between nodes
1363
+ distance = np.linalg.norm(
1364
+ coords_a - coords_b
1365
+ )
1366
+
1367
+ self.distance_matrix[index_a, index_b] = distance
1368
+ self.distance_matrix[index_b, index_a] = distance
1369
+ return self.distance_matrix
1370
+
1371
+ def get_node_tree(self):
1372
+ if self.node_tree is not None:
1373
+ return self.node_tree
1374
+ self.node_tree = scipy.spatial.KDTree(
1375
+ self.nodes['presort'][:, 1:3]
1376
+ )
1377
+ return self.node_tree
1378
+
1379
+ def find_nearest_node(self, coords, return_node_coords=True):
1380
+ node_tree = self.get_node_tree()
1381
+ result = node_tree.query(coords, 1)[1]
1382
+ if return_node_coords:
1383
+ return result, self.nodes['presort'][result][1:3]
1384
+ return result
1385
+
1386
+ def determine_path_along_nodes(self, start_coordinate, end_coordinate):
1387
+ """
1388
+
1389
+ """
1390
+ start, (sx, sy) = self.find_nearest_node(start_coordinate, True)
1391
+ end, (ex, ey) = self.find_nearest_node(end_coordinate, True)
1392
+
1393
+ path_dist, path_pred = scipy.sparse.csgraph.shortest_path(
1394
+ self.get_distance_matrix(),
1395
+ directed=False,
1396
+ return_predecessors=True,
1397
+ unweighted=False,
1398
+ )
1399
+ path_nodes = []
1400
+ path_pred[start]
1401
+
1402
+ # determine the nodes of the path
1403
+ N = self.nr_of_elements
1404
+ index = end
1405
+ path_nodes += [end]
1406
+ for i in range(N):
1407
+ next_index = path_pred[start][index]
1408
+ if next_index > 0:
1409
+ path_nodes += [path_pred[start][index]]
1410
+ index = next_index
1411
+ else:
1412
+ break
1413
+
1414
+ # determine elements adjacent to the path
1415
+ el_pairs = []
1416
+ for i in range(len(path_nodes) - 1):
1417
+ a1 = path_nodes[i]
1418
+ a2 = path_nodes[i+1]
1419
+ relevant_elements = np.intersect1d(
1420
+ np.where(np.any(self.elements == a1, axis=1))[0],
1421
+ np.where(np.any(self.elements == a2, axis=1))[0]
1422
+ )
1423
+
1424
+ if len(relevant_elements) == 2:
1425
+ el_pairs += [np.hstack(([a1, a2], relevant_elements))]
1426
+ return np.array(el_pairs)
@@ -73,7 +73,7 @@ class crmod_interface(object):
73
73
  tdm.register_phase_model(pid_pha)
74
74
  return tdm
75
75
 
76
- def forward_complex(self, log_sigma):
76
+ def forward_complex(self, log_sigma, silent=True):
77
77
  r"""Compute a model response, i.e. complex impedances
78
78
 
79
79
  Parameters
@@ -82,6 +82,8 @@ class crmod_interface(object):
82
82
  Model parameters. First column: :math:`log_e(\sigma)`, second
83
83
  column: :math:`\phi_{cond} [mrad]`. If first dimension is of
84
84
  length one, assume phase values to be zero.
85
+ silent : bool (True)
86
+ If True, suppress output of CRMod
85
87
 
86
88
  Returns
87
89
  -------
@@ -97,14 +99,14 @@ class crmod_interface(object):
97
99
  rpha = -log_sigma[1, :]
98
100
  m = np.vstack((rmag, rpha)).T
99
101
  tdm = self._get_tdm(m)
100
- measurements = tdm.measurements()
102
+ measurements = tdm.measurements(silent=silent)
101
103
  # convert R to log Y
102
104
  measurements[:, 0] = np.log(1.0 / measurements[:, 0])
103
105
  # convert rpha to cpha
104
106
  measurements[:, 1] *= -1
105
107
  return measurements
106
108
 
107
- def J(self, log_sigma):
109
+ def J(self, log_sigma, silent=True):
108
110
  """Return the sensitivity matrix
109
111
 
110
112
  Parameters
@@ -119,6 +121,7 @@ class crmod_interface(object):
119
121
  tdm.model(
120
122
  sensitivities=True,
121
123
  # output_directory=stage_dir + 'modeling',
124
+ silent=silent,
122
125
  )
123
126
 
124
127
  measurements = tdm.measurements()
@@ -156,7 +159,7 @@ class crmod_interface(object):
156
159
 
157
160
  return sensitivities_log
158
161
 
159
- def fwd_complex_R_sigma(self, model_ccomplex):
162
+ def fwd_complex_R_sigma(self, model_ccomplex, silent=False):
160
163
  """Compute the model response from linear complex conductivities
161
164
 
162
165
  Parameters
@@ -169,14 +172,14 @@ class crmod_interface(object):
169
172
  ) * 1000
170
173
  model_rmag_rpha = np.vstack((m_rmag, m_rpha)).T
171
174
  tdm = self._get_tdm(model_rmag_rpha)
172
- measurements = tdm.measurements()
175
+ measurements = tdm.measurements(silent=silent)
173
176
  rmag = measurements[:, 0]
174
177
  rpha = measurements[:, 1]
175
178
 
176
179
  rcomplex = rmag * np.exp(1j * rpha / 1000)
177
180
  return rcomplex
178
181
 
179
- def fwd_complex_R_rho(self, model_rcomplex):
182
+ def fwd_complex_R_rho(self, model_rcomplex, silent=False):
180
183
  """Compute the forward response.
181
184
 
182
185
  Parameters
@@ -196,14 +199,14 @@ class crmod_interface(object):
196
199
  ) * 1000
197
200
  model_rmag_rpha = np.vstack((m_rmag, m_rpha)).T
198
201
  tdm = self._get_tdm(model_rmag_rpha)
199
- measurements = tdm.measurements()
202
+ measurements = tdm.measurements(silent=silent)
200
203
  rmag = measurements[:, 0]
201
204
  rpha = measurements[:, 1]
202
205
 
203
206
  rcomplex = rmag * np.exp(1j * rpha / 1000)
204
207
  return rcomplex
205
208
 
206
- def J_complex_R_sigma(self, model_ccomplex):
209
+ def J_complex_R_sigma(self, model_ccomplex, silent=False):
207
210
  """Compute the Jacobian
208
211
 
209
212
  """
@@ -220,6 +223,7 @@ class crmod_interface(object):
220
223
  tdm.model(
221
224
  sensitivities=True,
222
225
  # output_directory=stage_dir + 'modeling',
226
+ silent=silent,
223
227
  )
224
228
 
225
229
  # measurements = tdm.measurements()