Friday, March 4, 2011

Ticking function grapher

Hello everyone,

I am trying to figure out the following problem. I am building Yet another math function grapher, The function is drawn on its predefined x,y range, that's all good.

Now I am working on the background and the ticking of X, Y axes (if any axes are shown).

I worked out the following. I have a fixed width of 250 p The tick gap should be between 12.5 and 50p.

The ticks should indicate either unit or half unit range, by that i mean the following.

x range (-5, 5): one tick = 1

x range (-1, 1): one tick = 0.5 or 0.1 depending on the gap that each of this option would generate.

x range (0.1, 0.3): 0.05

Given a Xrange How would you get the number of ticks between either full or half unit range ?

Or maybe there are other way to approach this type of problems.

From stackoverflow
  • Using deltaX

    if deltax between 2 and 10 half increment if deltax between 10 and 20 unit increment if smaller than 2 we multiply by 10 and test again if larger than 20 we divide Then we get the position of the first unit or half increment on the width using xmin.

    I still need to test this solution.

  • One way to do this would be to "normalise" the difference between the minimum and maximum and do a case distinction on that value. In python:

    delta = maximum - minimum
    factor = 10**math.ceil(math.log(delta,10))  # smallest power of 10 greater than delta
    normalised_delta = delta / factor           # 0.1 <= normalised_delta < 1
    if normalised_delta/5 >= 0.1:
      step_size = 0.1
    elif normalised_delta/5 >= 0.05:
      step_size = 0.05
    elif normalised_delta/20 <= 0.01:
      step_size = 0.01
    step_size = step_size * factor
    

    The above code assumes you want the biggest possible gap. For the smallest you would use the following if:

    if normalised_delta/20 == 0.005:
      step_size = 0.005
    elif normalised_delta/20 <= 0.01:
      step_size = 0.01
    elif normalised_delta/5 >= 0.05:
      step_size = 0.05
    

    Besides the possibility that there are more than one suitable values, there is also the somewhat worrisome possibility that there are none. Take for example the range [0,24] where a gap of 12.5p would give a step size of 1.2 and a gap of 50p would give step size 4.8. There is no "unit" or "half unit" in between. The problem is that the difference between a gap of 12.5p and one of 50p is a factor 4 while the difference between 0.01 and 0.05 is a factor 5. So you will have to widen the range of allowable gaps a bit and adjust the code accordingly.

    Clarification of some of the magic numbers: divisions by 20 and 5 correspond to the number of segments with the minimal and maximal gap size, respectively (ie. 250/12.5 and 250/50). As the normalised delta is in the range [0.1,1), you get that dividing it by 20 and 5 gives you [0.005,0.05) and [0.02,0.2), respectively. These ranges result in the possible (normalised) step sizes of 0.005 and 0.01 for the first range and 0.05 and 0.1 for the second.

    coulix : Thanks ! the factor = 10**math.ceil(math.log(delta,10)) did the tric !
  • You might want to take a look at Jgraph, which solves a complementary problem: it is a data grapher rather than a function grapher. But there are a lot of things in common such as dealing with major and minor tick marks, axis labels, and so on and so forth. I find the input language a little verbose for my taste, but Jgraph produces really nice technical graphs. There are a lot of examples on the web site and probably some good ideas you could steal.

    And you know what they say: talent imitates, but genius steals :-)

  • This seems to do what i was expecting.

    import math

    def main(): getTickGap(-1,1.5)

    def next_multiple(x, y): return math.ceil(x/y)*y

    def getTickGap(xmin, xmax): xdelta = xmax -xmin width = 250 # smallest power of 10 greater than delta factor = 10**math.ceil(math.log(xdelta,10)) # 0.1 <= normalised_delta < 1 normalised_delta = xdelta / factor print("normalised_delta", normalised_delta)

    # we want largest gap
    if normalised_delta/4 >= 0.1:
      step_size = 0.1
    elif normalised_delta/4 >= 0.05:
      step_size = 0.05
    elif normalised_delta/20 <= 0.01:
      step_size = 0.01
    step_size = step_size * factor
    
    
    ##    if normalised_delta/20 == 0.005:
    ##      step_size = 0.005
    ##    elif normalised_delta/20 <= 0.01:
    ##      step_size = 0.01
    ##    elif normalised_delta/4 >= 0.05:
    ##      step_size = 0.05
    ##    step_size = step_size * factor
    print("step_size", step_size)
    totalsteps = xdelta/step_size
    print("Total steps", totalsteps)
    print("Range [", xmin, ",", xmax, "]")
    
    firstInc = next_multiple(xmin, step_size)
    count = (250/xdelta)*(firstInc - xmin)
    print("firstInc ", firstInc, 'tick at ', count)
    print("start at ", firstInc - xmin, (width/totalsteps)*(firstInc - xmin))
    inc = firstInc
    
    while (inc <xmax):
        inc += step_size
        count += (width/totalsteps)
        print(" inc", inc, "tick at ", count)
    

    if name == "main": main()

  • On range -1, 0

    i get

    normalised_delta 1.0
    step_size 0.1
    Total steps 10.0
    Range [ -1 , 0 ]
    firstInc  -1.0 tick at  0.0
    start at  0.0 0.0
     inc -0.9 tick at  25.0
     inc -0.8 tick at  50.0
     inc -0.7 tick at  75.0
     inc -0.6 tick at  100.0
     inc -0.5 tick at  125.0
     inc -0.4 tick at  150.0
     inc -0.3 tick at  175.0
     inc -0.2 tick at  200.0
     inc -0.1 tick at  225.0
     inc -1.38777878078e-16 tick at  250.0
     inc 0.1 tick at  275.0
    

    How come the second line from bottom get this number ????

    mweerden : This is due to the inaccuracies of floating-point numbers and operations on computers. Specifically, 0.1 does not have a precise representation and with + you keep adding the error. If you use -1.0+9*0.1 the error is much smaller. (See http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems)

Metamodelling tools..

What tools are available for metamodelling?

Especially for developing diagram editors, at the moment trying out Eclipse GMF

Wondering what other options are out there? Any comparison available?

From stackoverflow
  • Although generally a UML tool, I would look at StarUML. It supports additional modules beyond what are already built in. If it doesn't have what you need built in or as a module, I supposed you could make your own, but I don't know how difficult that is.

  • Dia has an API for this - I was able to fairly trivially frig their UML editor into a basic ER modelling tool by changing the arrow styles. With a DB reversengineering tool I found in sourceforge (took the schema and spat out dia files) you could use this to document databases. While what I did was fairly trivial, the API was quite straightforward and it didn't take me that long to work out how to make the change.

    If you're of a mind to try out Smalltalk There used to be a Smalltalk meta-case framework called DOME which does this sort of thing. If you download VisualWorks, DOME is one of the contributed packages.

  • Your question is simply to broad, to get a single answer - due to many aspects.

    First, meta-modelling is not a set term, but rather a very fuzzy thing, including modelling models of models and reaching out to terms like MDA.

    Second, there are numerous options to developing diagram editors - going the Eclipse way is surely a nice option.

    To get you at least started in the Eclipse department:

    • have a look at MOF, that is architecture for "meta-modelling" from the OMG (the guys, that maintain UML)
    • from there approach EMOF, a sub set which is supported by the Eclipse Modelling Framework in the incarnation of Ecore.
    • building something on top of GMF might be indeed a good idea, because that's the way existing diagram editors for the Eclipse platform take (e.g. Omondo's EclipseUML)
    • there are a lot of tools existing in the Eclipse environment, that can utilize Ecore - I simply hope, that GMF builts on top of Ecore itself.
  • GMF is a nice example. At the core of this sits EMF/Ecore, like computerkram sais. Ecore is also used for the base of Eclipse's UML2 . The prestige use case and proof of concept for GMF is certainly UML2 Tools.

  • Meta-modeling is mostly done in Smalltalk.

    You might want to take a look at MOOSE (http://moose.unibe.ch). There are a lot of tools being developed for program understanding. Most are Smalltalk based. There is also some java and c++ work.

    Two of the most impressive tools are CodeCity and Mondrian. CodeCity can visualize code development over time, Mondrian provides scriptable visualization technology.

    And of course there is the classic HotDraw, which is also available in java.

    For web development there is also Magritte, providing meta-descriptions for Seaside.

  • I would strongly recommend you look into DSM (Domain Specific Modeling) as a general topic, meta-modeling is directly related. There are eclipse based tools like GMF that currently require java coding, but integrate nicely with other eclipse tools and UML. However there are two other classes out there.

    1. MetaCase which I will call a pure DSM tool as it focuses on allowing a developer/modeler with out nearly as much coding create a usable graphical model. Additionally it can be easily deployed for others to use. GMF and Microsoft's Beta software factory/DSM tool fall into this category.

    2. Pure Meta-modeling tools which are not intended for DSM tooling, code generation, and the like. I do not follow these tools as closely as I am interested in applications that generate tooling for SMEs, Domain Experts, and others to use and contribute value to an active project not modeling for models sake, or just documentation and theory.

    If you want to learn more about number 1, the tooling applications for DSMs/Meta-modeling, then check out my post "DSMForum.org great resources, worth a look." or just navigate directly to the DSMForum.org

Processing Linux SIGnals using Gambas

I would like to send a (as yet undetermined) SIGnal from a bash script to a Gambas program when a specific file has been changed.

How can I get my Gambas program to process this SIGnal?

From stackoverflow

IDebugControl::WaitForEvent works once then returns E_HANDLE

I'm trying to make a small tool that makes use of the Debugger Engine API, but I'm having very limited success.

I can get my IDebugClient and IDebugControl instances, and from there I am able to attach into an already running user process. I then enter a main loop where I call WaitForEvent, OutputStackTrace, SetExecutionStatus(DEBUG_STATUS_GO), and repeat. In essence this will be a very crude sampling based profiler.

Good so far..

My loop runs for one full iteration, I can see a stack trace being displayed and then the target process going back into a running state.

The problem I have is that on my 2nd iteration the call to WaitForEvent returns E_HANDLE ("The handle is invalid"). I cannot see in the documentation why this error should be returned. Does anyone know why this might be happening?

From stackoverflow
  • The problem turned out to be that I was compiling, linking, and running against an old version of the SDK. Now that I've upgraded my SDK to the latest version (which I presume is the version that the online docs refer to) I get behaviour that is at least consistent with the docs.

    I still have problems, but no longer this problem.

How can I list the missing dates from an array of non-continuous dates in Java?

I have a table of data sorted by date, from which a user can select a set of data by supplying a start and end date. The data itself is non-continuous, in that I don't have data for weekends and public holidays.

I would like to be able to list all the days that I don't have data for in the extracted dataset. Is there an easy way, in Java, to go:

  1. Here is an ordered array of dates.
  2. This is the selected start date. (The first date in the array is not always the start date)
  3. This is the selected end date. (The last date in the array is not always the end date)
  4. Return a list of dates which have no data.
From stackoverflow
  • You should be able to create a filtered iterator that provides this. Perhaps have the method for the iterator accept the start and stop date of your sub-collection. As for the actual implementation of the iterator, I can't think of anything much more elegant than a brute-force run at the whole collection once the start element has been found.

  • You could create a temp list and x it as needed.

    (Not actual Java. Sorry, my memory of it is horrible.)

    dates = [...]; // list you have now;
    
    // build list
    unused = [];
    for (Date i = startdate; i < enddate; i += day) {
        unused.push(i);
    }
    
    // remove used dates
    for (int j = 0; j < dates.length; j += 1) {
        if (unused.indexOf((Date) dates[j]) > -1) { // time = 00:00:00
            unused.remove(unused.indexOf((Date) dates[j]));
        }
    }
    
  • You can either create a list of all possible dates between start and end date and then remove dates which appear in the list of given data (works best when most dates are missing), or you can start with an empty list of dates and add ones that don't appear in the given data.

    Either way, you basically iterate over the range of dates between the start date and end date, keeping track of where you are in the list of given dates. You could think of it as a 'merge-like' operation where you step through two lists in parallel, processing the records that appear in one list but not in the other. In pseudo-code, the empty list version might be:

    # given   - array of given dates
    # N       - number of dates in given array
    # missing - array of dates missing
    
    i = 0;    # Index into given date array
    j = 0;    # Index into missing data array
    for (current_date = start_date; current_date <= end_date; current_date++)
    {
        while (given[i] < current_date && i < N)
            i++
        if (i >= N)
            break
        if (given[i] != current_date)
            missing[j++] = current_date
    }
    while (current_date < end_date)
    {
        missing[j++] = current_date
        current_date++
    }
    

    I'm assuming that the date type is quantized in units of a day; that is, date + 1 (or date++) is the day after date.

  • While the other answers already given look rather simple and enjoyable and hold some good ideas (I especially agree with the Iterator suggestion by Nerdfest), I thought I'd give this a shot anyway and code a solution just to show how I'd do it for the first iteration, I'm sure there's room for improvement in what's below.

    I also maybe took your requirements a bit too literally but you know how to adjust the code to your liking. Oh and sorry for horrible naming of objects. Also since this sample uses Calendar, remember that Calendar.roll() may not update the entire Calendar object in some cases so that's a potential bug right there.

    protected List<Calendar> getDatesWithNoData(Calendar start, Calendar end,
      Calendar[] existingDates) throws ParseException {
    
     List<Calendar> missingData = new ArrayList<Calendar>();
    
     for(Calendar c=start ; c.compareTo(end)<=0 ; c.roll(Calendar.DAY_OF_MONTH, true) ) {
    
      if(!isInDataSet(c, existingDates)) {
       Calendar c2 = Calendar.getInstance();
       c2.setTimeInMillis(c.getTimeInMillis());
    
       missingData.add(c2);
      }
     }
     return missingData;
    }
    
    protected boolean isInDataSet(Calendar toSearch, Calendar[] dataSet) {
     for(Calendar l : dataSet) {
      if(toSearch.equals(l)) return true;
     }
     return false;
    }
    
  • Start with this: what's a date? Is it GMT or local?

    If it's GMT, each day is the java.util.Date.getTime() value divided by 86400000. You can quickly run through your array, and add the resulting Long values to a TreeSet (which is sorted). Then iterate the TreeSet to find gaps.

    If a date is local time, you'll have to add/subtract an appropriate offset before dividing.

mod_python.publisher always gives content type 'text/plain'

I've just set up mod python with apache and I'm trying to get a simple script to work, but what happens is it publishes all my html as plain text when I load the page. I figured this is a problem with mod_python.publisher, The handler I set it too. I searched through the source of it and found the line where it differentiates between 'text/plain' and 'text/html' and it searches the last hundred characters of the file it's outputting for ' in my script, so I put it in, and then it still didn't work. I even tried commenting out some of the code so that publisher would set everything as 'text/html' but it still did the same thing when I refreshed the page. Maybe I've set up something wrong.

Heres my configuration in the httpd.conf

< Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs">
SetHandler mod_python
PythonHandler mod_python.publisher
PythonDebug On
< /Directory >

From stackoverflow
  • Your configuration looks okay: I've got a working mod_python.publisher script with essentially the same settings.

    A few other thoughts:

    • When you tried editing the publisher source code, did you restart your web server? It only loads Python libraries once, when the server is first started.

    • Publisher's autodetection looks for a closing HTML tag: </html>. Is that what you added? (I can't see it in your question, but possibly it just got stripped out when you posted it.)

    • If nothing else works, you can always set the content type explicitly. It's more code, but it's guaranteed to work consistently. Set the content_type field on your request to 'text/html'.

    For example, if your script looks like this right now:

    def index(req, an_arg='default'):
        return some_html
    

    it would become:

    def index(req, an_arg='default'):
        req.content_type = 'text/html'
        return some_html
    

Need to get specific indexes out of a PHP array

I made an array in PHP which holds a bucnh of unix timestamps.

I'm trying to make a function that will return an array containing the indexes of the 3 largest numbers in that array.

For instance, if the largest numbers are located at indexes 3,5 and 8

And if the largest is 5, second largest is 8 and smallest of the three is number 3, I want an array that holds the values (5,8,3) in that order.

And frankly, I don't have a clue how to pull this off. Does anybody know how to do this?

From stackoverflow
  • Simon posted the simple and probably good-enough performing method.

    The other option, only if you have a really large array, is to scan through the array and keep track of the indexes of the three highest values you see. This is O(n), but (especially since its in interpreted PHP code, not a compiled built-in function), probably slower for all but the largest of arrays.

  • You could use asort to sort the array and maintain index and then use slice along with the 4th parameter, again to maintain the index, to grap the top x number of elements you are after, and finally use array_keys.

    There may well be a quicker way, but it's just to show there are plenty of PHP array functions to help you achieve the effect you're looking for.

    Vordreller : Thanks, this solved my problem with one little exception. The function that gave me the 3 largest values as the first 3 numbers in the new array needs to be arsort, instead of asort
    Simon : Ah, glad it helped. Alternatively you could have used array_slice to slice the other end of the array - but it's the same difference really.
  • In pseudo-code:

    function select(list[1..n], k)
         for i from 1 to k
             maxIndex = i
             maxValue = list[i]
             for j from i+1 to n
                 if list[j] > maxValue
                     maxIndex = j
                     maxValue = list[j]
             swap list[i] and list[maxIndex]
         return list[k]
    
    newarray[] = select(array, 1);
    newarray[] = select(array, 2);
    newarray[] = select(array, 3);
    
  • In PHP code:

    function threeLargest($array){
     krsort($array, "SORT_NUMERIC");
     $return[0] = $array[0];
     $return[1] = $array[1];
     $return[2] = $array[2];
     return $return;
    }
    
    Vordreller : this did not work. For some reason, non of the sorting functions for arrays seem to be working in my debugger...