diff --git a/unitgrade/__init__.py b/unitgrade/__init__.py index 605eedf9521bbb549018417e8ade8e8c9e7a6f40..f59054178dcb91369f13288d1571a4f2ab10a9ea 100644 --- a/unitgrade/__init__.py +++ b/unitgrade/__init__.py @@ -23,8 +23,13 @@ def cache_read(file_name): import compress_pickle # Import here because if you import in top the __version__ tag will fail. # file_name = cn_(file_name) if cache_prefix else file_name if os.path.exists(file_name): - with open(file_name, 'rb') as f: - return compress_pickle.load(f, compression="lzma") + try: + with open(file_name, 'rb') as f: + return compress_pickle.load(f, compression="lzma") + except Exception as e: + print("Tried to load a bad pickle file at", file_name) + print("If the file appears to be automatically generated, you can try to delete it, otherwise download a new version") + print(e) # return pickle.load(f) else: return None diff --git a/unitgrade/__pycache__/__init__.cpython-38.pyc b/unitgrade/__pycache__/__init__.cpython-38.pyc index dc097231ab1cc676f96b6e553c3f3a9d29cccfee..1e78fe7d5e0b0725357397a89d63a7266b1f0aa7 100644 Binary files a/unitgrade/__pycache__/__init__.cpython-38.pyc and b/unitgrade/__pycache__/__init__.cpython-38.pyc differ diff --git a/unitgrade/__pycache__/unitgrade.cpython-38.pyc b/unitgrade/__pycache__/unitgrade.cpython-38.pyc index 5601367584ffb2dd33f99e057b47d73958c6bf42..be8062ad9576348beec485019d587f4c0f1b248f 100644 Binary files a/unitgrade/__pycache__/unitgrade.cpython-38.pyc and b/unitgrade/__pycache__/unitgrade.cpython-38.pyc differ diff --git a/unitgrade/__pycache__/unitgrade_helpers.cpython-38.pyc b/unitgrade/__pycache__/unitgrade_helpers.cpython-38.pyc index d7f1dcc24f9053facd9ab6146edd241ac30a3ca1..e83dce19af0550bd9e960167059ea3b304191b28 100644 Binary files a/unitgrade/__pycache__/unitgrade_helpers.cpython-38.pyc and b/unitgrade/__pycache__/unitgrade_helpers.cpython-38.pyc differ diff --git a/unitgrade/unitgrade.py b/unitgrade/unitgrade.py index e57ccbc0b1637ecfaa96a429a631809113040604..e1006731ce1109b5a234e8a1cb4f5f831a1d9db2 100644 --- a/unitgrade/unitgrade.py +++ b/unitgrade/unitgrade.py @@ -94,6 +94,17 @@ class QItem(unittest.TestCase): if self.title is None: self.title = self.name + def assertNorm(self, computed, expected, tol=None): + if tol == None: + tol = self.tol + diff = np.abs( (np.asarray(computed).flat- np.asarray(expected)).flat ) + nrm = np.sum( diff ** 2) + + if nrm > tol: + print(f"Not equal within tolerance {tol}; norm of difference was {nrm}") + print(f"Element-wise differences {diff.tolist()}") + self.assertEqual(computed, expected, msg=f"Not equal within tolerance {tol}") + def assertL2(self, computed, expected, tol=None): if tol == None: tol = self.tol @@ -332,24 +343,46 @@ class ActiveProgress(): self.t = t self._running = False self.title = title + self.dt = 0.1 + + self.n = int(np.round(self.t / self.dt)) + # self.pbar = tqdm.tqdm(total=self.n) + + if start: self.start() def start(self): self._running = True - self.thread = threading.Thread(target=self.run, args=(10,)) + self.thread = threading.Thread(target=self.run) self.thread.start() def terminate(self): + + self._running = False self.thread.join() + if hasattr(self, 'pbar') and self.pbar is not None: + self.pbar.update(1) + self.pbar.close() + self.pbar=None + sys.stdout.flush() - def run(self, n): - dt = 0.1 + def run(self): + self.pbar = tqdm.tqdm(total=self.n, file=sys.stdout, position=0, leave=False, desc=self.title, ncols=100, + bar_format='{l_bar}{bar}| [{elapsed}<{remaining}]') # , unit_scale=dt, unit='seconds'): - n = int(np.round(self.t/dt)) - for _ in tqdm.tqdm(range(n), file=sys.stdout, position=0, leave=False, desc=self.title, ncols=100, bar_format='{l_bar}{bar}| [{elapsed}<{remaining}]'): #, unit_scale=dt, unit='seconds'): + for _ in range(self.n-1): # Don't terminate completely; leave bar at 99% done until terminate. if not self._running: + self.pbar.close() + self.pbar = None break - time.sleep(dt) \ No newline at end of file + + time.sleep(self.dt) + self.pbar.update(1) + + # if self.pbar is not None: + # self.pbar.close() + # self.pbar = None + # for _ in tqdm.tqdm(range(n), file=sys.stdout, position=0, leave=False, desc=self.title, ncols=100, bar_format='{l_bar}{bar}| [{elapsed}<{remaining}]'): #, unit_scale=dt, unit='seconds'): diff --git a/unitgrade/unitgrade_helpers.py b/unitgrade/unitgrade_helpers.py index 5edd3ee7520abdb46fa992f516f78ef6f670b102..c73c496f4f37516ccefe13bb583ef735bf9ef6e8 100644 --- a/unitgrade/unitgrade_helpers.py +++ b/unitgrade/unitgrade_helpers.py @@ -76,11 +76,13 @@ def evaluate_report_student(report, question=None, qitem=None, unmute=None, pass table = table_data print(tabulate(table)) print(" ") - print("Note your results have not yet been registered. \nTo register your results, please run the file:") fr = inspect.getouterframes(inspect.currentframe())[1].filename - print(">>>", os.path.basename(fr)[:-3] + "_grade.py") - print("In the same manner as you ran this file.") + gfile = os.path.basename(fr)[:-3] + "_grade.py" + if os.path.exists(gfile): + print("Note your results have not yet been registered. \nTo register your results, please run the file:") + print(">>>", gfile) + print("In the same manner as you ran this file.") return results @@ -133,7 +135,6 @@ def evaluate_report(report, question=None, qitem=None, passall=False, verbose=Fa start = time.time() cc = None - if show_progress_bar: # cc.start() cc = ActiveProgress(t=q.estimated_time, title=q_title_print) @@ -143,12 +144,12 @@ def evaluate_report(report, question=None, qitem=None, passall=False, verbose=Fa q.init() # Initialize the question. Useful for sharing resources. if show_progress_bar: cc.terminate() + sys.stdout.flush() print(q_title_print, end="") q.has_called_init_ = True q_time =np.round( time.time()-start, 2) - print(" "* max(0,nL - len(q_title_print) ) + " (" + str(q_time) + " seconds)") # if q.name in report.payloads else "") print("=" * nL) @@ -159,7 +160,6 @@ def evaluate_report(report, question=None, qitem=None, passall=False, verbose=Fa cc = ActiveProgress(t=item.estimated_time, title=item_title_print) else: print(item_title_print + ( '.'*max(0, nL-4-len(ss)) ), end="") - hidden = issubclass(item.__class__, Hidden) # if not hidden: # print(ss, end="") diff --git a/unitgrade/version.py b/unitgrade/version.py index acf3be3eb86fb072c535366ae4b8e590326c2835..51e0a0611dc153362362f544329cb2287f967a38 100644 --- a/unitgrade/version.py +++ b/unitgrade/version.py @@ -1 +1 @@ -__version__ = "0.1.3" \ No newline at end of file +__version__ = "0.1.4" \ No newline at end of file