Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply scaling factor when reading the cube files #11

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading