Skip to content

lpf file reader v2 + file transfer v1 #32

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

Merged
merged 7 commits into from
Aug 25, 2022
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
8 changes: 0 additions & 8 deletions doc/source/grpc_tools.rst

This file was deleted.

1 change: 0 additions & 1 deletion doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
:hidden:
:maxdepth: 3

grpc_tools
class_documentation


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ classifiers=[
"Operating System :: OS Independent",
]
dependencies=[
"ansys-optics-speos-grpcapi>=4,<5",
"ansys-optics-speos-grpcapi>=5,<6",
"grpcio-tools",
]

Expand Down
1 change: 0 additions & 1 deletion src/ansys/pyoptics/speos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@

"""
from ansys.pyoptics.speos._version import __version__
from ansys.pyoptics.speos.grpc_stub import get_stub_insecure_channel
40 changes: 0 additions & 40 deletions src/ansys/pyoptics/speos/grpc_stub.py

This file was deleted.

3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ def grpc_server_on(config) -> bool:


# set test_path var depending on if we are using the servers in a docker container or not
local_test_path = os.path.join(local_path, "assets/")
if config.get("SpeosServerOnDocker"):
test_path = "/app/assets/"
else:
test_path = os.path.join(local_path, "assets/")
test_path = local_test_path

# Wait for the grpc server - in case the timeout is reached raise an error
if not grpc_server_on(config):
Expand Down
7 changes: 4 additions & 3 deletions tests/test_anisotropic_bsdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
import math
import os

from ansys.api.speos import grpc_stub
import ansys.api.speos.bsdf.v1.anisotropic_bsdf_pb2 as anisotropic_bsdf__v1__pb2
import ansys.api.speos.bsdf.v1.anisotropic_bsdf_pb2_grpc as anisotropic_bsdf__v1__pb2_grpc
from google.protobuf.empty_pb2 import Empty

from ansys.pyoptics import speos
from conftest import config, test_path
import helper

Expand Down Expand Up @@ -261,8 +261,9 @@ def compareSpecularEnhancementData(data1, data2):


def test_grpc_anisotropic_bsdf():
stub = speos.get_stub_insecure_channel(
port=config.get("SpeosServerPort"), stub_type=anisotropic_bsdf__v1__pb2_grpc.AnisotropicBsdfServiceStub
stub = grpc_stub.get_stub_insecure_channel(
target="localhost:" + str(config.get("SpeosServerPort")),
stub_type=anisotropic_bsdf__v1__pb2_grpc.AnisotropicBsdfServiceStub,
)

# anisotropic bsdf
Expand Down
7 changes: 4 additions & 3 deletions tests/test_bsdf_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@
import math
import os

from ansys.api.speos import grpc_stub
import ansys.api.speos.bsdf.v1.bsdf_creation_pb2 as bsdf_creation__v1__pb2
import ansys.api.speos.bsdf.v1.bsdf_creation_pb2_grpc as bsdf_creation__v1__pb2_grpc

from ansys.pyoptics import speos
from conftest import config, test_path
import helper


def test_grpc_spectral_bsdf():
stub = speos.get_stub_insecure_channel(
port=config.get("SpeosServerPort"), stub_type=bsdf_creation__v1__pb2_grpc.BsdfCreationServiceStub
stub = grpc_stub.get_stub_insecure_channel(
target="localhost:" + str(config.get("SpeosServerPort")),
stub_type=bsdf_creation__v1__pb2_grpc.BsdfCreationServiceStub,
)

# BSDF180
Expand Down
153 changes: 112 additions & 41 deletions tests/test_lpf_file_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@
"""
import os

import ansys.api.speos.lpf.v1.lpf_file_reader_pb2 as lpf_file_reader__v1__pb2
import ansys.api.speos.lpf.v1.lpf_file_reader_pb2_grpc as lpf_file_reader__v1__pb2_grpc
from google.protobuf.empty_pb2 import Empty
from ansys.api.speos import grpc_stub
import ansys.api.speos.file.v1.file_transfer as file_transfer_helper__v1
import ansys.api.speos.file.v1.file_transfer_pb2 as file_transfer__v1__pb2
import ansys.api.speos.file.v1.file_transfer_pb2_grpc as file_transfer__v1__pb2_grpc
import ansys.api.speos.lpf.v2.lpf_file_reader_pb2 as lpf_file_reader__v2__pb2
import ansys.api.speos.lpf.v2.lpf_file_reader_pb2_grpc as lpf_file_reader__v2__pb2_grpc

from ansys.pyoptics import speos
from conftest import config, test_path
from conftest import config, local_test_path, test_path


def test_lpf_file_reader_mono_v1_DirectSimu():
# Lpf file reader creation
stub = speos.get_stub_insecure_channel(
port=config.get("SpeosServerPort"), stub_type=lpf_file_reader__v1__pb2_grpc.LpfFileReader_MonoStub
stub = grpc_stub.get_stub_insecure_channel(
target="localhost:" + str(config.get("SpeosServerPort")),
stub_type=lpf_file_reader__v2__pb2_grpc.LpfFileReader_MonoStub,
)

# Init with file path
# Init with file local path
path = os.path.join(test_path, "basic_DirectSimu.lpf")
stub.InitLpfFileName(lpf_file_reader__v1__pb2.InitLpfFileNameRequest_Mono(lpf_file_path=path))
stub.InitLpfFileName(lpf_file_reader__v2__pb2.InitLpfFileName_Request_Mono(lpf_file_uri=path))

# GetInformation
res_information = stub.GetInformation(lpf_file_reader__v1__pb2.GetInformationRequest_Mono())
res_information = stub.GetInformation(lpf_file_reader__v2__pb2.GetInformation_Request_Mono())
nb_of_traces = res_information.nb_of_traces
assert nb_of_traces == 24817
assert res_information.nb_of_xmps == 3
Expand All @@ -42,46 +45,47 @@ def test_lpf_file_reader_mono_v1_DirectSimu():

# Read
raypaths = []
for rp in stub.Read(lpf_file_reader__v1__pb2.ReadRequest_Mono()):
for rp in stub.Read(lpf_file_reader__v2__pb2.Read_Request_Mono()):
raypaths.append(rp)
assert len(raypaths) == nb_of_traces

# Check result (first entry)
expected_nb_of_impact = 5
assert len(raypaths[0].impacts) == expected_nb_of_impact
assert raypaths[0].impacts[1] == lpf_file_reader__v1__pb2.TripletFloat(x=3.19368935, y=14.999999, z=-3.94779062)
assert raypaths[0].impacts[1] == lpf_file_reader__v2__pb2.TripletFloat(x=3.19368935, y=14.999999, z=-3.94779062)
assert len(raypaths[0].wavelengths) == expected_nb_of_impact
assert raypaths[0].wavelengths[1] == 691.44708251953125
assert len(raypaths[0].body_context_ids) == expected_nb_of_impact
assert raypaths[0].body_context_ids[1] == 2001802324
assert len(raypaths[0].unique_face_ids) == expected_nb_of_impact
assert raypaths[0].unique_face_ids[1] == 1815582994
assert raypaths[0].lastDirection == lpf_file_reader__v1__pb2.TripletFloat(
assert raypaths[0].lastDirection == lpf_file_reader__v2__pb2.TripletFloat(
x=0.0606396869, y=0.995341122, z=-0.0749590397
)
assert len(raypaths[0].interaction_statuses) == expected_nb_of_impact
assert raypaths[0].interaction_statuses[0] == lpf_file_reader__v1__pb2.RayPath.PhotonStatus.StatusJustEmitted
assert raypaths[0].interaction_statuses[0] == lpf_file_reader__v2__pb2.RayPath.PhotonStatus.StatusJustEmitted
assert (
raypaths[0].interaction_statuses[1] == lpf_file_reader__v1__pb2.RayPath.PhotonStatus.StatusSpecularTransmitted
raypaths[0].interaction_statuses[1] == lpf_file_reader__v2__pb2.RayPath.PhotonStatus.StatusSpecularTransmitted
)
assert len(raypaths[0].sensor_contributions) == 0

# Close
stub.CloseLpfFileName(Empty())
stub.CloseLpfFileName(lpf_file_reader__v2__pb2.CloseLpfFileName_Request_Mono())


def test_lpf_file_reader_mono_v1_InverseSimu():
# Lpf file reader creation
stub = speos.get_stub_insecure_channel(
port=config.get("SpeosServerPort"), stub_type=lpf_file_reader__v1__pb2_grpc.LpfFileReader_MonoStub
stub = grpc_stub.get_stub_insecure_channel(
target="localhost:" + str(config.get("SpeosServerPort")),
stub_type=lpf_file_reader__v2__pb2_grpc.LpfFileReader_MonoStub,
)

# Init with file path
# Init with file local path
path = os.path.join(test_path, "basic_InverseSimu.lpf")
stub.InitLpfFileName(lpf_file_reader__v1__pb2.InitLpfFileNameRequest_Mono(lpf_file_path=path))
stub.InitLpfFileName(lpf_file_reader__v2__pb2.InitLpfFileName_Request_Mono(lpf_file_uri=path))

# GetInformation
res_information = stub.GetInformation(lpf_file_reader__v1__pb2.GetInformationRequest_Mono())
res_information = stub.GetInformation(lpf_file_reader__v2__pb2.GetInformation_Request_Mono())
nb_of_traces = res_information.nb_of_traces
assert nb_of_traces == 21044
assert res_information.nb_of_xmps == 1
Expand All @@ -91,81 +95,148 @@ def test_lpf_file_reader_mono_v1_InverseSimu():

# Read
raypaths = []
for rp in stub.Read(lpf_file_reader__v1__pb2.ReadRequest_Mono()):
for rp in stub.Read(lpf_file_reader__v2__pb2.Read_Request_Mono()):
raypaths.append(rp)
assert len(raypaths) == nb_of_traces

# Check sensor_contributions in first raypath
assert len(raypaths[0].sensor_contributions) == 1
assert raypaths[0].sensor_contributions[0].sensor_id == 0
assert raypaths[0].sensor_contributions[0].coordinates == lpf_file_reader__v1__pb2.DoubletDouble(
assert raypaths[0].sensor_contributions[0].coordinates == lpf_file_reader__v2__pb2.DoubletDouble(
x=-0.20848463202592682, y=0.1897648665199252
)

# Close
stub.CloseLpfFileName(Empty())
stub.CloseLpfFileName(lpf_file_reader__v2__pb2.CloseLpfFileName_Request_Mono())


def test_lpf_file_reader_multi_v1():
# Lpf file reader multi creation
stub = speos.get_stub_insecure_channel(
port=config.get("SpeosServerPort"), stub_type=lpf_file_reader__v1__pb2_grpc.LpfFileReader_MultiStub
stub = grpc_stub.get_stub_insecure_channel(
target="localhost:" + str(config.get("SpeosServerPort")),
stub_type=lpf_file_reader__v2__pb2_grpc.LpfFileReader_MultiStub,
)

# Create a reader and retrieve its associated guid
guid = stub.Create(Empty())
create_lpf_reader_response = stub.Create(lpf_file_reader__v2__pb2.Create_Request_Multi())
guid = create_lpf_reader_response.lpf_reader_guid

# Init with file path
# Init with file local path
path = os.path.join(test_path, "basic_DirectSimu.lpf")
stub.InitLpfFileName(lpf_file_reader__v1__pb2.InitLpfFileNameRequest_Multi(id=guid, lpf_file_path=path))
stub.InitLpfFileName(
lpf_file_reader__v2__pb2.InitLpfFileName_Request_Multi(lpf_reader_guid=guid, lpf_file_uri=path)
)

# GetInformation
res_information = stub.GetInformation(lpf_file_reader__v1__pb2.GetInformationRequest_Multi(id=guid))
res_information = stub.GetInformation(lpf_file_reader__v2__pb2.GetInformation_Request_Multi(lpf_reader_guid=guid))
nb_of_traces = res_information.nb_of_traces
assert nb_of_traces == 24817
assert res_information.nb_of_xmps == 3

# Create a second reader
guid2 = stub.Create(Empty())
guid2 = stub.Create(lpf_file_reader__v2__pb2.Create_Request_Multi()).lpf_reader_guid
# Init second reader
path2 = os.path.join(test_path, "basic_InverseSimu.lpf")
stub.InitLpfFileName(lpf_file_reader__v1__pb2.InitLpfFileNameRequest_Multi(id=guid2, lpf_file_path=path2))
stub.InitLpfFileName(
lpf_file_reader__v2__pb2.InitLpfFileName_Request_Multi(lpf_reader_guid=guid2, lpf_file_uri=path2)
)

# GetInformation and read second
res_information = stub.GetInformation(lpf_file_reader__v1__pb2.GetInformationRequest_Multi(id=guid2))
res_information = stub.GetInformation(lpf_file_reader__v2__pb2.GetInformation_Request_Multi(lpf_reader_guid=guid2))
nb_of_traces2 = res_information.nb_of_traces
assert nb_of_traces2 == 21044
raypaths2 = []
for rp in stub.Read(lpf_file_reader__v1__pb2.ReadRequest_Multi(id=guid2)):
for rp in stub.Read(lpf_file_reader__v2__pb2.Read_Request_Multi(lpf_reader_guid=guid2)):
raypaths2.append(rp)
assert len(raypaths2) == nb_of_traces2

# Check sensor_contributions in first raypath
assert len(raypaths2[0].sensor_contributions) == 1
assert raypaths2[0].sensor_contributions[0].sensor_id == 0
assert raypaths2[0].sensor_contributions[0].coordinates == lpf_file_reader__v1__pb2.DoubletDouble(
assert raypaths2[0].sensor_contributions[0].coordinates == lpf_file_reader__v2__pb2.DoubletDouble(
x=-0.20848463202592682, y=0.1897648665199252
)

# Delete the second reader
stub.Delete(guid2)
stub.Delete(lpf_file_reader__v2__pb2.Delete_Request_Multi(lpf_reader_guid=guid2))

# Read the first
raypaths = []
for rp in stub.Read(lpf_file_reader__v1__pb2.ReadRequest_Multi(id=guid)):
for rp in stub.Read(lpf_file_reader__v2__pb2.Read_Request_Multi(lpf_reader_guid=guid)):
raypaths.append(rp)
assert len(raypaths) == nb_of_traces

# Check some result (first entry)
expected_nb_of_impact = 5
assert len(raypaths[0].impacts) == expected_nb_of_impact
assert raypaths[0].impacts[1] == lpf_file_reader__v1__pb2.TripletFloat(x=3.19368935, y=14.999999, z=-3.94779062)
assert raypaths[0].impacts[1] == lpf_file_reader__v2__pb2.TripletFloat(x=3.19368935, y=14.999999, z=-3.94779062)
assert len(raypaths[0].interaction_statuses) == expected_nb_of_impact
assert (
raypaths[0].interaction_statuses[1] == lpf_file_reader__v1__pb2.RayPath.PhotonStatus.StatusSpecularTransmitted
raypaths[0].interaction_statuses[1] == lpf_file_reader__v2__pb2.RayPath.PhotonStatus.StatusSpecularTransmitted
)
assert len(raypaths[0].sensor_contributions) == 0

# Close and Delete the first
stub.CloseLpfFileName(guid)
stub.Delete(guid)
stub.CloseLpfFileName(lpf_file_reader__v2__pb2.CloseLpfFileName_Request_Multi(lpf_reader_guid=guid))
stub.Delete(lpf_file_reader__v2__pb2.Delete_Request_Multi(lpf_reader_guid=guid))


def test_lpf_file_reader_mono_v1_DirectSimu_with_upload():
# local file upload to the server
path = os.path.join(local_test_path, "basic_DirectSimu.lpf")
file_transfer_stub = grpc_stub.get_stub_insecure_channel(
target="localhost:" + str(config.get("SpeosServerPort")),
stub_type=file_transfer__v1__pb2_grpc.FileTransferServiceStub,
)
upload_response = file_transfer_helper__v1.upload_file(file_transfer_stub, path)

# Lpf file reader creation
stub = grpc_stub.get_stub_insecure_channel(
target="localhost:" + str(config.get("SpeosServerPort")),
stub_type=lpf_file_reader__v2__pb2_grpc.LpfFileReader_MonoStub,
)

# Init with uri from file transfer
stub.InitLpfFileName(lpf_file_reader__v2__pb2.InitLpfFileName_Request_Mono(lpf_file_uri=upload_response.uri))

# GetInformation
res_information = stub.GetInformation(lpf_file_reader__v2__pb2.GetInformation_Request_Mono())
nb_of_traces = res_information.nb_of_traces
assert nb_of_traces == 24817
assert res_information.nb_of_xmps == 3
assert res_information.has_sensor_contributions == False # No contributions stored in Direct simu
assert len(res_information.sensor_names) == 3
assert res_information.sensor_names[0] == "Irradiance Sensor (0)"
assert res_information.sensor_names[2] == "Irradiance Sensor (2)"

# Read
raypaths = []
for rp in stub.Read(lpf_file_reader__v2__pb2.Read_Request_Mono()):
raypaths.append(rp)
assert len(raypaths) == nb_of_traces

# Check result (first entry)
expected_nb_of_impact = 5
assert len(raypaths[0].impacts) == expected_nb_of_impact
assert raypaths[0].impacts[1] == lpf_file_reader__v2__pb2.TripletFloat(x=3.19368935, y=14.999999, z=-3.94779062)
assert len(raypaths[0].wavelengths) == expected_nb_of_impact
assert raypaths[0].wavelengths[1] == 691.44708251953125
assert len(raypaths[0].body_context_ids) == expected_nb_of_impact
assert raypaths[0].body_context_ids[1] == 2001802324
assert len(raypaths[0].unique_face_ids) == expected_nb_of_impact
assert raypaths[0].unique_face_ids[1] == 1815582994
assert raypaths[0].lastDirection == lpf_file_reader__v2__pb2.TripletFloat(
x=0.0606396869, y=0.995341122, z=-0.0749590397
)
assert len(raypaths[0].interaction_statuses) == expected_nb_of_impact
assert raypaths[0].interaction_statuses[0] == lpf_file_reader__v2__pb2.RayPath.PhotonStatus.StatusJustEmitted
assert (
raypaths[0].interaction_statuses[1] == lpf_file_reader__v2__pb2.RayPath.PhotonStatus.StatusSpecularTransmitted
)
assert len(raypaths[0].sensor_contributions) == 0

# Close
stub.CloseLpfFileName(lpf_file_reader__v2__pb2.CloseLpfFileName_Request_Mono())

# Delete the file previously uploaded to the server - it is no more needed
file_transfer_stub.Delete(file_transfer__v1__pb2.Delete_Request(uri=upload_response.uri))
Loading