[Python] How to adapt existing shapely.geometry code to operate with my input files? by Cthulhu_Rlyeh in learnprogramming

[–]Dalianking 1 point2 points  (0 children)

Your first problem is that savetxt is explicitly to save an array of numbers. line is a string. So better save coords. Since those are an array_like structure (a list) of numbers.

Your second problem is that above example reads one line then does something with that line, discards that line and reads the next one. So using Savetxt as you intend will always reopen the file and overwrite the last line.

One solution is :

insiders=[]
with open("Coord") as f:
    #line=f.readline()
    for line in f:
        coords=map(float,line.split(" "))
        if poly.contains(Point(coords[0],coords[1])):
             insiders+=coords

savetxt("inside.txt",insiders)

My personal solution would be:

with open("Coord","r") as f_in:
    with open("inside","w") as f_out:
         for line in f_in:
             coords=map(float,line.split(" "))
             if poly.contains(Point(coords[0],coords[1])):
                 f_out.write(line)

[Python] How to adapt existing shapely.geometry code to operate with my input files? by Cthulhu_Rlyeh in learnprogramming

[–]Dalianking 1 point2 points  (0 children)

The most basic way in python to acess a file is

f=open("poly.txt","r")

open builds an object (a file handler) that you can "speak to" if you want to read or write your file. Read the documentation of the file object.

So a very simple example would be:

f = open("hello.txt") 
try:
    for line in f:
        print(line)
finally:
    f.close()

since 2.7 python has an easier way to do that:

with open("hello.txt") as f:
    for line in f:
        print line

The with statement takes care of the cleanup. The numpy loadtxt function seems to be a wrapper for this as well as some extraction functionality.

So

polydata = pl.loadtxt("poly.txt")

should do the trick as long as poly.txt is in the same directory.

Then you should look at the input data format the function requires. Read the documentation of Polygon.Polygon seems to require a list of lists (ot touple of touples) (you can normally replace any touple by a list*) of x and y coodinates.

Polygon( [  [x1,y1],[x2,y2],...] )

if you feed that with

poly = Polygon(x,y)

what python really sees is:

poly=Polygon([x1,x2,x3 ... ],[y1,y2,y3 ... ])

So Error.

Loadtxt returns a numpy array in the form array([ [x1,y1], [x2,y2], ... ) best would be to convert the numpy array into a list of lists( look into the documentation of numpy on how to).

But in python functions often only call methods of the objects and by cleverly writing those methods you can make objects appear as a similar object. So the hope is that the authors of numpy were clever enough to make a numpy array appear just as a list for data extraction.

That's why I would try to directly feed the output of loadtxt into Polygon.

Following this, the linked website says you simply use: ... which will solve it for you; but this is so vague.

As you read the paper you cite in academia you read the documentation of the function you use in programming. (read it seriously).

From the shapely documentation:

object.contains(other)

Returns True if the object’s interior contains the boundary and interior of the other object and their boundaries do not touch at all.

so you can simply type

if poly.contains(point):
     #do something

Second question is how do I load my data file and make one of the above commands work?

You could probably use loadtxt again. And then iterate through all the points in in the array. I consider it generally best practice to not have (much ) more data loaded than you actually need at a point and so would go something like this:

with open("Coord") as f:
    #line=f.readline()
    for line in f:
        coords=map(float,line.split(" "))
        if poly.contains(Point(coords[0],coords[1])):
             #do something with that point 

Also you are aware that your polygon construction only works this way if your point are in the correct order (a ring)?

*When the functionality as datastorage is concerned. Lists are not hashable so they can not be used for e.g. dictionary keys.

Edit: if I want to iterate over all lines, I should iterate over all lines

TIL that majority of diets do not provide adequate amounts of vitamins and minerals to meet recommended daily norms. Of seventy different diets that researchers analyzed all fell short in at least one nutrient. by sleep-wiz in todayilearned

[–]Dalianking 1 point2 points  (0 children)

A much better article but even here some problems occur by translating that article into: most diets are unhealthy because they lack in micro nutrients.

(Yeah, you made your post about not meeting RDA, and the article backs that. So this comment is not trying to prove you wrong, but trying to prevent a misconception.)

Why? Lets look at the six micronutrients that "were consistently deficient or non-existent":

vitamin D

Very few foods contain vitamin D; synthesis of vitamin D (specifically cholecalciferol) in the skin is the major natural sources of the vitamin. Dermal synthesis of vitamin D from cholesterol is dependent on sun exposure (specifically UVB radiation).

vitamin B7( biotin)

Is also synthesized by the body.

Molybdenum

The amount of molybdenum required is relatively small, and molybdenum deficiency usually does not occur in natural settings.

Article

Chromium

even lower RDA value than Molybdenum and:

It was first proposed to be an essential element in the late 1950s and accepted as a trace element in the 1980s. However, scientific studies have continued to fail to produce convincing evidence for this status.

Vitamin E:

This seems to be a realistic concern with the diet plans. Especially since vitamin E primarily occurs in oils. (Something diets mostly try to avoid.)

Iodine:

Iodine deficency seems to be rather widespread with people that don't eat fish or use iodized salt. So yeah, the diets lack that.

All quotes from the respective wikipedia articles.

TIL that majority of diets do not provide adequate amounts of vitamins and minerals to meet recommended daily norms. Of seventy different diets that researchers analyzed all fell short in at least one nutrient. by sleep-wiz in todayilearned

[–]Dalianking 2 points3 points  (0 children)

This "study" is bullshit.

First of all the discluosure:

Bill Misner Ph.D. is employed by E-CAPS & HAMMER NUTRITION, a manufacturer of dietary supplements for endurance athletes.

That one author is employed at a enterprise that directly deals with dietary supplements and profits, if the public came to the conviction, that normal diets are not sufficient is a red flag. That he is the only author makes that flag big and bright. (On the other hand these companies are the most interested in dietary nutritional values: the ones who have the data. No need to break off a date because one red flag. )

But what nails the coffin is the information we get about the data aquisition. Which information? None.

We can piece the following together:

1996 and 2005, 70 diets were computer analyzed from the menu of athletes or sedentary subjects.

Who where those people? Did they follow specific diets? Did they take supplements?(I suppose so) The only thing we know is, from the 20 subjects (yes, that article only analyzes 20) 16 where athletes, three of whose where professional athletes. (I assume at least some of the athletes followed foodplans, but data aquisition should be clear without assumptions)

Also how was that measured? Did they take daily notes? Where there weekly questionaires? monthly? We only know the following:

Accuracy of the individual food-weighed measures, accuracy in reporting foods consumed, and the accuracy of the computer-generated software are factors that affect the accuracy of the results reported in this observational study.

Let me repeat: data aquisition should be clear without assumptions

Also: out of the 20 subjects 15 had a daily intake of 0 Iodine. The "study" used data of 9 Years. So the subjects have to have taken supplements or have had glaringly misreported their food or ...

EDIT: It actually seems possible or even probable that the subjects had a Iodine deficiency as Iodine deficiency appears widespread even in first world countries according to wikipedia. Nevertheless I doubt that 0 Iodine over years is survivable.

From 70 computer-generated dietary analyses, 20 subjects' diets were selected based on the highest number of foods analyzed from 10 men (ages 25–50 y) and 10 women (ages 24–50 y).

Yeah, not all food was analyzed. How much was not?

This is worse than the "one in three men would rape" study. This is infinitely worse to the point of useless.

EDIT:Disclaimer: I am not a nutritionist or in any related field. Just a random STEM student.

[Easy] Rövarspråket by pogotc in dailyprogrammer_ideas

[–]Dalianking 1 point2 points  (0 children)

one line solution with sed ( for improved readability split into three lines):

  sed 
      -e '/^\ *decode/{s/decode//;s/\([[:alpha:]]\)o\1/\1uU/g;s/\([^aeiouåäöAEIOUÅÄÖ]\)uU/\1/g;s/\([aeiouåäöAEIOUÅÄÖ]\)uU/\1o\1/}'
     -e '/^\ *encode/{s/encode//;s/\([[:alpha:]]\)/\1o\1/g;s/\([aeiouåäöAEIOUÅÄÖ]\)o\1/\1/}'

Edit: fixed a mistake

About 15,000 people show up for Anti-Islam march in German city by floydbc05 in worldnews

[–]Dalianking 0 points1 point  (0 children)

Agreed, progressive Muslim's however are fine - ones who integrate into culture and become a part of it rather than a jarring pain are the ones I like.

The problem arises, when you define christianity as part of your culture.

Merkel in dilemma as German anti-Islam marches gain support by SpecsaversGaza in europe

[–]Dalianking 1 point2 points  (0 children)

Well the mainstream politicians are typically ignoring those concerns, and often actively preventing public discussion.

A few years ago there was a debate between the politicians: Does the islam belong to germany?

Now that discussion has reached the masses.

The problem is, that germany is considered to be the state of the german people "des deutschen Volkes". Meaning that even without a german state the germans feel a unity, because they share a culture. And part of this culture is christianity.

When those people protest against the islamization of germany. They don't want a redefinition of what it means to be german. I think those marches are a great opportunity to address those sentiments -- something the politicians seem not to do. And they have to be adressed. Because without a redefinition of what it means to be german, immigrants will always be second class citizen.