|
| 1 | +# This is an example of python code using VTS to Compute photon hitting density for homogeneous |
| 2 | +# medium at a given set of optical properties opRegions[0]. |
| 3 | +# This sample uses ComputeFluence in place of calling FluenceOfRhoAndZ on the forward solver object |
| 4 | +# and it uses a distributed point source SDA Forward Solver |
| 5 | +# |
| 6 | +# Import PythonNet |
| 7 | +from pythonnet import load |
| 8 | +load('coreclr') |
| 9 | +import clr |
| 10 | +# Import the Operating System so we can access the files for the VTS library |
| 11 | +import os |
| 12 | +file = '../libraries/Vts.dll' |
| 13 | +clr.AddReference(os.path.abspath(file)) |
| 14 | +import numpy as np |
| 15 | +import plotly.graph_objects as go |
| 16 | +import plotly.express as px |
| 17 | +from Vts import * |
| 18 | +from Vts.Common import * |
| 19 | +from Vts.Extensions import * |
| 20 | +from Vts.Modeling.Optimizers import * |
| 21 | +from Vts.Modeling.ForwardSolvers import * |
| 22 | +from Vts.SpectralMapping import * |
| 23 | +from Vts.Factories import * |
| 24 | +from Vts.MonteCarlo import * |
| 25 | +from Vts.MonteCarlo.Sources import * |
| 26 | +from Vts.MonteCarlo.Tissues import * |
| 27 | +from Vts.MonteCarlo.Detectors import * |
| 28 | +from Vts.MonteCarlo.Factories import * |
| 29 | +from Vts.MonteCarlo.PhotonData import * |
| 30 | +from Vts.MonteCarlo.PostProcessing import * |
| 31 | +from System import Array, Double, Object, Func, Math |
| 32 | +clr.AddReference("System.Core") |
| 33 | +from System.Linq import Enumerable |
| 34 | + |
| 35 | +solver = DistributedPointSourceSDAForwardSolver() |
| 36 | + |
| 37 | +topLayerThickness = 5 |
| 38 | +opRegions = Array.CreateInstance(IOpticalPropertyRegion, 1) |
| 39 | +opRegions[0] = LayerOpticalPropertyRegion(DoubleRange(0, topLayerThickness, 2), OpticalProperties(0.1, 1, 0.8, 1.4)) |
| 40 | +# Create the DoubleRange instance |
| 41 | +rhos_range = DoubleRange(0.1, 19.9, 100) # range of s-d separations in mm |
| 42 | + |
| 43 | +# Convert to .NET array |
| 44 | +rho_delta = rhos_range.GetDelta() |
| 45 | +print(rho_delta) |
| 46 | +# Start at 0.1, increment by 0.2, 100 elements |
| 47 | +rhos = 0.1 + rho_delta * np.arange(100) |
| 48 | +print(rhos) |
| 49 | + |
| 50 | +zs_range = DoubleRange(0.1, 19.9, 100) # range of depths in mm |
| 51 | + |
| 52 | +# Convert to .NET array |
| 53 | +zs_delta = zs_range.GetDelta() |
| 54 | +print(zs_delta) |
| 55 | +# Start at 0.1, increment by delta, 100 elements |
| 56 | +zs = 0.1 + zs_delta * np.arange(100) |
| 57 | +print(zs) |
| 58 | + |
| 59 | +allRhos = np.concatenate((-rhos[::-1], rhos)) |
| 60 | + |
| 61 | +opRegionsArray = Array[Array[IOpticalPropertyRegion]]([opRegions]) |
| 62 | +# predict the tissue's fluence(rho, z) for the given optical properties |
| 63 | +independentAxes = Array.CreateInstance(IndependentVariableAxis, 1) |
| 64 | +independentAxes[0] = IndependentVariableAxis.Z |
| 65 | +independentValues = Array.CreateInstance(Array[Double], 2) |
| 66 | +independentValues[0] = Array[Double](allRhos.tolist()) |
| 67 | +independentValues[1] = Array[Double](zs.tolist()) |
| 68 | +# Call the static method ComputeFluence in ComputationFactory to get the fluence data |
| 69 | +fluenceOfRhoAndZ = ComputationFactory.ComputeFluence(solver, FluenceSolutionDomainType.FluenceOfRhoAndZ, independentAxes, independentValues, opRegions, Array[Double](allRhos.tolist())) |
| 70 | + |
| 71 | +#PHD |
| 72 | +sourceDetectorSeparation = 10 |
| 73 | +opArray = Array.CreateInstance(OpticalProperties, 2) |
| 74 | +opArray[0] = OpticalProperties(0.1, 1, 0.8, 1.4) |
| 75 | +opArray[1] = OpticalProperties(0.01, 1, 0.8, 1.4) |
| 76 | + |
| 77 | +phdOfRhoAndZ = ComputationFactory.GetPHD(ForwardSolverType.TwoLayerSDA, fluenceOfRhoAndZ, sourceDetectorSeparation, opArray, Array[Double](allRhos.tolist()), Array[Double](zs.tolist())) |
| 78 | + |
| 79 | +# log transform |
| 80 | +log_phd = [Math.Log(f) for f in phdOfRhoAndZ] |
| 81 | + |
| 82 | +size = len(zs) |
| 83 | +# split into rows |
| 84 | +phdRowsToPlot = np.array([log_phd[i:i+size] for i in range(0, len(log_phd), size)]) |
| 85 | + |
| 86 | +# Heatmap function to convert the data into a heat map |
| 87 | +def heatmap(values, x, y, x_label="", y_label="", title=""): |
| 88 | + """Create a heatmap chart.""" |
| 89 | + # values should be a 2D array-like (list of lists or 2D numpy array) |
| 90 | + fig = go.Figure(data=go.Heatmap( |
| 91 | + z=values, |
| 92 | + x=x, |
| 93 | + y=y, |
| 94 | + transpose=True, |
| 95 | + colorscale='Hot', |
| 96 | + colorbar=dict(title=title) |
| 97 | + )) |
| 98 | + fig.update_layout( |
| 99 | + title=title, |
| 100 | + xaxis_title=x_label, |
| 101 | + yaxis_title=y_label, |
| 102 | + yaxis_autorange='reversed' |
| 103 | + ) |
| 104 | + return fig |
| 105 | + |
| 106 | +fluenceChart = heatmap(phdRowsToPlot.tolist(), allRhos.tolist(), list(zs), "ρ [mm]", "z [mm]", "log(phd(ρ, z) [mm-2])") |
| 107 | +fluenceChart.show(renderer="browser") |
0 commit comments