ANOVA possible?

Question: I have 2 groups (variable = condition) Control, Experimental - I have coded them as 1,2 in SPSS.
I have measured these at three time points - pre, ret1 ret2 (currently just a string variable)
I have one outcome variable I am interested in - X (I have 1-2 more that I'm also interested in)
Is it possible to run an ANOVA comparing the two groups for X at each time point? Do I need to code the time point variable also?
I would also like to compare groups seperately, so mean of Control X at pre, ret1 and ret2 and then same for Experiemntal X
Later I'm interested in the relationship between X and Y and/or Z, best method to use here?
Apologies for all the question and simple nature, I am new to stats and working my way through it. So far I ran an independent T-Test to compare control and Experimental X at one time point by using the Select Case function in SPSS - seems to me there is a better and more correct way than using multiple t-tests
Thanks for any help here! Jack
Answer: Just want to make sure I've got your design down pat.
What you're looking at is a mixed-design wherein all of your participants provide data (X) at three measurements (pre, ret1, ret2), and your participants can be split on a variable (condition).
The way to examine this is Analyze > General Linear Model > Repeated Measures.
SPSS will then ask you to input your repeated variable. This is your three measurements. You will need to give it a name, and tell SPSS how many levels to expect (3). Once you click "Add", click on "Define" to tell it what your model actually looks like.
What you will get now is a new window that wants Within-Subjects Variables and Between-Subjects Factors.
Within-Subjects Variables is where your data (X) goes. You've told SPSS that you have three levels, so it expects three things: pre, ret1, and ret2.
The Between-Subjects Factor is where you tell SPSS how you want your data split. This is where you give it condition which will make it possible to see if there is a relationship between condition and pre/ret1/ret2.
Really important note: When you set up your variables, make sure you set the "Measure" for the different variables. Specifically, your condition variable needs to be set as "Nominal" (though SPSS should infer this).
As for comparing groups separately, SPSS has a really nice function which lets you split your data. To get this, select Data > Split File. Save your data file before doing this, just in case. Make sure to select "Compare groups", and move the variable you're interested in (condition, sex, whatever) into the box. Select "Sort the file by grouping variables". When it is done, the data window in SPSS should have "Split by condition" in the bottom right.
Now, when you re-run your ANOVA, SPSS will give you two sets of output. One for when condition = 1, and one where condition = 2. To undo this, you will need to go to Data > Split File and select "Analyze all cases, do not create groups" and hit "OK". If you don't do this, SPSS will just keep working with a split file.
When you say relationship between X, Y and/or Z this makes me think of Pearson's r, but only because you used the word relationship, and because I know nothing else about X, Y and Z.
Answer: As an alternative to a standard repeated-measures ANOVA, you could also specify a mixed model and SPSS will output an ANOVA table for your fixed effects. You could specify the model by going to Analyze > Mixed Models > Linear. Put your subject ID variable in subjects and the time point variable in repeated. I would specify an unstructured covariance structure (unless you have a working theory on how the time points will be related, in which case maybe something like toeplitz?). Click continue and specify the model as you would normally (treatment group and time point), but make sure you click fixed and add the factors to the model, and include the intercept.
To compare the groups at each time point, you can click on EM Means, add treatment and time point, and check off compare main effects.
For assessing the effect of the time points within each treatment, you may want to run a simple effects model, i.e. splitting the data in half and running the model I just explained (minus the treatment group as a factor) on each half of the dataset.

ANOVA possible?


nginx won't respond to monit

Question: Although EngineX is running, monit can't seem to figure it out.
Here's my monit log:
<code>[PDT Apr 13 02:19:19] error    : HTTP error: Server returned status 400
[PDT Apr 13 02:19:19] error    : 'nginx' failed protocol test [HTTP] at INET[localhost:80] via TCP
[PDT Apr 13 02:19:19] info     : 'nginx' trying to restart
[PDT Apr 13 02:19:19] info     : 'nginx' stop: /etc/init.d/nginx
[PDT Apr 13 02:19:20] info     : 'nginx' start: /etc/init.d/nginx
</code>
The monitrc file contains the following configuration:
<code>if failed port 80 protocol http and request '/ping.txt' # check for response
     with timeout 20 seconds
     then restart
</code>
I can access the file through lynx http://localhost:80/ping.txt without any problems.
Why would monit have trouble requesting the file when nginx is running just fine?
Answer: Nginx older than 0.6.15 used to return 400 to requests like GET http://www.domain.com HTTP/1.0. I assume, you have updated Nginx in the last two years? If yes, please try to raise Nginx debugging level and post logs here.
Comment: Running nginx-0.8.35
Answer: The issue turned out be the cause of version discrepancy.
My original monitrc file was intended for v5+, but the box it was running on used Monit 4.1

nginx won't respond to monit


Tuning of Accidentals & Scales

Question: In his Book Basics, Simon Fischer wrote in a footnote that:

The exact tuning of a sharp or a flat depends on the key, style and
  character of the music. For example, Bb as the tonic of Bb Major is
  higher than the Bb as the third of G minor. Tuning also depends on
  whether it is a single or a double stop, and what notes other
  instruments are playing

So my first question is, does this imply that accidentals need not be a particular pitch all the time? it is subject to other factors? There is no set frequency for an accidental unlike a normal tone, say for example: A=440? However, this matter is further complicated on another part in the same book; where it is written that:

The exact tuning of major, minor, augmented and diminished intervals
  depends, to a certain extent, on individual taste and the character of
  the music. For example, some players prefer wider (brighter) major
  thirds, or narrower (darker) minor thirds, than others. But perfect
  intervals- fourths, fifths and octaves- are either in tune or not, and
  cannot be adjusted according to taste.

So this time in stead of accidentals the focus is on intervals. In this case, if the piece starts with an accidental, i.e. Bb then there cannot be any variation for it. But this also implies that a natural note can be sharpened or flattened. In Bb Major, one might want to slightly sharpen the D note so that it resolves more easily into Eb, that is to say a wider major third.
So, my final question in relation to all of this is that, does this mean no note in music has a particular pitch/frequency that must absolutely be maintained? it is subject to the construction of the piece and other factors?
Answer: There may be confusion between 'ordinary' notes (C, E, A -white keys on piano) and the # and b ( black keys).Don't think that 'ordinary' notes are any different from 'accidental' notes. When tuned to A=440Hz, then A will be that. Since, on violin, it's an open string, it can't be bent up or down from that, can it? I think what's trying to be said is that intervals rather than particular notes themselves can be (and are) stretched and squashed, particularly on unfretted stringed instruments, like violin. On fretted instruments it's still possible to move an interval slightly, but something such as a keyboard is set at 12EDO.Mentioning the Bb, it's its relativity to another note that's deemed important. In Bb, since it's the root, it will be pretty sacrosanct, whereas playing it as a minor third in G minor may call, to the player's discretion, for it not to be exactly the same pitch as the former Bb.
Answer: There are a number of reasons why notes cannot be said to have exact pitches...

As well as just intonation which represents the purest, most consonant tuning possible, many different systems of temperament are possible. Equal temperament is particularly common these days, but in many situations musicians will still gravitate towards something closer to just intonation. This may mean that in a piece that modulates (or plays chords that don't come from one key only), the pitches of particular notes may shift slightly.
Musicians are not limited to the chromatic, diatonic, or any other scale; they are free to play any pitch they want for artistic reasons. This might be seen as microtonality in the case of the use of a fixed set of pitches outside the chromatic scale; however, some styles (blues being an obvious example) reject the idea of using only a fixed set of pitches, also using ranges of pitch in certain areas of the octave through which notes can be bent and inflected.
You mention A=440, but that's not fixed; there is a wide range of different concert pitches that are used.

Does this mean no note in music has a particular pitch/frequency that
  must absolutely be maintained? it is subject to the construction of
  the piece and other factors?

Yes. None of the reasons I've given are specific to accidentals.
Comment: A=440Hz may not be the industry standard, but it's probably the most prevalent. But - once any group of musos has tuned to a particular A, that becomes the datum point, at that time, surely?
Comment: @Tim I only mentioned it as the OP stated *There is no set frequency for an accidental unlike a normal tone, say for example: A=440* - I thought he might mean he was seeing it as a strict absolute. Maybe I misinterpreted; I've relegated it to being my third point. In any case, in some keys the '440' A might itself be an accidental, rather than a 'normal note'.
Answer: I would answer your final question with yes: there are no absolute pitches or frequencies that must be maintained for particular notes, unless you choose to do so.  How to decide what frequency a note should have depends upon taste, style, and practicality.  And it's not a simple thing to decide.
Nowadays, the most "official" frequencies of pitches in Western music are considered to be those dictated by 12 tone equal temperament- that is, the semitones are separated by a factor of the twelfth root of two.  Most modern keyboard instruments are tuned this way.
https://en.wikipedia.org/wiki/Equal_temperament#Twelve-tone_equal_temperament
But as your quotes point out, this is not the whole story, especially for instruments where intonation is flexible- musicians tend to bend pitches to fit their tastes and perceptions.  I actually bend my major and minor thirds in the opposite direction that Fischer suggests: he is obviously going for more contrast between major and minor thirds, not more consonance.  But this is a matter of taste.

Tuning of Accidentals & Scales


Como quebrar a linha no prompt do VisuAlg?

Question: Minha saída:

Nome: Fulano de TalIdade: tantos anos

Como quebrar a linha no prompt do VisuAlg?
Para ficar assim:

Nome: Fulano de Tal
Idade: Tantos Anos
Comment: Se era Visual Alg, porque escreveu Visual Log na pergunta?
Comment: Poderia passa o link para baixar esse visual log
Comment: http://www.guanabara.info/2007/09/visualg-v25/
Answer: Para quebrar a linha em uma saida de texto utilize a função escreval(). A lista completa das funções, operadores e demais referências pode encontrar nesse link
algoritmo "quebra_de_linha"
var
   idade : inteiro
   nome : caractere

inicio
      escreval("Informe o nome")
      leia(nome)

      escreval("Informe a idade")
      leia(idade)

      escreva("")
      escreva("")

      escreval("nome: " + nome)
      escreval("idade: ", idade)

fimalgoritmo

saída:
Comment: ola agradeço pela sua ajuda me ajudo muito vlw
Answer: simples
var
nome:caracter
idade:inteiro

inicio
escreva("seu nome é: ")
leia(nome)
escreval(" ")
escreva("sua idade é: ")
leia(idade)
escreval(" ")

fimalgoritmo


Deserialize a polymorphic map with Jackson, where Type information is given within the string key

Question: Assume a class "Json2JavaModel"
public class Jason2JavaModel {

    public String someAttribute;

    public Map<String, Representation> representations;

    public String getSomeAttribute() {
        return someAttribute;
    }

    public void setSomeAttribute(String someAttribute) {
        this.someAttribute = someAttribute;
    }

    @JsonProperty(value = "_embedded")
    public Map<String, Representation> getRepresentations() {
        return representations;
    }

    public void setRepresentations(
            Map<String, Representation> representations) {
        this.representations = representations;
    }
}

Where Representation is a common base interface for different JSON representations. On serialization side, there's no problem, because Jackson knows the actual Java type. But deserialization has to be done polymorphic. The type information is stored within the map key for every entry (not as canonical Class name but something unique). So it would be possible to provide a key string <-> destination class config. Is there a way to tell jackson, that it should use the map key as type definition to deserialize the entries value?
Best regards,
Marius
Answer: You are looking for an ObjectMapper. Have a look at my answer to JSON POJO consumer of polymorphic objects for an example.
Comment: Hi OldCurmudgeon and thx for the reply. I looked into your solution and it would work, if we wrapped the Map into it's own class like EmbeddedRepresentationsContainer. Then we could use your approach for a customized EmbeddedRepresentationsContainerDeserializer. Alas this structure change would mean to break our current API or introduce more methods.  If by any means possible, we'd like to stick with the map and teach Jackson how to deserialize it correctly.


[fixed]Trigger an ajax Submit Form button by clicking on a div

Question: I've a form inside a custom template and I would like to trigger the submit button clicking on one div. Then the function submitForm sends the form-data to a webservices using Drupal httpClient.
The twig template is like:

 This is the div containing some stuff that I want to click to submit the form.

<form id="form-id">
{{ form.elem }}
{{ form.actions.submit }}
</form>

I tried with a simple jQuery function like:
jQuery(function($) {
    $(document).ready(function () {
        $('#rectangle-div').on('click', function () {
            var wrapper = $('#form-id');
            $('.form-submit', wrapper).trigger( "click" );
        });
    });
});

I tried also to write a selector like:
$('button[id^="edit-mod-submit"]', wrapper).trigger( "click");

if I try with a simple submit like
$('form-id').submit();

the js code submits the form but refresh the page avoiding ajax and furthermore doesn't send data to the WS:
(if I use the submit button the data goes to the WS).
Any other ideas?
UPDATE #1: FIXED
I added inside the ajax option of my submit button the event 'click' and now, using jQuery, I am able to send the post request to the WS. The form is like:
 $form['actions']['submit'] = array(
            '#type' => 'submit',
            '#value' => $this->t('Save'),
            '#button_type' => 'primary',
            '#ajax' => [
                'callback' => '::setMessage',
                'event' => 'click',
            ],

        );

The jQuery function is:
jQuery(function($) {
        var wrapper = $('#form-id');
        $('.form-submit', wrapper).trigger( "click" );
    });

Now I should intercept the response coming from the webserver and then execute a JS script, but these are other questions :)
Comment: Why not add your "fixed" as an answer?
Comment: Is this copied and pasted from your code? The `` is missing a " after Rectangle which would cause the rest of the code to not work correctly
Comment: @ColinShipton it is not the original code, but I corrected the error.
Answer: Your code is not triggering an AJAX call, it will just submit the form normally via JavaScript rather than pressing a button, etc. If you want submit the form via AJAX you will need something like:
  var url = $('#form-id').attr('action');
  $.getJSON(url, {
    field1: 'data1',
    field2: 'data2'
  }).done(function(data) {
    console.log(data);
  });

Place this inside $('#rectangle-div').on('click', function () {
Comment: Ok I tried this piece of code but it cannot enter inside the done(function) and the ws doesn't receive any post request. Furthermore: why do I use Key,Value pairs inside the function if I use the form to send data to the webservice?
Comment: You need to use key/value pairs as you're not submitting the form, you're replacing the submit with an AJAX call, etc. can you post the whole code rather than just snippets?
Comment: Also, the getJSON function also has a .fail (i.e. `.fail(function( jqxhr, textStatus, error ) {`
Comment: the error is "parsererror: SyntaxError: JSON.parse: unexpected character at line 3 column 1 of the JSON data".
The original code is very similar, but it contains more data. In any case now I can contact the webserver using jQuery, but I'm not able to receive the response. I'm going to update the question.
Comment: Sounds to me as if the webservice is not returning JSON


ListVIew in Fragment Very Laggy

Question: I have a ViewPager with 3 fragments. The rightmost fragment has a ListView inside it. The problem is that on two weaker phones I tested this on, it works seemingly smooth and not laggy. However, when I test it on my Note 3, the transition from the middle fragment to this one is very laggy and over 300 frames are skipped according to the logcat. Also, if I lock the phone and then unlock it back onto the ListView it is very laggy to scroll or do anything, unless I swipe left twice to the leftmost fragment. This is my onCreateView, onAttach and onStart methods as well as the adapter below.
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.fragment_c,container,false);
    mainalyout = (LinearLayout) view.findViewById(R.id.linear_layout_listview);
    listView =  (ListView) view.findViewById(R.id.followed_cities);
    horizontal_scroll = (HorizontalScrollView) view.findViewById(R.id.horizontal_scroll_view);
    swipe = (LinearLayout) view.findViewById(R.id.scroll_up);
    layout =  (LinearLayout) view.findViewById(R.id.scroll_view_layout);
    swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
    return view;

}
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    context = MyApp.getContext();
    session = new SessionManager(context);
    userString = session.getUserDetails();
    username = userString.get("username");
    viewPager = (ViewPager) activity.findViewById(R.id.pic_pager);
    parent = (FragmentActivityTesting) activity;
    username = userString.get("username");
    queue = Volley.newRequestQueue(context);
    followed_cities = session.getFollowedCities();
    try {
        citysearcher = (citysearcher) activity;
    } catch(Exception e) {}
    try {
        slideshowready = (slideshowready) activity;
    }catch (Exception e) {}

}
@Override
public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
    super.onStart();
    swipeRefreshLayout.setOnRefreshListener(this);
    if (followed_cities.contains("")) {
        followed_cities.clear();
    }
    if (getActivity().getIntent().getStringExtra("launcher").equals("add")) {
        get_followed(username);
    }
    if (followed_cities.isEmpty()) {
        followed_cities.add(new CityShort("","NONE"));
        ArrayAdapter<CityShort> adapter = new EmptyAdapter();
        adapterr = adapter;
        listView.setAdapter(adapterr);
    }
    else {
        ArrayAdapter<CityShort> adapter = new MyListAdapter();
        adapterr = adapter;
        listView.setAdapter(adapterr);
    }

My adapter :
private class MyListAdapter extends ArrayAdapter<CityShort> {
    public MyListAdapter() {
        super(getActivity(), R.layout.followed_item, followed_cities);

    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        itemView = convertView;
        if (itemView == null) {
            itemView = getActivity().getLayoutInflater().inflate(R.layout.followed_item, parent, false);
        }
        TextView city_name = (TextView) itemView.findViewById(R.id.followed_city_txt);

        final String curr_city = followed_cities.get(position).getCityName();
        city_name.setText(curr_city);
        city_name.setTag(followed_cities.get(position).getCityId());

        if (curr_city.length() > 15) {
            city_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        }
        listView.setOnTouchListener(swipeDetector);

        return itemView;

    }
    @Override

    public int getViewTypeCount() {

        return getCount();
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }
}

Answer: I saw you had 2 listviews (findViewById(R.id.followed_cities) and findViewById(R.id.horizontal_scroll_view)). The horizontal listview is not well-implemented for recycling items.
Consider a better official support solution with RecyclerView. It supports both vertical and horizontal and provides better performance (as least the number of frames skipped will be less than current listview).
Comment: Horizontal Scroll View is only loaded once, it isn't repopulated, it simply displays icons to the user.
Comment: The `getView()` method is also having performance problem also. Consider have ViewHolder pattern implemented. @mankee
Comment: http://developer.android.com/training/improving-layouts/smooth-scrolling.html ViewHolder pattern guide.
Answer: Two things that I see:
1) You have several LinearLayouts. I'm not certain what your xml layout looks like, but if LinearLayouts are nested they can certainly reduce visual rendering time. RelativeLayouts are more efficient(though I do use LinearLayouts when I'm first writing the xml file, because it's a little less time consuming for me as a general setup, then to later go back and update to using RelativeLayout).
2) You're doing all of these calculations on your MainThread, which is also Android's GPU essentially. You might be interested in processing all of this on a separate thread, this will certainly reduce rendering time. Check out "extends AsyncTask" and "Intent Service".
Comment: Which calculations are you referring to? Don't all view changes have to be done on the main thread since you can't access the UI elements from outside it?
Answer: Change these things and try if works..
ViewHolder holder = null;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = getActivity().getLayoutInflater().inflate(R.layout.followed_item, parent, false);
       city_name = (TextView) itemView.findViewById(R.id.followed_city_txt); //Make this class level variable
        convertView.setTag(holder);
    } else {
            holder = (ViewHolder) convertView.getTag();
    }

Answer: Replace your adapter with this

package com.munk.gaanasync;

import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.view.LayoutInflater;

import java.util.zip.Inflater;

private class MyListAdapter extends ArrayAdapter<CityShort>
{
    public MyListAdapter()
    {
        super(getActivity(), R.layout.followed_item, followed_cities);
       mInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        itemView = convertView;
        if (itemView == null)
        {
            holder = new ViewHolder();
            itemView = mInflater.inflate(R.layout.followed_item, parent, false);
            holder.city_name = (TextView) itemView.findViewById(R.id.followed_city_txt);
            itemView.setTag(holder);
        }
        else
        {
            holder = (Viewholder) itemView.getTag();
        }

        final String curr_city = followed_cities.get(position).getCityName();
        holder.city_name.setText(curr_city);
        holder.city_name.setTag(followed_cities.get(position).getCityId());

        if (curr_city.length() > 15)
        {
            holder.city_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        }
        listView.setOnTouchListener(swipeDetector);

        return itemView;
    }

    public static class ViewHolder
    {
        public TextView city_name;
    }

    @Override

    public int getViewTypeCount()
    {

        return getCount();
    }

    @Override
    public int getItemViewType(int position)
    {

        return position;
    }

    private Inflater mInflater;
}

Comment: error in this line `mInflater = getActivity().getLayoutInflater();` , required Java.util.zip.inflater, found android.view.LayoutInflater
Comment: replace it with mInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

ListVIew in Fragment Very Laggy


Interpretation of this Chord Progression

Question: Marshmello and Anne-Marie released a song called "Friends" this Friday. I was sitting at the piano and looked up the chords for the song to sing it.
The song has the chord progression Amin-C-F-E for the most part. Please tell me what you think of my interpretation, I'm trying to get better at this.
I interpreted this i-III-VI-V as a progression in a minor (with a dominant, borrowed from Amajor) that has a constant tonic-dominant movement.
The III chord substitutes as a dominant (at least from what I read, it can substitute as a dominant if it resolves to a tonic, please feel free to give any opinions on that).
This substitute Dominant resolves to the VI chord, a tonic/submediant/substitute tonic (which one is the correct way?).
The following V is borrowed from the Amajor/Aharmonic Minor scale.
Also, the large amount of halfsteps/shared notes from chord to chord is great, it makes it sounds so fluid.
What do you think of this interpretation?
Answer: All of your ideas about tonic/dominant substitutes are fine, but it's actually much simpler than that.
III chords often move to VI; it's really that easy! III to VI is a descending fifth, so it's a part of the circle of fifths progression.
In fact, the i III VI progression is rather common; check out the theme to The Office (US version), which begins with these three exact chords, just in major.
The VI then moves to V, which is another very common move, especially in popular music.
And you're absolutely right about the voice leading: with so many commons tones and motions of a single step between chords, the chord progression sounds very smooth.
Answer: As Richard says! However, the dominant isn't borrowed from anywhere! It's there in nearly all minors - harmonic and melodic, which both possess G#, but not natural minor. The V>I, or sometimes, as here, V>i is so common in music. Look at just about any chord sequence, and a chord will often be followed by a chord up a fourth. Effectively V>I, even though they may not be the V and I of the key.
This is where ii>V>I in jazz is seen, again, it may not be Dm>G>C in key C, but could be E>Am>Dm, all jumps of fourths, another way of looking at chords moving up from a preceding V to I (or i).


Criar uma estrutura a partir de um xml

Question: Estou usando o Framework SWXMLHash para ler o XML e preciso criar de alguma forma a seguinte estrutura a partir do XML
Estrututa a ser criada:
Catalogo
   Book1
    Autor = Gambardella
    Genre = Computer
   Book1
   Book2
    Autor = Ralls
    Genre = Fantasy
   Book2

XML que estou usando
<root>
      <catalog>
        <book id=\"bk101\">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications with XML.</description>
        </book>
        <book id=\"bk102\">
          <author>Ralls, Kim</author>
          <title>Midnight Rain</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-12-16</publish_date>
          <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
      </catalog>
    </root>

Código em Swift
for retorno in dados["root"]["catalog"]["book"]{
 //
}

Answer: Aparentemente o Catalog é um array de books, correto? Se você já tem o XML como um dicionário e a classe Book criada, uma maneira de ler o dicionário seria a seguinte:
for rawBook in dados["root"]["catalog"] {
    guard let author = rawBook["author"] as? String,
          let genre = rawBook["genre"] as? String else {
              continue
          }
    let book = Book(autor: author, genre: genre)
    //...
}

Criar uma estrutura a partir de um xml


How to display field collection items left to right in views grid

Question: I created a content type as follows:
Content Type > field collection field > accepts unlimited values.
Field collection field > text field, Image field. > Each accepts one value.
Add content > content type x > added 5 field collection items. So I have one content with 5 field collection items. Each item has one text field value and one image value.
Views:
Created views grid > number of columns equal to 3.
Fields > added field collection field.

I see all the items in a single column. I would like to display items left to right. How can I do it?
Answer: Click on Content: MyProducts (within fields).
Then scroll down a bit, until you see MULTIPLE FIELD SETTINGS, click on it to expand it and then uncheck the Display all items in the same row box.


Does one need to adjust for multiple comparisons when using MANOVA?

Question: There are 10 dependent variables and 2 groups. When I wanted to compare 10 dependent variables between two groups, I performed MANOVA.
I wonder if there is a multiple comparison problem in that case. Should I have performed Bonferroni correction because of 10 dependent variables? Or is Bonferroni useless because MANOVA just does analysis once?
Comment: I think you mean *dependent variables* when you say "independent variables". A dependent variable is a response or outcome, whereas an independent variable is an explanatory or predictor variable.
Comment: You're right, I made mistake. So, now I correct the word.
Answer: There is no need for correction.
Think about it like that: you could conduct 10 independent ANOVAs for each of the 10 dependent variables (in case of only 2 groups ANOVA reduces to t-test, but it does not matter for this question). Then you would get 10 different p-values, and certainly would need to correct for multiple comparisons, e.g. by applying Bonferroni correction. But if you use MANOVA, then you get only one single p-value and so there is no need for any corrections. The test is done by considering all 10 variables together, so there is no "multiple testing" (meaning that you are not doing any test "multiple times").
Comment: I think you mean *dependent variables* when you say "independent variables". A dependent variable is a response or outcome, whereas an independent variable is an explanatory or predictor variable. This answer is correct; in addition, you might want to talk about whether or not corrections for multiple testing are needed for 10 follow-up ANOVAs if the initial MANOVA is significant, though.
Comment: @gung: Yes, you are right: I was confused by the same terminological mistake in the original question. I will correct it now. Regarding your additional question: actually I don't know what the best practice is. I would be very interested myself if you could briefly explain.
Answer: There's no need to adjust the alpha level of the MANOVA test itself, as amoeba noted. However, a significant MANOVA doesn't allow you to make statements about any of your 10 DVs--that would require conducting individual t-tests, which would require correcting for multiple comparisons.


Read view field value in VBO action

Question: I have a view grid that has custom actions to perform based on selected grid row. To read the view fields from the grid dpm-ed the below command:
dpm($this->view->result);
The above command returns:

I am trying to specifically read the "field_comment_id_comment_field_data_nid" and I get null every time:
 dpm($this->view->result[0]->field_comment_id_comment_field_data_nid);

Update: I modified the dpm and I see the value correctly. Unsure of how to loop through the result array. still trying.
Comment: You need to pay a bit more attention to the debug output - `$this->view->result` is an array
Comment: `ResultRow` doesn't have a `get` method. The output says `field_comment_id_comment_field_data_nid` is a public property of the object so you can access it directly
Comment: I tried the below code: dpm($this->view->result->field_comment_id_comment_field_data_nid->Value) to read the value directly and I get null
Comment: can i get some more help? I'm still seeing null on my end :( newbie here..
Answer: Alright. Below solution works:
foreach($this->view->result as $result)
  dpm($result->field_comment_id_comment_field_data_nid);
}

thanks @clive for pointing on the array thing.

Read view field value in VBO action


mysql stored procedure syntax error

Question: Ok, this is only the second stored procedure I've written.  I think you'll get the idea, I'm trying to close a credit line, and all invoices, charges, notes, etc with it.  But I get a syntax error.
The goal is to CALL close_account_proc(398985994)
DELIMITER $$

CREATE
    PROCEDURE `cc`.`close_account_proc`(cid INT)
    @uid_usr := uid_usr FROM credit_acc WHERE type_acc = 'init' AND credit_used_acc = cid;
    UPDATE credit_acc SET status_acc = 'closed', void_date_acc = NOW() WHERE credit_used_acc = cid;
    UPDATE payment_acc SET status_acc = 'voided', void_date_acc = NOW()  WHERE creditid_acc = cid;
    UPDATE sbal_sbl SET status_sbl = 'voided', void_date_sbl = NOW()  WHERE credit_used_acc = cid;
    INSERT INTO notes_not SET uid_usr = @uid_usr, initials_not = 'SYS',status_not = 'complete', date_not = NOW(), text_not = 'Closed credit line '.cid;
    UPDATE invoices_inv SET status_inv = 'voided', void_date_inv = NOW()  WHERE credit_used_acc = cid;
    BEGIN
    END$$

DELIMITER ;

So, anway, I get this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@uid_usr := uid_usr from credit_acc where type_acc = 'init' and credit_used_acc ' at line 5.
Any ideas?
Answer: DELIMITER $$

CREATE PROCEDURE `cc`.`close_account_proc`(cid INT)
BEGIN
    /* Check that it's what you wanted */
    SELECT  uid_usr
    INTO    @uid_usr
    FROM    credit_acc
    WHERE   type_acc = 'init'
            AND credit_used_acc = cid;

    UPDATE credit_acc SET status_acc = 'closed', void_date_acc = NOW() WHERE credit_used_acc = cid;
    UPDATE payment_acc SET status_acc = 'voided', void_date_acc = NOW()  WHERE creditid_acc = cid;
    UPDATE sbal_sbl SET status_sbl = 'voided', void_date_sbl = NOW()  WHERE credit_used_acc = cid;

    /* Check that it's what you wanted */

    INSERT
    INTO    notes_not (uid_usr, initials_not, status_not, date_not, text_not)
    VALUES  (@uid_usr, 'SYS', 'complete', NOW(), CONCAT('Closed credit line ', cid));

    UPDATE invoices_inv SET status_inv = 'voided', void_date_inv = NOW()  WHERE credit_used_acc = cid;
END
$$

DELIMITER ;

Answer: Hmm, im no expert at stored procedures, but isn't it.
CREATE PROCEDURE `cc`.`close_account_proc`(cid INT)
    BEGIN
        // your stuff
    END$$

Comment: Yes, you're correct, but there was a lot more wrong with it :)

mysql stored procedure syntax error


relative paths for accessing rails assets

Question: I'm stuck! xD I've been working on a Rails project and am having trouble accessing my assets using relative paths. A friend of mine is working on the html/css side of things while I'm handling the controllers and models. My friend recently gave me a batch of files structured in the following way:
app/assets/images/*.jpg
app/assets/stylesheets/*.css
app/assets/javascripts/*.js
app/assets/fonts/*.* (+some more css files in here)

Within my app/views/layouts directory, I have a layout named final.html.erb which is used for my entire webapp. I also have 1 page(html body content) that I'm trying to render with this layout in app/views/pages named final_page.html.erb  ... the necessary routing is in place for the page to load; however, it only loads the context of the final_page.html.erb (no images, styling, or fonts). When I go to the console and type "rails server" and visit localhost:3000, the page shows up ... naked lol. The console outputs the following:

Started GET "/" for 127.0.0.1 at 2012-07-28 21:15:02 -0700
Connecting to database specified by database.yml
Processing by PagesController#final_page as HTML
  Rendered pages/final_page.html.erb within layouts/final (8.4ms)
Completed 200 OK in 82ms (Views: 81.0ms | ActiveRecord: 0.0ms)

Started GET "/assets/stylesheets/style.css" for 127.0.0.1 at 2012-07-28 21:15:04 -0700

ActionController::RoutingError (No route matches [GET] "/assets/stylesheets/style.css"):
  actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.6) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.6) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.6) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.6) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.6) lib/rails/engine.rb:479:in `call'
  railties (3.2.6) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.6) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

  Rendered /var/lib/gems/1.9.1/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (2.0ms)

I think that the problem is that I'm trying to access files using relative paths. My layout file looks like this:
<!doctype html>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="viewport" content="width=device-width">
<title>:: Final ::</title>
<link rel="stylesheet" type="text/css" href="../../assets/stylesheets/style.css">
<link href="../../assets/stylesheets/desktop.css" rel="stylesheet" type="text/css" media="only screen and (min-width:769px) and (max-width:1000px)">

  <%= yield %>

Also, within the body of my final_page.html.erb file, I try to access images using relative paths as well...like so:

My friend wrote most of this html, and he has 0 experience working with rails. I decided to change the requests for assets like so:
From:
<link rel="stylesheet" type="text/css" href="../../assets/stylesheets/style.css">

To:
<%= stylesheet_link_tag "application.css" %>

From:

To:
<%= javascript_include_tag "application.js" %>

From:

To:
<%= image_tag "mainImg.jpg"  %>

This somewhat helps, as the images load, and very little of the styling comes through; however, it's way off from what it's supposed to look like. I'm thinking it's because my friend makes relative calls within the css files themselves:
body {
    background-image: url(../images/bg.jpg);
    background-repeat: repeat;
}

I've tried replacing these with url(<%= asset_path 'bg.jpg' %>) etc...
but it has no effect. I've tried so many things, and have read so many posts. I'm wondering if there's a way that Rails will allow me to use relative paths. I've tried:
config.assets.enabled = false

but that doesn't help ... please ... what am I doing wrong? I don't think my friend will want to stop using relative paths for his work, as it is how he does things. The site fires up well outside of Rails, but I need it to work with Rails for my webapp. Any advice would be greatly appreciated. Thank you for having the patience for reading all of this.
P.S. I'm using Ruby 1.9.3, and Rails 3.2.6
Answer: Try to use /assets/style.css not /assets/stylesheets/style.css and so on.
If there is such structure:
app
  assets
    stylesheets
    javascripts

Then to get access to files in app/assets or in app/assets/any_folder you should use path /assets/file.
Updated
Here try:
body {
    background-image: url(bg.jpg);
    background-repeat: repeat;
}

Or:
body {
    background-image: url(/assets/bg.jpg);
    background-repeat: repeat;
}

Comment: This actually solved 50% of the problem xD thanks a lot! However, the styling is still off. I'm not sure if it's because the css files are using relative paths or? :( Will I have to go into the css files and change the paths to images it refers to? like:

body {
    background-image: url(../images/bg.jpg);
    background-repeat: repeat;
}

From url(../images/bg.jpg) to url(/assets/bg.jpg)?
Comment: Oh my word ... that worked 100% ... I used the url(/assets/bg.jpg) fix for all of the image references in the css files and it did the trick!!! Thank you SO SO SO much!
Answer: Rails 3.1 introduced the new Asset Pipeline.  I would recommend you read over this: http://guides.rubyonrails.org/asset_pipeline.html
In your app/views/layouts/application.html.erb file, make sure you have this:
<%= stylesheet_link_tag "application", :media => "all" %>

In your app/assets/stylesheets/application.css file, make sure you have this line:
*= require_tree .

(This will ensure that any css files that you have in the app/assets/stylesheets directory will be available to your application)
in your css file you can just do this to reference images in your app/assets/images directory:
background-image: url(bg.jpg);

Comment: What about Fonts in another directory?
Comment: For fonts, I think you need to add this line to config/application.rb:

    config.assets.paths << Rails.root.join("app", "assets", "fonts")
Answer: In case you want to use local resources in your configuration initializers and you have this folder structure:

app
  assets
    stylesheets
    javascripts

All you have to do is: (in this sample im using Spree base config/initializer)

Spree.config do |config|
  config.admin_interface_logo = '/assets/logo/custom_admin.png'
  config.logo = '/assets/logo/custom_store.jpg'
end

So use /assets/your_file_or_path and it will work :D


handelling multiple views in android

Question: I have used multiple activities to handle mutiple views in android . I found that some where in the blog but I'm lost inside it.
I'm not able to switch between 2 views , my code is as follows:
main file
public class MultiViewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button next = (Button) findViewById(R.id.button1);
        next.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
           Intent myIntent = new Intent(view.getContext(), MultiViews2.class);
           startActivityForResult(myIntent, 0);
         }
       });
    }
}

xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button android:text="View 2"
          android:id="@+id/button1"
         android:layout_width="fill_parent"
          android:layout_height="wrap_content">
      </Button>

</LinearLayout>

another activity:
 public class MultiViews2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main2);
      Button next = (Button) findViewById(R.id.button1);
            next.setOnClickListener(new View.OnClickListener() {
             public void onClick(View view) {
               Intent myIntent = new Intent(view.getContext(), MultiViewActivity.class);
               startActivityForResult(myIntent, 0);
             }
           });
    }
  }

xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button android:text="View 1"
          android:id="@+id/button2"
         android:layout_width="fill_parent"
          android:layout_height="wrap_content">
      </Button>

</LinearLayout>

manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.multiview.org"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MultiViewActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MultiViews2"></activity>
    </application>

</manifest>

whenever I click on button2 it shows me error the application has stopped unexpectedly.Anything I missed above. I'm very new to android programming.
Comment: Can you post the LogCat error also, so that we get to know the exact problem??
Answer: Problem in Below line in MultiViews2 Class
Button next = (Button) findViewById(R.id.button2);

Instead Of
Button next = (Button) findViewById(R.id.button1);

Answer: Well considering your views are confusing and I cant say which one is main.xml and which one is main2.xml - the error is in one of the activities, both of them if you see have button 1 being referenced.
 Button next = (Button) findViewById(R.id.button1);

So obviously for one it should be
  Button next = (Button) findViewById(R.id.button2);

make that change and it should work. And for ease of understanding change view 1 to correspond to button 1 and similarly for 2. Else you'll run into more such problems

handelling multiple views in android


Setting up ADLDS and managing it with internal ADLDS users

Question: I configured ADLDS according to this blog post. I went further and followed this Microsoft article about allowing internal users to access ADLDS thru ADSI Edit but whatever I do i get access denied (just asking me continuously for login and password). Is there some magic thing I'm missing. I can login with Administrative account from the system. Just the internal logins and passwords that I set according to descriptions can't login.
Any magic I forgot to use? :-)
Answer: The answer is simple. Login as CN=Admin,CN=....,DC=COMPANY,DC=PL (full path to ADLDS where account exists). I was using "Admin" or other account names without providing full path which I guess was assuming Local account and didn't let me in.


what props are used for in vuejs

Question: I came across this snippet of code:

  props: {
    clicks: {
      type: Object,
      default: null,
    }
    events: {
      type: Object,
      default: null,
    },
  }

I didn't understand from where props are retreived, from database ? or received from another component ?
Answer: In Vue Props are declared in the component script. They are passed from a parent component down to child component.
Here is an example of a Child Component accepting the value of username being passed from its parent component:
<template>

   {{username}}

</template>

Here is the Parent Component passing the "username" prop using a static variable:
<child-component-name username='John Smith' />

In many cases the parent component will bind and object making it dynamic. Here is the whole Parent Component Prop passing "username" to child with this example:
<template>

   <child-component-name :username="user.username" />

</template>

In your example the parent component is used to count the clicks and events then passes the values to the child component like the following:
<child-component-name :clicks="yourClicks" :events="yourEvents" />

Answer: The props in Vue components always come from the immediate parent component.
Answer: In Vue, props (or properties), are the way that we pass data from a parent component down to it's child components.
This is specifically used to share data from its immediate parent to the child.
In youe case, your parent component template will be something like.
<template>

        <child-component-name :clicks="myClickObject" :events="myEventsObject">
        </child-component-name>

</template>

Where myClickObject and myEventsObject are objects defined in your immediate parent component from which child-component-name has been called.
Comment: so it is data sent from
Comment: not from `

what props are used for in vuejs


Adapting path/directory to different users in python

Question: I have an Excel file saved on a share point. I would like that every users who has access to the file can run the python code automatically. The code looks like this:
df = pd.read_excel(r'C:\Users\username\file path')

I would like to replace the username with the names of the user. So for example Martin would have
df = pd.read_excel(r'C:\Users\Martin\file path')

So far I got:
user = easygui.enterbox('Please insert your username')

df = pd.read_excel(r'C:\Users\{user}\file path')
``

Answer: A possible solution:
df = pd.read_excel(fr'C:\Users\{user}\file path')

Answer: You probably want pathlib.Path.home():
from pathlib import Path

excel_path = Path.home() / "path" / "filename.xlsx"
df = pd.read_excel(str(excel_path))


How would YOU compute IMDB movie rating?

Question: I'm doing this only for learning purposes. I've no intentions of reversing the methods of IMDB.
I asked myself I owned IMDB or similar website. How would I compute the movie rating?
All I can think of is Arithmetic Mean
For a movie data provided below computation would be

(38591*10 + 27994*9 + 32732*8 + 17864*7 + 7361*6 + 2965*5 + 1562*4 + 1073*3 + 891*2 + 3401*1) / 134434 = 8.17055953

My rating 8.17055953 which is close to IMDB's Arithmetic Mean.

Whats wrong with my score? Why is it not ideal (because IMDB didn't use it)?
If you had to compute. How would you have done it? What factors you would consider?

Note:
I'm not asking about the ranking mechanism for the Top/Bottom listing (that is shown here : http://www.imdb.com/chart/top). This question is about how IMDb turns the arithmetic mean into the weighted mean. Take "Manos: The Hands of Fate" — Unweighted mean 2.1, weighted mean 1.5. (Or "The Godfather", another example of ballot stuffing.)
Answer: Partial answer.  See the help page entitled: The vote average for film "X" should be Y! Why are you displaying another rating?
In short, IMDb uses:

a complex voter weighting system to make sure
  that the final rating is
  representative of the general voting
  population and not subject to over
  influence from individuals who are not
  regular participants in the poll.

Also note that:

In order to avoid leaving the scheme open to abuse, [IMDb does not] disclose the exact methods used.
Answer: First, define the theoretical construct of interest. There are many ways that a rating can be defined:

What is the theoretical target population? The entire world, English speakers, people who visit IMDB, people who have seen the movie in question?
What is the target time frame? It is the rating of the movie now or averaged over its release time.
Is it a democratic rating or an expert rating? Some people are more knowledgeable about the worth of movies. Some people are better able to differentiate a good from a bad movie. Some people are more consistent in their ratings over time. Should ratings from people who are "better" at rating movies be given more worth. This relates to a philosophical question of aesthetics and the meaning of intersubjective goodness.
Assuming you could get honest ratings from the entire target population in the entire time frame weighted or not by expertise, what is the mapping between these ratings and the composite rating? This could be the arithmetic mean. Alternatively, there are many other ways of combining individual ratings. For example, you could use an interpolated median. Some alternatives would have minimal effect on the rank order of films, but would have a major influence on the absolute value of the rating.
Is the number of people interested in the movie relevant to the rating?

Second, use all the available information to estimate the theoretical construct.
This is where the issues discussed by others would be important.

the role of demographic adjustments would depend on your definition of the target population
a weight for trust could be incorporated. Many indicators could be used:

the number of previous ratings (more ratings would suggest someone who is engaged in the site more)
the degree to which previous ratings are consistent with other raters or at least a subset of raters (greater consistency would suggest thoughtful and honest responding;
the degree to which responses are distributed over an extended period of time (this would suggest that the person is less likely to be attempting to game the system)
degree of engagement with the site in general: e.g., accessing the site, contributing to discussion boards (more engagement, more trust)

as mentioned by @csgillespie you could weight more recent votes greater if you wanted to estimate current attitudes to the film
You could weight for expertise in ratings. This would be correlated with trust ratings, but there is a difference.

Third, validate and monitor the estimation process using external trusted data sources.
Answer:
Whats wrong with my score? Why is it not ideal (because IMDB didn't
  use it)?

If the score was only for your use, then nothing is wrong with your calculation.
However, IMDB try to make it difficult for people to obviously influence the final score.

If you had to compute. How would you have done it? What factors you
  would consider?

Here are a few factors that you could consider (but will be unable to check):

The final score may be weighted according to how many votes have been cast.
Votes may be weighted by a time variable. For example, votes cast last year are less important than votes cast today.
Votes cast by users who have voted for other movies have more weight, i,e. a reputation coefficient.
Perhaps they incorporate data from other sites.
Comment: They have ratings break downs by gender and age groups as well.  I suspect that they may average within those groups and then taking the average of the group means for their reported score.
Comment: @drnexus Yes, good point about age, but I'm not sure about gender...


Outline for text file?

Question: In visual-studio-code I can have a text file with folding based on tabs.
I find this a light way to keep notes.
Any ideas if there's a way to show those fold levels in the outline window.
This would be perfect for navigating the level 1 folds (maybe also 2).
Opening outline on a .txt files displays 'No symbols found in xxx.txt'.
So I'm guessing there needs to be a symbol tree defined using a textmate definition?
e.g. https://github.com/redhat-developer/vscode-yaml/blob/4cf1d44e5c524a4e7cf8f306085a748610413ac2/syntaxes/yaml.tmLanguage.json
BTW, switching filetype to markdown kinda works if you set edit the level 1 folds to be headings. But then you lose the ability to fold inner levels unless you also set them as lower markdown headings. This then causes the outline view to get cluttered.
Answer: Try this extension Better Outline, add some comments for level 1 in your text document which can be displayed as outline, then it can be used for navigating the level 1.
But it does not support level 2 in your case.
Comment: Cheers Peter much appreciater

Outline for text file?


Contagem de elementos de uma matriz, armazenando informações em outra

Question: Olá, sou iniciante em python e estou fazendo um exercício que se constitui em ler todos os números de uma matriz e adicionar em outra matriz os números existentes na anterior e quantas vezes eles aparecem. Por consequência, a matriz 2 vai ter duas linhas, uma contendo os numeros, e na segunda quantas vezes eles aparecem.
Por exemplo, se existe um número 10 e ele aparece 3 vezes, a segunda matriz terá na primeira linha o numero 10, e na segunda o número 3, os dois na mesma coluna.
OBS: Considere a primeira matriz como uma qualquer já declarada.
...

matriz2=[]
linha1 = []
linha2 = []
qntde=0
linha1.append(0)
for l in range(numerodelinhas):
    for c in range(numerodecolunas):
        qntde = 0
        if(linha1.count(matriz[l][c])==0):
            if(l==0 and c == 0):
                linha1[0]=matriz[0][0]
            if(l != 0 or c != 0):
                linha1.append(matriz[l][c])
            for n in range(numerodelinhas):
                qntde = qntde + matriz[n].count(matriz[l][c])
            linha2.append(qntde)

matriz2.append(linha1)
matriz2.append(linha2)
cm2=0
for n in matriz2[0]:
    print (matriz2[0][cm2]," - ",matriz2[1][cm2])
    cm2+=1

O código deu certo, mas ele não me agrada nem um pouco. Teria um jeito de deixa-lo mais enxuto?
Answer: Dá pra tirar proveito da classe collections.Counter, que recebe um iterable ou mapping e faz a contagem de elementos.
O único ajuste é transformar a matriz em uma lista, mas isso é facilmente resolvido com o método itertools.chain().
Segue um exemplo de uso:
from itertools import chain
from collections import Counter

# Matrix 4x4 apenas para exemplo
matriz = [
    [2, 4, 6, 8],
    [1, 2, 3, 5],
    [1, 1, 10, 20],
    [1, 2, 4, 7],
]

contador = Counter(chain.from_iterable(matriz))

# printa o objeto Counter, sendo as chaves o elemento
# contado e os valores quantas vezes eles aparecem na matriz
print(contador)

Output:
Counter({
    1: 4,
    2: 3,
    4: 2,
    6: 1,
    8: 1,
    3: 1,
    5: 1,
    10: 1,
    20: 1,
    7: 1
})

Repl.it
edit: chain(*matriz) alterado para chain.from_iterable(matriz) para manter o carregamento da matriz lazy. (sugestão: @nosklo)
Comment: Neste caso não faz diferença mas normalmente é melhor usar o `chain.from_iterable`:  `Counter(chain.from_iterable(matriz))`
Comment: Verdade @nosklo, usei unpacking por praticidade, mas em um caso real talvez não seja performático. Vou editar a resposta, obrigado.
Answer: Tem um jeito muito mais fácil de lidar com arrays e matrizes em python, que é utilizar uma biblioteca chamada numpy. Exemplo:
In [1]: import numpy as np

# matriz1 gerada aleatoriamente de números de 0 a 5 com tamanho 3x3
In [2]: matriz1 = np.random.randint(0, 5, size=(3,3))

In [3]: matriz1
Out[3]:
array([[1, 2, 4],
       [3, 1, 1],
       [2, 4, 4]])

# retorna os elementos únicos e o número de vezes em que cada um aparece
In [4]: elementos, contagem = np.unique(matriz1, return_counts=True)

# empilha os dois arrays de forma vertical, elementos em cima e contagem embaixo
In [5]: matriz2 = np.vstack([elementos, contagem])

In [6]: matriz2
Out[6]:
array([[1, 2, 3, 4],
       [3, 2, 1, 3]])

Para mais informações e documentação: http://www.numpy.org. O numpy é a biblioteca base para quem trabalha com machine learning e ciência de dados. Existem vários tutoriais disponívels, vale a pena dar uma conferida.


Python: Find Amount of Handwriting in Video

Question: Do you know of an algorithm that can see that there is handwriting on an image? I am not interested in knowing what the handwriting says, but only that there is one present?
I have a video of someone filling a slide with handwriting. My goal is to determine how much of the slide has been filled with handwriting already.

The video in question can be downloaded here: http://www.filedropper.com/00_6
For this particular video, a great solution was already suggested in Quantify how much a slide has been filled with handwriting
The solution is based on summing the amount of the specific color used for the handwriting. However, if the handwriting is not in blue but any other color that can also be found on non-handwriting, this approach will not work.
Therefore, I am interested to know, if there exists a more general solution to determine if there is handwriting present on an image?
What I have done so far:
I was thinking of extracting the contours of an image, and then somehow detect the handwriting part based on how curvy the contours are (but I have no clue how to do that part). it might not be the best idea, though, as again it's not always correct...
import cv2
import matplotlib.pyplot as plt

img = cv2.imread(PATH TO IMAGE)
print("img shape=", img.shape)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("image", gray)
cv2.waitKey(1)
#### extract all contours
# Find Canny edges
edged = cv2.Canny(gray, 30, 200)
cv2.waitKey(0)

# Finding Contours
# Use a copy of the image e.g. edged.copy()
# since findContours alters the image
contours, hierarchy = cv2.findContours(edged,
    cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

cv2.imshow('Canny Edges After Contouring', edged)
cv2.waitKey(0)

print("Number of Contours found = " + str(len(contours)))

# Draw all contours
# -1 signifies drawing all contours
cv2.drawContours(img, contours, -1, (0, 255, 0), 3)

cv2.imshow('Contours', img)
cv2.waitKey(0)

Comment: The color of the handwritten text is different (blue vs black) so this seems pretty easy. I understand you noted it not being blue, but you could also have not black as what you're looking for.
Comment: @hrokr Thanks for your comment. To clarify: The color of the handwriting could be anything. Hence this will not help much for my case.
Comment: You can do this with something like OpenCV but the computational requirements are pretty substantial. And since it's good practice for educators to make notes in a different color, I think being pretty sure would be worth it. Are there any cases where both the notes and printed text are the same color or are you trying to plan for a more robust (if rare and computationally expensive) solution?
Comment: @hrokr Yes to both! Actually I do have videos where the educator uses black color as ink or adds object of the same color as the ink in the video. Therefore, I am planning for a more robust method.
Comment: OK, so that's helpful but a constraint like that would be good to know up front. Are there any other constraints you can think of? For example, always math? Are there marked transitions between slides?
Comment: Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220822/discussion-between-hrokr-and-henry).
Comment: Is it always the same background?...or is it possible to have a template of the screen without any hands and/or written text prior to when the video starts? If so you could subtract the template from the current frame and the result will be the written text.
Comment: @kavko very good question and suggestion. Unfortunately, the background is not always the same, though it usually does not change for one scene. By a scene, I mean all the frames in the video in which the slide does not change, but he is simply writing on it. If you watch the video, you will notice that there are 3 such scenes. What one might be able to do is to use the first frame of each scene as a background... what do you think?
Comment: If the first frame would be empty (like a template) then that would be possible and quite simple to implement. But in the video you posted there already is something written (plus the hand that is writing) on the first frame - which would make this task not so trivial. I asked the question because I didn't know if the video has been shortened for easier distribution. Have you looked at OpenCV's cv2.BackgroundSubtractorMOG2() function - could be useful in this case.
Answer: You can identify the space taken by hand-writing by masking the pixels from the template, and then do the same for the difference between further frames and the template. You can use dilation, opening, and thresholding for this.
Let's start with your template. Let's identify the parts we will mask:
import cv2
import numpy as np

template = cv2.imread('template.jpg')

Now, let's broaden the occupied pixels to make a zone that we will mask (hide) later:
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
kernel = np.ones((5, 5),np.uint8)
dilation = cv2.dilate(255 - template, kernel,iterations = 5)

Then, we will threshold to turn this into a black and white mask:
_, thresh = cv2.threshold(dilation,25,255,cv2.THRESH_BINARY_INV)

In later frames, we will subtract this mask from the picture, by turning all these pixels to white. For instance:
import numpy as np
import cv2
vidcap = cv2.VideoCapture('0_0.mp4')
success,image = vidcap.read()
count = 0
frames = []

while count < 500:
  frames.append(image)
  success,image = vidcap.read()
  count += 1

mask = np.where(thresh == 0)

example = frames[300]
example[mask] = [255, 255, 255]
cv2.imshow('', example)
cv2.waitKey(0)

Now, we will create a function that will return the difference between the template and a given picture. We will also use opening to get rid of the left over single pixels that would make it ugly.
def difference_with_mask(image):
    grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    kernel = np.ones((5, 5), np.uint8)
    dilation = cv2.dilate(255 - grayscale, kernel, iterations=5)
    _, thresh = cv2.threshold(dilation, 25, 255, cv2.THRESH_BINARY_INV)
    thresh[mask] = 255
    closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    return closing

cv2.imshow('', difference_with_mask(frames[400]))
cv2.waitKey(0)

To address the fact that you don't want to have the hand detected as hand-writing, I suggest that instead of using the mask for every individual frame, you use the 95th percentile of the 15 last 30th frame... hang on. Look at this:
results = []
for ix, frame in enumerate(frames):
    if ix % 30 == 0:
        history.append(frame)
    results.append(np.quantile(history, 0.95, axis=0))
    print(ix)

Now, the example frame becomes this (the hand is removed because it wasn't mostly present in the 15 last 30th frames):

As you can see a little part of the hand-writing is missing. It will come later, because of the time-dependent percentile transformation we're doing. You'll see later: in my example with frame 18,400, the text that is missing in the image above is present. Then, you can use the function I gave you and this will be the result:

And here we go! Note that this solution, which doesn't include the hand, will take longer to compute because there's a few calculations needing to be done. Using just an image with no regard to the hand would calculate instantly, to the extent that you could probably run it on your webcam feed in real time.
Final Example:
Here's the frame 18,400:

Final image:

You can play with the function if you want the mask to wrap more thinly around the text:

Full code:
import os
import numpy as np
import cv2
vidcap = cv2.VideoCapture('0_0.mp4')
success,image = vidcap.read()
count = 0
from collections import deque
frames = deque(maxlen=700)

while count < 500:
  frames.append(image)
  success,image = vidcap.read()
  count += 1

template = cv2.imread('template.jpg')
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
kernel = np.ones((5, 5),np.uint8)
dilation = cv2.dilate(255 - template, kernel,iterations = 5)

cv2.imwrite('dilation.jpg', dilation)
cv2.imshow('', dilation)
cv2.waitKey(0)

_, thresh = cv2.threshold(dilation,25,255,cv2.THRESH_BINARY_INV)
cv2.imwrite('thresh.jpg', thresh)
cv2.imshow('', thresh)
cv2.waitKey(0)

mask = np.where(thresh == 0)

example = frames[400]
cv2.imwrite('original.jpg', example)
cv2.imshow('', example)
cv2.waitKey(0)

example[mask] = 255
cv2.imwrite('example_masked.jpg', example)
cv2.imshow('', example)
cv2.waitKey(0)

def difference_with_mask(image):
    grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    kernel = np.ones((5, 5), np.uint8)
    dilation = cv2.dilate(255 - grayscale, kernel, iterations=5)
    _, thresh = cv2.threshold(dilation, 25, 255, cv2.THRESH_BINARY_INV)
    thresh[mask] = 255
    closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    return closing

cv2.imshow('', difference_with_mask(frames[400]))
cv2.waitKey(0)

masked_example = difference_with_mask(frames[400])
cv2.imwrite('masked_example.jpg', masked_example)

from collections import deque
history = deque(maxlen=15)

results = []
for ix, frame in enumerate(frames):
    if ix % 30 == 0:
        history.append(frame)
    results.append(np.quantile(history, 0.95, axis=0))
    print(ix)
    if ix > 500:
        break

cv2.imshow('', frames[400])
cv2.waitKey(0)

cv2.imshow('', results[400].astype(np.uint8))
cv2.imwrite('percentiled_frame.jpg', results[400].astype(np.uint8))
cv2.waitKey(0)

cv2.imshow('', difference_with_mask(results[400].astype(np.uint8)))
cv2.imwrite('final.jpg', difference_with_mask(results[400].astype(np.uint8)))
cv2.waitKey(0)

Answer: You could try to make a template before detection which you could use to deduct it on the current frame of the video. One way you could make such a template is to iterate through every pixel of the frame and look-up if it has a higher value (white) in that coordinate than the value that is stored in the list.
Here is an example of such a template from your video by iterating through the first two seconds:

Once you have that it is simple to detect the text. You can use the cv2.absdiff() function to make difference of template and frame. Here is an example:

Once you have this image it is trivial to search for writting (threshold + contour search or something similar).
Here is an example code:
import numpy as np
import cv2

cap = cv2.VideoCapture('0_0.mp4')  # read video

bgr = cap.read()[1]  # get first frame
frame = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)  # transform to grayscale
template = frame.copy()  # make a copy of the grayscale

h, w = frame.shape[:2]  # height, width

matrix = []  # a list for [y, x] coordinares
# fill matrix with all coordinates of the image (height x width)
for j in range(h):
    for i in range(w):
        matrix.append([j, i])

fps = cap.get(cv2.CAP_PROP_FPS)  # frames per second of the video
seconds = 2  # How many seconds of the video you wish to look the template for
k = seconds * fps  # calculate how many frames of the video is in that many seconds
i = 0  # some iterator to count the frames
lowest = []  # list that will store highest values of each pixel on the fram - that will build our template

# store the value of the first frame - just so you can compare it in the next step
for j in matrix:
    y = j[0]
    x = j[1]
    lowest.append(template[y, x])

# loop through the number of frames calculated before
while(i < k):
    bgr = cap.read()[1]  # bgr image
    frame = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)  # transform to grayscale
    # iterate through every pixel (pixels are located in the matrix)
    for l, j in enumerate(matrix):
        y = j[0]  # x coordinate
        x = j[1]  # y coordinate
        temp = template[y, x]  # value of pixel in template
        cur = frame[y, x]  # value of pixel in the current frame
        if cur > temp:  # if the current frame has higher value change the value in the "lowest" list
            lowest[l] = cur
    i += 1  # increment the iterator

    # just for vizualization
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

i = 0  # new iteratir to increment position in the "lowest" list
template = np.ones((h, w), dtype=np.uint8)*255  #  new empty white image
# iterate through the matrix and change the value of the new empty white image to that value
# in the "lowest" list
for j in matrix:
    template[j[0], j[1]] = lowest[i]
    i += 1

# just for visualization - template
cv2.imwrite("template.png", template)
cv2.imshow("template", template)
cv2.waitKey(0)
cv2.destroyAllWindows()

counter = 0  # counter of countours: logicaly if the number of countours would
# rapidly decrease than that means that a new template is in order
mean_compare = 0  # this is needed for a simple color checker if the contour is
# the same color as the oders
# this is the difference between the frame of the video and created template
while(cap.isOpened()):
    bgr = cap.read()[1]  # bgr image
    frame = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)  # grayscale
    img = cv2.absdiff(template, frame)  # resulted difference
    thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]  # thresholded image
    kernel = np.ones((5, 5), dtype=np.uint8)  # simple kernel
    thresh = cv2.dilate(thresh, kernel, iterations=1)  # dilate thresholded image
    cnts, h = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  # contour search
    if len(cnts) < counter*0.5 and counter > 50:  # check if new template is in order
        # search for new template again
        break
    else:
        counter = len(cnts) # update counter
        for cnt in cnts:  # iterate through contours
            size = cv2.contourArea(cnt)  # size of contours - to filter out noise
            if 20 < size < 30000:  # noise criterion
                mask = np.zeros(frame.shape, np.uint8)  # empry mask - needed for color compare
                cv2.drawContours(mask, [cnt], -1, 255, -1)  # draw contour on mask
                mean = cv2.mean(bgr, mask=mask)  # the mean color of the contour

                if not mean_compare:  # first will set the template color
                    mean_compare = mean
                else:
                    k1 = 0.85  # koeficient how much each channels value in rgb image can be smaller
                    k2 = 1.15 # koeficient how much each channels value in rgb image can be bigger
                    # condition
                    b = bool(mean_compare[0] * k1 < mean[0] < mean_compare[0] * k2)
                    g = bool(mean_compare[1] * k1 < mean[1] < mean_compare[1] * k2)
                    r = bool(mean_compare[2] * k1 < mean[2] < mean_compare[2] * k2)
                    if b and g and r:
                        cv2.drawContours(bgr, [cnt], -1, (0, 255, 0), 2)  # draw on rgb image

    # just for visualization
    cv2.imshow('img', bgr)
    if cv2.waitKey(1) & 0xFF == ord('s'):
        cv2.imwrite(str(j)+".png", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# release the video object and destroy window
cap.release()
cv2.destroyAllWindows()

One possible result with a simple size and color filter:

NOTE: This template search algorithm is very slow because of the nested loops and can probably be optimized to make it faster - you need a little more math knowledge than me. Also, you will need to make a check if the template changes in the same video - I'm guessing that shouldn't be too difficult.
A simpler idea on how to make it a bit faster is to resize the frames to let's say 20% and make the same template search. After that resize it back to the original and dilate the template. It will not be as nice of a result but it will make a mask on where the text and lines of the template are. Then simply draw it over the frame.
Comment: +1 That's very impressive! A question: You write in the beginning "that is stored in the list.", what "list" do you mean? Many thanks!
Comment: You can create a list of values by itterating through the first frame and store those values (grayscale images have values 0-black to 255-white) in some list. Then compare those values with the values of the next frame - when the hand moves out of the picture, those values will be higher (255 - white) - you replace them with the highest one and you can make a "template". I hope I made it a bit more clear
Answer: I don't think you really need the code in this case and it would be rather long if you did. But here's an algorithm to do it.
Use OpenCV's EAST (Efficient Accurate Scene Text detector) model at the beginning to establish the starting text on the slide. That gives you a bounding box(es) of the initial percentage of the slide covered with slide text as opposed to handwritten explanatory text.
Every, say 1-5 seconds (people don't write all that fast), compare that baseline image with the current image and the previous image.

If the current image has more text than the previous image but the initial bounding boxes are NOT the same, you have a new and rather busy slide.

If the current image has more text than the previous image but the initial bounding boxes are ARE the same, more text is being added.

If the current image had less text than the previous image but the initial bounding boxes are NOT the same, you again have a new slide -- only, not busy and with space like the last one to write.

If the current image has less text than the previous image but the initial bounding boxes are ARE the same, you either have a duplicate slide with what will presumably be more text or the teacher is erasing a section to continue, or modify their explanation. Meaning, you'll need some way of addressing this.

When you have a new slide, take the previous image, and compare the bounding boxes of all text, subtracting the boxes for the initial state.
Computationally, this isn't going to be cheap (you certainly won't be doing this life, at least not for a number of years) but it's robust, and sampling the text every so many seconds of time will help.
Personally, I would approach this as an ensemble. That is an initial bounding box then look at the color of the text. If you can get away with the percentage of different color text, do. And when you can't, you'll still be good.
Comment: It's a good idea, but what if he just adds more non-handwritten text, like animated bullet points that appear one after the other onto the slide? Your answer is very similar to my approach in this other question I had: https://stackoverflow.com/questions/63624333/quantify-how-much-a-slide-has-been-filled-with-handwriting
Comment: Basically, you don't even need to detect a text, you can also just look at how much was added to each frame comparing to the previous frame. However, I'm really interested in knowing, if there is hand-writing on the frame, yes or no, and then if yes, how much compared to the previous?
Comment: Ok, in the comment section of the question, you said: *"Sometimes, it's even a drawing, but I guess this will make it just even more complicated, So forget about that for now."* But here you're saying, essentially: *"But what about this edge case that wasn't disclosed and which I've also said not to worry about?"*  It can't be P and ~P at the same time. What set of constraints and requirements would get you to a good MVP or better?
Answer: In addition to the great answers that people provided, I have two other suggestions.
The first one, is the CNN methods. It's totally workable to use some object detection routine, or even a segmentation method (like U-NET) to differentiate between the texts. It is easy because you can find millions of images from digital text books and also handwritten documents to train your model.
The Second approach is to locate and to extract every single symbol on the image, separately (with a simple method like the one you used so far, or with connectedcomponent). Since typographic letters and symbols have a unique shape and style (similar fonts - unlike the handwritten letters) you can match all the found letters with sample typographic letters that you gathered separately to distinguish between the handwritten and the typographic. Feature-point-based matching (like SURF) could be a good tool for this approach.


Angular 2 Set Default Form Control Value Presentation Component

Question: I am a bit lost here.  I have an angular 2 app built with store architecture (ngrx) and using smart and presentation components.
Everything is working well in relation to store and retrieving data, but i am stuck when it comes to setting the default form control value in the presentation component.  It keeps failing to compile because the property does not exist, which is obviously a timing issue as the property is not there when it first tries to load the form.
So how to I set the default value or initial value in the form control only after the @Input() customer value is available.
Presentation Component
    import { Component, OnInit, Input, Output, EventEmitter, ChangeDetectionStrategy} from '@angular/core';
import { FormBuilder, FormGroup, FormArray, Validators, FormControl } from "@angular/forms";

// Mojito Models
import { CustomerModel } from '../../models/customer-model';

@Component({
  selector: 'mj-customer-detail',
  templateUrl: './customer-detail.component.html',
  styleUrls: ['./customer-detail.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CustomerDetailComponent implements OnInit {

  @Input() customer: CustomerModel;
  @Output() showView: EventEmitter<string> = new EventEmitter<string>();

  customerForm: FormGroup;

  constructor(private fb: FormBuilder) {}

  ngOnInit() {

    this.customerForm = this.fb.group({
      name: [this.customer.name, Validators.required],  // This fails to compile
      overview: ['', Validators.required],
      imagePath: ['', Validators.required]
    });

  }

}

Template
{{customer?.name}} **// This works fine it displays the customer name**

<form [formGroup]="customerForm"  (ngSubmit)="onSaveCustomer(contact)">

    <md-card class="demo-card demo-basic">
        <md-card-content>

                        <md-input-container style="width: 100%">
                            <input mdInput placeholder="Image Url" style="width: 100%" formControlName="imagePath" type="text" id="imagePath" #imagePath/>
                        </md-input-container>

            <md-input-container style="width: 100%">
                <input mdInput placeholder="Name" style="width: 100%" formControlName="name" type="text" id="name" #name/>
            </md-input-container>
            <md-input-container style="width: 100%">
                <input mdInput placeholder="Overview" style="width: 100%" formControlName="overview" type="text" id="overview" #overview/>
            </md-input-container>

        </md-card-content>
    </md-card>

</form>

SMART COMPONENT
Customer Component
import { Component, OnInit } from '@angular/core';
import { Router } from "@angular/router";

// 3rd Party Modules
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';

// Mojito Components
import { ApplicationState } from '../../store/application-state';
import { CustomerEffects } from '../../store/effects/customers.effects';
import { ADD_CUSTOMER_SUCCESS, getCustomers, addCustomer, CustomerSelected } from '../../store/actions/customer.actions';
import { CustomerModel } from '../models/customer-model';

@Component({
  selector: 'mj-customer',
  templateUrl: './customer.component.html',
  styleUrls: ['./customer.component.scss']
})
export class CustomerComponent implements OnInit {

  customers$: Observable<CustomerModel>;
  customer$: Observable<CustomerModel>;
  addCustomerSuccess$ : Observable<any>;
  showView: string = 'list';

  constructor(private router: Router, private store: Store<ApplicationState>, private customerEffects : CustomerEffects) {

    this.store.dispatch(getCustomers());
    this.customers$ = store.select("customers");
    this.customer$ = store.select("customer");

    this.addCustomerSuccess$ = this.customerEffects.addCustomer$.filter(( { type } ) => type === ADD_CUSTOMER_SUCCESS);

  }

  addCustomer( customer: CustomerModel ) {

    this.store.dispatch(addCustomer(customer));

  }

  ngOnInit() {

  }

  onSelectedCustomer(selectedCustomerId){

    this.store.dispatch(CustomerSelected(selectedCustomerId));

  }

  changeView(viewType){

    this.showView = viewType;

  }

}

Customer Template
<mj-customer-detail (showView)="changeView($event)" [hidden]="showView!='detail'" [customer]="customer$ |async"></mj-customer-detail>

Comment: What's the exact error message? Where does `customer` come from? (how is it passed to the input and where did it get there)
Comment: Hi Ive added the full code for smart component as well.  Essentially i have the smart component (customer.component.ts) using ngrx store to pull data for customers array and customer object as an observable.  Then i pass the customer into the customer-detail.component as an Input().  I know this is working as when i put in {{customer?.name}} in the customer-detail.component.html it displays the customers name.
Comment: You get `customer` async, this means it's not yet available when you call `this.customerForm = this.fb.group({...`. The binding works because of `?.` which doesn't throw an exception when at first `customer` is still null`.
Answer: Just update the form when you get the value:
  ngOnChanges() {
    if(this.customer) {
      this.customerForm.get('name').setValue(this.customer.name);
    }
  }

  ngOnInit() {
    this.customerForm = this.fb.group({
      name: [null, Validators.required],  // This fails to compile
      overview: ['', Validators.required],
      imagePath: ['', Validators.required]
    });
  }

Comment: Oh man - thank you so much.  I have been playing around with that for a few days.  That works great!!!
Comment: Thanks for the feedback. Glad to hear you could make it work :)


Displaying the node title in different region

Question: My designer just killed me, I realised he put node titles in a different region of the page layout, just beneath a menu and before left/right sidebars. No going back now.
How can I display titles of things (nodes, views etc) in a different region and hide it from the content region?
Answer: Not sure which version of Drupal you're using, but the method is basically the same. In D6 just place  where ever you need it in the page.tpl.php template and remove it from the node.tpl.php template (or use conditionals to output it there when needed).
Answer: If you don't want to touch the template file and prefer to use the UI, you can also use the Blockify or Omega Tools module. Either one of these will put the page title into a block for you to place in any region of your choosing.
Comment: does your proposed solution work for rendering node and views titles?
Comment: yes it does. :)
Comment: - Step 1: enable blockify module.
- Step 2: update blockify settings to expose page title as block.
- Step 3: update omega tools settings to hide page title


Restrict text format use to a particular node/edit URL

Question: I have a custom text format that i use in the Wysiwyg profile with ckeditor. Using Drupal 7.
I want to restrict that text format use to a certain node/edit URL.
What would be the best option to achieve that either Programmatically or using custom modules?
Any advise is appreciated!
Answer: You can't achieve that in config, since text formats are configured per field instance, but not as granular as for to get to the node level.
You'll need too implement a hook_form_alter(), and then check the url to find out if you want to allow the format or not. Something like this:
<code>function mymodule_form_alter($form, &$form_state, $form_id) {
  if ($form_id == 'node_type_form_to_alter' && (current_path() == 'node/%nid/edit')) {
    // Enforce desired text_format here, by altering $form array.
  }
}
</code>
That's what I would do for that case. You can take the opposite approach, and implement the same form_alter, but instead of enforcing it on a particular URL, just making sure that it's not available unless the url is the one you want to target.
Hope it helps.
EDIT: To enforce the format, you need to find the field you want to alter, inside the $form array. Wysiwyg fields use the 'text_format' when declaring the field through Form API (example here).
So, you have to find the desired field, and change the '#format' property (as in the example), to the machine name of the text format you want to use. Also, you'll see a 'format' propery in the element array. You might want to change that too if you want avoid certain formats from appearing in the dropdown widget.
I recommend you use xdebug to see the structure of the $form variable inside the hook_form_alter() implementation. Alternatively, enable the devel module, and stick a "dpm($form);" in that function, to render the structure on screen when visiting the node form.


Does a neural network require non-linear activations?

Question: I am currently working on an auto-encoder to create latent vectors from multivariate time series. The architectures I've tested so far all revolve around some flavours and combinations of 1d convolutions and resnets (wavenet style). Basically, my most basic standard building block is
1DConv()->ActivationFunction()
I've so far tested on Relus, tanh gates and sigmoid gates. I train all networks on reconstruction error of my time series using MSE or variants thereof.
So far, I've observed that removing non-linear activations completely from my networks actually tends to improve learning time and reconstruction capability of my nets. This seems counterintuitive to me, as I thought it's precisely the activation functions that give neural networks the expressive power they have. How can this be?
Some additional info:
-My data is rather noisy and I suspect that there is only little structural information to be learned and compressed using my auto encoders.
Answer: In general polynomial regression yields better results than neural networks, but they suffer from combinatorial explosion, so in order to tackle this we use neural networks with non-linear activations
Sometimes linear models are sufficient so if neural network with only linear activations does the job then use linear/logistic regression and compare results. I would expect that these should be very close
One remark: convolutional layers are one thing that helps in dimensionality reduction so this is one thing that is independent from activation functions
Comment: While I agree that the performance of neural networks is exaggerated, saying that "in general" polynomial regression outperforms them is simply not true. For example, for real-life image, or language data this would be unlikely to happen.
Comment: IMHO 2(+)Dconv-nets and RNNs are just different kind of models, but for tabular data trees and linear models should be more than enough
Comment: They all fall into neural networks category and you didn't make this distinction in your answer. Moreover, embeddings + 1d conv layers would probably work better for language data then polynomial regression.
Answer: Deep dense (fully connected) neural network with only linear activation functions reduce to single-layer network, as explained in the following answer on StackOverflow. Using non-linear activation functions is generally good idea, because it lets network to use more complicated features and be more efficient (both in terms of performance and convergence time).
More advanced network structures like convolutional, or recurrent networks, enable us to process data in more flexible way, so using such layers even without activation functions would work more efficiently for some data, then simple dense network.
Recently there were many papers shaking some of the commonly held beliefs about neural networks. It can be the case, that for some problem, some atypical network structure could work. Such "crazy" ideas worked well in the past. Maybe your data is simple enough, that you don't need to add further complications to network structure? Still, if obtaining such results, you should check if you need to use deep network, as this could suggest that you do not need many layers, but maybe just more neurons and single hidden layer.
In every case when getting results that seem strange, double check your code for potential bugs, maybe ask someone for code review. You can check the What should I do when my neural network doesn't learn? thread for some hints on debugging neural networks code. Try different regularization, learning rate, optimizer, etc.
Comment: Thanks for the extensive answer. I've generally been quiet extensive so far in terms of architectures and depths (I rely on single-batch overfitting to measure the max expressiveness of the networks).


There is no Fermat number that is divisible by $97$.

Question: Because $97$ is a prime number of the form $3.2^n+1$, The order of $2$ modulo $97$ is either $3$, $2^k$ or $3\cdot2^k$ for some $0\le k\le n$.
Since $2^{2^k}-1=F_0F_1F_2\cdots F_{k-1}$,The order of $2$ modulo $97$ is not divisible by $3$ if and only if $97$ divides a Fermat number $F_k$ with $0\le k\le n-1$.
Now order of $2$ modulo $97$ is $48$, so there is no Fermat number $F_k$ with $0\le k\le n-1$ that is divisible by $97$. but I don't how obtain this result for$k\ge n$?
Comment: Also, from this(http://math.stackexchange.com/questions/83938/a-theorem-about-prime-divisors-of-generalized-fermat-numbers), $$k\cdot2^{n+2}+1=97\implies n\le3$$
Comment: Hint: if $97 \mid 2^s + 1$, then $2^{2s} \equiv 1 \pmod{97}$.
Answer: I think there is a small thing you're overlooking. We have that $97\mid 2^e-1$ if and only if $\mbox{ord}_{97}(2)\mid e$.
If $97\mid F_k$, then $97\mid2^{2^{k+1}}-1$, thus...


How can I run an analysis of variance with one independent variable and multiple dependent variables?

Question: From my limited statistical knowledge, I could use MANOVA if I had multiple independent variables (x1, x2...xn). What can I do (specifically in R) with one "x" variable and multiple "y" groups? I'm trying to see if there is any relationship between the y's with respect to their regression with x. I've already set up a loop that computes bivariate, piecewise linear regressions between each pair (x-y1, x-y2, ... x-yn), but that does not include any analysis of variation between the y variables. Does anybody know how I might do this (in a statistically sound manner, of course) in R? My data looks like this:
x         y1       y2      y3      y4      y5
4.19    5.51    19.76   50.00   19.36   54.07
8.60    10.16   33.01   82.99   38.48   44.95
8.03    7.82    31.29   79.05   40.12   59.18
6.64    8.99    27.13   69.13   30.44   59.02
7.03    8.22    25.29   74.45   36.02   50.88
1.50    5.90    10.69   22.88   10.34   34.50
4.36    7.61    19.27   44.47   20.06   24.62
7.17    8.30    26.72   68.68   31.61   20.16
2.68    5.61    14.25   37.07   15.20   67.75
7.91    7.75    30.93   82.01   38.62   65.36
3.74    5.24    16.42   40.17   17.54   15.19

Comment: You are confused. You would do a MANOVA if you have multiple *dependent* variables. If you have multiple independent variables, but only one dependent, you have a univariate problem and can use ANOVA.
Comment: No, then you would do *something else*. What depends on the question you want to answer. My comment was intended to clarify your confusion regarding what is a multivariate and what is a univariate problem.
Comment: Your question is not clear at all. Please improve the question body and give a lot more details and background.
Comment: @Roland Even if my independent variable, X, is numeric and doesn't have multiple levels/classes?
Comment: @Roland Thanks for your response. What I want to answer is this: are any of the y variables related to each other as they relate to their relationship with x?
Comment: For example, do you want to compare the slopes and/or the intercepts of the 5 regression lines?
Answer: While what question you are trying to answer is unclear, I think you are trying to do  canonical correlation analysis (CCA), which attempts to explain a set of dependent variables (DVs) with a set of independent variables (IVs). This technique creates a linear combination of the IVs such that will account for the maximum variance of another linear combination of the DVs. Your case is special in that you have only 1 IV.
In R, you can do it in different ways:
Approach 1
manova(cbind(y1, y2, y3, y4, y5) ~ x, data=your_df)

Approach 2
library(CCA)
X <- cbind(your_df$x) ## cbind necessary to make
Y <- cbind(your_df$x) ## X and Y matrices
matcor(X, Y)

You interpret CCA results in a way similar to interpreting MANOVA results. For more information see this tutorial.
Comment: Unfortunately the linked tutorial `http://www.ats.ucla.edu/stat/r/dae/canonical.htm` is not available any more (Nov 2021).