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

[Bug] i.spectral: Textual output format seems incoherent #4160

Open
echoix opened this issue Aug 9, 2024 · 0 comments
Open

[Bug] i.spectral: Textual output format seems incoherent #4160

echoix opened this issue Aug 9, 2024 · 0 comments
Labels
bug Something isn't working imagery Python Related code is in Python

Comments

@echoix
Copy link
Member

echoix commented Aug 9, 2024

Describe the bug

The code in question is:

def write2textf(what, output):
outf = open(output, "w")
i = 0
for row in enumerate(what):
i = i + 1
outf.write("%d, %s\n" % (i, row))
outf.close()

Here it's not related to this issue specifically, but I don't undestand why enumerate is used here, but the count it provides isn't used here. (Enumerate is to go through the iterable, but also bundle the index in a tuple, by default the start argument is at 0). And the loop also keeps track of an i variable. Are we sure that the output that is written to the file is what is expected? It writes i as a %d (a number), but then row as a %s (a string). Without running it, it should then be the string representation of the row, that is in fact here a tuple of the count and the item of what. Is it expected? Where is it used?

Originally posted by @echoix in #4134 (comment)

To me it seems like a glitch, when the script was ported from shell to Python.

There are no examples or tests that can be used as a reference for how the textfile output is supposed to look like. With the current code it will look something like this:

1, (0, [1, 1, 255, 45, 65])
2, (1, [2, 2, 255, 233, 165])
3, (2, [3, 3, 125, 133, 155])
4, (3, [4, 4, 167, 34, 67])

That does not seem natural to me as I would expect rather CSV like text output...

I would likely go for something like:

Path(output).write_text("\n".join([f"{i},{','.join(map(str, row))}"for i, row in enumerate(what, 1)]) + "\n")

Unless there is a reason for the current output format...

Originally posted by @ninsbl in #4134 (comment)

I'm for using i, row with enumerate and outputting just that. That looks like the intention in the original code. I'm not sure how this compares to the one-liner.

Originally posted by @wenzeslaus in #4134 (comment)

Expected behavior

A usable output format

Additional context

See the linked thread to follow.

@echoix echoix added bug Something isn't working Python Related code is in Python imagery labels Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working imagery Python Related code is in Python
Projects
None yet
Development

No branches or pull requests

1 participant