Skip to content

Commit

Permalink
Fixed issue with c_file_reader.py and added tests (HEPData#141)
Browse files Browse the repository at this point in the history
* Fixes in CFileReader and added tests
  • Loading branch information
SirVheod authored May 15, 2020
1 parent c28cd2c commit 114c7b9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 44 deletions.
64 changes: 20 additions & 44 deletions hepdata_lib/c_file_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,10 @@ def create_tgraph_dict(self, graph_list, list_of_tgraphs):
while count < len(graphs) -1:
xvalues = self.read_graph(graphs[count])
yvalues = self.read_graph(graphs[count+1])
try:
if (all(isinstance(x, int) for x in xvalues)
and all(isinstance(x, int) for x in yvalues)):
for value in xvalues:
x_values.append(value)
for value in yvalues:
y_values.append(value)
elif(any(not isinstance(x, int) for x in xvalues)
or any(not isinstance(x, int) for x in yvalues)):
for value in xvalues:
x_values.append(float(value))
for value in yvalues:
y_values.append(float(value))
except ValueError:
raise IndexError("Invalid values. Int or float required.")
for value in xvalues:
x_values.append(value)
for value in yvalues:
y_values.append(value)
tgraph = self.create_tgraph(x_values, y_values)
tgraph = dict(tgraph)
list_of_tgraphs.append(tgraph)
Expand All @@ -133,33 +122,14 @@ def create_tgrapherrors_dict(self, graph_list):
yvalues = self.read_graph(graph_list[count+1])
dxvalues = self.read_graph(graph_list[count+2])
dyvalues = self.read_graph(graph_list[count+3])
try:
if (all(isinstance(x, int) for x in xvalues)
and all(isinstance(x, int) for x in yvalues)
and all(isinstance(x, int) for x in dxvalues)
and all(isinstance(x, int) for x in dyvalues)):
for value in xvalues:
x_values.append(value)
for value in yvalues:
y_values.append(value)
for value in dxvalues:
dx_values.append(value)
for value in dyvalues:
dy_values.append(value)
elif(any(not isinstance(x, int) for x in xvalues)
or any(not isinstance(x, int) for x in xvalues)
or any(not isinstance(x, int) for x in dxvalues)
or any(not isinstance(x, int) for x in dyvalues)):
for value in xvalues:
x_values.append(float(value))
for value in yvalues:
y_values.append(float(value))
for value in dxvalues:
dx_values.append(float(value))
for value in dyvalues:
dy_values.append(float(value))
except ValueError:
raise IndexError("Invalid values. Int or float required.")
for value in xvalues:
x_values.append(value)
for value in yvalues:
y_values.append(value)
for value in dxvalues:
dx_values.append(value)
for value in dyvalues:
dy_values.append(value)
tgraph_error = self.create_tgrapherrors(x_values, y_values, dx_values, dy_values)
tgraph_error = dict(tgraph_error)
list_of_tgraphs.append(tgraph_error)
Expand Down Expand Up @@ -199,7 +169,10 @@ def create_tgrapherrors(self, x_value, y_value, dx_value, dy_value):
y_values.append(y_value[value])
dx_values.append(dx_value[value])
dy_values.append(dy_value[value])
t_object = TGraphErrors(length, x_values, y_values, dx_values, dy_values)
try:
t_object = TGraphErrors(length, x_values, y_values, dx_values, dy_values)
except TypeError:
raise TypeError("Invalid value in TGraphErrors constructor!")
graph = ru.get_graph_points(t_object)

return graph
Expand All @@ -222,7 +195,10 @@ def create_tgraph(self, x_value, y_value):
for value in range(length):
x_values.append(x_value[value])
y_values.append(y_value[value])
t_object = TGraph(length, x_values, y_values)
try:
t_object = TGraph(length, x_values, y_values)
except TypeError:
raise TypeError("Invalid value in TGraph constructor!")
graph = ru.get_graph_points(t_object)

return graph
Expand Down
53 changes: 53 additions & 0 deletions tests/test_cfilereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,49 @@ def test_get_graphs(self):
with self.assertRaises(ValueError):
reader.get_graphs()

# Testing graphs with half float falf int values
with open(test_file, "w") as testfile:
testfile.write(
'void test() {\n' +
'Double_t Graph0_fx1[2] = {1,2};\n' +
'Double_t Graph0_fy1[2] = { \n3.23423,\n2.23423};' +
'TGraph *graph = new TGraph(2,Graph0_fx1,Graph0_fy1);\n' +
'graph->SetName("Graph0");\n' +
'Double_t Graph2_fx1001[30] = {1,\n2};\n' +
'Double_t Graph2_fy1001[30] = {3.2,\n2.1};\n' +
'Double_t Graph2_fex1001[30] = {0,\n0};\n' +
'Double_t Graph2_fey1001[30] = { 0 ,0 };\n' +
'TGraphErrors gre = TGraphErrors(30,Graph2_fx1001,Graph2_fy1001,' +
'Graph2_fex1001,Graph2_fey1001);\n' +
'gre.SetName("Graph2");}')

reader = CFileReader(test_file)
tgraphs = reader.get_graphs()
self.assertTrue(tgraphs["Graph0"]["x"] == [1.0, 2.0])
self.assertTrue(tgraphs["Graph0"]["y"] == [3.23423, 2.23423])
self.assertTrue(tgraphs["Graph2"]["x"] == [1.0, 2.0])
self.assertTrue(tgraphs["Graph2"]["y"] == [3.2, 2.1])
self.assertTrue(tgraphs["Graph2"]["dx"] == [0, 0])
self.assertTrue(tgraphs["Graph2"]["dy"] == [0, 0])

# Testing graphs without name
with open(test_file, "w") as testfile:
testfile.write(
'void test() {\n' +
'Double_t Graph0_fx2[5] ={ 1.2, 2.2 };\n' +
'Double_t Graph0_fy2[5] = { \n0.123345, \n0.343564};\n' +
'graph = new TGraph( 5, Graph0_fx2,Graph0_fy2);\n' +
'Double_t Graph2_fx1001[30] = {1,\n2};\n' +
'Double_t Graph2_fy1001[30] = {3.2,\n2.1};\n' +
'Double_t Graph2_fex1001[30] = {0,\n0};\n' +
'Double_t Graph2_fey1001[30] = { 0 ,0 };\n' +
'TGraphErrors gre = TGraphErrors(30,Graph2_fx1001,Graph2_fy1001,' +
'Graph2_fex1001,Graph2_fey1001);}')

reader = CFileReader(test_file)
tgraphs = reader.get_graphs()
self.assertTrue(set(tgraphs.keys()) == set(["tgraph", "tgraph"]))

self.addCleanup(os.remove, test_file)
self.doCleanups()

Expand All @@ -148,6 +191,16 @@ def test_create_tgrapherrors(self):
self.assertTrue(set(graph.keys()) == set(["x", "y", "dx", "dy"]))
self.assertTrue(all(graph["x"] == x_value))
self.assertTrue(all(graph["y"] == y_value))

# Testing TGraphErrors with int values only
x_value = [1, 2, 3]
y_value = [3, 2, 1]
dx_value = [0, 0, 0]
dy_value = [0, 0, 0]
reader = CFileReader(c_file)
with self.assertRaises(TypeError):
reader.create_tgrapherrors(x_value, y_value, dx_value, dy_value)

self.addCleanup(os.remove, c_file)
self.doCleanups()

Expand Down

0 comments on commit 114c7b9

Please sign in to comment.