Unfortunately your question and code example is extremely long. Consider: Read before posting: How do I get my question answered?
This forum is populated by industry workers, academics and students who volunteer their help as a contribution to the community. By posting a much more terse question with only the salient points, you’re much more likely to receive a response.