def validate_analysis_period(self):
a_per = self.header.analysis_period<EOL>n_ap = [a_per.st_month, a_per.end_month]<EOL>sort_datetimes, sort_values = zip(*sorted(zip(self.datetimes, self.values)))<EOL>if not a_per.is_reversed and not a_per.is_annual:<EOL><INDENT>if sort_datetimes[<NUM_LIT:0>] < a_per.st_month:<EOL><INDENT>n_ap[<NUM_LIT:0>] = sort_datetimes[<NUM_LIT:0>]<EOL><DEDENT>if sort_datetimes[-<NUM_LIT:1>] > a_per.end_month:<EOL><INDENT>n_ap[<NUM_LIT:1>] = sort_datetimes[-<NUM_LIT:1>]<EOL><DEDENT><DEDENT>elif a_per.is_reversed:<EOL><INDENT>last_ind = None<EOL>for i, date_t in enumerate(sort_datetimes):<EOL><INDENT>last_ind = i if date_t <= a_per.end_time.month else last_ind<EOL><DEDENT>if last_ind is not None:<EOL><INDENT>last_ind = last_ind + <NUM_LIT:1><EOL>sort_datetimes = sort_datetimes[last_ind:] + sort_datetimes[:last_ind]<EOL>sort_values = sort_values[last_ind:] + sort_values[:last_ind]<EOL><DEDENT>if sort_datetimes[<NUM_LIT:0>] > a_per.end_time.month andsort_datetimes[<NUM_LIT:0>] < a_per.st_time.month:<EOL><INDENT>n_ap = [<NUM_LIT:1>, <NUM_LIT:12>]<EOL>sort_datetimes, sort_values = zip(*sorted(zip(<EOL>self.datetimes, self.values)))<EOL><DEDENT><DEDENT>for i in xrange(len(sort_datetimes)):<EOL><INDENT>assert sort_datetimes[i] != sort_datetimes[i - <NUM_LIT:1>], '<STR_LIT>''<STR_LIT>'.format(sort_datetimes[i])<EOL><DEDENT>new_ap = AnalysisPeriod(st_month=n_ap[<NUM_LIT:0>], end_month=n_ap[<NUM_LIT:1>])<EOL>new_header = self.header.duplicate()<EOL>new_header._analysis_period = new_ap<EOL>new_coll = MonthlyCollection(new_header, sort_values, sort_datetimes)<EOL>new_coll._validated_a_period = True<EOL>return new_coll<EOL>
Get a collection where the header analysis_period aligns with datetimes.

        This means that checks for three criteria will be performed:
        1) All months in the data collection are chronological starting from the
            analysis_period start month to the end month.
        2) No duplicate months exist in the data collection.
        3) There are no months that lie outside of the analysis_period range.

        Note that there is no need to run this check any time that a
        data collection has been derived from a continuous one or when the
        validated_a_period attribute of the collection is True.