Skip to content

STY: prefer _DefaultEventParser to _Counter#6355

Merged
massich merged 2 commits intomne-tools:masterfrom
massich:try_event_id
May 22, 2019
Merged

STY: prefer _DefaultEventParser to _Counter#6355
massich merged 2 commits intomne-tools:masterfrom
massich:try_event_id

Conversation

@massich
Copy link
Copy Markdown
Contributor

@massich massich commented May 21, 2019

Things I wanted to ask in #6326 but I didn't have the chance.

It avoids returning lambda: None + adds a test in case event_id='random string'. What I did not got away with is a form to not pass raw to _check_event_id. (which can be big and so on). @larsoner can you see a way? Maybe defining the function within or braking it again in two functions which we end up at the same place as we started.

Anyways, feel free to push in the branch or close

@agramfort
Copy link
Copy Markdown
Member

why is the size of raw a problem? it's passed by reference. What do I miss. Patch looks good

@massich
Copy link
Copy Markdown
Contributor Author

massich commented May 21, 2019

It's just me getting confused. https://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference
Python passes the parameters by assignment. Which is a way to say that they pass the reference by value. (so yes some sort of passed by reference)

@codecov
Copy link
Copy Markdown

codecov bot commented May 21, 2019

Codecov Report

Merging #6355 into master will increase coverage by <.01%.
The diff coverage is 95.65%.

@@            Coverage Diff             @@
##           master    #6355      +/-   ##
==========================================
+ Coverage   89.31%   89.31%   +<.01%     
==========================================
  Files         405      405              
  Lines       74266    74271       +5     
  Branches    12297    12297              
==========================================
+ Hits        66330    66335       +5     
  Misses       5072     5072              
  Partials     2864     2864

@massich
Copy link
Copy Markdown
Contributor Author

massich commented May 21, 2019

This would make codecov more happy, but I don't think is more readable.

def __call__(self, description, offset=1):
    if description not in self.event_ids:
        out = offset + len(self.event_ids)
        self.event_ids[description] = out
    else:
        out = self.event_ids[description]

    return out

(else: pass would also keep codecov happy I think)
Is there any function in dict that does this and I'm not aware of it?

@larsoner
Copy link
Copy Markdown
Member

    if description not in self.event_ids:
        out = offset + len(self.event_ids)
        self.event_ids[description] = out
    else:
        out = self.event_ids[description]

is equiv to

    self.event_ids[description] = self.event_ids.get(description, offset + len(self.event_ids))
    out = self.event_ids[description]

@massich
Copy link
Copy Markdown
Contributor Author

massich commented May 22, 2019

~/code/mne-python refactor_digitation* 2m 55s
(mne) ❯ ipython
Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: %paste                                                                                                                              
def xx():
    dd = dict()
    for _ in range(50):
        if 'a' not in dd:
            dd['a'] = len(dd)+1
        out = dd['a']
    for _ in range(50):
        dd = dict()
        if 'a' not in dd:
            dd['a'] = len(dd)+1
        out = dd['a']

def yy():
    dd = dict()
    for _ in range(50):
        dd['a'] = dd.get('a', len(dd)+1)
        out = dd['a']
    for _ in range(50):
        dd['a'] = dd.get('a', len(dd)+1)
        out = dd['a']
## -- End pasted text --

In [2]: %timeit -n100 -r100 xx()                                                                                                            
11.5 µs ± 2.31 µs per loop (mean ± std. dev. of 100 runs, 100 loops each)

In [3]: %timeit -n100 -r100 yy()                                                                                                            
14.9 µs ± 2.42 µs per loop (mean ± std. dev. of 100 runs, 100 loops each)

In [4]: %timeit -n100 -r100 xx()                                                                                                            
10.6 µs ± 701 ns per loop (mean ± std. dev. of 100 runs, 100 loops each)

In [5]: %timeit -n100 -r100 yy()                                                                                                            
The slowest run took 7.67 times longer than the fastest. This could mean that an intermediate result is being cached.
17 µs ± 13 µs per loop (mean ± std. dev. of 100 runs, 100 loops each)

In [6]: %timeit -n100 -r100 xx()                                                                                                            
11.2 µs ± 1.74 µs per loop (mean ± std. dev. of 100 runs, 100 loops each)

In [7]: %timeit -n100 -r100 yy()                                                                                                            
14.9 µs ± 3.33 µs per loop (mean ± std. dev. of 100 runs, 100 loops each)

In [8]: %timeit -n100 -r100 xx()                                                                                                            
11.3 µs ± 1.58 µs per loop (mean ± std. dev. of 100 runs, 100 loops each)

In [9]: %timeit -n100 -r100 yy()                                                                                                            
14.5 µs ± 1.57 µs per loop (mean ± std. dev. of 100 runs, 100 loops each)

@massich
Copy link
Copy Markdown
Contributor Author

massich commented May 22, 2019

I'll merge as it is. Thx @larsoner

@massich massich merged commit 06c7785 into mne-tools:master May 22, 2019
@massich massich deleted the try_event_id branch May 27, 2019 11:41
larsoner pushed a commit to larsoner/mne-python that referenced this pull request Jun 11, 2019
agramfort pushed a commit that referenced this pull request Jun 12, 2019
* FIX: allow raw_fif to parse brainvision events (#6417)

* TST: usecase

* FIX: check annotation description base names

* fix: remove pathlib

* RFC: Alternative without getattr.

* imports

* Fix: avoid adding 'barinvision' as event_id keyword

* remove _get_auto_event_id

* update whatsnew

* Update brainvision.py

* cosmit

* fix

* use rstrip

* MRG+1, DOC: footer logos (#6420)

* add individual inst. logos; add uni-graz

* add image titles (hover)

* force add missing logos

* codespell skip SVG files

* fix: make inria PNG; better logo wrapping

* fix footer overlap; slightly larger logos

* STY: prefer _DefaultEventParser to _Counter (#6355)

* Fix docstrings (#6385)

* docstring deduplication

* more n_jobs

* fix: import fill_doc

* fix crossrefs; deduplicate docdict def for random_state

* FIX: Conftest [circle front]

* FIX: Ref [circle front]
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