Skip to content

Commit 717103b

Browse files
authored
Merge pull request #98 from openalea/mtg
Convert xlsx to MTG
2 parents a13f199 + 0669323 commit 717103b

File tree

3 files changed

+55
-55
lines changed

3 files changed

+55
-55
lines changed

example/tutorials/convert_excel_to_mtg.ipynb

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
"metadata": {},
88
"outputs": [],
99
"source": [
10+
"import os\n",
1011
"from pathlib import Path\n",
11-
"import pandas as pd\n",
1212
"from datetime import datetime\n",
1313
"\n",
14+
"import pandas as pd\n",
15+
"\n",
16+
"from openalea.mtg import MTG\n",
1417
"from openalea.mtg.io import write_mtg\n",
15-
"from openalea.deploy.shared_data import shared_data\n",
1618
"from oawidgets.plantgl import PlantGL\n",
1719
"\n",
1820
"import openalea.strawberry\n",
19-
"from openalea.strawberry.import_mtgfile import import_mtgfile,import_mtg_from_csv, union,strawberry_reader_csv\n",
21+
"from openalea.strawberry.data import data_directory\n",
22+
"from openalea.strawberry.import_mtgfile import import_mtgfile,import_mtg_from_csv, union, strawberry_reader_csv\n",
2023
"from openalea.strawberry import analysis\n",
2124
"from openalea.strawberry import visu2d, visu3d\n",
2225
"from openalea.plantgl.all import Viewer"
@@ -41,7 +44,9 @@
4144
"metadata": {},
4245
"outputs": [],
4346
"source": [
44-
"shared_data(openalea.strawberry) / \"breeding_value\""
47+
"path_data = data_directory / \"breeding_value\"\n",
48+
"files=list(path_data.glob(\"*xlsx\"))\n",
49+
"print(files)"
4550
]
4651
},
4752
{
@@ -54,6 +59,16 @@
5459
"mtgs=import_mtg_from_csv(files=files, first_property=\"experimental_names\",symbol_at_scale=dict(P=1,T=2, F=3, f=3, b=3, HT=3, bt=3, ht=3,s=3))"
5560
]
5661
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"id": "10cedf67",
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"list(mtgs)"
70+
]
71+
},
5772
{
5873
"cell_type": "code",
5974
"execution_count": null,
@@ -112,7 +127,7 @@
112127
"data=path_data / filename\n",
113128
"f = open(str(data), 'w',encoding='UTF8')\n",
114129
"f.write(mtg_lines)\n",
115-
"f.close()"
130+
"f.close()\n"
116131
]
117132
},
118133
{
@@ -122,17 +137,9 @@
122137
"metadata": {},
123138
"outputs": [],
124139
"source": [
125-
"t= import_mtgfile(filename=\"Belle_et_Bonne\")"
126-
]
127-
},
128-
{
129-
"cell_type": "code",
130-
"execution_count": null,
131-
"id": "65a6098c-15e3-4544-8ba0-ffcef2d64a6b",
132-
"metadata": {},
133-
"outputs": [],
134-
"source": [
135-
"import_mtgfile?"
140+
"\n",
141+
"t= MTG(data)\n",
142+
"t.display()"
136143
]
137144
},
138145
{
@@ -145,7 +152,7 @@
145152
},
146153
{
147154
"cell_type": "code",
148-
"execution_count": 1,
155+
"execution_count": null,
149156
"id": "f8cca6f3-bf39-476f-95a8-c90decd57fc4",
150157
"metadata": {},
151158
"outputs": [],
@@ -167,21 +174,22 @@
167174
},
168175
{
169176
"cell_type": "code",
170-
"execution_count": 2,
177+
"execution_count": null,
171178
"id": "ae301e8f",
172179
"metadata": {},
173180
"outputs": [],
174181
"source": [
175182
"def path_data(directory_name=None, filename=None):\n",
176183
" if directory_name is None:\n",
177-
" pathdir = shared_data(openalea.strawberry)\n",
184+
" pathdir = Path(data_directory)\n",
178185
" else:\n",
179-
" pathdir= shared_data(openalea.strawberry) / directory_name\n",
186+
" pathdir= Path(data_directory) / directory_name\n",
187+
" if (not pathdir.exists()) and (Path(directory_name).exists()):\n",
188+
" pathdir = Path(directory_name)\n",
189+
" \n",
180190
" \n",
181-
" if filename is None:\n",
182-
" pathdir = pathdir\n",
183-
" else:\n",
184-
" pathdir = pathdir / filename +\".xlsx\"\n",
191+
" if filename:\n",
192+
" pathdir = pathdir / (filename +\".xlsx\")\n",
185193
" \n",
186194
" return pathdir\n",
187195
"\n",
@@ -204,26 +212,28 @@
204212
" \n",
205213
" path_file=path_data(directory_name=directory_name, filename=filename)\n",
206214
" \n",
207-
" if path_file.endswith(\".xlsx\"):\n",
215+
" if path_file.name.endswith(\".xlsx\"):\n",
208216
" g=strawberry_reader_csv(file=path_file,first_property=\"experimental_name\",symbole_at_scale=symbole_at_scale)\n",
209217
" g= union(g)\n",
210218
" else: \n",
211-
" files=list(path_file.glob(\"*xlsx\"))\n",
219+
" files=list(path_file.glob(\"*.xlsx\"))\n",
212220
" #assert (files== None, \"directory no contain .xlsx file\")\n",
213221
" g= import_mtg_from_csv(files=files,first_property=\"experimental_name\",symbol_at_scale=symbole_at_scale)\n",
214222
" g=union(g)\n",
215223
" \n",
216-
" for k in g.property('Sample_date').keys():\n",
217-
" g.property('Sample_date')[k]=datetime.strftime(g.property('Sample_date')[k], '%d/%m/%Y')\n",
218-
" g.property('Architecture_date')[k]=datetime.strftime(g.property('Architecture_date')[k], '%d/%m/%Y')\n",
224+
" sample_dates = g.property('Sample_date')\n",
225+
" archi_dates = g.property('Architecture_date')\n",
226+
" for k in sample_dates:\n",
227+
" sample_dates[k]=datetime.strftime(sample_dates[k], '%d/%m/%Y')\n",
228+
" archi_dates[k]=datetime.strftime(archi_dates[k], '%d/%m/%Y')\n",
219229
" \n",
220230
" # convert total_flowers name in FLWRNUMBER (properties requiered in the source code)\n",
221231
" g.properties()[\"FLWRNUMBER\"]=g.properties()[\"total_flowers\"]\n",
222232
" del g.properties()[\"total_flowers\"]\n",
223233
" \n",
224234
" max_order= max(g.property(\"order\").values())+1\n",
225235
" mtg_lines = write_mtg(g, properties,class_at_scale=symbole_at_scale,nb_tab=max_order)\n",
226-
" name = path_file.replace('.xlsx','.mtg')\n",
236+
" name = path_file.name.replace('.xlsx','.mtg')\n",
227237
" \n",
228238
" f = open(str(name), 'w',encoding='UTF8')\n",
229239
" f.write(mtg_lines)\n",
@@ -238,6 +248,14 @@
238248
"### For a single .xlsx file"
239249
]
240250
},
251+
{
252+
"cell_type": "code",
253+
"execution_count": null,
254+
"id": "781e9db9",
255+
"metadata": {},
256+
"outputs": [],
257+
"source": []
258+
},
241259
{
242260
"cell_type": "code",
243261
"execution_count": null,
@@ -261,26 +279,15 @@
261279
},
262280
{
263281
"cell_type": "code",
264-
"execution_count": 3,
282+
"execution_count": null,
265283
"id": "fa2a156e-3d99-47f4-8829-b1d8377288bf",
266284
"metadata": {},
267-
"outputs": [
268-
{
269-
"name": "stderr",
270-
"output_type": "stream",
271-
"text": [
272-
"c:\\users\\mlabadie\\documents\\github\\strawberry\\src\\openalea\\strawberry\\import_mtgfile.py:222: Warning: Redeclaration of module 'F'.\n",
273-
" l.declare(module)\n",
274-
"c:\\users\\mlabadie\\documents\\github\\strawberry\\src\\openalea\\strawberry\\import_mtgfile.py:222: Warning: Redeclaration of module 'f'.\n",
275-
" l.declare(module)\n"
276-
]
277-
}
278-
],
285+
"outputs": [],
279286
"source": [
280287
"directory_name= \"breeding_value\" # name of your directory containing .xlsx file\n",
281288
"\n",
282-
"path=shared_data(openalea.strawberry).glob(directory_name+'\\*.xlsx')\n",
283-
"names=[filename.split(\"\\\\\")[-1].split(\".\")[0] for filename in path]\n",
289+
"path = (data_directory/directory_name).glob('*.xlsx')\n",
290+
"names=[filename.split(os.sep)[-1].split(\".\")[0] for filename in path]\n",
284291
"for name in names:\n",
285292
" write_excel_to_mtg(directory_name=directory_name,filename=name)"
286293
]
@@ -292,14 +299,6 @@
292299
"metadata": {},
293300
"outputs": [],
294301
"source": []
295-
},
296-
{
297-
"cell_type": "code",
298-
"execution_count": null,
299-
"id": "b99ee175-73a6-40cc-8e79-aad048d88ad6",
300-
"metadata": {},
301-
"outputs": [],
302-
"source": []
303302
}
304303
],
305304
"metadata": {
@@ -318,7 +317,7 @@
318317
"name": "python",
319318
"nbconvert_exporter": "python",
320319
"pygments_lexer": "ipython3",
321-
"version": "3.9.12"
320+
"version": "3.12.1"
322321
}
323322
},
324323
"nbformat": 4,
-28 Bytes
Binary file not shown.

src/openalea/strawberry/import_mtgfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22
from __future__ import print_function
33

4+
import os
45
import pandas as pd
56
import numpy as np
67
from openpyxl import load_workbook
@@ -269,7 +270,7 @@ def import_mtg_from_csv(files,first_property,symbol_at_scale=dict(P=1,T=2, F=3,
269270
'''
270271
mtgs={}
271272
for file in files:
272-
name= str(file).split('\\')[-1].split(".")[0]
273+
name= str(file).split(os.sep)[-1].split(".")[0]
273274
print(name)
274275
mtg= strawberry_reader_csv(file)
275276
mtgs[name]=mtg

0 commit comments

Comments
 (0)