matsim-org / matsim-code-examples

A repository containing code examples around MATSim
GNU General Public License v3.0
83 stars 179 forks source link

Cannot read emissions events #179

Open mrieser opened 5 years ago

mrieser commented 5 years ago

I'm trying out the emissions contrib, and there seems to be a problem.

I can first run RunEmissionsOfflineExample, which produces an emissions-events file. I then try to use EmissionGridAnalyzer which takes as input this emissions-events file. When running the analyzer, I get the following exception:

java.lang.IllegalArgumentException: No enum constant org.matsim.contrib.emissions.types.Pollutant.NOx
at java.lang.Enum.valueOf(Enum.java:238)
at org.matsim.contrib.emissions.types.Pollutant.valueOf(Pollutant.java:3)
at org.matsim.contrib.emissions.analysis.EmissionsOnLinkEventHandler.lambda$handleEmissionEvent$0(EmissionsOnLinkEventHandler.java:59)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.matsim.contrib.emissions.analysis.EmissionsOnLinkEventHandler.handleEmissionEvent(EmissionsOnLinkEventHandler.java:59)
at org.matsim.contrib.emissions.analysis.EmissionsOnLinkEventHandler.handleEvent(EmissionsOnLinkEventHandler.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.matsim.core.events.EventsManagerImpl.computeEvent(EventsManagerImpl.java:226)
... 26 more

Looking at the emissions-events file, I see that sometimes NOx is used as attribute name, and sometimes NOX (note the different cases of X). If I change all to NOX, the analyzer can process the file. So there seem to be at least two sources that create the emission-events, one correctly using NOX, and the other using the un-parsable NOx as attribute name.

This basically makes the emission-events file unreadable by standard code.

kainagel commented 5 years ago

Originally, we had enums here. Joe Malloy at some point changed the code such that all emissions that are registered in the input file are automatically used to generate corresponding emissions events. In consequence, the whole thing is no longer typed.

We have other places in MATSim where we moved from types to strings. However, we always then register these strings in the config file (activity types, modes, ...). This is not the case here.

Not sure how to proceed. At VSP, it seems that stuff was more or less patched together so that it works again, but without a clean design decision. Independent from this, clearly material in matsim should work and be secured by tets.

From a VIA perspective, I would say that the config approach would not help you; on the other hand, you could just do what Joe did and aggregate whatever you find in the events file. Maybe the events analyzer that you are looking at here should in fact just do the same?

Note that I am just telling history. I would be happy to clarify this.

mrieser commented 5 years ago

It's for once unrelated to Via at the moment. I'm really just trying to use the emissions contrib within a MATSim scenario.

mrieser commented 5 years ago

Based on the StackTrace, it looks as if there are still parts of the Enum in use when using the events-reader, which causes the problem.

@joemolloy do you have an idea how to fix this? I find it especially strange that I have for the same event types (e.g. coldEmissionEvents) both cases, NOx and NOX, in different events. I would have assumed that all events of one type get created the same way, but that does not seem to be the case.

JWJoubert commented 5 years ago

@mrieser

I can first run RunEmissionsOfflineExample, which produces an emissions-events file.

I have tried to get as far as you, but keep getting an error

com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: java.lang.NoClassDefFoundError: HbefaVehicleCategory

Did you not run into that problem? Not even the tests on my side run successfully.

kainagel commented 5 years ago

I just looked at it; this is probably caused by @billyc and/or @Janekdererste . I wrote a separate email since it seems that they are not reading this issue here. (In fact, maybe it should move to jira, since it is not a code-examples problem.)

Janekdererste commented 5 years ago

I've introduced the Pollutant Enum, because I programmed along an exsisting analysis from benjamins playground which hat its own Pollutant enums.

I can replace the Pollutant enum by simple String keys. Then, pollution would simply be stored as a String, Double pair.

mrieser commented 5 years ago

Thanks for looking into it. A string key would probably help reading the data in, but I'm still confused why I see NOX and NOx in the events. For me, they should be the same.

JWJoubert commented 5 years ago

@mrieser the only place where I see the difference (or at least NOx hard-coded) is in org.matsim.contrib.emissions.events.TestColdEmissionEventImpl (line 52)

private final Set<String> coldPollutants = new HashSet<>(Arrays.asList("CO", "FC", "HC", "NMHC", "NOx", "NO2","PM"));
joemolloy commented 5 years ago

@mrieser , do you have NOx or NOX in your hbefa input files?

mrieser commented 5 years ago

@joemolloy : I've taken the example files from the repository (e.g. https://raw.githubusercontent.com/matsim-org/matsim/master/contribs/emissions/test/input/org/matsim/contrib/emissions/sample_EFA_ColdStart_vehcat_2005average.txt) which uses NOx.

mrieser commented 5 years ago

@JWJoubert : Yes, I've found that place as well. Still, making it flexible seems not to work together with the Enum still being used while being parsed. And all my input files seem to use NOx, which does not explain why I get sometimes NOX in the events.

Will have a closer look as well when I find time.