Skip to content

Commit

Permalink
Apply scaling factor when reading the cube files (#11)
Browse files Browse the repository at this point in the history
When reading a cube file, the tool will automatically apply the scaling factor read from it.
This is the default behaviour and it can be disabled.
  • Loading branch information
yakutovicha authored Dec 17, 2024
1 parent 7a38c6c commit 51cb60a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions cubehandler/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(
self.cell_n = cell_n

@classmethod
def from_file_handle(cls, filehandle, read_data=True):
def from_file_handle(cls, filehandle, read_data=True, apply_scaling=True):
f = filehandle
c = cls()
c.title = f.readline().rstrip()
Expand Down Expand Up @@ -131,18 +131,24 @@ def from_file_handle(cls, filehandle, read_data=True):
# data = np.array(f.read().split(), dtype=float)

c.data = c.data.reshape(c.cell_n)
if apply_scaling:
c.data *= c.scaling_factor

return c

@classmethod
def from_file(cls, filepath, read_data=True):
def from_file(cls, filepath, read_data=True, apply_scaling=True):
with open(filepath) as f:
c = cls.from_file_handle(f, read_data=read_data)
c = cls.from_file_handle(
f, read_data=read_data, apply_scaling=apply_scaling
)
return c

@classmethod
def from_content(cls, content, read_data=True):
return cls.from_file_handle(io.StringIO(content), read_data=read_data)
def from_content(cls, content, read_data=True, apply_scaling=True):
return cls.from_file_handle(
io.StringIO(content), read_data=read_data, apply_scaling=apply_scaling
)

def write_cube_file(self, filename, low_precision=False):

Expand Down
2 changes: 1 addition & 1 deletion tests/test_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ def test_reduce_data_density():
cube.reduce_data_density(points_per_angstrom=2)
cube.write_cube_file("low_res.cube", low_precision=True)
low_res = Cube.from_file("low_res.cube")
low_res_integral = np.sum(low_res.data**2) * low_res.dv_au * low_res.scaling_f**2
low_res_integral = np.sum(low_res.data**2) * low_res.dv_au
assert np.abs(low_res_integral - integral) < 0.01
assert cube.scaling_f == 0.2848452

0 comments on commit 51cb60a

Please sign in to comment.