Skip to content

Commit 1f54c9e

Browse files
tg359Fraser Greenroyd
authored andcommitted
Updated to reflect changes in Python_Toolkit
1 parent 3e8d78e commit 1f54c9e

12 files changed

Lines changed: 78 additions & 259 deletions

LadybugTools_Engine/Compute/EPWtoCSV.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public static string EPWtoCSV(string epwFile, bool includeAdditional = false)
4848
BH.Engine.Base.Compute.RecordError($"{epwFile} doesn't appear to exist!");
4949
return null;
5050
}
51-
52-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
51+
52+
PythonEnvironment env = InstallPythonEnv_LBT(true);
5353
string additionalProperties = includeAdditional ? "True" : "False";
5454

5555
string pythonScript = string.Join("\n", new List<string>()

LadybugTools_Engine/Compute/EPWtoCustomObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static CustomObject EPWtoCustomObject(string epwFile)
4848
return null;
4949
}
5050

51-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
51+
PythonEnvironment env = InstallPythonEnv_LBT(true);
5252

5353
string pythonScript = string.Join("\n", new List<string>()
5454
{

LadybugTools_Engine/Compute/ExternalComfort.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static ExternalComfort ExternalComfort(SimulationResult simulationResult,
7171

7272
// send to Python to simulate/load
7373
string externalComfortJsonStr = System.Text.RegularExpressions.Regex.Unescape(externalComfort.ToJson());
74-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
74+
PythonEnvironment env = InstallPythonEnv_LBT(true);
7575
string pythonScript = string.Join("\n", new List<string>()
7676
{
7777
"import json",

LadybugTools_Engine/Compute/GEMtoHBJSON.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static string GEMtoHBJSON(string gem)
4949
return null;
5050
}
5151

52-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
52+
PythonEnvironment env = InstallPythonEnv_LBT(true);
5353

5454
string gemFile = System.IO.Path.GetFullPath(gem);
5555
string outputDirectory = System.IO.Path.GetDirectoryName(gem);

LadybugTools_Engine/Compute/HBJSONtoGEM.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static string HBJSONtoGEM(string hbjson)
4949
return null;
5050
}
5151

52-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
52+
PythonEnvironment env = InstallPythonEnv_LBT(true);
5353

5454
string hbjsonFile = System.IO.Path.GetFullPath(hbjson);
5555
string outputDirectory = System.IO.Path.GetDirectoryName(hbjsonFile);

LadybugTools_Engine/Compute/InstallPythonEnv_LBT.cs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,61 @@
2222

2323
using BH.oM.Base.Attributes;
2424
using BH.oM.Python;
25+
using BH.oM.Python.Enums;
2526
using System.ComponentModel;
26-
using System.Collections.Generic;
2727
using System.IO;
28+
using BH.Engine.Python;
2829

2930
namespace BH.Engine.LadybugTools
3031
{
3132
public static partial class Compute
3233
{
3334
[Description("Create the BHoM Python environment for LadybugTools_Toolkit. This creates a replica of what is found in the Pollination installed python environment, for extension using BHoM.")]
3435
[Input("run", "Run the installation process.")]
36+
[Input("reinstall", "Reinstall the environment if it already exists.")]
3537
[Output("env", "The LadybugTools_Toolkit Python Environment, with BHoM code accessible.")]
36-
public static PythonEnvironment InstallPythonEnv_LBT(bool run = false)
38+
public static PythonEnvironment InstallPythonEnv_LBT(bool run = false, bool reinstall = false)
3739
{
40+
// check if referenced Python is installed
3841
string referencedExecutable = @"C:\Program Files\ladybug_tools\python\python.exe";
42+
if (!File.Exists(referencedExecutable))
43+
{
44+
Base.Compute.RecordError($"Could not find referenced python executable at {referencedExecutable}. Please install Pollination try again.");
45+
return null;
46+
}
47+
48+
if (!run)
49+
return null;
3950

40-
PythonEnvironment referencedEnvironment = Python.Compute.InstallReferencedVirtualenv(
41-
name: Query.ToolkitName(),
42-
executable: referencedExecutable,
43-
localPackage: Path.Combine(Python.Query.CodeDirectory(), Query.ToolkitName()),
44-
run: run
45-
);
51+
// find out whether this environment already exists
52+
bool exists = Python.Query.VirtualEnvironmentExists(Query.ToolkitName());
4653

47-
// reload environment to establish the new executable path
48-
PythonEnvironment localEnvironment = Python.Query.VirtualEnv(Query.ToolkitName());
54+
if (reinstall)
55+
Python.Compute.RemoveVirtualEnvironment(Query.ToolkitName());
56+
57+
// obtain python version
58+
PythonVersion pythonVersion = Python.Query.Version(referencedExecutable);
4959

50-
// check here to ensure that referenced executable is using same version as local BHoM environment executable
51-
List<string> packagesToCheck = new List<string>() { "lbt-ladybug", "lbt-dragonfly", "lbt-honeybee", "lbt-recipes" };
52-
foreach ( string package in packagesToCheck )
60+
// create virtualenvironment
61+
PythonEnvironment env = Python.Compute.VirtualEnvironment(version: pythonVersion, name: Query.ToolkitName(), reload: true);
62+
63+
// return null if environment could not be created/loaded
64+
if (env == null)
65+
return null;
66+
67+
// install packages if this is a reinstall, or the environment did not originally exist
68+
if (reinstall || !exists)
5369
{
54-
string installed = Python.Compute.RunCommandStdout($"{Python.Modify.AddQuotesIfRequired(localEnvironment.Executable)} -m pip freeze | FindStr {package}");
55-
string referenced = Python.Compute.RunCommandStdout($"{Python.Modify.AddQuotesIfRequired(referencedEnvironment.Executable)} -m pip freeze | FindStr {package}");
56-
if (installed != referenced)
57-
{
58-
Base.Compute.RecordWarning($"BHoM environment {package} does not match referenced package version ({installed} != {referenced}). " +
59-
$"This can be caused by the BHoM version and installed version becoming out of sync. " +
60-
$"Try deleting the {Python.Query.VirtualEnvDirectory("LadybugTools_Toolkit")} directory and re-running the " +
61-
$"{System.Reflection.MethodBase.GetCurrentMethod().Name} method again to fix this.");
62-
}
70+
// install local package
71+
env.InstallPackageLocal(Path.Combine(Python.Query.DirectoryCode(), Query.ToolkitName()));
72+
73+
// create requiremetns from referenced executable
74+
string requirementsTxt = Python.Compute.RequirementsTxt(referencedExecutable);
75+
env.InstallRequirements(requirementsTxt);
76+
File.Delete(requirementsTxt);
6377
}
6478

65-
return localEnvironment;
79+
return env;
6680
}
6781
}
6882
}

LadybugTools_Engine/Compute/SimulationResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static SimulationResult SimulationResult(string epwFile, ILadybugToolsMat
7777

7878
// send to Python to simulate/load
7979
string simulationResultJsonStr = System.Text.RegularExpressions.Regex.Unescape(simulationResult.ToJson());
80-
PythonEnvironment env = Python.Query.VirtualEnv(Query.ToolkitName());
80+
PythonEnvironment env = InstallPythonEnv_LBT(true);
8181
string pythonScript = string.Join("\n", new List<string>()
8282
{
8383
"import json",

LadybugTools_Engine/LadybugTools_Engine.csproj

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
@@ -159,16 +159,15 @@
159159
</PropertyGroup>
160160
<PropertyGroup>
161161
<PreBuildEvent>
162-
:: create infrastructure necessary to support Python environments
163162
if not exist "C:\ProgramData\BHoM\Extensions\PythonEnvironments" mkdir "C:\ProgramData\BHoM\Extensions\PythonEnvironments"
164163
if not exist "C:\ProgramData\BHoM\Extensions\PythonCode" mkdir "C:\ProgramData\BHoM\Extensions\PythonCode"
165-
:: remove any old versions within target directory for current toolkits Python code
164+
166165
if exist "C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)" rmdir "C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)" /S /Q
167-
:: move Python code over to toolkit extensions folder, redirecting output to temp log file to silence it
166+
mkdir "C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)"
167+
168168
robocopy "$(ProjectDir)Python" "C:\ProgramData\BHoM\Extensions\PythonCode\$(SolutionName)" /mir /xf "*.pyc" "*.ipynb" /xd "__*__" ".*" &gt; output.log
169-
:: remove temporary log file
170169
del output.log
171-
</PreBuildEvent>
170+
</PreBuildEvent>
172171
</PropertyGroup>
173172
<Import Project="..\packages\RhinoCommon.6.33.20343.16431\build\net45\RhinoCommon.targets" Condition="Exists('..\packages\RhinoCommon.6.33.20343.16431\build\net45\RhinoCommon.targets')" />
174173
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
@@ -177,4 +176,4 @@
177176
</PropertyGroup>
178177
<Error Condition="!Exists('..\packages\RhinoCommon.6.33.20343.16431\build\net45\RhinoCommon.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\RhinoCommon.6.33.20343.16431\build\net45\RhinoCommon.targets'))" />
179178
</Target>
180-
</Project>
179+
</Project>
Lines changed: 13 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -1,206 +1,13 @@
1-
aiofiles==23.1.0
2-
aiohttp==3.8.4
3-
aiosignal==1.3.1
4-
aiosqlite==0.19.0
5-
anyio==3.7.0
6-
argon2-cffi==21.3.0
7-
argon2-cffi-bindings==21.2.0
8-
arrow==1.2.3
9-
astroid==2.15.5
10-
async-timeout==4.0.2
11-
asynctest==0.13.0
12-
attrs==23.1.0
13-
Babel==2.12.1
14-
backcall==0.2.0
15-
beautifulsoup4==4.12.2
16-
black==23.3.0
17-
bleach==6.0.0
18-
cached-property==1.5.2
19-
case-converter==1.1.0
20-
certifi==2023.5.7
21-
cffi==1.15.1
22-
charset-normalizer==3.1.0
23-
click==7.1.2
24-
click-plugins==1.1.1
25-
colorama==0.4.6
26-
coverage==7.2.7
27-
cramjam==2.6.2
28-
cycler==0.11.0
29-
debugpy==1.6.7
30-
decorator==5.1.1
31-
defusedxml==0.7.1
32-
dill==0.3.6
33-
distlib==0.3.6
34-
dnspython==2.3.0
35-
docutils==0.20.1
36-
dragonfly-core==1.41.16
37-
dragonfly-doe2==0.11.3
38-
dragonfly-energy==1.23.23
39-
dragonfly-radiance==0.2.54
40-
dragonfly-schema==1.9.2
41-
dragonfly-uwg==0.5.352
42-
entrypoints==0.4
43-
et-xmlfile==1.1.0
44-
exceptiongroup==1.1.1
45-
fastjsonschema==2.17.1
46-
fastparquet==0.8.1
47-
filelock==3.12.2
48-
fonttools==4.38.0
49-
fortranformat==2.0.0
50-
fqdn==1.5.1
51-
frozenlist==1.3.3
52-
fsspec==2023.1.0
53-
greenlet==2.0.2
54-
honeybee-core==1.55.15
55-
honeybee-display==0.2.19
56-
honeybee-energy==1.100.2
57-
honeybee-energy-standards==2.2.6
58-
honeybee-ies==0.7.1
59-
honeybee-radiance==1.64.183
60-
honeybee-radiance-command==1.22.2
61-
honeybee-radiance-folder==2.11.11
62-
honeybee-radiance-postprocess==0.4.219
63-
honeybee-schema==1.53.2
64-
honeybee-standards==2.0.6
65-
honeybee-vtk==0.39.1
66-
idna==3.4
67-
importlib-metadata==6.7.0
68-
importlib-resources==5.12.0
69-
iniconfig==2.0.0
70-
ipykernel==6.16.2
71-
ipython==7.34.0
72-
ipython-genutils==0.2.0
73-
isoduration==20.11.0
74-
isort==5.11.5
75-
jedi==0.18.2
76-
Jinja2==3.1.2
77-
joblib==1.2.0
78-
json5==0.9.14
79-
jsonpointer==2.4
80-
jsonschema==4.3.3
81-
jupyter-events==0.6.3
82-
jupyter-server==1.24.0
83-
jupyter-ydoc==1.0.2
84-
jupyter_client==7.4.9
85-
jupyter_core==4.12.0
86-
jupyter_server_fileid==0.9.0
87-
jupyter_server_ydoc==0.8.0
88-
jupyterlab==3.6.4
89-
jupyterlab-pygments==0.2.2
90-
jupyterlab_server==2.23.0
91-
kiwisolver==1.4.4
92-
ladybug-comfort==0.16.35
93-
ladybug-core==0.41.25
94-
ladybug-display==0.9.1
95-
ladybug-geometry==1.27.1
96-
ladybug-geometry-polyskel==1.4.18
97-
ladybug-radiance==0.2.0
98-
ladybug-rhino==1.39.14
99-
lazy-object-proxy==1.9.0
100-
lbt-dragonfly==0.10.146
101-
lbt-honeybee==0.7.238
102-
lbt-ladybug==0.27.24
103-
lbt-recipes==0.25.8
104-
lockfile==0.12.2
105-
luigi==3.0.3
106-
MarkupSafe==2.1.3
107-
matplotlib==3.5.3
108-
matplotlib-inline==0.1.6
109-
mccabe==0.7.0
110-
meteostat==1.6.5
111-
mistune==3.0.1
112-
multidict==6.0.4
113-
mypy-extensions==1.0.0
114-
nbclassic==1.0.0
115-
nbclient==0.7.4
116-
nbconvert==7.6.0
117-
nbformat==5.8.0
118-
nest-asyncio==1.5.6
119-
notebook==6.5.4
120-
notebook_shim==0.2.3
121-
numexpr==2.8.4
122-
numpy==1.21.6
123-
opencv-python==4.7.0.72
124-
openpyxl==3.1.2
125-
packaging==23.1
126-
pandas==1.3.5
127-
pandas-downcast==1.2.4
128-
pandocfilters==1.5.0
129-
parso==0.8.3
130-
pathspec==0.11.1
131-
pickleshare==0.7.5
132-
Pillow==9.5.0
133-
pip-chill==1.0.3
134-
pkgutil_resolve_name==1.3.10
135-
platformdirs==3.8.0
136-
pluggy==1.2.0
137-
pollination-handlers==0.10.0
138-
prometheus-client==0.17.0
139-
prompt-toolkit==3.0.38
140-
psutil==5.9.5
141-
pyarrow==12.0.1
142-
pycparser==2.21
143-
pydantic==1.10.9
144-
pydantic-openapi-helper==0.2.10
145-
Pygments==2.15.1
146-
pylint==2.17.4
147-
pymongo==4.4.0
148-
pyodbc==4.0.39
149-
pyparsing==3.1.0
150-
pyperclip==1.8.2
151-
pyrsistent==0.19.3
152-
pytest==7.4.0
153-
pytest-cov==4.1.0
154-
pytest-order==1.1.0
155-
python-daemon==3.0.1
156-
python-dateutil==2.8.2
157-
python-json-logger==2.0.7
158-
# Editable install with no version control (python-toolkit==0.0.0)
159-
-e c:\programdata\bhom\extensions\pythoncode\python_toolkit\src
160-
pytz==2023.3
161-
pywin32==306
162-
pywinpty==2.0.10
163-
PyYAML==6.0
164-
pyzmq==25.1.0
165-
queenbee==1.26.7
166-
queenbee-local==0.5.4
167-
requests==2.31.0
168-
rfc3339-validator==0.1.4
169-
rfc3986-validator==0.1.1
170-
scikit-learn==1.0.2
171-
scipy==1.7.3
172-
Send2Trash==1.8.2
173-
shapely==2.0.1
174-
six==1.16.0
175-
sniffio==1.3.0
176-
sockets==1.0.0
177-
soupsieve==2.4.1
178-
SQLAlchemy==2.0.17
179-
tables==3.7.0
180-
tenacity==6.3.1
181-
terminado==0.17.1
182-
threadpoolctl==3.1.0
183-
tinycss2==1.2.1
184-
tomli==2.0.1
185-
tomlkit==0.11.8
186-
tornado==6.2
187-
tqdm==4.65.0
188-
traitlets==5.9.0
189-
typed-ast==1.5.4
190-
typing_extensions==4.6.3
191-
uri-template==1.3.0
192-
urllib3==2.0.3
193-
uwg==5.8.11
194-
virtualenv==20.23.1
195-
vtk==9.2.6
196-
wcwidth==0.2.6
197-
webcolors==1.13
198-
webencodings==0.5.1
199-
websocket-client==1.6.1
200-
wrapt==1.15.0
201-
wslink==1.11.0
202-
xlrd==2.0.1
203-
y-py==0.6.0
204-
yarl==1.9.2
205-
ypy-websocket==0.12.1
206-
zipp==3.15.0
1+
case-converter
2+
ladybug-charts
3+
openpyxl
4+
pyarrow
5+
pylint
6+
pytest
7+
pytest-cov
8+
pytest-order
9+
scikit-learn
10+
scipy
11+
tables
12+
tqdm
13+
xlrd

0 commit comments

Comments
 (0)