Skip to content

Fix reading edgelist when delimiter is whitespace, e.g. tab - #7465

Merged
dschult merged 5 commits into
networkx:mainfrom
cyschneck:deliminator_7462
Jul 8, 2024
Merged

dschult merged 5 commits into
networkx:mainfrom
cyschneck:deliminator_7462

Conversation

@cyschneck

Copy link
Copy Markdown
Contributor

PR Summary

Replace strip with rstrip to remove whitespace

This line.strip without an argument, means a white-space delimited file (e.g. tab, which is quite common) with empty edge attribute(s) in the last column(s) results in a crash with error Edge data [...] and data_keys [...] are not the same length.

Issue

Closes #7462

@cyschneck
cyschneck marked this pull request as ready for review May 28, 2024 07:45

@dschult dschult left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If I understand the subtleties here, it looks like this will no longer strip anything from the beginning of a line and will only strip a newline character off of the end of the line. So, it fixes the problem with a tab delimiter, but breaks an input file that has spaces at the front or end of a line. It looks like all tests pass.

I guess to fix the currently broken [backward compatibility] cases and leave the current behavior for extra whitespace at start and end, we should strip all white space except the delimiter. Something like:

    stripchars = None if delimiter is None else string.whitespace.replace(delimiter, "")
    ....
    <other stuff and start loop, etc>
    s = line.strip(stripchars).split(delimiter)

Does this sounds reasonable? Anybody have other ideas?

continue
try:
(u, deg) = line.strip().split(delimiter)
(u, deg) = line.rstrip("\n").split(delimiter)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure we should treat this the same way. deg has to be there for this to work. It can't be empty or whitespace. So unless I'm missing something we should never have trouble with a tab delimiter that has only whitespace after it -- or at least that line should raise an error.

@carissableker

carissableker commented May 29, 2024 •

Copy link
Copy Markdown

Hi @dschult

As the bug reporter, I have the following comment:

If I understand the subtleties here, it looks like this will no longer strip anything from the beginning of a line and will only strip a newline character off of the end of the line.

Yes, this is correct. In my opinion this is the simplest and most predictable behaviour. The aim of this line is to prepare the input by removing the completely expected newline and split the line into its components by using the explicit user-provided delimitator. Doing anything more than this, e.g. attempting to do undocumented whitespace "cleaning" would be obscured, unpredictable and/or become very complex.

So, it fixes the problem with a tab delimiter, but breaks an input file that has spaces at the front or end of a line. It looks like all tests pass.

It would not break (crash), it would respect the user input (an attribute/node name with leading or trailing white space).

I guess to fix the currently broken cases and leave the current behavior for extra whitespace at start and end, we should strip all white space except the delimiter. Something like:

    stripchars = None if delimiter is None else string.whitespace.replace(delimiter, "")
    ....
    <other stuff and start loop, etc>
    s = line.strip(stripchars).split(delimiter)

This is not consistent though. The following lines, (for an equivalent undirected edge) would have a different result:

A\tB\s\n

and

B\s\tA\n

or alternatively:

\sA\tB\n

and

B\t\sA\n

Of course, it may not be the users intent to have a node name of A or B , but it is what was provided to networkx and I don't think it should be networkx's prerogative to attempt to deduce what the user means and unilaterally "fix" it, as there lies many dragons.

Compare this behaviour to other software, e.g. Excel or pandas, removing leading/trailing whitespace is usually done on per cell basis (instead of line), and is only done on explicit user direction. The only place where editing of (non newline or delimiter) whitespace in the background may be relevant is perhaps in "fixed width" format.

Alternatively

If you do require that node names should not have leading or trailing whitespace, this should be removed per item after split(deliminator) (and then be documented). E.g.:

s = [x.strip() for x in s]

In this case, also the two examples above would be consistent.

@cyschneck

Copy link
Copy Markdown
Contributor Author

For purposes of an example if it splits on a tab then

import string
stripchars = string.whitespace.replace('\t', '')

Current Behavior (some conditions retain white space)

If there a space after the B value and the tab

line = 'A\tB \n'
s = line.strip(stripchars).split('\t')

Produces a clean value before and after the values A and B

['A', 'B']

However, if the space is after the B value and before the tab

line = 'B \tA\n'
s = line.strip(stripchars).split('\t')

Produces an output that still includes the space after B

['B ', 'A']

rstrip() Behavior (conditions retain white space)

line = 'A\tB \n'
s  = line.rstrip('\n').split('\t')

Produces B with a space trailing it

['A', 'B ']

As well as the if the space precedes the tab

line = 'B \tA\n'
s = line.rstrip('\n').split('\t')

Where, the B is first in the list, but still includes the space

['B ', 'A']

Proposed List Comprehension (no additional white space)

This seems promising since it does fully remove all white space around each element in the list

line = 'B \tA\n'
s =[x.strip() for x in line if x.strip() != '']

No space around B or A

['B', 'A']

And

s = 'A\tB \n'
s =[x.strip() for x in line if x.strip() != '']

Also with no space around the B or A

['A', 'B']

@dschult dschult changed the title Replace strip with rstrip Fix reading edgelist when delimiter is whitespace, e.g. tab May 30, 2024
@dschult

dschult commented May 30, 2024

Copy link
Copy Markdown
Member

Yes -- when I said "broken" I meant it breaks backward compatibility -- not crashing anything. Sorry for the confusion.

I understand the rationale for either stripping whitespace from every entry or none of the entries. stripping the whitespace on the start of the first and end of the last without doing anything in between is not consistent across entries in a line. I tend to lean toward the simpler approach of not stripping whitespace at either end -- just remove the newline. And that is what is in this PR.

But, I'm worried about backward compatibility: Do we need to deprecate the current behavior? And what idiom can we give those for whom the tools work with their current file to get it to work with the new code. It seems like trouble could only occur for folks with node_type=None and delimiter set to something. For example: line = " b,a\n" or line="b,a \n". I guess we could deprecate with a warning to strip their lines like:

with open(path, "rb") as file:
    G = nx.parse_edgelist([line.strip() for line in file], delimiter=",")

Is there a better way? Or does someone feel this doesn't need a deprecation?

Or ;P maybe we should just tell people to fix the current Issue by doing:

with open(path, "rb") as file:
    G = nx.parse_edgelist([line.replace("\t", "@#$") for line in file], delimiter="@#$")

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've taken a look at this again and it should be fine as it is.
Anyone depending on the quirky whitespace handling we currently use will be able to adjust fairly easily.

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I took the liberty of adding some tests drawn from the comments here and the discussion in #7462 to ensure that this behavior is covered in the test suite moving forward.

Both 6222808 and 4abdd23 are tests that fail on current main and pass with these changes indicating the behavior change. Interestingly there is a difference between bipartite.parse_edgelist and parse_edgelist --- the former raised an exception for this case as demonstrated in #7462, but it turns out that this "worked" with parse_edgelist in that the logic ended up placing empty data fields with a sentinel value of None.

While these may trip some users up, I'm still in favor of adding these to improve consistency both across different data files and across the different parsing functions.

I also added a regression test for parse_adjlist, though there is no behavior change there!

Since I've added so much to the PR, I'll hold off and let another reviewer push the green button to make sure these tests make sense!

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @rossbar for the additional tests and the review!
I approve these changes and will merge now.

@dschult
dschult merged commit 88a0e7d into networkx:main Jul 8, 2024
@jarrodmillman jarrodmillman added this to the 3.4 milestone Jul 8, 2024
rossbar added a commit to Peiffap/networkx that referenced this pull request Sep 6, 2024
…#7465)

* rstrip delimiter

* Add test for bipartite edgelist char stripping.

* Regression tests for adjlist parsing rstrip.

* Add test for edgelist char stripping.

---------

Co-authored-by: Ross Barnowski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

edgelist reader removes deliminators if deliminator is white-space and file has empty fields [BUG]

5 participants