Skip to content

Commit

Permalink
import python modules in the top level and other formatting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pranabdas committed May 24, 2024
1 parent 20ddff1 commit 0dbbe45
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 278 deletions.
22 changes: 11 additions & 11 deletions src/crop_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@
@author: Pranab Das (GitHub: @pranabdas)
"""

import numpy as np


def crop_2d(data, x, y, x_min, x_max, y_min, y_max):
'''
"""
data_crop, x_crop, y_crop = crop_2d(data, x, y, x_min, x_max, y_min, y_max)
'''
import numpy as np
"""

# make sure `x_min` is less than `x_max`
if (x_min > x_max):
if x_min > x_max:
x_min, x_max = x_max, x_min

if (y_min > y_max):
if y_min > y_max:
y_min, y_max = y_max, y_min

# in case `x` is not in increasing order, reverse `x_min` and `x_max`
if (x[0] > x[-1]):
if x[0] > x[-1]:
x_min, x_max = x_max, x_min

if (y[0] > y[-1]):
if y[0] > y[-1]:
y_min, y_max = y_max, y_min

x_min_index = np.nanargmin(abs(x - x_min))
Expand All @@ -34,9 +35,8 @@ def crop_2d(data, x, y, x_min, x_max, y_min, y_max):
y_max_index = np.nanargmin(abs(y - y_max))

# slicing excludes the max index
crop_data = data[x_min_index: (x_max_index + 1),
y_min_index: (y_max_index + 1)]
crop_x = x[x_min_index: (x_max_index + 1)]
crop_y = y[y_min_index: (y_max_index + 1)]
crop_data = data[x_min_index : (x_max_index + 1), y_min_index : (y_max_index + 1)]
crop_x = x[x_min_index : (x_max_index + 1)]
crop_y = y[y_min_index : (y_max_index + 1)]

return crop_data, crop_x, crop_y
23 changes: 10 additions & 13 deletions src/custom_loader_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
@author: Pranab Das (GitHub: @pranabdas)
"""

import itertools
import math
import warnings
import numpy as np


def custom_loader_one(fname, polar_start=None, polar_end=None):
import itertools
import math
import numpy as np
import warnings

# suppress numpy warning related to max_rows behavior
with warnings.catch_warnings():
Expand All @@ -21,8 +22,7 @@ def custom_loader_one(fname, polar_start=None, polar_end=None):

data = itertools.islice(open(fname), None)
# exclude blank and comment lines, comments must begin with `#`
data = itertools.dropwhile(
lambda x: x.strip() == "" or x.strip()[0] == "#", data)
data = itertools.dropwhile(lambda x: x.strip() == "" or x.strip()[0] == "#", data)
# skip first/theta row
data = np.loadtxt(data, dtype="float64", skiprows=1)

Expand All @@ -32,16 +32,15 @@ def custom_loader_one(fname, polar_start=None, polar_end=None):
# note: assert can be switched off globally using `-O` or `-OO` flag
assert polar_len * len(energy) == len(data), "dimension mismatch"

if (polar_start and polar_end):
if polar_start and polar_end:
phi = np.linspace(polar_start, polar_end, num=polar_len)
else:
phi = np.arange(polar_len)

intensity = np.zeros((len(energy), len(theta), polar_len), dtype="float64")

for i in range(len(data)):
assert math.isclose(data[i, 0], energy[i % len(energy)]), \
"energy value irregularity"
assert math.isclose(data[i, 0], energy[i % len(energy)]), "energy value irregularity"
intensity[i % len(energy), :, i // len(energy)] = data[i, 1:]

return intensity, energy, theta, phi
Expand All @@ -66,8 +65,6 @@ def gen_test_data():
... with redirect_stdout(f):
... gen_test_data()
"""
import numpy as np

print("# generated fake/random data to test `custom_loader_one`")
print("\n\t", end="")

Expand All @@ -80,8 +77,8 @@ def gen_test_data():
ph = np.linspace(-3, 3, 5, dtype="float64")
e = np.linspace(10, 15, 15, dtype="float64")

for i in range(len(ph)*len(e)):
for i in range(len(ph) * len(e)):
print(e[i % len(e)], end=" ")
for _ in range(len(th)):
print(np.random.rand()*50, end=" ")
print(np.random.rand() * 50, end=" ")
print()
12 changes: 7 additions & 5 deletions src/cv2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
@author: Pranab Das (GitHub: @pranabdas)
"""

from astropy.convolution import convolve, Box2DKernel
import numpy as np


def cv2d(data, x, y, bw=5, c1=0.001, c2=0.001, w=1):
from astropy.convolution import convolve, Box2DKernel
import numpy as np

# https://docs.astropy.org/en/latest/api/astropy.convolution.Box2DKernel.html
# https://docs.astropy.org/en/latest/api/astropy.convolution.convolve.html
data_smth = convolve(data, Box2DKernel(bw), boundary='extend')
data_smth = convolve(data, Box2DKernel(bw), boundary="extend")

dx = np.gradient(data_smth, axis=0)
dy = np.gradient(data_smth, axis=1) * w
Expand All @@ -22,7 +23,8 @@ def cv2d(data, x, y, bw=5, c1=0.001, c2=0.001, w=1):
dxdy = np.gradient(np.gradient(data_smth, y, axis=1), x, axis=0) * w

# 2D curvature - https://doi.org/10.1063/1.3585113
cv2d = ((1 + c1*dx**2)*c2*d2y - 2*c1*c2*dx*dy*dxdy +
(1 + c2*dy**2)*c1*d2x) / (1 + c1*dx**2 + c2*dy**2)**1.5
cv2d = ((1 + c1 * dx**2) * c2 * d2y - 2 * c1 * c2 * dx * dy * dxdy + (1 + c2 * dy**2) * c1 * d2x) / (
1 + c1 * dx**2 + c2 * dy**2
) ** 1.5

return cv2d
128 changes: 77 additions & 51 deletions src/export_itx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,95 +6,121 @@
@author: Pranab Das (GitHub: @pranabdas)
"""

import numpy as np

def export_itx(path, data, x=[], y=[], z=[], wave_name='wave', x_label='x-label', y_label='y-label', z_label='z-label'):
'''

def export_itx(
path, data, x=[], y=[], z=[], wave_name="wave", x_label="x-label", y_label="y-label", z_label="z-label"
):
"""
export_itx(path, data, x=[], y=[], z =[], wave_name='wave',
x_label='x-label', y_label='y-label', z_label='z-label')
Exports Igor text data. This function support writing/exporting 1-, 2-, and
3-dimensional data waves.
'''
import numpy as np

"""
dimsize = (np.asarray(data)).shape
# Axis scaling
if (len(x) < 2):
if len(x) < 2:
x_offset = 0
x_delta = 1
else:
x_offset = x[0]
x_delta = (x[-1] - x[0])/(len(x)-1)
x_delta = (x[-1] - x[0]) / (len(x) - 1)

if (len(y) < 2):
if len(y) < 2:
y_offset = 0
y_delta = 1
else:
y_offset = y[0]
y_delta = (y[-1] - y[0])/(len(y)-1)
y_delta = (y[-1] - y[0]) / (len(y) - 1)

if (len(z) < 2):
if len(z) < 2:
z_offset = 0
z_delta = 1
else:
z_offset = z[0]
z_delta = (z[-1] - z[0])/(len(z)-1)
z_delta = (z[-1] - z[0]) / (len(z) - 1)

# 1-dimensional array
if (len(dimsize) == 1):
with open(path, 'w') as fid:
fid.write('IGOR\nWAVES/D\t{0}\nBEGIN\n'.format(wave_name))
if len(dimsize) == 1:
with open(path, "w") as fid:
fid.write("IGOR\nWAVES/D\t{0}\nBEGIN\n".format(wave_name))
for ii in range(len(data)):
if (data[ii] == 0):
fid.write('\t0')
if data[ii] == 0:
fid.write("\t0")
else:
fid.write(str('\t%f' % data[ii]).rstrip("0"))
fid.write('\n')
fid.write('END\n')
fid.write(('X SetScale/P x {0}, {1}, "{2}", {3}; \
SetScale/P y 0, 0, "", {3}; \n'.format(x_offset, x_delta, x_label,
wave_name)))
fid.write(str("\t%f" % data[ii]).rstrip("0"))
fid.write("\n")
fid.write("END\n")
fid.write(
(
'X SetScale/P x {0}, {1}, "{2}", {3}; \
SetScale/P y 0, 0, "", {3}; \n'.format(
x_offset, x_delta, x_label, wave_name
)
)
)
fid.close()

# 2-dimensional array
if (len(dimsize) == 2):
with open(path, 'w') as fid:
fid.write('IGOR\nWAVES/N=({0},{1})\t{2}\nBEGIN\n'.format(
dimsize[0], dimsize[1], wave_name))
if len(dimsize) == 2:
with open(path, "w") as fid:
fid.write("IGOR\nWAVES/N=({0},{1})\t{2}\nBEGIN\n".format(dimsize[0], dimsize[1], wave_name))
for ii in range(len(x)):
for jj in range(len(y)):
if (data[ii][jj] == 0):
fid.write('\t0')
if data[ii][jj] == 0:
fid.write("\t0")
else:
fid.write(str('\t%f' % data[ii][jj]).rstrip("0"))
fid.write('\n')
fid.write('\n')
fid.write('END\n')
fid.write(('X SetScale/P x {0}, {1}, "{2}", {3}; SetScale/P y {4},\
{5}, "{6}", {7}; SetScale d 0,0,"", {8}\n'.format(x_offset,
x_delta, x_label, wave_name, y_offset, y_delta, y_label,
wave_name, wave_name)))
fid.write(str("\t%f" % data[ii][jj]).rstrip("0"))
fid.write("\n")
fid.write("\n")
fid.write("END\n")
fid.write(
(
'X SetScale/P x {0}, {1}, "{2}", {3}; SetScale/P y {4},\
{5}, "{6}", {7}; SetScale d 0,0,"", {8}\n'.format(
x_offset, x_delta, x_label, wave_name, y_offset, y_delta, y_label, wave_name, wave_name
)
)
)
fid.close()

# 3-dimensional array
if (len(dimsize) == 3):
with open(path, 'w') as fid:
fid.write('IGOR\nWAVES/N=({0},{1},{2})\t{3}\nBEGIN\n'.format(
dimsize[0], dimsize[1], dimsize[2], wave_name))
if len(dimsize) == 3:
with open(path, "w") as fid:
fid.write(
"IGOR\nWAVES/N=({0},{1},{2})\t{3}\nBEGIN\n".format(dimsize[0], dimsize[1], dimsize[2], wave_name)
)
for kk in range(len(z)):
for ii in range(len(x)):
for jj in range(len(y)):
if (data[ii][jj][kk] == 0):
fid.write('\t0')
if data[ii][jj][kk] == 0:
fid.write("\t0")
else:
fid.write(
str('\t%f' % data[ii][jj][kk]).rstrip("0"))
fid.write('\n')
fid.write('\n')
fid.write('END\n')
fid.write(('X SetScale/P x {0}, {1}, "{2}", {3}; SetScale/P y {4},\
fid.write(str("\t%f" % data[ii][jj][kk]).rstrip("0"))
fid.write("\n")
fid.write("\n")
fid.write("END\n")
fid.write(
(
'X SetScale/P x {0}, {1}, "{2}", {3}; SetScale/P y {4},\
{5}, "{6}", {7}; SetScale/P z {8}, {9}, "{10}", {11}; \
SetScale d 0,0,"", {12}\n'.format(x_offset, x_delta, x_label,
wave_name, y_offset, y_delta, y_label, wave_name, z_offset,
z_delta, z_label, wave_name, wave_name)))
SetScale d 0,0,"", {12}\n'.format(
x_offset,
x_delta,
x_label,
wave_name,
y_offset,
y_delta,
y_label,
wave_name,
z_offset,
z_delta,
z_label,
wave_name,
wave_name,
)
)
)
fid.close()
Loading

0 comments on commit 0dbbe45

Please sign in to comment.