Skip to content

Commit

Permalink
fixes tests
Browse files Browse the repository at this point in the history
Signed-off-by: nagesh bansal <nageshbansal59@gmail.com>
  • Loading branch information
Nageshbansal committed Sep 19, 2023
1 parent f356b58 commit 04a4db1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 27 deletions.
2 changes: 1 addition & 1 deletion neonwranglerpy/lib/clip_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def clip_plot(plt, list_data, bff=12, savepath=""):
# plt['easting'] = plt['easting'].astype(int)
# plt['northing'] = plt['northing'].astype(int)
# tile = plt["plt_e"].values.astype(str) + "_" + plt["plt_n"].values.astype(str)
tiles = str(plt.plt_e[0]) + "_" + str(plt.plt_n[0])
tiles = str(plt["plt_e"]) + "_" + str(plt["plt_n"])
tiles = [f for f in list_data if tiles in f]
missed_plots = []

Expand Down
4 changes: 4 additions & 0 deletions neonwranglerpy/lib/extract_hsi_to_tif.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ def array2raster(newRaster,
originY = extent['yMax']
res = reflArray_metadata['res']['pixelWidth']
transform = Affine.translation(originX, originY) * Affine.scale(res, -res)

if not os.path.exists(ras_dir):
os.makedirs(ras_dir)
with rasterio.open(
"{}/{}".format(ras_dir, newRaster),
'w',
Expand Down Expand Up @@ -306,6 +309,7 @@ def generate_raster(h5_path,
solar_tilename = os.path.splitext(
os.path.basename(rgb_filename))[0] + "_solar_sensor_angle{}.tif".format(suffix)
# Save georeference crop to file
save_dir = os.path.abspath(save_dir)
array2raster(solar_tilename,
sol_sens_angle,
metadata,
Expand Down
Binary file not shown.
67 changes: 42 additions & 25 deletions tests/test_h5refl2array.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
import unittest
import numpy as np
import h5py
import subprocess
import os
import pytest

class TestFunc(unittest.TestCase):
from neonwranglerpy.lib.extract_hsi_to_tif import h5refl2array

def setUp(self):
# Prepare a dummy hdf5 file for testing
self.filename = "testfile.h5"
with h5py.File(self.filename, 'w') as f:
# create datasets in the hdf5 file similar to the actual data the function expects
## Omitted here for brevity
# Main Paths
file_location = os.path.dirname(os.path.realpath(__file__))
neonwranglerpy_root_dir = os.path.abspath(os.path.join(file_location, os.pardir))

def test_reflection2array(self):
reflArray, metadata, sol_az, sol_zn, sns_az, sns_zn = h5refl2array(self.filename)
# Paths of the raw data files used
raw_dir_files = os.path.normpath(os.path.join(neonwranglerpy_root_dir, 'raw_data'))

# Test the type of the returned objects
self.assertTrue(isinstance(reflArray, np.ndarray))
self.assertTrue(isinstance(metadata, dict))
self.assertTrue(isinstance(sol_az, list))
self.assertTrue(isinstance(sol_zn, list))
self.assertTrue(isinstance(sns_az, np.ndarray))
self.assertTrue(isinstance(sns_zn, np.ndarray))
test_reflection2array_data = [
('test_reflection2array', "h5_data/DP3.30006.001/2017/FullSite/D16/2017_ABBY_1/L3/Spectrometer/Reflectance"
"/NEON_D16_ABBY_DP3_559000_5070000_reflectance.h5")
]

# Test the shape or any other property of the returned objects
# This will really depend on what you're expecting
## Omitted here for brevity

def tearDown(self):
# Removes the test file
os.remove(self.filename)
def setup_module():
"""Automatically sets up the environment before the module runs."""
os.chdir(neonwranglerpy_root_dir)
subprocess.call(['cp', '-r', 'tests/raw_data', neonwranglerpy_root_dir])

if __name__ == '__main__':
unittest.main()

def teardown_module():
"""Automatically clean up after the module."""
os.chdir(neonwranglerpy_root_dir)
subprocess.call(['rm', '-r', 'raw_data'])


def setup_functions():
"""Set up functions."""
teardown_module()
setup_module()


@pytest.mark.parametrize("test_name, path", test_reflection2array_data)
def test_reflection2array(test_name, path):
setup_functions()
path = os.path.join(raw_dir_files, path)
reflArray, metadata, sol_az, sol_zn, sns_az, sns_zn = h5refl2array(path)

# Test the type of the returned objects
assert (isinstance(reflArray, np.ndarray))
assert (isinstance(metadata, dict))
assert (isinstance(sol_az, np.ndarray))
assert (isinstance(sol_zn, np.ndarray))
assert (isinstance(sns_az, np.ndarray))
assert (isinstance(sns_zn, np.ndarray))
10 changes: 9 additions & 1 deletion tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@
'utmZone': ['6N', '6N'],
'easting': [559120, 559120],
'northing': [5070120, 5070120]
}, "DP3.30015.001", "raster_data", "2018", ["NEON_D16_ABBY_DP3_559000_5070000_CHM.tif", True])
}, "DP3.30015.001", "raster_data", "2018", ["NEON_D16_ABBY_DP3_559000_5070000_CHM.tif", True]),
('test_extract_h5', {
'plotID': ['TEST_0000', 'TEST_0000'],
'subplotID': ['A', "A"],
'siteID': ['ABBY', 'ABBY'],
'utmZone': ['6N', '6N'],
'easting': [559120, 559120],
'northing': [5070120, 5070120]
}, "DP3.30006.001", "h5_data", "2017", ["NEON_D16_ABBY_DP3_559000_5070000_reflectance_hyperspectral.tif", True])
]


Expand Down

0 comments on commit 04a4db1

Please sign in to comment.