Skip to content
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

Error reading Vensim model (parsimonious.exceptions.IncompleteParseError) #443

Closed
mak63-dev opened this issue Jun 3, 2024 · 50 comments · Fixed by #444
Closed

Error reading Vensim model (parsimonious.exceptions.IncompleteParseError) #443

mak63-dev opened this issue Jun 3, 2024 · 50 comments · Fixed by #444

Comments

@mak63-dev
Copy link

mak63-dev commented Jun 3, 2024

Hello,

I am not able to read a relatively complex Vensim model with eight model views.

I tried two ways and neither worked.

  1. Read the model with the default arguments:

model = pysd.read_vensim('C:\Users\...path...\model.mdl')
*** TypeError: cannot unpack non-iterable NoneType object

  1. Read the model with split_views = True

model = pysd.read_vensim('C:\Users\...path...\model.mdl', split_views=True)
*** parsimonious.exceptions.IncompleteParseError: Rule 'line' matched in its entirety, but it didn't consume all the text. The non-matching portion of the text begins with '|14|BU|0-0-128' (line 1, column 71).

In case 2, the text '|14|BU|0-0-128' raising the error is located toward the end of the mdl file (in text view), having to do with the sketch information generated automatically by Vensim. In particular, the line is in a block related to the eighth and last view:
...
10,73,simulator mode,194,665,68,11,8,131,0,18,-1,0,0,0,-1--1--1,0-0-0,|14|BU|0-0-128
12,74,105057302,192,632,80,20,3,60,0,0,0,0,0,0
simulator mode,0,1,1
30,75,f1303_17 - Copy0027.bmp,370,641,79,29,8,3,0,0,-1,0,0,0
12,76,34931712,812,203,221,110,3,188,0,0,1,0,0,0
...

Thanks for any advice.

@rogersamso
Copy link
Contributor

rogersamso commented Jun 27, 2024

Hi @mak63-dev,

Thanks for reporting this error.

Can you please try using either underline or bold font, but not both, and let us know if this solves the problem?

Thank you

@enekomartinmartinez
Copy link
Collaborator

Hi @mak63-dev

Now the issue with split_views should be solved thanks to @rogersamso's contribution. But I am not sure about the first one. Could you test again with the last version in the master branch, please?

@mak63-dev
Copy link
Author

Hi Roger & Eneko,

Pulled the latest version from git and retried. Indeed now it no longer crashes with split_views where it was crashing before.

But with or without split_views it is now giving me the first error:

File C:\miniconda3\Lib\site-packages\pysd\builders\python\subscripts.py:60 in subscripts
self._subscripts[sub.name] = ExtSubscript(

File C:\miniconda3\Lib\site-packages\pysd\py_backend\external.py:1022 in init
row_last, col_last = self._split_excel_cell(lastcell)

TypeError: cannot unpack non-iterable NoneType object

I wonder if, as a workaround, I could edit the model in text mode and remove all the graphics related code that's generated automatically by Vensim toward the end, then load the bare model without the graphics. Would this work?

Thanks for any help.

@rogersamso
Copy link
Contributor

rogersamso commented Jun 28, 2024

Hi @mak63-dev,

The issue you have now does not seem to be related with the Vensim sketch. It looks like the issue is caused by the use of GET DIRECT SUBSCRIPT or GET XLS SUBSCRIPT in your model.

This function has the following signature GET DIRECT SUBSCRIPT('file','tab',firstcell,'lastcell', 'prefix'), and the error seems to be complaining about not being able to read the lastcell argument correctly.

Before anything else, please verify all uses of these functions in your model, and see if the arguments are pointing to the right Excel cell.

If you don't spot any potential issue, we can investigate further.

Thanks!

@mak63-dev
Copy link
Author

Hi Roger.

The model runs without problems in Vensim. There are no broken links to files or issues with reading the data.

Thanks

@rogersamso
Copy link
Contributor

Hi @mak63-dev,

Unfortunately, the fact that the model runs in Vensim is actually not a guarantee. Vensim is quite flexible and non-opinionated, which can occasionally allow for certain bugs to arise.

Can you share a small sample of your code, or a toy model, where the stated functions are used, so that we can try to pinpoint where the error might originate?

Thanks

@mak63-dev
Copy link
Author

Thanks again Roger,

Sure, I use GET DIRECT SUBSCRIPT quite a bit, not using GET XLS SUBSCRIPT at all.

Here are all the instances in my model where I call this function. I hope it helps:


.subscripts

********************************************************~
|

store:
GET DIRECT SUBSCRIPT( 'C:\...\cum_store.csv'
,'cum_store', 'a2', 'a', 's')
~ Dmnl
~ stores
|
gitem:
GET DIRECT SUBSCRIPT( 'C:\...\s_data.csv'
, 's_data', 'a2', 'a', 'i')
~ Dmnl
~ all the product family items
|
item:
GET DIRECT SUBSCRIPT( 'C:\...\sales_17.csv'
,'sales_17', 'b1', '1', 'i')
~ Dmnl
~ active items in the current store
|
fitem:
GET DIRECT SUBSCRIPT( 'C:\...\pred_sales_17.csv'
,'pred_sales_17', 'b1', '1', 'i')
~ Dmnl
~ items carried by the currently simulated store with adequate data for a
forecast
|

ditem:
GET DIRECT SUBSCRIPT( 'C:\...\d_item_indices.csv'
,'d_item_indices', 'a1', '1', 'i')
~ Dmnl
~ all items with identical basic features requiring an additional
'other' feature
|
fixed d item:
GET DIRECT SUBSCRIPT( 'C:\...\d_item_indices.csv'
,'d_item_indices', 'a2', '2', 'i')
~ Dmnl
~ all items with identical basic features requiring an additional 'other'
feature
to differentiate
|

free d item:
GET DIRECT SUBSCRIPT( 'C:\...\d_items.csv'
,'d_items', 'b1', '1', 'i')
~ Dmnl
~ all items with identical basic features requiring an additional 'other'
feature
to differentiate
|

brand:
GET DIRECT SUBSCRIPT( 'C:\...\s_brands.csv'
, 's_brands', 'a1', 'a', 'b')
~ Dmnl
~ |

day of week:
GET DIRECT SUBSCRIPT( 'C:\...\sales_dow_full.csv'
,'sales_dow_full', 'b1', '1', 'w')
~ Dmnl
~ day ow week index: 0 = Monday, 6 = Sunday
|
single holiday:
GET DIRECT SUBSCRIPT( 'C:\...\single_holidays.csv'
, 'single_holidays', 'a1', '', 'h')
~ Dmnl
~ single holiday id's
|
double holiday:
GET DIRECT SUBSCRIPT( 'C:\...\multi_holidays.csv'
, 'multi_holidays', 'a1', '1', 'h')
~ Dmnl
~ double holiday id's
|

s p:
GET DIRECT SUBSCRIPT( 'C:\...\s inputs\s_p.csv'
, 's_p', 'a1', '', 'p')
~ Dmnl
~ s p id's
|
d p:
GET DIRECT SUBSCRIPT( 'C:\...\m_p.csv'
, 'm_p', 'a1', '1', 'p')
~ Dmnl
~ d p id's
|

These are all. I hope it helps.

@rogersamso
Copy link
Contributor

Looks like these might be the problems:

s p:
GET DIRECT SUBSCRIPT( 'C:\...\s inputs\s_p.csv'
, 's_p', 'a1', '', 'p')
~ Dmnl
~ s p id's
|

single holiday:
GET DIRECT SUBSCRIPT( 'C:\...\single_holidays.csv'
, 'single_holidays', 'a1', '', 'h')
~ Dmnl
~ single holiday id's
|

@mak63-dev
Copy link
Author

What is the problem with them? Do I need to specify the last cell instead of letting the default option with '' ?

@rogersamso
Copy link
Contributor

What is the problem with them? Do I need to specify the last cell instead of letting the default option with '' ?

Can you check if that fixes your problem?

@enekomartinmartinez
Copy link
Collaborator

I think the current implementation needs last_cell to be a full cell (letter + number), but Vensim also supports using only a letter or a number as last_cell and will read all the rows/columns starting from first_cell. It also supports only quotes to get everything to the right and below. Could check if we can give support for that easily. But in the case of the no info provided, we need to know how Vensim reads the subscripts (if column by column or row per row), could you send an example of how the subscripts in

s p:
GET DIRECT SUBSCRIPT( 'C:\...\s inputs\s_p.csv'
, 's_p', 'a1', '', 'p')
~ Dmnl
~ s p id's
|

are defined in the file, and how are they shown in Vensim?

@mak63-dev
Copy link
Author

In the example above, it would start from a1 and read horizontally in that row everything to the right.
Changing the model to check if it will run with the last cell specified

@mak63-dev
Copy link
Author

ok, as a quick check, I tried instead of using an empty quote for last cell to pass only a number (which basically says only read everything else in this row/column). This didn't work either.

So, if indeed both a letter and number are needed for the last cell, then all my uses in the model cause problems because I don't specify a last cell. Generally, it is often the case one needs to read an entire row, or column, and only occasionally read partly up to a certain cell. So it would be really helpful to support the 'defaults'. Also, often the files get modified and you don't want to have to change everything in the model.

Does it seem plausible that you might support this? Or I should be going through the files carefully to specify the last cell?

@mak63-dev
Copy link
Author

Further to this discussion, the Vensim manual explains clearly the convention here:

https://www.vensim.com/documentation/fn_get_direct_subscript.html

@enekomartinmartinez
Copy link
Collaborator

Hi, it should be easy to fix, I will try to do it this weekend

@enekomartinmartinez
Copy link
Collaborator

@mak63-dev could you please test with the version in #447 branch update_extsubscript? This should solve the error with the subscripts reading.

@mak63-dev
Copy link
Author

Hi Eneko,

Thanks for taking the time to look into this.
Got the version from update_extsubscript and tried with the updated code but I'm still getting the error:

File C:\miniconda3\Lib\site-packages\pysd\pysd.py:155 in read_vensim
py_model_file = ModelBuilder(abs_model).build_model()

File C:\miniconda3\Lib\site-packages\pysd\builders\python\python_model_builder.py:42 in init
SectionBuilder(section)

File C:\miniconda3\Lib\site-packages\pysd\builders\python\python_model_builder.py:86 in init
self.subscripts = SubscriptManager(

File C:\miniconda3\Lib\site-packages\pysd\builders\python\subscripts.py:30 in init
self.subscripts = abstract_subscripts

File C:\miniconda3\Lib\site-packages\pysd\builders\python\subscripts.py:60 in subscripts
self._subscripts[sub.name] = ExtSubscript(

File C:\miniconda3\Lib\site-packages\pysd\py_backend\external.py:1022 in init
row_last, col_last = self._split_excel_cell(lastcell)

TypeError: cannot unpack non-iterable NoneType object

@enekomartinmartinez
Copy link
Collaborator

Hi, for the error message seems that you are still running the version in master branch. Can you reinstall please? Note that two branches have the same version value, so some installers may not update. You may need to remove and install pysd again.

@mak63-dev
Copy link
Author

I uninstalled pysd, deleted the cloned repository and tried creating a new repository using github directly from

https://github.com/SDXorg/pysd/tree/update_extsubscript

I get an error message: "The repository does not seem to exist anymore. You may not have access, or it may have been deleted or renamed". Not sure why I'm getting this error, I can navigate to the URL via a browser.

@enekomartinmartinez
Copy link
Collaborator

I guess you are using GitHub Desktop, right? I cannot give support in that case as I have never used it.

But I guess you can clone https://github.com/SDXorg/pysd.git as usual and change the branch later somehow.

@mak63-dev
Copy link
Author

I am not getting this to work. Switched to the right branch, rebuilt the library and still getting the same error. Can't tell from the version number either because it shows as 3.14.0
Maybe I'll wait until there is a proper commit to the master branch and then retry.

@mak63-dev
Copy link
Author

ok, I've made some 'progress'. I was able to build the library with the fix using the command

pip install git+https://github.com/SDXorg/pysd.git@update_extsubscript

Now I'm still getting an error, but its different. The new error is shown below:

File C:\miniconda3\Lib\site-packages\pysd\pysd.py:155 in read_vensim
py_model_file = ModelBuilder(abs_model).build_model()

File C:\miniconda3\Lib\site-packages\pysd\builders\python\python_model_builder.py:42 in init
SectionBuilder(section)

File C:\miniconda3\Lib\site-packages\pysd\builders\python\python_model_builder.py:86 in init
self.subscripts = SubscriptManager(

File C:\miniconda3\Lib\site-packages\pysd\builders\python\subscripts.py:30 in init
self.subscripts = abstract_subscripts

File C:\miniconda3\Lib\site-packages\pysd\builders\python\subscripts.py:60 in subscripts
self._subscripts[sub.name] = ExtSubscript(

File C:\miniconda3\Lib\site-packages\pysd\py_backend\external.py:1023 in init
subs = self.get_subscripts_cell(*split, lastcell)

File C:\miniconda3\Lib\site-packages\pysd\py_backend\external.py:1057 in get_subscripts_cell
data = pd.read_excel(

File C:\miniconda3\Lib\site-packages\pandas\io\excel_base.py:495 in read_excel
io = ExcelFile(

File C:\miniconda3\Lib\site-packages\pandas\io\excel_base.py:1554 in init
raise ValueError(

ValueError: Excel file format cannot be determined, you must specify an engine manually.

@mak63-dev
Copy link
Author

From the new errors, it looks like it's following a different sequence of function calls from before. Looks like it's trying to read an Excel file, whereas I am reading from a csv file...

@enekomartinmartinez
Copy link
Collaborator

Hi @mak63-dev

I guess this is because your data is saved in .csv files. If you do not have many of them, I recommend saving them as .xls or .xlsx files (and modifying the extension in the model). We currently don't support .csv files. I will be happy to add this support, but I may need much more time for it as it requires more testing and changing several things...

@enekomartinmartinez
Copy link
Collaborator

enekomartinmartinez commented Jul 1, 2024

In any case, could you send an example of how your csv files are structured? I guess there are "single sheet" Excel files like, right? This would be useful to add support in the future

@mak63-dev
Copy link
Author

These are just generic ASCII files, like .txt files, with ',' separated fields (by default, or another delimiter if specified). They can be edited with any basic editor. For example, one of the subscripts in my model is

store:
GET DIRECT SUBSCRIPT( 'C:...\cum_store.csv'
,'cum_store', 'a2', 'a', 's')
~ Dmnl
~ stores
|

and reads from a file that looks like below. The subscripts are read starting from a2 (the 0 under "store") and getting the entire rest of the column under "store"), so 0-36

store,sales,adj_demand,bln_demand,size,n_days
0,6882,6926.420891567252,5015.6241406813415,512610,1211
1,5152,5160.775240436563,3620.2224737404,344840,1211
2,10838,10935.854344420462,7418.75802080286,805095,1211
3,6144,6161.564397713924,4450.346809633231,461225,1210
4,3153,3157.246272382307,2505.6622639073153,224960,1210
5,7819,7827.275171393158,5858.803039091432,570555,1211
6,5147,5164.496326477711,3889.9110242162537,388890,1211
7,4270,4306.668807448917,3137.2245902833915,323000,1211
8,47,47.0,46.0,3220,23
9,2970,2987.6939192640207,2296.8029860273123,193595,1211
10,5092,5119.097781151385,3900.3549331543672,375855,1211
11,5481,5512.913215397109,3813.8797142453946,371420,1210
12,4260,4286.062469994591,3006.924140532463,296500,1211
13,7503,7521.613500453263,5354.54701040083,566745,1211
14,10741,10766.295464706822,7353.605591648074,718795,1211
15,9061,9105.655948747913,6298.244583340483,571185,1210
16,9166,9203.157634202667,6393.674736244693,603140,1211
17,4479,4479.097421736181,3458.046274138154,331845,1211
18,5478,5491.207385277128,4224.6919690112045,392475,1211
19,8772,8838.110820235639,6319.1057183255425,608790,1210
20,13502,13572.465658191284,9072.192451323506,940315,1211
21,7903,7927.488437386526,6035.300649347015,582340,1210
22,7275,7278.746759903979,5564.349337512386,517255,1211
23,8934,9003.209435190709,6485.4212763507485,630475,1211
24,11675,11708.48900788761,8205.882381217156,828680,1211
25,11199,11258.543534549684,7317.246797948742,722830,1211
26,6381,6421.164575818191,4593.035333592501,413230,1208
27,6065,6083.048459392475,4473.617360500059,413700,1211
28,8327,8374.455856338538,6037.90658134379,602455,1210
29,10037,10089.84025806295,7174.530543597178,678245,1211
30,5476,5493.474310183651,4158.94716915358,398455,1210
31,4818,4832.1704019372855,3590.267199013336,328960,1210
32,4358,4365.361536294854,3290.0887718982176,291365,1210
33,10285,10298.694127629757,7130.69191298495,748805,1211
34,10781,10800.077107968893,7365.527864461407,763030,1211
35,1393,1393.8571428559237,1155.7603009780075,74480,1210
36,3,3.0,3.0,210,4

@enekomartinmartinez
Copy link
Collaborator

I think I could try to implement them, both for subscripts and data, but it will take more time to release as testing will take time. But from the current implementation of GET functions should be easy to give support to this kind of files

@mak63-dev
Copy link
Author

right - it's also the corresponding methods for data :)

@enekomartinmartinez
Copy link
Collaborator

I have merged the updates in the master. I will develop the support for csv and tab files in a new branch during this week.

@enekomartinmartinez
Copy link
Collaborator

@mak63-dev could I use that data for testing?

@mak63-dev
Copy link
Author

sure, go ahead!
:)

@enekomartinmartinez
Copy link
Collaborator

Could you also try to export your file to a tab-separated file (.tab) and try the same example above in Vensim, but with the .tab? and the same with a .txt separated by whitespaces?

@mak63-dev
Copy link
Author

The model I'm testing uses many csv files and I'd have to change all of them to test. Let me build a test model that just reads subscripts and data from a csv file and will test with that. Probably tomorrow

@enekomartinmartinez
Copy link
Collaborator

Yes, that would be very useful so we can directly add to our testing suite. Sorry for asking you for that but I don't have access to Vensim anymore :S

@mak63-dev
Copy link
Author

no problem - but I think anybody could use the free version

@enekomartinmartinez
Copy link
Collaborator

Meanwhile, you can test your full model with the code in the branch https://github.com/SDXorg/pysd/tree/support_external_csv

We are still missing the testing against Vensim, but with regular CSV files, it should work properly.

@mak63-dev
Copy link
Author

will get to this tomorrow and will let you know

@mak63-dev
Copy link
Author

meanwhile, tried with the latest code and get this now:

File C:\miniconda3\Lib\site-packages\pysd\pysd.py:155 in read_vensim
py_model_file = ModelBuilder(abs_model).build_model()

File C:\miniconda3\Lib\site-packages\pysd\builders\python\python_model_builder.py:42 in init
SectionBuilder(section)

File C:\miniconda3\Lib\site-packages\pysd\builders\python\python_model_builder.py:86 in init
self.subscripts = SubscriptManager(

File C:\miniconda3\Lib\site-packages\pysd\builders\python\subscripts.py:33 in init
self.subscript2num = self._get_subscript2num()

File C:\miniconda3\Lib\site-packages\pysd\builders\python\subscripts.py:165 in _get_subscript2num
== np.arange(sub_index[0], sub_index[0]+len(sub_index))):

IndexError: list index out of range

@mak63-dev
Copy link
Author

perhaps the last cell is still the issue? if the ending cell is just a letter, then it reads from the starting cell all the way down on the column of the letter in the ending cell. If the ending cell is just a number, then it reads from the starting cell all the way to end of the row with that number

@enekomartinmartinez
Copy link
Collaborator

This could be related to some subscripts not being read properly, i.e., subscript range having length 0.

I will improve the error message so we have information about where it is happening.

@mak63-dev
Copy link
Author

would be helpful to know the file name, field etc

@enekomartinmartinez
Copy link
Collaborator

Can you try the new version, please?

@mak63-dev
Copy link
Author

The error comes from here:
single holiday:

GET DIRECT SUBSCRIPT( 'C:...\single_holidays.csv'
, 'single_holidays', 'a1', '', 'h')
~ Dmnl
~ single holiday id's
|

File C:\miniconda3\Lib\site-packages\pysd\pysd.py:155 in read_vensim
py_model_file = ModelBuilder(abs_model).build_model()

File C:\miniconda3\Lib\site-packages\pysd\builders\python\python_model_builder.py:42 in init
SectionBuilder(section)

File C:\miniconda3\Lib\site-packages\pysd\builders\python\python_model_builder.py:86 in init
self.subscripts = SubscriptManager(

File C:\miniconda3\Lib\site-packages\pysd\builders\python\subscripts.py:30 in init
self.subscripts = abstract_subscripts

File C:\miniconda3\Lib\site-packages\pysd\builders\python\subscripts.py:69 in subscripts
raise ValueError(

ValueError: Subscript range empty:
{'file': 'C:\\...\\single_holidays.csv', 'tab': 'single_holidays', 'firstcell': 'a1', 'lastcell': '1', 'prefix': 'h'}

The file is a csv that looks like this:

1,2,3,4,5,6,7,8,9,10,

It's just a single row, with no headers, So it should read from a1 to the end (can use either '' (blanc) or '1' as argument for last cell)

@enekomartinmartinez
Copy link
Collaborator

Fixed, previous implementation was not working for subscripts defined in the first line, now it should work properly

@mak63-dev
Copy link
Author

Definitely made progress :)

It now gives a number of warning with the subscripts. The first one is this:

C:\miniconda3\Lib\site-packages\pysd\builders\python\subscripts.py:350: UserWarning:
Dimension given by subscripts:
{'i481', 'i720', 'i142', 'i929', 'i1187', 'i1570', 'agg', 'i671', 'i614', 'i228', 'i1741', 'i928', 'i1705', 'i615', 'i931', 'i577', 'i1330', 'i2041', 'i1706', 'i1345', 'i1788', 'i1300', 'i1787', 'i221', 'i1320'}
is incomplete using extended item instead.

There is no index defined in the model that corresponds just to these entries as in the warning. The index with prefix 'i' plus the index named 'agg' (which is for the sum) are related to the following series of sub-indices which are read in from file:

gitem:
GET DIRECT SUBSCRIPT( 'C:\..\s_data.csv'
, 's_data', 'a2', 'a', 'i')
~ Dmnl
~
|

item:
GET DIRECT SUBSCRIPT( 'C:\...\sales_17.csv'
,'sales_17', 'b1', '1', 'i')
~ Dmnl
~
|

fitem:
GET DIRECT SUBSCRIPT( 'C:\...\pred_sales_17.csv'
,'pred_sales_17', 'b1', '1', 'i')
~ Dmnl
~
|
ditem:
GET DIRECT SUBSCRIPT( 'C:\...\d_item_indices.csv'
,'d_item_indices', 'a1', '1', 'i')
~ Dmnl
~
|
fixed d item:
GET DIRECT SUBSCRIPT( 'C:\...\d_item_indices.csv'
,'d_item_indices', 'a2', '2', 'i')
~ Dmnl
~
|
free d item:
GET DIRECT SUBSCRIPT( 'C:\...\d_items.csv'
,'d_items', 'b1', '1', 'i')
~ Dmnl
~
|
extended item: gitem, agg
~
~ |

All these files are read in correctly and in the dictionary they appear right:

Subscript_dict:
{... 'gitem': ['i142', 'i221', 'i228', 'i340', 'i481', 'i577', 'i614', 'i615', 'i671', 'i720', 'i928', 'i929', 'i931', 'i1187', 'i1300', 'i1320', 'i1330', 'i1345', 'i1570', 'i1705', 'i1706', 'i1741', 'i1787', 'i1788', 'i2034', 'i2041'], 'item': ['i142', 'i221', 'i228', 'i481', 'i577', 'i614', 'i615', 'i671', 'i720', 'i928', 'i929', 'i931', 'i1187', 'i1300', 'i1320', 'i1330', 'i1345', 'i1570', 'i1705', 'i1706', 'i1741', 'i1787', 'i1788', 'i2041'], 'fitem': ['i142', 'i221', 'i481', 'i614', 'i615', 'i720', 'i1330', 'i1570', 'i1705', 'i1787', 'i1788', 'i2041'], 'ditem': ['i142', 'i221', 'i481', 'i614', 'i615', 'i720', 'i928', 'i929', 'i931', 'i1187', 'i1300', 'i1570', 'i1705', 'i1706', 'i1787', 'i1788', 'i2041'], 'fixed d item': ['i221', 'i614', 'i931', 'i1187', 'i1570', 'i1705', 'i1788', 'i2041'], 'free d item': ['i142', 'i481', 'i615', 'i720', 'i928', 'i929', 'i1300', 'i1706', 'i1787'], 'extended item': ['i142', 'i221', 'i228', 'i340', 'i481', 'i577', 'i614', 'i615', 'i671', 'i720', 'i928', 'i929', 'i931', 'i1187', 'i1300', 'i1320', 'i1330', 'i1345', 'i1570', 'i1705', 'i1706', 'i1741', 'i1787', 'i1788', 'i2034', 'i2041', 'agg'], ...

So the question is, what index does it think it is not reading correctly in the warning, because it does not seem to correspond to anything defined in the model

@mak63-dev
Copy link
Author

maybe the error comes not from reading the subscripts but from reading data from a csv file

@enekomartinmartinez
Copy link
Collaborator

Hi @mak63-dev

That warning can usually be ignored. PySD is xarray-based, so all the variable coordinates should have a dimension name. Usually, the dimension name is taken from a subscript range or subranges that match. But when there are some cases where the subscripts used to define any variable doesn't match exactly with one range or subrange. In those cases, this warning arises.

Try to run the model and see if everything runs okay.

@mak63-dev
Copy link
Author

mak63-dev commented Jul 2, 2024 via email

@mak63-dev
Copy link
Author

Tried anyway - I get further warnings and in the end it crashes...

warnings.warn(
C:\miniconda3\Lib\site-packages\pysd\builders\python\python_expressions_builder.py:462: UserWarning: Trying to translate 'DELAY MATERIAL' which it is not implemented on PySD. The translated model will crash...
warnings.warn(
C:\miniconda3\Lib\site-packages\pysd\builders\python\python_expressions_builder.py:462: UserWarning: Trying to translate 'GET DATA MAX' which it is not implemented on PySD. The translated model will crash...
warnings.warn(
C:\miniconda3\Lib\site-packages\pysd\builders\python\python_expressions_builder.py:462: UserWarning: Trying to translate 'VECTOR ELM MAP' which it is not implemented on PySD. The translated model will crash...
warn(
Traceback (most recent call last):

File C:\miniconda3\Lib\site-packages\IPython\core\interactiveshell.py:3577 in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

Cell In[2], line 1
model = pysd.read_vensim('C:\...\f1303_17 - Copy.mdl')

File C:\miniconda3\Lib\site-packages\pysd\pysd.py:158 in read_vensim
model = load(py_model_file, data_files, initialize, missing_values)

File C:\miniconda3\Lib\site-packages\pysd\pysd.py:198 in load
return Model(py_model_file, data_files, initialize, missing_values)

File C:\miniconda3\Lib\site-packages\pysd\py_backend\model.py:1402 in init
super().init(py_model_file, None, None, Time(),

File C:\miniconda3\Lib\site-packages\pysd\py_backend\model.py:95 in init
self.components = Components(str(py_model_file), self.set_components)

File C:\miniconda3\Lib\site-packages\pysd\py_backend\components.py:55 in init
object.setattr(self, "_components", self._load(py_model_file))

File C:\miniconda3\Lib\site-packages\pysd\py_backend\components.py:80 in _load
spec.loader.exec_module(module)

File :991 in exec_module

File :1129 in get_code

File :1059 in source_to_code

File :488 in _call_with_frames_removed

File ~...\f1303_17 - Copy.py:1423
"C:...\sales_dow_full.csv",
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

@enekomartinmartinez
Copy link
Collaborator

If you want we can keep the discussion via slack
https://join.slack.com/t/sdtoolsandmet-slj3251/shared_invite/zt-2lxnes1yt-2grrvIPyTp0UvBz6tVIn1Q

The last three warnings, DELAY MATERIAL, GET DATA MAX, and VECTOR ELM MAP, are not supported by PySD. Then, if you cannot replace them, it will be necessary to add them to PySD. I could give support but I don't have time to work on them, in particular developing the proper tests may take time I cannot invert right now, but I will be happy to help.

The error you have is probably related to the \ character in the file path, as \s is an escape character. My recommendation is to always writte the paths with / instead, as this is also supported by Vensim. I could check if this can be fixed during the translation somehow. But I strongly recommend using notation that can be extrapolated to any OS filesystem. If your model files data is in the same folder as the model or in a subfolder, I would recommend using relative paths, i.e., C:...\sales_dow_full.csv -> sales_dow_full.csv. This way, if you share your model later, you will still be able to run it on another machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants