Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, where to start? Here is just a first observation:</p> <p>You have a lot of data there, and it seems code and data are mixed into each other.</p> <p>Data and Code should be separate. Data is an external source, something you modify or read in. You could probably adapt your code to quickly parse Data from a good editable representation to a representation useful for your algorithms. I suspect your code will be shorter, clearer, and less error prone (did you notice all of the 'rates' dictionaries have multiple keys, and you miss a lot of 'hi' keys?).</p> <p>If you need better abstractions such as matrices and arrays of data, look into <code>numpy</code></p> <hr> <p>Edit 1</p> <p>Did you count your number of dimensions? You have a many-dimensional matrix here with X dimensions: analysis_type, population_stratification, income_level, index</p> <p>If I see right this is a 3x4x3x3 (= 108 entries) "matrix" or "lookup table". If this is the data your model builds on, fine. But can't you put those numbers in a file, or table that you read in? Your code would be next to trivial.</p> <hr> <p>Edit 2</p> <p>Ok, I'll bite for some minor python style: Testing for values in a Set or a Range.</p> <p>Instead of:</p> <pre><code>if analysis_type == 1 or analysis_type == 2 or analsis_type == 4: </code></pre> <p>you can use</p> <pre><code>if analysis_type in (1, 2, 4): </code></pre> <p>or even using readable names as (CUBIC, ..) as suggested.</p> <p>Instead of:</p> <pre><code>if x &lt; high_income and x &gt;= medium_income: </code></pre> <p>you can used chained conditions; Python is one of the few programming languages where conditions chain to make nautral if statements:</p> <pre><code>if medium_income &lt;= x &lt; high_income: </code></pre> <hr> <p>Edit 3</p> <p>More important than small code figures is of course code design and refactoring. Edit 2 can only give you some polish.</p> <p>You should learn to loathe duplicate code.</p> <p>Also, you have quite a lot of branches in one function. That is a good sign you should break it up into multiple functions. It can also reduce duplication. For example, when one variable like <code>analysis_type</code> can totally change what the function does, why have two different behaviors in one function? You shouldn't have the whole program in one function. Perhaps analysis_type == 3 is better expressed in its own function (as an example)?</p> <p>Do you understand that your function <code>trip_rates</code> basically does an array lookup, where the array lookup is hardcoded as if ..: return .. if : return .., and the array is written out in full in the function? What if <code>trip_rates</code> could be implemented like this? Would it be possible?</p> <pre><code>data_model = compute_table(low_income, ...) return data_model[analysis_type][population_stratification] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload