Skip to content

Commit bea6170

Browse files
committed
faster translation
1 parent 1c87bc9 commit bea6170

7 files changed

+133
-35
lines changed

libmanager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ def check4zaid(self, zaid):
116116
117117
"""
118118
libraries = []
119-
for table in self.XS.find_table(zaid):
120-
libraries.append(table.name.split('.')[-1])
119+
for libname in self.XS.find_table(zaid, mode='default-fast'):
120+
libraries.append(libname)
121121

122122
return libraries
123123

matreader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ def from_input(cls, inputfile):
993993
----------
994994
cls : TYPE
995995
DESCRIPTION.
996-
inputfile : inputfile.InputFile
996+
inputfile : os.PathLike
997997
MCNP input file containing the material section.
998998
999999
Returns

tests/.coverage

0 Bytes
Binary file not shown.

tests/inputfile_test.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ def test_change_density(self):
102102
newinp = deepcopy(self.testInput)
103103
density = 1
104104
newinp.change_density(density, cellidx=2)
105-
cellcard = newinp.get_card_byID('cells', '2')
106105
modline = '2 13 {} -128 129 1 -2 \n'.format(str(density))
107106
assert newinp.cards['cells'][2].lines == modline
108107

@@ -273,14 +272,15 @@ def test_add_track_contribution(self):
273272
card = newinp.get_card_byID('settings', 'FU124')
274273
assert card.lines[0] == 'FU124 0 1001 1002\n'
275274

275+
276276
class TestD1S5_Input:
277277
inp = D1S5_InputFile.from_text(DIS_INP_PATH)
278278
irrad = IrradiationFile.from_text(IRRAD_PATH)
279279
react = ReactionFile.from_text(REACT_PATH)
280280

281281
lm = LibManager(XSDIR_FILE, activationfile=ACTIVATION_FILE,
282282
isotopes_file=ISOTOPES_FILE)
283-
283+
284284
def test_add_stopCard(self):
285285
precision = 'F5-0.001'
286286
ctme = 500
@@ -298,5 +298,3 @@ def test_add_stopCard(self):
298298
assert False
299299
except ValueError:
300300
assert True
301-
302-

tests/utilitiesgui_test.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@
3737
XSDIR_FILE = os.path.join(cp, 'TestFiles', 'libmanager', 'xsdir')
3838
ISOTOPES_FILE = os.path.join(modules_path, 'Isotopes.txt')
3939

40+
4041
class SessionMockup:
41-
42+
4243
def __init__(self):
4344
self.lib_manager = LibManager(XSDIR_FILE,
4445
activationfile=ACTIVATION_FILE,
4546
isotopes_file=ISOTOPES_FILE)
4647

48+
4749
class TestUtilities:
4850
session = SessionMockup()
4951
inputfile = os.path.join(cp, 'TestFiles', 'utilitiesgui', 'test.i')
@@ -58,7 +60,7 @@ def test_translate_input(self):
5860
outpath=self.outpath)
5961
shutil.rmtree(self.outpath)
6062
assert ans
61-
63+
6264
def test_print_libraries(self):
6365
"""
6466
This is properly tested in libmanager_test
@@ -101,7 +103,7 @@ def test_generate_material(self):
101103
txt_equal(fileA, fileB)
102104

103105
shutil.rmtree(outpath)
104-
106+
105107
def test_switch_fractions(self):
106108

107109
# Switches are properly tested in matreader
@@ -111,7 +113,7 @@ def test_switch_fractions(self):
111113
outpath=self.outpath)
112114
shutil.rmtree(self.outpath)
113115
assert True
114-
116+
115117
def test_change_ACElib_suffix(self, monkeypatch):
116118
acefolder = os.path.join(cp, 'TestFiles', 'utilitiesgui', 'ACEchange',
117119
'99c')
@@ -133,7 +135,7 @@ def test_change_ACElib_suffix(self, monkeypatch):
133135
assert False
134136
break
135137
shutil.rmtree(newfolder)
136-
138+
137139
def test_get_reaction_file(self, monkeypatch):
138140
# The correctness of the file is already tested in parserD1S
139141
filepath = os.path.join(cp, 'TestFiles', 'utilitiesgui', 'd1stest.i')
@@ -143,13 +145,22 @@ def test_get_reaction_file(self, monkeypatch):
143145
shutil.rmtree(self.outpath)
144146
assert True
145147

148+
def test_input_with_option(self, monkeypatch):
149+
msg = ''
150+
options = ['option1', 'option2']
151+
inputs = iter(['wrongoption', 'option1'])
152+
monkeypatch.setattr('builtins.input', lambda msg: next(inputs))
153+
valid_input = uty.input_with_options(msg, options)
154+
assert valid_input == 'option1'
155+
146156

147157
def excel_equal(fileA, fileB, n_sheets):
148158
for i in range(n_sheets):
149159
sheetA = pd.read_excel(fileA, sheet_name=i)
150160
sheetB = pd.read_excel(fileB, sheet_name=i)
151161
assert sheetA.equals(sheetB)
152162

163+
153164
def txt_equal(fileA, fileB):
154165
with open(fileA, 'r') as infileA, open(fileB, 'r') as infileB:
155166
for lineA, lineB in zip(infileA, infileB):

tests/xsdirpyne_test.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
@author: Davide Laghi
3+
4+
Copyright 2021, the JADE Development Team. All rights reserved.
5+
6+
This file is part of JADE.
7+
8+
JADE is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation, either version 3 of the License, or
11+
(at your option) any later version.
12+
13+
JADE is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with JADE. If not, see <http://www.gnu.org/licenses/>.
20+
"""
21+
22+
from xsdirpyne import Xsdir
23+
24+
import sys
25+
import os
26+
27+
cp = os.path.dirname(os.path.abspath(__file__))
28+
modules_path = os.path.dirname(cp)
29+
sys.path.insert(1, modules_path)
30+
31+
XSDIR_FILE = os.path.join(cp, 'TestFiles', 'libmanager', 'xsdir')
32+
33+
34+
class TestXsdir:
35+
"""No need to test the entire class since it is from Pyne distro.
36+
New functionalities or modifications will be tested instead
37+
"""
38+
39+
xsdir = Xsdir(XSDIR_FILE)
40+
41+
def test_find_table(self):
42+
names = ['1001.31c', '20000.21c', '8016.00c']
43+
for zaidname in names:
44+
ans = self.xsdir.find_table(zaidname, mode='exact')
45+
assert ans
46+
47+
zaidname = '1001'
48+
tables = self.xsdir.find_table(zaidname, mode='default')
49+
assert len(tables) == 43
50+
assert tables[0].name == '1001.03c'
51+
52+
zaidname = '1001'
53+
libs = self.xsdir.find_table(zaidname, mode='default-fast')
54+
assert len(libs) == 43
55+
assert libs[0] == '03c'
56+
57+
def test_find_zaids(self):
58+
lib = '21c'
59+
zaids = self.xsdir.find_zaids(lib)
60+
print(zaids)
61+
assert len(zaids) == 80

xsdirpyne.py

+51-23
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
or implied, of the stakeholders of the PyNE project or the employers of PyNE developers.
3232
"""
3333
import os
34+
from typing import List, Tuple
3435

3536

3637
class Xsdir(object):
@@ -72,6 +73,16 @@ def __init__(self, filename):
7273

7374
self.read()
7475

76+
# It is useful to have a list of the available tables names to be
77+
# computed only once at initializations
78+
tablenames = []
79+
for table in self:
80+
name = table.name
81+
zaidname = name[:-4]
82+
libname = name[-3:]
83+
tablenames.append((zaidname, libname))
84+
self.tablenames = tablenames
85+
7586
def read(self):
7687
"""Populate the Xsdir object by reading the file.
7788
"""
@@ -142,9 +153,14 @@ def read(self):
142153
if len(words) > 10:
143154
table.ptable = (words[10] == 'ptable')
144155

145-
146-
def find_table(self, name, mode = 'default'):
156+
def find_table(self, name, mode='default'):
147157
"""Find all tables for a given ZIAD.
158+
*Modified for JADE, a bug was corrected since table.name do not
159+
find natural zaids.
160+
161+
mode: 'default' default behaviour
162+
'exact' check also the library
163+
'default-fast' accelerated loop giving only the lib names
148164
149165
Parameters
150166
----------
@@ -155,31 +171,43 @@ def find_table(self, name, mode = 'default'):
155171
-------
156172
tables : list
157173
All XsdirTable objects for a given ZIAD.
158-
159-
mode: 'default' default behaviour
160-
'exact' check also the library
161174
"""
162175
if mode == 'exact':
163-
toreturn = False
164-
else:
176+
# Faster, checks for the exact name
177+
ans = self._exact_loop(name, self.tablenames)
178+
179+
elif mode == 'default':
180+
# Checks all available libraries for the zaid
165181
tables = []
166-
167-
for table in self:
168-
#if name in table.name:#BUG! for natural zaids!!!
169-
################# correction ####################
170-
tablename = table.name.split('.')[0]
171-
if mode == 'default':
172-
if name == tablename:
182+
for table in self:
183+
# tablename = table.name.split('.')[0]
184+
if name == table.name[:-4]:
173185
tables.append(table)
174-
elif mode == 'exact':
175-
if name == table.name:
176-
toreturn = True
177-
break
178-
################# end correction ####################
179-
if mode == 'exact':
180-
return toreturn
181-
else:
182-
return tables
186+
187+
ans = tables
188+
189+
elif mode == 'default-fast':
190+
ans = self._all_fast_loop(name, self.tablenames)
191+
192+
return ans
193+
194+
@staticmethod
195+
def _exact_loop(name: str, tablenames: List[Tuple[str, str]]) -> bool:
196+
print(name)
197+
for zaidname, libname in tablenames:
198+
if name == zaidname+'.'+libname:
199+
return True
200+
201+
return False
202+
203+
@staticmethod
204+
def _all_fast_loop(name: str, tablenames: List[Tuple[str, str]]) -> List[str]:
205+
libs = []
206+
for zaidname, libname in tablenames:
207+
if name == zaidname:
208+
libs.append(libname)
209+
210+
return libs
183211

184212
################# Added by Davide Laghi ###############################
185213
def find_zaids(self, lib):

0 commit comments

Comments
 (0)