pysd library ParseError - python

I'm using a library called pysd to translate vensim files to Python, but when I try to do it (library functions) I get a parse error but don't understand what it means.
This is my log.
ParseError Traceback (most recent call last)
<ipython-input-1-9b0f6b9bac1f> in <module>()
1 get_ipython().magic(u'pylab inline')
2 import pysd
----> 3 model = pysd.read_vensim('201520_1A_Volare_Ev.Tecnica.itmx')
/Library/Python/2.7/site-packages/pysd/pysd.pyc in read_vensim(mdl_file)
45 """
46 from .vensim2py import translate_vensim
---> 47 py_model_file = translate_vensim(mdl_file)
48 model = PySD(py_model_file)
49 model.mdl_file = mdl_file
/Library/Python/2.7/site-packages/pysd/vensim2py.pyc in translate_vensim(mdl_file)
651 for section in file_sections:
652 if section['name'] == 'main':
--> 653 model_elements += get_model_elements(section['string'])
654
655 # extract equation components
/Library/Python/2.7/site-packages/pysd/vensim2py.pyc in get_model_elements(model_str)
158 """
159 parser = parsimonious.Grammar(model_structure_grammar)
--> 160 tree = parser.parse(model_str)
161
162 class ModelParser(parsimonious.NodeVisitor):
/Library/Python/2.7/site-packages/parsimonious/grammar.pyc in parse(self, text, pos)
121 """
122 self._check_default_rule()
--> 123 return self.default_rule.parse(text, pos=pos)
124
125 def match(self, text, pos=0):
/Library/Python/2.7/site-packages/parsimonious/expressions.pyc in parse(self, text, pos)
108
109 """
--> 110 node = self.match(text, pos=pos)
111 if node.end < len(text):
112 raise IncompleteParseError(text, node.end, self)
/Library/Python/2.7/site-packages/parsimonious/expressions.pyc in match(self, text, pos)
125 node = self.match_core(text, pos, {}, error)
126 if node is None:
--> 127 raise error
128 return node
129
ParseError: Rule 'escape_group' didn't match at '' (line 1, column 20243).

.itmx is an iThink extension, which unfortunately PySD doesn't support (yet). In the future, we'll work out a conversion pathway that lets you bring these in.

Related

Brightway2: LCA scores & calculations

My problem is about getting emissions results of my functional unit from a ecoinvent excel spreadsheet format.
I managed to get activities/process impacts thanks to ca.annotated_top_processes(lca) or lca.top_activities()but emissions/biosphere flows can't be displayed but through ca.hinton_matrix(lca, rows=10, cols=10). How can I get specific scores ?
Here's the situation:
import brightway2 as bw
from stats_arrays import *
import bw2analyzer as bwa
projects.set_current("excel_import_verif1")
bw.databases
db = bw.Database('IoTBOLLCA') #Excel spreadsheet
CC = [method for method in bw.methods if "('ReCiPe Midpoint (H) V1.13', 'climate change', 'GWP100')" in str(method)][0]
FU = [i for i in db if 'FU' in i['name']][0]
lca = bw.LCA({FU:1},CC)
lca.lci()
lca.lcia()
lca.score
ca = bwa.ContributionAnalysis()
lca.top_emissions()
and I get this error
TypeError Traceback (most recent call last)
File ~\Anaconda3\envs\bw2\lib\site-packages\scipy\sparse\_sputils.py:208, in isintlike(x)
207 try:
--> 208 operator.index(x)
209 except (TypeError, ValueError):
TypeError: 'numpy.float64' object cannot be interpreted as an integer
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
Input In [28], in <cell line: 1>()
----> 1 lca.top_emissions()
File ~\Anaconda3\envs\bw2\lib\site-packages\bw2calc\lca.py:575, in LCA.top_emissions(self, **kwargs)
573 except ImportError:
574 raise ImportError("`bw2analyzer` is not installed")
--> 575 return ContributionAnalysis().annotated_top_emissions(self, **kwargs)
File ~\Anaconda3\envs\bw2\lib\site-packages\bw2analyzer\contribution.py:152, in ContributionAnalysis.annotated_top_emissions(self, lca, names, **kwargs)
146 """Get list of most damaging biosphere flows in an LCA, sorted by ``abs(direct impact)``.
147
148 Returns a list of tuples: ``(lca score, inventory amount, activity)``. If ``names`` is False, they returns the process key as the last element.
149
150 """
151 ra, rp, rb = lca.reverse_dict()
--> 152 results = [
153 (score, lca.inventory[index, :].sum(), rb[index])
154 for score, index in self.top_emissions(
155 lca.characterized_inventory, **kwargs
156 )
157 ]
158 if names:
159 results = [(x[0], x[1], get_activity(x[2])) for x in results]
File ~\Anaconda3\envs\bw2\lib\site-packages\bw2analyzer\contribution.py:153, in <listcomp>(.0)
146 """Get list of most damaging biosphere flows in an LCA, sorted by ``abs(direct impact)``.
147
148 Returns a list of tuples: ``(lca score, inventory amount, activity)``. If ``names`` is False, they returns the process key as the last element.
149
150 """
151 ra, rp, rb = lca.reverse_dict()
152 results = [
--> 153 (score, lca.inventory[index, :].sum(), rb[index])
154 for score, index in self.top_emissions(
155 lca.characterized_inventory, **kwargs
156 )
157 ]
158 if names:
159 results = [(x[0], x[1], get_activity(x[2])) for x in results]
File ~\Anaconda3\envs\bw2\lib\site-packages\scipy\sparse\_index.py:47, in IndexMixin.__getitem__(self, key)
46 def __getitem__(self, key):
---> 47 row, col = self._validate_indices(key)
49 # Dispatch to specialized methods.
50 if isinstance(row, INT_TYPES):
File ~\Anaconda3\envs\bw2\lib\site-packages\scipy\sparse\_index.py:152, in IndexMixin._validate_indices(self, key)
149 M, N = self.shape
150 row, col = _unpack_index(key)
--> 152 if isintlike(row):
153 row = int(row)
154 if row < -M or row >= M:
File ~\Anaconda3\envs\bw2\lib\site-packages\scipy\sparse\_sputils.py:216, in isintlike(x)
214 if loose_int:
215 msg = "Inexact indices into sparse matrices are not allowed"
--> 216 raise ValueError(msg)
217 return loose_int
218 return True
ValueError: Inexact indices into sparse matrices are not allowed
This is an error as of Scipy version 1.9; for now, you can force a downgrade to Scipy 1.8.something.
This has been noted as an issue, but the focus for BW development is in other areas currently.

Q: Errors when usinig gluonts of LSTNet: GluonTSDataError

I've been studying time series forecasting, and I'm trying to learn how to use gluon-ts&python.
Here is the source code of gluon-ts:
https://github.com/awslabs/gluon-ts/
I tried to use LSTNetEstimator module, however, it turns out an error as follow. And I found that all the discussions of gluon-ts is about DeepAR.
Is there anyone that may come to help?
GluonTSDataError: Input for field "target" does not have the requireddimension (field: target, ndim observed: 1, expected ndim: 2)
I think it has something to do with my custom dataset, which includes a dataframe with the shape of [320000,3] and columns of ['time','Power_Cdp','Power_Dp'].
here is the trainset:
from gluonts.dataset.common import ListDataset
from gluonts.model.lstnet import LSTNetEstimator
from gluonts.mx.trainer import Trainer
training_data = ListDataset(
[{"start": LSTNet_df.index[0], "target": LSTNet_df['Power_Cdp'][:-10000}],
freq = "15min")
estimator = LSTNetEstimator(freq="15min", prediction_length=24*4, context_length=24*4,
num_series=48*4, skip_size=72*4, ar_window=24*4, channels=32,
trainer=Trainer(epochs=10))
predictor = estimator.train(training_data=training_data) # Error
The full report is bellow.
GluonTSDataError Traceback (most recent call last)
<ipython-input-311-ad48fdc20df5> in <module>
7 num_series=48*4, skip_size=72*4, ar_window=24*4, channels=32,
8 trainer=Trainer(epochs=10))
----> 9 predictor = estimator.train(training_data=training_data)
~/.local/lib/python3.8/site-packages/gluonts/mx/model/estimator.py in train(self, training_data, validation_data, num_workers, num_prefetch, shuffle_buffer_length, cache_data, **kwargs)
192 **kwargs,
193 ) -> Predictor:
--> 194 return self.train_model(
195 training_data=training_data,
196 validation_data=validation_data,
~/.local/lib/python3.8/site-packages/gluonts/mx/model/estimator.py in train_model(self, training_data, validation_data, num_workers, num_prefetch, shuffle_buffer_length, cache_data)
145 transformed_training_data = transformation.apply(training_data)
146
--> 147 training_data_loader = self.create_training_data_loader(
148 transformed_training_data
149 if not cache_data
~/.local/lib/python3.8/site-packages/gluonts/model/lstnet/_estimator.py in create_training_data_loader(self, data, **kwargs)
216 ) -> DataLoader:
217 input_names = get_hybrid_forward_input_names(LSTNetTrain)
--> 218 with env._let(max_idle_transforms=maybe_len(data) or 0):
219 instance_splitter = self._create_instance_splitter("training")
220 return TrainDataLoader(
~/.local/lib/python3.8/site-packages/gluonts/itertools.py in maybe_len(obj)
21 def maybe_len(obj) -> Optional[int]:
22 try:
---> 23 return len(obj)
24 except (NotImplementedError, AttributeError):
25 return None
~/.local/lib/python3.8/site-packages/gluonts/transform/_base.py in __len__(self)
99 # NOTE this is unsafe when transformations are run with is_train = True
100 # since some transformations may not be deterministic (instance splitter)
--> 101 return sum(1 for _ in self)
102
103 def __iter__(self) -> Iterator[DataEntry]:
~/.local/lib/python3.8/site-packages/gluonts/transform/_base.py in <genexpr>(.0)
99 # NOTE this is unsafe when transformations are run with is_train = True
100 # since some transformations may not be deterministic (instance splitter)
--> 101 return sum(1 for _ in self)
102
103 def __iter__(self) -> Iterator[DataEntry]:
~/.local/lib/python3.8/site-packages/gluonts/transform/_base.py in __iter__(self)
102
103 def __iter__(self) -> Iterator[DataEntry]:
--> 104 yield from self.transformation(
105 self.base_dataset, is_train=self.is_train
106 )
~/.local/lib/python3.8/site-packages/gluonts/transform/_base.py in __call__(self, data_it, is_train)
122 self, data_it: Iterable[DataEntry], is_train: bool
123 ) -> Iterator:
--> 124 for data_entry in data_it:
125 try:
126 yield self.map_transform(data_entry.copy(), is_train)
~/.local/lib/python3.8/site-packages/gluonts/transform/_base.py in __call__(self, data_it, is_train)
126 yield self.map_transform(data_entry.copy(), is_train)
127 except Exception as e:
--> 128 raise e
129
130 #abc.abstractmethod
~/.local/lib/python3.8/site-packages/gluonts/transform/_base.py in __call__(self, data_it, is_train)
124 for data_entry in data_it:
125 try:
--> 126 yield self.map_transform(data_entry.copy(), is_train)
127 except Exception as e:
128 raise e
~/.local/lib/python3.8/site-packages/gluonts/transform/_base.py in map_transform(self, data, is_train)
139
140 def map_transform(self, data: DataEntry, is_train: bool) -> DataEntry:
--> 141 return self.transform(data)
142
143 #abc.abstractmethod
~/.local/lib/python3.8/site-packages/gluonts/transform/convert.py in transform(self, data)
127 value = np.asarray(data[self.field], dtype=self.dtype)
128
--> 129 assert_data_error(
130 value.ndim == self.expected_ndim,
131 'Input for field "{self.field}" does not have the required'
~/.local/lib/python3.8/site-packages/gluonts/exceptions.py in assert_data_error(condition, message, *args, **kwargs)
114 exception message.
115 """
--> 116 assert_gluonts(GluonTSDataError, condition, message, *args, **kwargs)
~/.local/lib/python3.8/site-packages/gluonts/exceptions.py in assert_gluonts(exception_class, condition, message, *args, **kwargs)
93 """
94 if not condition:
---> 95 raise exception_class(message.format(*args, **kwargs))
96
97
GluonTSDataError: Input for field "target" does not have the requireddimension (field: target, ndim observed: 1, expected ndim: 2)
#darth baba I tried again lately, and this time I used DeepVAR.
Here is what got.
I reset "target": ... , stepped into my code and found
gluonts.exceptions.GluonTSDataError: Array 'target' has bad shape - expected 1 dimensions, got 2.
This is because I set "target": train_df[['Power_Cdp', 'Power_Active_Fan']], which might be reported as an error at ./gluonts/dataset/common.py +385.
I found self.req_ndim != value.ndim , which suggested that I could have input a wrong shape of target, thus I reset input like "target": train_df.index[:] and this problem solved.
But it reports an another error,
gluonts.exceptions.GluonTSDataError: Input for field "target" does not have the requireddimension (field: target, ndim observed: 1, expected ndim: 2)
To be sincerely, this one confused me a lot, for the gluonts's code seems so complicated.
I checked again and found it reports error at ./gluonts/transform/convert.py +129.
It seems like expected_ndim is not equals to value.ndim in this case.
However, after rewriting expected_ndim manually, the training progress worked eventually.
I have no idea whether this modification is right.
Although the avg_epoch_loss are decreasing, the final forecast is not as good as expected.

python sequence item 0: expected str instance, NoneType found

I'm trying to convert a local .xml file to .ttl and I used this code:
import rdflib
g=rdflib.Graph()
g.parse("C:\\Users\\Username\\Desktop\\public-12T23.xml",format='xml')
print (g.serialize(format='turtle'))
and I got this error
TypeError Traceback (most recent call last)
<ipython-input-3-d52cc014f61e> in <module>()
2 g=rdflib.Graph()
3
----> 4 g.parse("C:\\Users\\Saeid\\Desktop\\public-12T23.xml", format='xml')
5 print (g.serialize(format='turtle'))
C:\Program Files\Anaconda3\lib\site-packages\rdflib\graph.py in parse(self, >source, publicID, format, location, file, data, **args)
1041 parser = plugin.get(format, Parser)()
1042 try:
-> 1043 parser.parse(source, self, **args)
1044 finally:
1045 if source.auto_close:
C:\Program Files\Anaconda3\lib\site-packages\rdflib\plugins\parsers\rdfxml.py >in parse(self, source, sink, **args)
576 # content_handler.reset()
577 # self._parser.reset()
--> 578 self._parser.parse(source)
C:\Program Files\Anaconda3\lib\xml\sax\expatreader.py in parse(self, source)
108 self.reset()
109 self._cont_handler.setDocumentLocator(ExpatLocator(self))
--> 110 xmlreader.IncrementalParser.parse(self, source)
111
112 def prepareParser(self, source):
C:\Program Files\Anaconda3\lib\xml\sax\xmlreader.py in parse(self, source)
123 buffer = file.read(self._bufsize)
124 while buffer:
--> 125 self.feed(buffer)
126 buffer = file.read(self._bufsize)
127 self.close()
C:\Program Files\Anaconda3\lib\xml\sax\expatreader.py in feed(self, data, >isFinal)
208 # document. When feeding chunks, they are not normally >final -
209 # except when invoked from close.
--> 210 self._parser.Parse(data, isFinal)
211 except expat.error as e:
212 exc = SAXParseException(expat.ErrorString(e.code), e, self)
..\Modules\pyexpat.c in EndElement()
C:\Program Files\Anaconda3\lib\xml\sax\expatreader.py in end_element_ns(self, >name)
368 pair = tuple(pair)
369
--> 370 self._cont_handler.endElementNS(pair, None)
371
372 # this is not used (call directly to ContentHandler)
C:\Program Files\Anaconda3\lib\site-packages\rdflib\plugins\parsers\rdfxml.py >in endElementNS(self, name, qname)
158
159 def endElementNS(self, name, qname):
--> 160 self.current.end(name, qname)
161 self.stack.pop()
162
C:\Program Files\Anaconda3\lib\site-packages\rdflib\plugins\parsers\rdfxml.py >in node_element_end(self, name, qname)
330 if self.parent.object and self.current != self.stack[2]:
331
--> 332 self.error("Repeat node-elements inside property elements: >%s"%"".join(name))
333
334 self.parent.object = self.current.subject
TypeError: sequence item 0: expected str instance, NoneType found
I know that it was not as easy as I was thought, but I got the same error when I used these script here.

SageMath notebook is not opening

I'm trying to open the Sage's notebook, but it isn't working.
I have no idea where this error came from, because the notebook was working this week. I guess it just popped up out of nowhere.
The message's error is:
sage: notebook()
---------------------------------------------------------------------------
EnvironmentError Traceback (most recent call last)
<ipython-input-4-3728cb3d7c7d> in <module>()
----> 1 notebook()
/home/jerome/opt/SageMath/src/sage/misc/lazy_import.pyx in sage.misc.lazy_import.LazyImport.__call__ (/home/jerome/opt/SageMath/src/build/cythonized/sage/misc/lazy_import.c:3634)()
384 True
385 """
--> 386 return self._get_object()(*args, **kwds)
387
388 def __repr__(self):
/home/jerome/opt/SageMath/src/sage/misc/lazy_import.pyx in sage.misc.lazy_import.LazyImport._get_object (/home/jerome/opt/SageMath/src/build/cythonized/sage/misc/lazy_import.c:2241)()
244 elif self._at_startup and not startup_guard:
245 print('Option ``at_startup=True`` for lazy import {0} not needed anymore'.format(self._name))
--> 246 self._object = getattr(__import__(self._module, {}, {}, [self._name]), self._name)
247 alias = self._as_name or self._name
248 if self._deprecation is not None:
/home/jerome/opt/SageMath/local/lib/python2.7/site-packages/sagenb/notebook/notebook_object.py in <module>()
15 import time, os, shutil, signal, tempfile
16
---> 17 import notebook as _notebook
18
19 import run_notebook
/home/jerome/opt/SageMath/local/lib/python2.7/site-packages/sagenb/notebook/notebook.py in <module>()
33
34 # Sage libraries
---> 35 from sagenb.misc.misc import (pad_zeros, cputime, tmp_dir, load, save,
36 ignore_nonexistent_files, unicode_str)
37
/home/jerome/opt/SageMath/local/lib/python2.7/site-packages/sagenb/misc/misc.py in <module>()
379
380 try:
--> 381 from sage.misc.cython import cython
382 except ImportError:
383 #stub
/home/jerome/opt/SageMath/local/lib/python2.7/site-packages/sage/misc/cython.py in <module>()
28
29 # CBLAS can be one of multiple implementations
---> 30 cblas_pc = pkgconfig.parse('cblas')
31 cblas_libs = list(cblas_pc['libraries'])
32 cblas_library_dirs = list(cblas_pc['library_dirs'])
/home/jerome/opt/SageMath/local/lib/python2.7/site-packages/pkgconfig-1.1.0-py2.7.egg/pkgconfig/pkgconfig.py in parse(packages)
185
186 for package in packages.split():
--> 187 for k, v in parse_package(package).items():
188 result[k].update(v)
189
/home/jerome/opt/SageMath/local/lib/python2.7/site-packages/pkgconfig-1.1.0-py2.7.egg/pkgconfig/pkgconfig.py in parse_package(package)
158
159 # Execute the query to pkg-config and clean the result.
--> 160 out = _query(package, '--cflags --libs')
161 out = out.replace('\\"', '')
162
/home/jerome/opt/SageMath/local/lib/python2.7/site-packages/pkgconfig-1.1.0-py2.7.egg/pkgconfig/pkgconfig.py in _wrapper(*args, **kwargs)
56 return func(*args, **kwargs)
57 except OSError:
---> 58 raise EnvironmentError("pkg-config is not installed")
59
60 return _wrapper
EnvironmentError: pkg-config is not installed
If you guys can help me, I'll be very thankful!

Why I am getting the following error opening a xlsx?

Hello I am trying to open a .xlsx file with python, my code is really simple it looks as follows:
import openpyxl
wb = openpyxl.load_workbook('prod334.xlsx')
However I am getting an error, I am not sure about the cause of this error since I am just opening the file, I would like to appreciate any suggestion to overcome this failure.
The error that I am getting is the following:
__init__() got an unexpected keyword argument 'vertAlign'
TypeError Traceback (most recent call last)
main.py in <module>()
2
3
----> 4 wb = openpyxl.load_workbook('prod334.xlsx')
/usr/local/lib/anaconda/lib/python2.7/site-packages/openpyxl/reader/excel.pyc in load_workbook(filename, use_iterators, keep_vba, guess_types, data_only)
163
164 try:
--> 165 _load_workbook(wb, archive, filename, use_iterators, keep_vba)
166 except KeyError:
167 e = exc_info()[1]
/usr/local/lib/anaconda/lib/python2.7/site-packages/openpyxl/reader/excel.pyc in _load_workbook(wb, archive, filename, use_iterators, keep_vba)
210 assert wb.loaded_theme == None, "even though the theme information is missing there is a theme object ?"
211
--> 212 style_properties = read_style_table(archive.read(ARC_STYLE))
213 style_table = style_properties.pop('table')
214 wb.shared_styles = style_properties.pop('list')
/usr/local/lib/anaconda/lib/python2.7/site-packages/openpyxl/reader/style.pyc in read_style_table(xml_source)
221 def read_style_table(xml_source):
222 p = SharedStylesParser(xml_source)
--> 223 p.parse()
224 return p.style_prop
/usr/local/lib/anaconda/lib/python2.7/site-packages/openpyxl/reader/style.pyc in parse(self)
37 self.parse_color_index()
38 self.style_prop['color_index'] = self.color_index
---> 39 self.font_list = list(self.parse_fonts())
40 self.fill_list = list(self.parse_fills())
41 self.border_list = list(self.parse_borders())
/usr/local/lib/anaconda/lib/python2.7/site-packages/openpyxl/reader/style.pyc in parse_fonts(self)
89 if fonts is not None:
90 for node in safe_iterator(fonts, '{%s}font' % SHEET_MAIN_NS):
---> 91 yield self.parse_font(node)
92
93 def parse_font(self, font_node):
/usr/local/lib/anaconda/lib/python2.7/site-packages/openpyxl/reader/style.pyc in parse_font(self, font_node)
105 if color is not None:
106 font['color'] = Color(**dict(color.items()))
--> 107 return Font(**font)
108
109 def parse_fills(self):
TypeError: __init__() got an unexpected keyword argument 'vertAlign'

Categories