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

Added documentation for PyBTLS interface #49

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Rizqiansyah
Copy link
Collaborator

Hi all,

This pull request adds the (severely needed) documentation for the interface modules, as requested by @CaiZhou2.

Copy link
Owner

@ccaprani ccaprani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of good functions here, but there seems to be a lot of replication of the internals of the C++ code which should be unnecessary. Each class/function should have a clear need in an identified workflow.
Some naming is counter to python conventions.
Use of files...a clear use-case workflow is needed since when run through python most outputs should be just in memory perhaps?

"""
Private method to get influence lines.
IL computed using PyCBA, then compressed using the maxdistance algorithm.
"""
# IL to be compressed
ils = cba.InfluenceLines(self.L, np.ones(len(self.L)), self.R)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means pycba is a dependency for PyBTLS. It's not a good idea to have too many dependencies. Surely a user could work out their own IL outside PyBTLS and just pass it in; it would avoid a hard dependency.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to add a method (in a separate PR) to manually add IL. Perhaps we can save this method, by checking if PyCBA is installed, then users can use this method. Else throw an exception?

class Bridge:
"""
Python side Bridge Class.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just a wrapper of the PyBTLS class?

Copy link
Collaborator Author

@Rizqiansyah Rizqiansyah Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't directly interface with the Bridge class in c++. It simply wraps the txt/csv file creation.

with open(path, "w") as f:
f.write(self._create_il_txt())

def write_as_txt(self, il_path="IL.txt", bridge_path="bridge.txt"):
"""
Method to write both the bridge and influence line text file for BTLS to run.
"""
self.write_bridge_as_txt(bridge_path)
self.write_il_as_txt(il_path)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once something is defined in memory, we should be able to avoid needing to write to HDD.

# via DataFrame (base class has this covered)
# or via file, which needs to be redefined to not use the _from_txt_to_dataframe_kwargs() method
# And block the _from_txt_to_dataframe_kwargs() method from working

def _from_txt_to_dataframe_kwargs(self, txt, *args, **kwargs):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a required format of the dataframe and/or textfile?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default Lane Flow files are all in csv, which this class can load.

It's only blocking the _from_txt_to_dataframe_kwargs as this method strictly initializes using a txt file, loading it as string, parsing it line by line, and then create a dataframe. It's too complicated this way; the pandas csv loader can do the job just fine.

Also just a quick note the reason _from_txt_to_dataframe_kwargs is there is because it's inherited from DFBased class.


"""


class BridgeFlow(_ListLike):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is an internal class of PyBTLS. Not sure why a user would need access to it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm still not happy with how the lane flows are defined in python right now. Hopefully Ziyi and I can reach a better workflow soon. Ziyi will let you know.

@@ -8,6 +8,11 @@


class _BtlsWrapper_DefaultOutputs:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unnecessary. Also naming convention - stick to PEP.

@@ -18,6 +23,17 @@ def __str__(self):


class CumulativeStatistics(_DfBased):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for reading in a file output from PyBTLS C++?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now it's only reading from output txt/csv file.

This file provide functions to import BTLS ouput for a given bridge span.
These functions are for convenience;
the output file names from BTLS are sometimes inconsistent.
To import directly from a know file path, please use the appropriate Classes directly.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess in normal python useage, we won't need to read from files; but probably useful to have these anyway. Should "OutputReader" not be a class perhaps?

@@ -11,6 +11,11 @@


class BaseTrafficFile:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems an internal class to the C++ code and not for users to interface with?

Not wholly supported after initialisation, though user can manipulate the existing DataFrame like regular
"""


class _DfBased(pd.DataFrame):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stick to naming conventions.

@Rizqiansyah
Copy link
Collaborator Author

Lots of good functions here, but there seems to be a lot of replication of the internals of the C++ code which should be unnecessary. Each class/function should have a clear need in an identified workflow. Some naming is counter to python conventions. Use of files...a clear use-case workflow is needed since when run through python most outputs should be just in memory perhaps?

Thanks Colin. We (mostly Ziyi) is trying to resolve the replicated functions. This PR is just the documentations for the wrapper of the old method writing to txt/csv files, but in more python friendly ways (e.g., using DataFrame, etc.). There's an example workflow in the tests/PyBTLS_interface folder. I haven't changed any functions/classes since that example was written last year. This PR only added documentation.

On the outputs, I find it useful to have the output written to HDD, as I often run multiple instances of BTLS, then merge the outputs (very easily using the classes written here using pandas.concat). I don't think it will be trivial to code for this use case if outputs are only in memory.

I also changed the class names to follow the convention better, should be in the next commit soon.

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 this pull request may close these issues.

3 participants