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

incrementFileName issue (and solution) #189

Open
pbmanis opened this issue Dec 5, 2023 · 0 comments
Open

incrementFileName issue (and solution) #189

pbmanis opened this issue Dec 5, 2023 · 0 comments

Comments

@pbmanis
Copy link
Contributor

pbmanis commented Dec 5, 2023

Recently I have run into a little problem with the automatic filename (directory name) auto increment failing, because it thinks that a directory already exists when it does not. The issue is in util.DataManager, incrementFIleName, and only occurs occasionally with filenames that have numbers in them that follow and underscore, and where the numbers occur earlier in the string than the increment NNN number. The program picks the number and then finds that a file with the same prefix exists. For example, protocols named :
CCIV_1nA_max
and
CCIV_1nA_max_1s_pos_only
cause this problem.

The current code is:

def incrementFileName(self, fileName, useExt=True):
        """Given fileName.ext, finds the next available fileName_NNN.ext"""
        files = self.ls()
        if useExt:
            (fileName, ext) = os.path.splitext(fileName)
        else:
            ext = ''
        regex = re.compile(fileName + r'_(\d+)')
        files = [f for f in files if regex.match(f)]
        if len(files) > 0:
            files.sort()
            maxVal = int(regex.match(files[-1]).group(1)) + 1
        else:
            maxVal = 0
        ret = fileName + ('_%03d' % maxVal) + ext
        return ret

replacing the regex with:

regex = re.compile(fileName + r'_(\d{3})')

helps, and is what I have implemented to address this admittedly rare problem.

A better solution might be:

regex = re.compile(fileName + r'_(\d{3)')$

to force the match to the end of the string. Another solution is to get all instances of _#(##) and get the last group instead of group(1), which might be more general.

I'd submit a PR, but my copy of acq4 has so many little (and some big) changes that it would not be the most efficient way to go.

--pbm

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

No branches or pull requests

1 participant