From 04fbebff381e48cea98cba41c9306a3d0b3331b3 Mon Sep 17 00:00:00 2001
From: Tue Herlau <tuhe@dtu.dk>
Date: Mon, 23 May 2022 16:28:56 +0200
Subject: [PATCH] updates

---
 README.md                       | 111 +++++++++++++++-----------------
 docs/unitgrade.bib              |   6 +-
 src/unitgrade.egg-info/PKG-INFO |   2 +-
 src/unitgrade/evaluate.py       |   9 ++-
 src/unitgrade/framework.py      |  41 ++++++++----
 src/unitgrade/version.py        |   2 +-
 6 files changed, 93 insertions(+), 78 deletions(-)

diff --git a/README.md b/README.md
index 552d9f8..530c4cb 100644
--- a/README.md
+++ b/README.md
@@ -1,58 +1,65 @@
-
 # Unitgrade
 
-Unitgrade is an automatic report and exam evaluation framework that enables instructors to offer automatically evaluated programming assignments. 
- Unitgrade is build on pythons `unittest` framework so that the tests can be specified in a familiar syntax and will integrate with any modern IDE. What it offers beyond `unittest` is the ability to collect tests in reports (for automatic evaluation) and an easy and 100% safe mechanism for verifying the students results and creating additional, hidden tests. A powerful cache system allows instructors to automatically create test-answers based on a working solution. 
+Unitgrade is an automatic software testing framework that enables instructors to offer automatically evaluated programming assignments with a minimal overhead for students.
+
+Unitgrade is build on pythons `unittest` framework so that the tests can be specified and run in a familiar syntax, 
+ and will integrate well with any modern IDE. What it offers beyond `unittest` is the ability to collect tests in reports (for automatic evaluation) 
+and an easy and safe mechanism for verifying results. 
 
  - 100% Python `unittest` compatible
- - No external configuration files, just write a `unittest`
- - No unnatural limitations: If you can `unittest` it, it works.   
- - Granular security model: 
-    - Students get public `unittests` for easy development of solutions
-    - Students get a tamper-resistant file to create submissions which are uploaded
-    - Instructors can automatically verify the students solution using Docker VM and by running hidden tests
-    - Allow export of assignments to Autolab (no `make` file mysteries!)
- - Tests are quick to run and will integrate with your IDE
+ - Integrates with any modern IDE (VSCode, Pycharm, Eclipse)
+ - No external configuration files or setup required
+ - Tests are quick to run and will tell you where your mistake is
+ - Hint-system collects hints from code and display it with failed unittests
 
 ## Installation
-Unitgrade can be installed using `pip`:
-```
+Unitgrade is simply installed like any other package using `pip`:
+```terminal
 pip install unitgrade
 ```
-This will install unitgrade in your site-packages directory. If you want to upgrade an old installation of unitgrade:
-```
-pip install unitgrade --upgrade
-```
-If you are using anaconda+virtual environment you can install it as
+This will install unitgrade in your site-packages directory and you should be all set. If you want to upgrade an old version of unitgrade run:
+```terminal
+pip install unitgrade --upgrade --no-cache-dir
 ```
+If you are using anaconda+virtual environment you can install it as any other package:
+```terminal
 source activate myenv
 conda install git pip
 pip install unitgrade
 ```
-
-When you are done, you should be able to import unitgrade:
-```
-import unitgrade
+When you are done, you should be able to import unitgrade. Type `python` in the termial and try:
+```pycon
+>>> import unitgrade2
 ```
 
-## Evaluating a report
-Homework is broken down into **reports**. A report is a collection of questions which are individually scored, and each question may in turn involve multiple tests. Each report is therefore given an overall score based on a weighted average of how many tests are passed.
-In practice, a report consist of an ordinary python file which they simply run. It looks like this (to run this on your local machine, follow the instructions in the previous section):
+## Using Unitgrade
+In unitgrade, your homework assignments are called **reports** and are distributed as regular `.py`-files. I am going to use `cs101report1.py` as a generic example in the following, but a real-world example can be found here: https://gitlab.compute.dtu.dk/tuhe/unitgrade_private/-/blob/master/examples/example_simplest/students/cs101/report1.py .
+
+The report is simply a collection of questions which are individually scored, and each question may in turn involve checking several sub-cases.
+
+You should think of the tests as a help for you when you are debugging your code and when you are trying to figure out what to do. 
+I recommend running the tests through your IDE. In pycharm, this is as simple as right-clicking on the test and selecting `Run as unittest`. The image belows shows the outcome in Pycharm:
+
+![Using unittests in pycharm](https://gitlab.compute.dtu.dk/tuhe/unitgrade/-/raw/master/docs/pycharm.png)
+
+The tests are shown in the lower-left corner, and in this case they are all green meaning they have passed. If a test fails, you can right-click and select `debug as unittest`, or you can click on it and see the output it produced, and you can right-click on individual tests to re-run them.
+
+### Checking your score
+To check your score, you have to run the main script (`cs101report1.py`) as a regular python file. This can be done either through pycharm (Hint: Open the file and press `alt-shift-F10`) or in the console by running the command: 
 ```
 python cs101report1.py
 ```
-The file `cs101report1.py` is just an ordinary, non-obfuscated file which they can navigate and debug using a debugger. The file may contain the homework, or it may call functions the students have written.  Running the file creates console output which tells the students their current score for each test:
-
+The file will run and show an output where the score of each question is computed as a (weighted) average of the individual passed tests. An example is given below:
 ```terminal
  _   _       _ _   _____               _      
 | | | |     (_) | |  __ \             | |     
 | | | |_ __  _| |_| |  \/_ __ __ _  __| | ___ 
 | | | | '_ \| | __| | __| '__/ _` |/ _` |/ _ \
 | |_| | | | | | |_| |_\ \ | | (_| | (_| |  __/
- \___/|_| |_|_|\__|\____/_|  \__,_|\__,_|\___| v0.0.3, started: 07/09/2021 00:42:25
+ \___/|_| |_|_|\__|\____/_|  \__,_|\__,_|\___| v0.1.22, started: 19/05/2022 15:16:20
 
 Week 4: Looping (use --help for options)
-Question 1: Test the cluster analysis method                                                                            
+Question 1: Cluster analysis                                                                                            
  * q1.1) clusterAnalysis([0.8, 0.0, 0.6]) = [1, 2, 1] ?.............................................................PASS
  * q1.2) clusterAnalysis([0.5, 0.6, 0.3, 0.3]) = [2, 2, 1, 1] ?.....................................................PASS
  * q1.3) clusterAnalysis([0.2, 0.7, 0.3, 0.5, 0.0]) = [1, 2, 1, 2, 1] ?.............................................PASS
@@ -75,14 +82,14 @@ Question 3: Bacteria growth rates
  * q3.5) bacteriaGrowth(100, 0.4, 1000, 99) = 0 ?...................................................................PASS
  * q3)   Total.................................................................................................... 10/10
  
-Question 4: Test the fermentation rate question                                                                         
+Question 4: Fermentation rate                                                                                           
  * q4.1) fermentationRate([20.1, 19.3, 1.1, 18.2, 19.7, ...], 15, 25) = 19.600 ?....................................PASS
  * q4.2) fermentationRate([20.1, 19.3, 1.1, 18.2, 19.7, ...], 1, 200) = 29.975 ?....................................PASS
  * q4.3) fermentationRate([1.75], 1, 2) = 1.750 ?...................................................................PASS
  * q4.4) fermentationRate([20.1, 19.3, 1.1, 18.2, 19.7, ...], 18.2, 20) = 19.500 ?..................................PASS
  * q4)   Total.................................................................................................... 10/10
  
-Total points at 00:42:25 (0 minutes, 0 seconds)....................................................................40/40
+Total points at 15:16:20 (0 minutes, 0 seconds)....................................................................40/40
 Provisional evaluation
 ---------  -----
 q1) Total  10/10
@@ -98,38 +105,28 @@ To register your results, please run the file:
 In the same manner as you ran this file.
 
 ```
-Once you are happy with the result run the script with the `_grade.py`-postfix, in this case `cs101report1_grade.py`:
-
+### Handing in your homework
+Once you are happy with your results and want to hand in, you should run the script with the `_grade.py`-postfix, in this case `cs101report1_grade.py` (see console output above):
 ```
 python cs101report1_grade.py
 ```
-This runs the same tests, and generates a file `Report0_handin_18_of_18.token`. The file name indicates how many points you got. Upload this file to campusnet (and no other). 
-
-## Running the tests in pycharm
-Naturally, you can also run the tests in pycharm, and this offers you a lot of cool features such as integration with the debugger and the ability to see which tests have failed.
-To do this, simply right-click on the `report.py`-file and select `Run as unittest` (or alternatively, `debug as unittest`). This will take you to a screen such as shown below:
-
-![Using unittests in pycharm](https://gitlab.compute.dtu.dk/tuhe/unitgrade/-/raw/master/docs/pycharm.png)
-
-You can see all tests are green indicating they all pass. If you click on a test you can see the console output it generates and you can 
-right-click on the tests to re-run individual tests. 
-
+This script will run *the same tests as before* and generates a file named `Report0_handin_18_of_18.token` (this is called the `token`-file because of the extension). The token-file contains all your results and it is the token-file you should upload (and no other). Because you cannot (and most definitely should not!) edit it, it shows the number of points in the file-name. 
 
 ### Why are there two scripts?
-The reason why we use a standard test script, and one with the `_grade.py` extension, is because the tests should both be easy to debug, but at the same time we have to prevent accidential changes to the test scripts. Hence, we include two versions of the tests.
+The reason why we use a standard test script (one with the `_grade.py` extension and one without), is because the tests should both be easy to debug, but at the same time we have to avoid accidential changes to the test scripts. The tests themselves are the same, so if one script works, so should the other.
 
 # FAQ
  - **My non-grade script and the `_grade.py` script gives different number of points**
 Since the two scripts should contain the same code, the reason is nearly certainly that you have made an (accidental) change to the test scripts. Please ensure both scripts are up-to-date and if the problem persists, try to get support.
    
  - **Why is there a `unitgrade` directory with a bunch of pickle files? Should I also upload them?**
-No. The file contains the pre-computed test results your code is compared against. If you want to load this file manually, the unitgrade package contains helpful functions for doing so.
+No. The file contains the pre-computed test results your code is compared against. You should only upload the `.token` file, nothing else
    
  - **I am worried you might think I cheated because I opened the '_grade.py' script/token file**
 This should not be a concern. Both files are in a binary format (i.e., if you open them in a text editor they look like garbage), which means that if you make an accidential change, they will with all probability simply fail to work. 
    
  - **I think I might have edited the `report1.py` file. Is this a problem since one of the tests have now been altered?**
-Feel free to edit/break this file as much as you like if it helps you work out the correct solution. In fact, I recommend you just run `report1.py` from your IDE and use the debugger to work out the current state of your program. However, since the `report1_grade.py` script contains a seperate version of the tests, please ensure your `report1.py` file is up to date.
+Feel free to edit/break this file as much as you like if it helps you work out the correct solution. However, since the `report1_grade.py` script contains a seperate version of the tests, please ensure both files are in sync to avoid unexpected behavior. 
    
 ### Debugging your code/making the tests pass
 The course material should contain information about the intended function of the scripts used in the tests, and the file `report1.py` should mainly be used to check which of your code is being run. In other words, first make sure your code solves the exercises, and only later run the test script which is less easy/nice to read. 
@@ -137,8 +134,8 @@ However, obivously you might get to a situation where your code seems to work, b
 
  - **I am 99% sure my code is correct, but the test still fails. Why is that?**
 The testing framework offers a great deal of flexibility in terms of what is compared. This is either: (i) The value a function returns, (ii) what the code print to the console (iii) something derived from these.
-   Since the test *might* compare the console output, i.e. what you generate using `print("...")`-statements, innnocent changes to the script, like an extra print statement, can cause the test to fail, which is counter-intuitive. For this reason, please look at the error message carefully (or the code in `report1.py`) to understand what is being compared. 
-   
+When a test fails, you should always try to insert a breakpoint on exactly the line that generate the problem, run the test in the debugger, and figure out what the expected result was supposed to be. This should give you a clear hint as to what may be wrong.
+
 One possibility that might trick some is that if the test compares a value computed by your code, the datatype of that value is important. For instance, a `list` is not the same as a python `ndarray`, and a `tuple` is different from a `list`. This is the correct behavior of a test: These things are not alike and correct code should not confuse them. 
 
  - **The `report1.py` class is really confusing. I can see the code it runs on my computer, but not the expected output. Why is it like this?**
@@ -148,10 +145,7 @@ To make sure the desired output of the tests is always up to date, the tests are
 There are a number of console options available to help you figure out what your program should output and what it currently outputs. They can be found using:
  ```python report1.py --help```
 Note these are disabled for the `report1_grade.py` script to avoid confusion. It is not recommended you use the grade script to debug your code.  
-
- - **How do I see the output generated by my scripts in the IDE?**
-The file `unitgrade/unitgrade.py` contains all relevant information. Look at the `QItem` class and the function `get_points`, which is the function that strings together all the tests. 
-
+   
  - **Since I cannot read the `.token` file, can I trust it contains the same number of points internally as the file name indicate?**
 Yes. 
 
@@ -160,24 +154,23 @@ Yes.
 That the script `report1_grade.py` is difficult to read is not the principle safety measure. Instead, it ensures there is no accidential tampering. If you muck around with these files and upload the result, we will very likely know.     
 
 - **I have private data on my computer. Will this be read or uploaded?**
-No. The code will look for and upload your solutions, but it will not read/look at other directories in your computer. In the example provided with this code, this means you should expect unitgrade to read/run all files in the `cs101courseware_example`-directory, but **no** other files on your computer. So as long as you keep your private files out of the base courseware directory, you should be fine. 
+No. The code will look for and upload your solutions, but it will not read/look at other directories in your computer. As long as your keep your private files out of the directory containing your homework you have nothing to worry about. 
 
 - **Does this code install any spyware/etc.? Does it communicate with a website/online service?**
-No. Unitgrade makes no changes outside the courseware directory and it does not do anything tricky. It reads/runs code and write the `.token` file.
+No. Unitgrade makes no changes outside the courseware directory and it does not do anything tricky. It reads/runs code and produce the `.token` file.
   
 - **I still have concerns about running code on my computer I cannot easily read**
 Please contact me and we can discuss your specific concerns.
-  
 
 # Citing
 ```bibtex
 @online{unitgrade,
-	title={Unitgrade (0.0.3): \texttt{pip install unitgrade}},
+	title={Unitgrade (0.1.22): \texttt{pip install unitgrade}},
 	url={https://lab.compute.dtu.dk/tuhe/unitgrade},
-	urldate = {2021-09-07}, 
+	urldate = {2022-05-19}, 
 	month={9},
 	publisher={Technical University of Denmark (DTU)},
 	author={Tue Herlau},
-	year={2021},
+	year={2022},
 }
 ```
\ No newline at end of file
diff --git a/docs/unitgrade.bib b/docs/unitgrade.bib
index b1cb0cd..6513188 100644
--- a/docs/unitgrade.bib
+++ b/docs/unitgrade.bib
@@ -1,9 +1,9 @@
 @online{unitgrade,
-	title={Unitgrade (0.1.18): \texttt{pip install unitgrade}},
+	title={Unitgrade (0.1.22): \texttt{pip install unitgrade}},
 	url={https://lab.compute.dtu.dk/tuhe/unitgrade},
-	urldate = {2021-09-20}, 
+	urldate = {2022-05-19}, 
 	month={9},
 	publisher={Technical University of Denmark (DTU)},
 	author={Tue Herlau},
-	year={2021},
+	year={2022},
 }
\ No newline at end of file
diff --git a/src/unitgrade.egg-info/PKG-INFO b/src/unitgrade.egg-info/PKG-INFO
index a61ec2e..772041a 100644
--- a/src/unitgrade.egg-info/PKG-INFO
+++ b/src/unitgrade.egg-info/PKG-INFO
@@ -1,6 +1,6 @@
 Metadata-Version: 2.1
 Name: unitgrade
-Version: 0.1.20
+Version: 0.1.21
 Summary: A student homework/exam evaluation framework build on pythons unittest framework.
 Home-page: https://lab.compute.dtu.dk/tuhe/unitgrade
 Author: Tue Herlau
diff --git a/src/unitgrade/evaluate.py b/src/unitgrade/evaluate.py
index 1be3c94..d6efdd8 100644
--- a/src/unitgrade/evaluate.py
+++ b/src/unitgrade/evaluate.py
@@ -229,8 +229,15 @@ def evaluate_report(report, question=None, qitem=None, passall=False, verbose=Fa
 
 def python_code_str_id(python_code, strip_comments_and_docstring=True):
     s = python_code
+
     if strip_comments_and_docstring:
-        s = remove_comments_and_docstrings(s)
+        try:
+            s = remove_comments_and_docstrings(s)
+        except Exception as e:
+            print("--"*10)
+            print(python_code)
+            print(e)
+
     s = "".join([c.strip() for c in s.split()])
     hash_object = hashlib.blake2b(s.encode())
     return hash_object.hexdigest()
diff --git a/src/unitgrade/framework.py b/src/unitgrade/framework.py
index a2d9820..daa7cf2 100644
--- a/src/unitgrade/framework.py
+++ b/src/unitgrade/framework.py
@@ -45,21 +45,15 @@ class Report:
 
     def _import_base_relative(self):
         if hasattr(self.pack_imports[0], '__path__'):
-            # im = self.pack_imports[0]
-            # print("im!!!!")
-            # print(im.__path__)
-            # if isinstance(im, list):
-            #     print("im is", im)
-            # __path__ was originally not a list; but now it seems like it behaves like a list. Add check if problem persists and treat it as either list or string.
-            # root_dir = self.pack_imports[0].__path__[0]._path[0]
             root_dir = self.pack_imports[0].__path__[0]
-
         else:
             root_dir = self.pack_imports[0].__file__
 
         root_dir = os.path.dirname(root_dir)
         relative_path = os.path.relpath(self._file(), root_dir)
         modules = os.path.normpath(relative_path[:-3]).split(os.sep)
+        relative_path = relative_path.replace("\\", "/")
+
         return root_dir, relative_path, modules
 
     def __init__(self, strict=False, payload=None):
@@ -390,15 +384,31 @@ class UTestCase(unittest.TestCase):
 
 
     def assertLinf(self, first, second=None, tol=1e-5, msg=None):
+        """ Test in the L_infinity norm.
+        :param first:
+        :param second:
+        :param tol:
+        :param msg:
+        :return:
+        """
         if second is None:
             return self.wrap_assert(self.assertLinf, first, tol=tol, msg=msg)
         else:
             diff = self._shape_equal(first, second)
-            if max(diff.flat) >= tol:
+            np.testing.assert_allclose(first, second, atol=tol)
+            
+            max_diff = max(diff.flat)
+            if max_diff >= tol:
                 from unittest.util import safe_repr
                 # msg = f'{safe_repr(first)} != {safe_repr(second)} : Not equal within tolerance {tol}'
                 # print(msg)
-                self.assertEqual(first, second, msg=f'Not equal within tolerance {tol}')
+                # np.testing.assert_almost_equal
+                # import numpy as np
+                print(f"|first - second|_max = {max_diff} > {tol} ")
+                np.testing.assert_almost_equal(first, second)
+                # If the above fail, make sure to throw an error:
+                self.assertFalse(max_diff >= tol, msg=f'Input arrays are not equal within tolerance {tol}')
+                # self.assertEqual(first, second, msg=f'Not equal within tolerance {tol}')
 
     def assertL2(self, first, second=None, tol=1e-5, msg=None, relative=False):
         if second is None:
@@ -412,10 +422,15 @@ class UTestCase(unittest.TestCase):
             diff = ( ( np.asarray( diff.flatten() )**2).sum() )**.5
 
             scale = (2/(np.linalg.norm(np.asarray(first).flat) + np.linalg.norm(np.asarray(second).flat)) ) if relative else 1
-            if max(diff.flat)*scale >= tol:
+            max_diff = diff*scale
+            if max_diff >= tol:
                 msg = "" if msg is None else msg
-                print(f"|first - second|_2 = {diff} > {tol} ")
-                self.assertEqual(first, second, msg=msg + f"Not equal within tolerance {tol}")
+                print(f"|first - second|_2 = {max_diff} > {tol} ")
+                # Deletage to numpy. Let numpy make nicer messages.
+                np.testing.assert_almost_equal(first, second) # This function does not take a msg parameter.
+                # Make sure to throw an error no matter what.
+                self.assertFalse(max_diff >= tol, msg=f'Input arrays are not equal within tolerance {tol}')
+                # self.assertEqual(first, second, msg=msg + f"Not equal within tolerance {tol}")
 
     def _cache_file(self):
         return os.path.dirname(inspect.getabsfile(type(self))) + "/unitgrade_data/" + self.__class__.__name__ + ".pkl"
diff --git a/src/unitgrade/version.py b/src/unitgrade/version.py
index 6cd5b39..3f671b2 100644
--- a/src/unitgrade/version.py
+++ b/src/unitgrade/version.py
@@ -1 +1 @@
-__version__ = "0.1.20"
\ No newline at end of file
+__version__ = "0.1.22"
\ No newline at end of file
-- 
GitLab