content
stringlengths
0
894k
type
stringclasses
2 values
""" References: https://fdc.nal.usda.gov/api-guide.html#food-detail-endpoint https://fdc.nal.usda.gov/portal-data/external/dataDictionary """ import datetime from typing import List, Dict, Union from datatrans import utils from datatrans.fooddata.detail.base import IdMixin from datatrans.fooddata.detail.nutrient import FoodNutrient, NutrientConversionFactor from datatrans.fooddata.search.request import FoodDataType from datatrans.utils.classes import JSONEnum as Enum __all__ = ['FoodClass', 'FoodCategory', 'FoodCategoryInstance', 'FoodAttribute', 'FoodAttributeType', 'FoodAttributeTypeInstance', 'MeasureUnit', 'FoodPortion', 'BrandedFood', 'SrLegacyFood'] def parse_fooddata_date(date_str: str) -> datetime.date: """ Wrapper specific for fooddata's format """ return utils.fooddata.parse_date(date_str, sep='/', format='MDY') def parse_food_nutrients(data: List[Dict[str, Union[str, int, float]]]) -> List[FoodNutrient]: return [FoodNutrient(_dict_=d) for d in data] def parse_label_nutrients(data: Dict[str, Dict[str, float]]) -> List[Dict[str, float]]: """ Change incoming data to be in list format. """ return [{k: v['value']} for k, v in data.items()] def parse_nutrient_conversion_factors(data: List[Dict[str, Union[str, float]]]) -> List[NutrientConversionFactor]: return [NutrientConversionFactor(_dict_=d) for d in data] def parse_food_portions(data: List[Dict[str, Union[str, float, int]]]) -> List['FoodPortion']: return [FoodPortion(_dict_=d) for d in data] def parse_food_attributes(data: List[Dict[str, Union[int, str, dict]]]) -> List['FoodAttribute']: return [FoodAttribute(_dict_=d) for d in data] class FoodClass(Enum): FOUNDATION = 'FinalFood' SURVEY = 'Survey' BRANDED = 'Branded' LEGACY = 'FinalFood' class FoodCategory(IdMixin, utils.DataClass): """Foods of defined similarity Attributes: id (int): code (str): Food group code description (str): Description of the food group """ __slots__ = ('id', 'code', 'description') __attr__ = ( ('id', int), ('code', str), ('description', str), ) class FoodCategoryInstance(Enum): DAIRY_AND_EGG_PRODUCTS = FoodCategory(_dict_={'id': 1, 'code': '0100', 'description': 'Dairy and Egg Products'}) SPICES_AND_HERBS = FoodCategory(_dict_={'id': 2, 'code': '0200', 'description': 'Spices and Herbs'}) BABY_FOODS = FoodCategory(_dict_={'id': 3, 'code': '0300', 'description': 'Baby Foods'}) FATS_AND_OILS = FoodCategory(_dict_={'id': 4, 'code': '0400', 'description': 'Fats and Oils'}) POULTRY_PRODUCTS = FoodCategory(_dict_={'id': 5, 'code': '0500', 'description': 'Poultry Products'}) SOUPS_SAUCES_AND_GRAVIES = FoodCategory(_dict_={'id': 6, 'code': '0600', 'description': 'Soups, Sauces, and Gravies'}) SAUSAGES_AND_LUNCHEON_MEATS = FoodCategory(_dict_={'id': 7, 'code': '0700', 'description': 'Sausages and Luncheon Meats'}) BREAKFAST_CEREALS = FoodCategory(_dict_={'id': 8, 'code': '0800', 'description': 'Breakfast Cereals'}) FRUITS_AND_FRUIT_JUICES = FoodCategory(_dict_={'id': 9, 'code': '0900', 'description': 'Fruits and Fruit Juices'}) PORK_PRODUCTS = FoodCategory(_dict_={'id': 10, 'code': '1000', 'description': 'Pork Products'}) VEGETABLES_AND_VEGETABLE_PRODUCTS = FoodCategory(_dict_={'id': 11, 'code': '1100', 'description': 'Vegetables and Vegetable Products'}) NUT_AND_SEED_PRODUCTS = FoodCategory(_dict_={'id': 12, 'code': '1200', 'description': 'Nut and Seed Products'}) BEEF_PRODUCTS = FoodCategory(_dict_={'id': 13, 'code': '1300', 'description': 'Beef Products'}) BEVERAGES = FoodCategory(_dict_={'id': 14, 'code': '1400', 'description': 'Beverages'}) FINFISH_AND_SHELLFISH_PRODUCTS = FoodCategory(_dict_={'id': 15, 'code': '1500', 'description': 'Finfish and Shellfish Products'}) LEGUMES_AND_LEGUME_PRODUCTS = FoodCategory(_dict_={'id': 16, 'code': '1600', 'description': 'Legumes and Legume Products'}) LAMB_VEAL_AND_GAME_PRODUCTS = FoodCategory(_dict_={'id': 17, 'code': '1700', 'description': 'Lamb, Veal, and Game Products'}) BAKED_PRODUCTS = FoodCategory(_dict_={'id': 18, 'code': '1800', 'description': 'Baked Products'}) SWEETS = FoodCategory(_dict_={'id': 19, 'code': '1900', 'description': 'Sweets'}) CEREAL_GRAINS_AND_PASTA = FoodCategory(_dict_={'id': 20, 'code': '2000', 'description': 'Cereal Grains and Pasta'}) FAST_FOODS = FoodCategory(_dict_={'id': 21, 'code': '2100', 'description': 'Fast Foods'}) MEALS_ENTREES_AND_SIDE_DISHES = FoodCategory(_dict_={'id': 22, 'code': '2200', 'description': 'Meals, Entrees, and Side Dishes'}) SNACKS = FoodCategory(_dict_={'id': 23, 'code': '2500', 'description': 'Snacks'}) AMERICAN_INDIAN_ALASKA_NATIVE_FOODS = FoodCategory(_dict_={'id': 24, 'code': '3500', 'description': 'American Indian/Alaska Native Foods'}) RESTAURANT_FOODS = FoodCategory(_dict_={'id': 25, 'code': '3600', 'description': 'Restaurant Foods'}) BRANDED_FOOD_PRODUCTS_DATABASE = FoodCategory(_dict_={'id': 26, 'code': '4500', 'description': 'Branded Food Products Database'}) QUALITY_CONTROL_MATERIALS = FoodCategory(_dict_={'id': 27, 'code': '2600', 'description': 'Quality Control Materials'}) ALCOHOLIC_BEVERAGES = FoodCategory(_dict_={'id': 28, 'code': '1410', 'description': 'Alcoholic Beverages'}) class FoodAttributeType(IdMixin, utils.DataClass): """The list of supported attributes associated with a food Attributes: id (int): name (str): Name of the attribute associated with the food - should be displayable to users description (str): Description of the attribute """ __attr__ = ( ('id', int), ('name', str), ('description', str), ) class FoodAttributeTypeInstance(Enum): ATTRIBUTE = FoodAttributeType(_dict_={'id': 999, 'name': 'Attribute', 'description': 'Generic attributes'}) COMMON_NAME = FoodAttributeType(_dict_={'id': 1000, 'name': 'Common Name', 'description': 'Common names associated with a food.'}) ADDITIONAL_DESCRIPTION = FoodAttributeType(_dict_={'id': 1001, 'name': 'Additional Description', 'description': 'Additional descriptions for the food.'}) ADJUSTMENTS = FoodAttributeType(_dict_={'id': 1002, 'name': 'Adjustments', 'description': 'Adjustments made to foods, including moisture and fat changes.'}) class FoodAttribute(IdMixin, utils.DataClass): """The value for a generic property of a food Attributes: id (int): fdc_id (int): ID of the food this food attribute pertains to sequence_number (int): The order the attribute will be displayed on the released food. food_attribute_type (FoodAttributeType): Type of food attribute to which this value is associated for a specific food name (str): Name of food attribute value: The actual value of the attribute """ __attr__ = ( ('id', int), ('fdc_id', int), ('sequence_number', int), ('food_attribute_type', FoodAttributeType), ('name', str), ('value', str), ) class MeasureUnit(IdMixin, utils.DataClass): """units for measuring quantities of foods Attributes: id (int): name: name of the unit abbreviation: abbreviated name of the unit """ __slots__ = ('id', 'name', 'abbreviation') __attr__ = ( ('id', int), ('name', str), ('abbreviation', str), ) class FoodPortion(IdMixin, utils.DataClass): """Discrete amount of food Attributes: id (int): fdc_id: ID of the food this food portion pertains to seq_num: The order the measure will be displayed on the released food. amount: The number of measure units that comprise the measure (e.g. if measure is 3 tsp, the amount is 3). Not defined for survey (FNDDS) foods (amount is instead embedded in portion description). measure_unit: The unit used for the measure (e.g. if measure is 3 tsp, the unit is tsp). For food types that do not use measure SR legacy foods and survey (FNDDS) foods), a value of '9999' is assigned to this field. portion_description: Foundation foods: Comments that provide more specificity on the measure. For example, for a pizza measure the dissemination text might be 1 slice is 1/8th of a 14 inch pizza"." Survey (FNDDS) foods: The household description of the portion. modifier: Foundation foods: Qualifier of the measure (e.g. related to food shape or form) (e.g. melted, crushed, diced). Survey (FNDDS) foods: The portion code. SR legacy foods: description of measures, including the unit of measure and the measure modifier (e.g. waffle round (4" dia)). gram_weight: The weight of the measure in grams data_points: The number of observations on which the measure is based footnote: Comments on any unusual aspects of the measure. These are released to the public. Examples might include caveats on the usage of a measure, or reasons why a measure gram weight is an unexpected value. min_year_acquired: Minimum purchase year of all acquisitions used to derive the measure value """ __slots__ = ('id', 'measure_unit', 'modifier', 'gram_weight', 'data_points', 'amount', 'sequence_number') __attr__ = ( ('id', int), ('measure_unit', MeasureUnit), ('modifier', str), ('gram_weight', float), ('data_points', int), ('amount', float), ('sequence_number', int), ('portion_description', str), # Unit ('min_year_acquired', int), ) class BrandedFood(utils.DataClass): """ Foods whose nutrient values are typically obtained from food label data provided by food brand owners. Attributes: fdc_id (int): ID of the food in the food table brand_owner: Brand owner for the food gtin_upc: GTIN or UPC code identifying the food ingredients: The list of ingredients (as it appears on the product label) serving_size (float): The amount of the serving size when expressed as gram or ml serving_size_unit: The unit used to express the serving size (gram or ml) household_serving_fulltext: amount and unit of serving size when expressed in household units branded_food_category: The category of the branded food, assigned by GDSN or Label Insight data_source: The source of the data for this food. GDSN (for GS1) or LI (for Label Insight). modified_date (datetime.date): This date reflects when the product data was last modified by the data provider, i.e., the manufacturer available_date (datetime.date): This is the date when the product record was available for inclusion in the database. """ __slots__ = ('fdc_id', 'brand_owner', 'gtin_upc', 'ingredients', 'serving_size', 'household_serving_full_text', 'branded_food_category', 'data_source', 'modified_date', 'available_date', 'food_class', 'description', 'food_nutrients', 'food_components', 'food_attributes', 'table_alias_name', 'serving_size_unit', 'label_nutrients', 'data_type', 'publication_date', 'food_portions', 'changes') __attr__ = ( # Excel ('fdc_id', int), ('brand_owner', str), ('gtin_upc', str), # 11 digits of number (0-9) ('ingredients', str), # csv (with spaces) ('serving_size', float), # may be int ('household_serving_full_text', str), # cup ('branded_food_category', str), ('data_source', str), # "LI" ('modified_date', datetime.date, parse_fooddata_date), ('available_date', datetime.date, parse_fooddata_date), # actual JSON ('food_class', FoodClass), # FoodClass.BRANDED ('description', str), ('food_nutrients', list, parse_food_nutrients), ('food_components', list), ('food_attributes', list), ('table_alias_name', str), # "branded_food" ('serving_size_unit', str), # lowercase g ('label_nutrients', list, # type: List[Dict[str, float]] parse_label_nutrients), ('data_type', FoodDataType), ('publication_date', datetime.date, parse_fooddata_date), ('food_portions', list, # type: List[FoodPortion] parse_food_portions), ('changes', str), ) class Food(utils.DataClass): """Any substance consumed by humans for nutrition, taste and/or aroma. Attributes: fdc_id (int): Unique permanent identifier of the food food_class (FoodClass): For internal use only data_type (FoodDataType): Type of food data (see Files tab for possible values). description (str): Description of the food food_category_id: Id of the food category the food belongs to publication_date (datetime.date): Date when the food was published to FoodData Central scientific_name (datetime.date): The scientific name for the food food_key: A string of characters used to identify both the current and all historical records for a specific food. """ __slots__ = ( 'fdc_id', 'description', 'data_type', 'published_date', 'all_highlight_fields', 'score', 'food_code', 'gtin_upc', 'brand_owner', 'additional_descriptions') __attr__ = ( ('fdc_id', int), ('food_class', FoodClass), ('data_type', FoodDataType), ('description', str), ('food_category_id', str), ('publication_date', datetime.date, parse_fooddata_date), ('scientific_name', str), ('food_key', str), ) class FoundationFood(utils.DataClass): """ Foods whose nutrient and food component values are derived primarily by chemical analysis. Foundation data also include extensive underlying metadata, such as the number of samples, the location and dates on which samples were obtained, analytical approaches used, and if appropriate, cultivar, genotype, and production practices. Attributes: fdc_id (int): ID of the food in the food table NDB_number: Unique number assigned for the food, different from fdc_id, assigned in SR footnote (str): Comments on any unusual aspects. These are released to the public Examples might include unusual aspects of the food overall. """ __attr__ = ( ('fdc_id', int), ('NDB_number', str), # temp ('footnote', str), # actual JSON ('food_class', FoodClass), ('food_nutrients', list, # type: List[FoodNutrient] parse_food_nutrients), ('description', str), ('food_components', list), ('food_attributes', list, # type: List[FoodAttribute] parse_food_attributes), ('table_alias_name', str), ('nutrient_conversion_factors', list, # type: List[NutrientConversionFactor] parse_nutrient_conversion_factors), ('is_historical_reference', bool), ('ndb_number', str), ('publication_date', datetime.date, parse_fooddata_date), ('food_category', FoodCategory), ('food_portions', list, # type: List[FoodPortion] parse_food_portions), ('data_type', FoodDataType), ('input_foods', list), ('changes', str), ) class SrLegacyFood(utils.DataClass): """ Foods from the April 2018 release of the USDA National Nutrient Database for Standard Reference. Nutrient and food component values are derived from chemical analysis and calculation. Attributes: fdc_id (int): ID of the food in the food table NDB_number: Unique number assigned for the food, different from fdc_id, assigned in SR """ __slots__ = ( 'ndb_number', 'fdc_id', 'food_class', 'description', 'food_nutrients', 'food_components', 'food_attributes', 'table_alias_name', 'nutrient_conversion_factors', 'is_historical_reference', 'data_type', 'data_type', 'food_category', 'food_portions', 'input_foods', 'publication_date', 'changes') __attr__ = ( # Excel ('ndb_number', str), ('fdc_id', int), # actual JSON ('food_class', FoodClass), ('description', str), ('food_nutrients', list, # type: List[FoodNutrient] parse_food_nutrients), ('food_components', list), ('scientific_name', str), ('food_attributes', list, # type: List[FoodAttribute] parse_food_attributes), ('table_alias_name', str), ('nutrient_conversion_factors', list, # type: List[NutrientConversionFactor] parse_nutrient_conversion_factors), ('is_historical_reference', bool), ('data_type', FoodDataType), ('food_category', FoodCategory), ('food_portions', list, # type: List[FoodPortion] parse_food_portions), ('input_foods', list), ('publication_date', datetime.date, parse_fooddata_date), ('changes', str), ('footnote', str), ) def __init__(self, _dict_: dict = None, **kwargs): super().__init__(_dict_, **kwargs) if self.food_class is not FoodClass.LEGACY: raise ValueError('invalid value for \'{}\': \'{}\' \'{}\'' .format(self.__class__.__name__, 'food_class', self.food_class)) if self.data_type is not FoodDataType.LEGACY: raise ValueError('invalid value for \'{}\': \'{}\' \'{}\'' .format(self.__class__.__name__, 'data_type', self.data_type)) if self.table_alias_name != 'sr_legacy_food': raise ValueError('invalid value for \'{}\': \'{}\' \'{}\'' .format(self.__class__.__name__, 'table_alias_name', self.table_alias_name)) @property def common_names(self): """ Returns the common name if any, else None """ for attr in self.food_attributes: if attr.food_attribute_type == FoodAttributeTypeInstance.COMMON_NAME.value: return attr.value class SurveyFnddsFood(utils.DataClass): """ Foods whose consumption is measured by the What We Eat In America dietary survey component of the National Health and Nutrition Examination Survey (NHANES). Survey nutrient values are usually calculated from Branded and SR Legacy data. Attributes: fdc_id (int): ID of the food in the food table food_code (str): A unique ID identifying the food within FNDDS wweia_category_code: Unique Identification code for WWEIA food category to which this food is assigned start_date (datetime.date): Start date indicates time period corresponding to WWEIA data end_date (datetime.date): End date indicates time period corresponding to WWEIA data """ __attr__ = ( ('fdc_id', int), ('food_code', str), ('start_date', datetime.date, parse_fooddata_date), ('end_date', datetime.date, parse_fooddata_date), ('food_class', FoodClass), ('description', str), ('food_nutrients', list, # type: List[FoodNutrient] parse_food_nutrients), ('food_components', list), ('scientific_name', str), ('food_attributes', list, # type: List[FoodAttribute] parse_food_attributes), ('table_alias_name', str), ('wweia_category', str), # need further process ('wweia_food_category', str), # need further process ('data_type', FoodDataType), ('publication_date', datetime.date, parse_fooddata_date), ('food_portions', list, # type: List[FoodPortion] parse_food_portions), ('input_foods', list), ('changes', str), )
python
import sys sys.modules['pkg_resources'] = None import pygments.styles import prompt_toolkit
python
# -- # File: aelladata/aelladata_consts.py # # Copyright (c) Aella Data Inc, 2018 # # This unpublished material is proprietary to Aella Data. # All rights reserved. The methods and # techniques described herein are considered trade secrets # and/or confidential. Reproduction or distribution, in whole # or in part, is forbidden except by express written permission # of Aella Data. # # -- AELLADATA_JSON_DEVICE_URL = "url" AELLADATA_JSON_QUERY = "query" AELLADATA_JSON_INDEX = "index" AELLADATA_JSON_TYPE = "type" AELLADATA_JSON_ROUTING = "routing" AELLADATA_JSON_TOTAL_HITS = "total_hits" AELLADATA_JSON_TIMED_OUT = "timed_out" AELLADATA_ERR_CONNECTIVITY_TEST = "Connectivity test failed" AELLADATA_SUCC_CONNECTIVITY_TEST = "Connectivity test passed" AELLADATA_ERR_SERVER_CONNECTION = "Connection failed" AELLADATA_ERR_FROM_SERVER = "API failed, Status code: {status}, Detail: {detail}" AELLADATA_MSG_CLUSTER_HEALTH = "Querying cluster health to check connectivity" AELLADATA_ERR_API_UNSUPPORTED_METHOD = "Unsupported method" AELLADATA_USING_BASE_URL = "Using url: {base_url}" AELLADATA_ERR_JSON_PARSE = "Unable to parse reply as a Json, raw string reply: '{raw_text}'"
python
from typing import Dict, List from rest_framework import serializers from rest_framework.exceptions import ValidationError from rest_framework.fields import empty from rest_framework.reverse import reverse from rest_framework_nested import serializers as nested_serializers from api.helpers import uuid_helpers from bettersocial.models import * class AuthorSerializer(serializers.ModelSerializer): type = models.CharField(max_length = 32) id = serializers.HyperlinkedIdentityField( view_name = 'api:author-detail', ) url = serializers.Field(default = None) host = serializers.SerializerMethodField( method_name = 'get_host' ) displayName = serializers.SerializerMethodField( method_name = 'get_name' ) github = serializers.SerializerMethodField( method_name = 'get_github' ) profileImage = serializers.Field(default = None) # Not required, but for convenience posts = serializers.HyperlinkedIdentityField( view_name = 'api:post-list', lookup_url_kwarg = 'author_pk', ) def get_github(self, instance: Author): return instance.github_url if instance.github_url else "" def get_host(self, instance: Author): return reverse('api:api-root', request = self.context['request']) def get_name(self, instance: Author): # TODO: 2021-10-25 simplify return instance.display_name def to_representation(self, instance): json = super().to_representation(instance) json['id'] = uuid_helpers.remove_uuid_dashes(json['id']) json['url'] = json['id'] return json class Meta: model = Author fields = [ 'type', 'id', 'url', 'host', 'displayName', 'github', 'profileImage', 'posts' ] extra_kwargs = { 'github': { 'source': 'github_url' } } class FollowerSerializer(serializers.ModelSerializer): class Meta: model = Follower fields = '__all__' class CommentSerializer(serializers.ModelSerializer): type = models.CharField(max_length = 32) id = nested_serializers.NestedHyperlinkedIdentityField( view_name = 'api:comment-detail', parent_lookup_kwargs = { 'post_pk': 'post__pk', 'author_pk': 'post__author__pk', } ) author = serializers.SerializerMethodField( method_name = 'get_author' ) def get_author(self, instance: Comment): # TODO: 2021-11-22 refactor for remote authors return AuthorSerializer(instance = instance.author_local, context = self.context, read_only = True).data def to_representation(self, instance): json = super().to_representation(instance) json['id'] = uuid_helpers.remove_uuid_dashes(json['id']) return json class Meta: model = Comment fields = [ 'type', 'author', 'comment', 'contentType', 'published', 'id', ] extra_kwargs = { 'contentType': { 'source': 'content_type', } } class PostSerializer(serializers.ModelSerializer): id = nested_serializers.NestedHyperlinkedIdentityField( view_name = 'api:post-detail', parent_lookup_kwargs = { 'author_pk': 'author__pk' } ) type = models.CharField(max_length = 32) count = serializers.IntegerField( source = 'comments.count', read_only = True, ) comments = nested_serializers.NestedHyperlinkedIdentityField( view_name = 'api:comment-list', read_only = True, lookup_url_kwarg = 'post_pk', parent_lookup_kwargs = { 'author_pk': 'author__pk', } ) author = AuthorSerializer(read_only = True) commentsSrc = serializers.SerializerMethodField( method_name = 'get_comments' ) def get_comments(self, instance: Post): # Gotta hardcode this stuff because there's no way to get the "list representation" without a circular import return { 'type': 'comments', 'page': 1, 'size': 5, 'post': None, # Both to be filled in to_representation because we can't reference an existing field here, apparently. 'id': None, 'comments': CommentSerializer(instance.comments.order_by('-published')[:5], context = self.context, many = True).data, } published = serializers.DateTimeField(format = 'iso-8601') def to_representation(self, instance): json = super().to_representation(instance) json['id'] = uuid_helpers.remove_uuid_dashes(json['id']) json['comments'] = uuid_helpers.remove_uuid_dashes(json['comments']) # Set defaults for source and origin, if they don't exist. This shouldn't really happen but just in case if json['source'] is None: json['source'] = json['id'] if json['origin'] is None: json['origin'] = json['id'] # Fill in repeated data because that's the spec json['commentsSrc']['post'] = json['id'] json['commentsSrc']['id'] = json['comments'] return json class Meta: model = Post fields = [ 'type', 'title', 'id', 'source', 'origin', 'description', 'contentType', 'content', 'author', 'categories', 'count', 'comments', 'commentsSrc', 'published', 'visibility', 'unlisted' ] extra_kwargs = { 'contentType': { 'source': 'content_type', } } class BaseLikeSerializer(serializers.ModelSerializer): summary = serializers.SerializerMethodField( method_name = 'get_summary' ) type = models.CharField(max_length = 32) author = serializers.SerializerMethodField( method_name = 'get_author' ) # TODO: 2021-10-25 refactor later for remote authors @staticmethod def helper_get_author(instance: Like) -> Author: return instance.author_local def get_summary(self, instance: Like): return f'{self.helper_get_author(instance).display_name} Likes your post' def get_author(self, instance: Like): return AuthorSerializer(self.helper_get_author(instance), context = self.context).data def to_representation(self, instance): json = super().to_representation(instance) json['@context'] = 'https://www.w3.org/ns/activitystreams' json.move_to_end('@context', last = False) return json class Meta: model = Like fields = [ # '@context', 'summary', 'type', 'author', # 'object', ] class PostLikeSerializer(BaseLikeSerializer): pass class CommentLikeSerializer(BaseLikeSerializer): pass class InboxItemSerializer(serializers.ModelSerializer): """The ModelSerializer part of this is only for the GET part -- POST is kind of custom""" def __init__(self, instance = None, data = empty, **kwargs): super().__init__(instance, data, **kwargs) self.types = { 'post': { 'model': Post, 'validator': self._validate_post }, 'comment': { 'model': Comment, 'validator': self._validate_comment }, 'like': { 'model': Like, 'validator': self._validate_like }, 'follow': { 'model': Follower, 'validator': self._validate_follower }, } def create(self, validated_data): # We have to access the raw request since DRF blows any fields that are not part of the Model data: Dict = self.context['request'].data inbox_item = InboxItem.objects.create( author_id = self.context['author_id'], dj_content_type = DjangoContentType.objects.get_for_model(model = self.types[data['type']]['model']), inbox_object = data ) return inbox_item def validate(self, attrs): # We have to access the raw request since DRF blows any fields that are not part of the Model data: Dict = self.context['request'].data # Make sure that type is there self._validate_required(data, ['type']) # Fix type field data['type'] = data['type'].strip().lower() if data['type'] not in self.types.keys(): raise ValidationError({ 'type': f'type must be one of {{{", ".join(self.types.keys())}}}!' }) # Access the validator for that type and call it. It might change request.data somehow self.types[data['type']]['validator'](data) # return attrs, not data, to make DRF happy return attrs def _validate_required(self, data: Dict, required_fields: List): for field in required_fields: if field not in data: raise ValidationError({ field: 'This field is required!' }) def _validate_post(self, data: Dict): # Don't really care about the other fields self._validate_required(data, [ 'title', 'id', 'description', 'contentType', 'content', 'author', 'visibility' ]) return data def _validate_comment(self, data: Dict): return data def _validate_like(self, data: Dict): return data def _validate_follower(self, data: Dict): # Don't really care about the other fields self._validate_required(data, [ 'actor', 'object', ]) if not isinstance(data['actor'], dict): raise ValidationError({ 'actor': 'This field must be an object containing an author!' }) # Supposedly our author if not isinstance(data['object'], dict): raise ValidationError({ 'object': 'This field must be an object containing an author!' }) self._validate_required(data['actor'], [ 'type', 'id', 'host' ]) # Supposedly our author self._validate_required(data['object'], [ 'type', 'id', 'host' ]) author_uuid = uuid_helpers.extract_author_uuid_from_id(data['object']['id']) if author_uuid is None: raise ValidationError({ 'object': 'The author\'s `id` field must have a valid author UUID!' }) # Make sure the target is our author if UUID(self.context['author_id']) != author_uuid: raise ValidationError({ 'object': 'The author\'s `id` field must match the author you\'re sending it to!' }) return data def to_representation(self, instance): json = super().to_representation(instance) # Flatten representation json = json['inbox_object'] return json class Meta: model = InboxItem fields = ['inbox_object'] class RemotePostSerializer(serializers.Serializer): post = models.JSONField() author_uuids = models.JSONField() def update(self, instance, validated_data): pass def create(self, validated_data): pass
python
# -*- coding: utf-8 -*- # Generated by Django 1.9.6 on 2016-06-07 07:14 from __future__ import unicode_literals from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('tasks', '0002_auto_20160607_1133'), ] operations = [ migrations.AddField( model_name='regexpchecker', name='flag_ignore_case', field=models.BooleanField(default=False, help_text="Python's re.IGNORECASE (re.I)"), ), ]
python
from os import path import numpy as np import torch import torch.utils.data as td import torchvision.datasets as dsets import torchvision.transforms as tt import vsrl_utils as vu from faster_rcnn.datasets.factory import get_imdb import faster_rcnn.roi_data_layer.roidb as rdl_roidb import faster_rcnn.fast_rcnn.config as cf import model as md """ Due to different conventions used by 3rd party software :(, you need the following: You need a VCOCO_ROOT directory that has images stored like this: $VCOCO_ROOT/coco/images/train2014/ $VCOCO_ROOT/coco/images/val2014/ You also need a COCO_ROOT directory that has images stored like this: $COCO_ROOT/images/ """ HOME = path.expanduser("~") VCOCO_ROOT = path.join(HOME, "data/v-coco/") COCO_ROOT = path.join(HOME, "data/coco/") class PathManager(object): def __init__(self, coco_root=COCO_ROOT, vcoco_root=VCOCO_ROOT): self.coco_root = coco_root self.vcoco_root = vcoco_root @property def coco_imgs(self): return path.join(self.coco_root, "images/") @property def coco_vcoco_ann(self): return path.join( self.vcoco_root, "data/instances_vcoco_all_2014.json") defaultpm = PathManager() def get_vsrl_labels(vcoco_set): return path.join(VCOCO_ROOT, "data/vcoco/%s.json" % vcoco_set) def get_ids(vcoco_set): return path.join(VCOCO_ROOT, "data/splits/%s.ids" % vcoco_set) def get_imgid_2_vcoco_labels(vcoco_all, coco): """ Get a dict from annotation id to vcoco image labels. """ ret = {} for verb_dict in vcoco_all: verb_dict = vu.attach_gt_boxes(verb_dict, coco) action_name = verb_dict["action_name"] for i in xrange(len(verb_dict["image_id"])): img_id = verb_dict["image_id"][i][0] if img_id not in ret: ret[img_id] = { "image_id": img_id, #"image_path": coco.loadImgs([img_id])[0]["filename"], "verbs": {}, } # Don't overwrite verb_dict while iterating. ret[img_id]["verbs"][action_name] = \ { "role_object_id": verb_dict["role_object_id"][i], "role_name": verb_dict["role_name"], "label": verb_dict["label"][i], "role_bbox": verb_dict["role_bbox"][i], "include": verb_dict["include"], "bbox": verb_dict["bbox"][i], } return ret def role_is_not_agent(agentrole_list): return "agent" != x[1] def split_action_role(agentrole): return agentrole.split("-") class VCocoTranslator(object): def __init__(self, vcoco_all, categories): self.ids_2_actions = sorted([x['action_name'] for x in vcoco_all]) self.actions_2_ids = { name: i for i, name in enumerate(self.ids_2_actions)} # We expect that "categories" already contains a background category self.ids_2_nouns = categories self.nouns_2_ids = {x: i for i, x in enumerate(self.ids_2_nouns)} #classes = ['__background__'] + sorted([x[' #self.ids_2_classes = sortedo # this is a mapping that combines verb with role for localization # tasks. actionroles = [] actionroles_nonagent = [] self.action_roles_lookup = {} for verb in vcoco_all: roles = verb["role_name"] self.action_roles_lookup[verb["action_name"]] = roles for role in roles: actionrole_name = "%s-%s" % (verb["action_name"], role) actionroles.append(actionrole_name) if role != "agent": actionroles_nonagent.append(actionrole_name) self.ids_2_actionroles = sorted(actionroles) self.ids_2_actionrolesnonagent = sorted(actionroles_nonagent) self.actionroles_2_ids = { x: i for i, x in enumerate(self.ids_2_actionroles)} self.actionrolesnonagent_2_ids = { x: i for i, x in enumerate(self.ids_2_actionrolesnonagent)} @property def num_actions(self): return len(self.ids_2_actions) @property def num_action_roles(self): return len(self.ids_2_actionroles) @property def num_action_nonagent_roles(self): return len(self.ids_2_actionrolesnonagent) def get_action_labels(self, vcoco_labels): """ Get numeric labels for v-coco action classes vcoco_labels: a dict like: {"verbs": {"verb_name": {"label": 0 or 1}}} """ ret = np.empty(self.num_actions) for verb_name, labels in vcoco_labels["verbs"].iteritems(): ret[self.actions_2_ids[verb_name]] = labels["label"] return ret def get_action_nonagent_role_locations(self, vcoco_labels): """ Get a np.ndarray with size [1 x NActionRolesNonagent x 5] (The first index of the bounding box is the label) """ ret = np.empty([1, self.num_action_nonagent_roles, 5], dtype=np.float) for index, actionrole in enumerate(self.ids_2_actionrolesnonagent): action, role = actionrole.split("-") position = vcoco_labels["verbs"][action]["role_name"].index(role) ret[0,index,1:] = self.get_nth_role_bbox( vcoco_labels["verbs"][action]["role_bbox"], position) ret[0,index,0] = vcoco_labels["verbs"][action]["label"] * 1. return ret def action_role_iter(self): return it.ifilter(role_is_not_agent, it.imap(split_action_role, a)) def get_nth_role_bbox(self, numpy_data, index): return numpy_data[(4*index):(4*(index + 1))] def get_human_object_gt_pairs(self, vcoco_labels): """ TODO should a human-object pair only be trained for the single action on which its label appears? NBoxes will be the number of positive instances where a g.t. object and human have a positive label. Returns a tuple: tup[0] - a [NBoxes x 4] numpy.ndarray of human boxes tup[1] - a [NBoxes x 4] numpy.ndarray of object boxes tup[2] - a [NBoxes x NActionNonagentRoles] numpy.ndarray of gt labels It also ignores boxes in vcoco_labels that don't have a dimensions. """ tup0 = [] tup1 = [] tup2 = [] for index, actionrole in enumerate(self.ids_2_actionrolesnonagent): action, role = actionrole.split("-") if vcoco_labels["verbs"][action]["label"]: # This h_position quantity is always 0, AFAIK. Since agents are # always listed first. h_position = vcoco_labels["verbs"][action]["role_name"].index( "agent") o_position = vcoco_labels["verbs"][action]["role_name"].index( role) role_bbox = vcoco_labels["verbs"][action]["role_bbox"] if np.any(np.isnan(self.get_nth_role_bbox( role_bbox, o_position))): continue tup0.append(self.get_nth_role_bbox(role_bbox, h_position)) tup1.append(self.get_nth_role_bbox(role_bbox, o_position)) gt_labels = np.zeros(self.num_action_nonagent_roles) gt_labels[index] = 1. tup2.append(gt_labels) if len(tup0) == 0: return None, None, None return map(np.vstack, [tup0, tup1, tup2]) def human_scores_to_agentrolenonagent(self, h_scores): # Make something that is [NxNActions] into something that puts those # action scores only in locations corresponding to action-nonagent # prediction slots. ret = np.empty([h_scores.shape[0], self.num_action_nonagent_roles]) for index, action in enumerate(self.ids_2_actions, start=0): roles = self.action_roles_lookup[action] for role in roles: if role == "agent": continue actionrole = "%s-%s" % (action, role) ret_ind = self.actionrolesnonagent_2_ids[actionrole] ret[:, ret_ind] = h_scores[:, index] return ret # TODO possibly the only thing that needs COCO_ROOT is this? consider removing class VCocoBoxes(dsets.coco.CocoDetection): """ Subclass of CocoDetection dataset offered by pytorch's torchvision library https://github.com/pytorch/vision/blob/master/torchvision/datasets/coco.py """ def __init__( self, vcoco_set, coco_root, transform=None, coco_transform=None, combined_transform=None): # Don't call the superconstructor (we don't have an annFile) pm = PathManager(coco_root=coco_root) self.root = pm.coco_imgs self.coco = vu.load_coco() self.vcoco_all = vu.load_vcoco(vcoco_set) # If we don't convert to int, COCO library index lookup fails :( self.ids = [int(x) for x in self.vcoco_all[0]["image_id"].ravel()] self.transform = transform self.target_transform = coco_transform self.combined_transform = combined_transform # Get per-image vcoco labels, indexed by image id. self.imgid_2_vcoco = get_imgid_2_vcoco_labels(self.vcoco_all, self.coco) def __getitem__(self, index): img_id = self.ids[index] vcoco_ann = self.imgid_2_vcoco[img_id] img, coco_ann = super(VCocoBoxes, self).__getitem__(index) target = (coco_ann, vcoco_ann) if self.combined_transform is not None: target = self.combined_transform(target) return (img, target) class RoiVCocoBoxes(VCocoBoxes): """ Subclass of CocoDetection dataset offered by pytorch's torchvision library https://github.com/pytorch/vision/blob/master/torchvision/datasets/coco.py """ def __init__( self, vcoco_set, coco_root, vcoco_root): super(RoiVCocoBoxes, self).__init__(vcoco_set, coco_root) # TODO this sets a global config, which I prefer not to do. But the # faster_rcnn code depends on it. cf.cfg_from_list(["DATA_DIR", vcoco_root]) if vcoco_set == "vcoco_train": coco_split = "train" elif vcoco_set == "vcoco_val": coco_split = "val" else: raise ValueError("Invalid vcoco_set '%s'" % vcoco_set) imdb_name = "coco_2014_" + coco_split self._imdb = get_imdb(imdb_name) rdl_roidb.prepare_roidb(self._imdb) self._roidb = self._imdb.roidb self.cocoimgid_2_roidbindex = { index: i for i, index in enumerate(self._imdb._image_index)} def __getitem__(self, index): img_id = self.ids[index] vcoco_ann = self.imgid_2_vcoco[img_id] roidb_entry = self._roidb[self.cocoimgid_2_roidbindex[img_id]] return (roidb_entry, vcoco_ann) def get_classes(self): return self._imdb._classes def targ_trans(target): return torch.Tensor(target[1]["verbs"]["throw"]["label"]) # TODO delete this. def get_loader(vcoco_set, coco_dir): transforms = tt.Compose([ tt.Scale(md.IMSIZE), tt.ToTensor(), ]) dataset = VCocoBoxes( vcoco_set, coco_dir, transform=transforms, combined_transform=targ_trans) return td.DataLoader(dataset, batch_size=16, shuffle=True, num_workers=4) def get_label_loader(vcoco_set, coco_dir, test=False): # Similar to get_loader(), but gives a loader that gives all the full labels transforms = tt.Compose([ tt.Scale(md.IMSIZE), tt.ToTensor(), ]) if not test: cls = VCocoBoxes else: cls = FakeVCocoBoxes dataset = cls(vcoco_set, coco_dir, transform=transforms) return td.DataLoader(dataset, batch_size=16, shuffle=True, num_workers=4) class FakeVCocoBoxes(VCocoBoxes): def __len__(self): return 40 class FakeDatasetLoader(object): def __init__(self, data): self.data = data def __iter__(self): return iter(self.data) def __len__(self): return len(self.data) def make_vcoco_test_loader(): loader = get_label_loader("vcoco_train", defaultpm.coco_imgs) outloc = "data/test_data.th" make_test_loader(loader, outloc) def get_test_dataset(loader): items = [] for i, data in enumerate(loader): items.append(data) if i > 0: break dataset = FakeDatasetLoader(items) return dataset def make_test_loader(loader, outloc): dataset = get_test_dataset(loader) torch.save(dataset, outloc) def get_test_loader(): dataset = torch.load("data/test_data.th") return dataset def make_roi_test_loader(): loader = RoiVCocoBoxes( "vcoco_train", defaultpm.coco_root, defaultpm.vcoco_root) outloc = "data/test_roi_data.th" dataset = get_test_dataset(loader) torch.save((dataset, loader._imdb._classes), outloc) def get_roi_test_loader(): dataset, classes = torch.load("data/test_roi_data.th") return dataset, classes
python
#Client name: Laura Atkins #Programmer name: Griffin Cosgrove #PA purpose: Program to determine net pay check of employees and provide new access codes. #My submission of this program indicates that I have neither received nor given unauthorized assistance in writing this program #Creating the prompts for the user to input their information s1=input("Enter employee name (first last):") no1=eval(input("Enter number of hours worked this last week:")) no2=eval(input("Enter hourly pay rate:")) s2=input("Enter Single or married (s or m):") no3=eval(input("Enter state tax witholding rate (as a decimal):")) #formula for gross pay pay=no1*no2 #formula for state witholding deduction. no4=no3*pay #conditional for applying the single or married tax rate. if s2=="s": witholding=.15*pay if s2=="m": witholding=.1*pay #formula for total deductions no5=witholding+no4 #Creating the access code y=len(s1)//3 #displaying the information the user entered and calculating and gross pay and deductions. #displaying the access code print(("\nEmployee Name:"),s1) print(("\nHours Worked:"), no1) print(("Pay Rate"),no2) print("Gross Pay: ",format(pay,',.2f'),sep='$') print("Deductions:") print("\tFederal Witholding: ",format(witholding,',.2f'),sep='$') print("\tState Witholding: ",format(no4,',.2f'),sep='$') print("\tTotal Deduction: ",format(no5,',.2f'),sep='$') print(("\nYour new access code is:"),s1[y:7],s1.upper()[-3:],min(s1),max(s1))
python
"""VBR ContainerType routes""" from typing import Dict from fastapi import APIRouter, Body, Depends, HTTPException from vbr.api import VBR_Api from vbr.utils.barcode import generate_barcode_string, sanitize_identifier_string from ..dependencies import * from .models import ContainerType, CreateContainerType, GenericResponse, transform from .utils import parameters_to_query router = APIRouter( prefix="/container_types", tags=["container_types"], responses={404: {"description": "Not found"}}, route_class=LoggingRoute, ) @router.get( "/", dependencies=[Depends(vbr_read_public)], response_model=List[ContainerType] ) def list_container_types( # See views/container_types_public.sql for possible filter names container_type_id: Optional[str] = None, name: Optional[str] = None, client: VBR_Api = Depends(vbr_admin_client), common=Depends(limit_offset), ): """List ContainerTypes. Refine results using filter parameters. Requires: **VBR_READ_PUBLIC**""" query = parameters_to_query(container_type_id=container_type_id, name=name) rows = [ transform(c) for c in client.vbr_client.query_view_rows( view_name="container_types_public", query=query, limit=common["limit"], offset=common["offset"], ) ] return rows @router.get( "/{container_type_id}", dependencies=[Depends(vbr_read_public)], response_model=ContainerType, ) def get_container_type_by_id( container_type_id: str, client: VBR_Api = Depends(vbr_admin_client), ): """Get a ContainerType by ID. Requires: **VBR_READ_PUBLIC**""" query = {"container_type_id": {"operator": "eq", "value": container_type_id}} row = transform( client.vbr_client.query_view_rows( view_name="container_types_public", query=query, limit=1, offset=0 )[0] ) return row # POST / @router.post( "/", dependencies=[Depends(vbr_write_public)], response_model=ContainerType ) def create_container_type( body: CreateContainerType = Body(...), client: Tapis = Depends(vbr_admin_client), ): """Create a new ContainerType. Requires: **VBR_WRITE_PUBLIC** """ try: container_type = client.create_container_type( name=body.name, description=body.description ) except Exception as exc: raise HTTPException(500, "Failed to create new container type: {0}".format(exc)) query = {"container_type_id": {"operator": "eq", "value": container_type.local_id}} row = transform( client.vbr_client.query_view_rows( view_name="container_types_public", query=query, limit=1, offset=0 )[0] ) return row # DELETE /{container_type_id} @router.delete( "/{container_type_id}", dependencies=[Depends(vbr_admin)], response_model=GenericResponse, ) def delete_container_type( container_type_id: str, client: VBR_Api = Depends(vbr_admin_client), ): """Delete a ContainerType. Requires: **VBR_ADMIN**""" container_type_id = vbr.utils.sanitize_identifier_string(container_type_id) container_type = client.get_container_type_by_local_id(container_type_id) client.vbr_client.delete_row(container_type) return {"message": "ContainerType deleted"} # TODO Later # PUT /{container_type_id} - update container_type
python
n1 = int(input("qual o salario do funcionario?")) s = 15 * n1 des = s/100 prom = n1 + des print("O salario atual do funcionario é de {} e com o aumento de 15% vai para {}".format(n1,prom))
python
# -*- coding: utf-8 -*- # Generated by Django 1.11 on 2018-11-03 20:19 from __future__ import unicode_literals from django.db import migrations, models import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ ('query', '0023_auto_20181101_2104'), ] operations = [ migrations.RemoveField( model_name='person', name='frame', ), migrations.RemoveField( model_name='pose', name='person', ), migrations.AlterField( model_name='face', name='frame', field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='query.Frame'), ), migrations.AlterField( model_name='pose', name='frame', field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='query.Frame'), ), migrations.RemoveField( model_name='face', name='person', ), migrations.AlterUniqueTogether( name='face', unique_together=set([('labeler', 'frame', 'bbox_x1', 'bbox_x2', 'bbox_y1', 'bbox_y2')]), ), migrations.DeleteModel( name='Person', ), ]
python
# encoding: utf-8 """ .. codeauthor:: Tsuyoshi Hombashi <[email protected]> """ from __future__ import absolute_import, print_function, unicode_literals import collections import re from textwrap import dedent import pytablewriter as ptw import pytest import six # noqa: W0611 from pytablewriter.style import Align, FontSize, Style, ThousandSeparator from tabledata import TableData from termcolor import colored from ..._common import print_test_result from ...data import ( float_header_list, float_value_matrix, headers, mix_header_list, mix_value_matrix, style_tabledata, styles, value_matrix, value_matrix_iter, value_matrix_iter_1, value_matrix_with_none, ) try: import pandas as pd SKIP_DATAFRAME_TEST = False except ImportError: SKIP_DATAFRAME_TEST = True Data = collections.namedtuple("Data", "table indent header value is_formatting_float expected") normal_test_data_list = [ Data( table="", indent=0, header=headers, value=value_matrix, is_formatting_float=True, expected=dedent( """\ | a | b | c |dd | e | |--:|----:|---|--:|----| | 1|123.1|a |1.0| 1| | 2| 2.2|bb |2.2| 2.2| | 3| 3.3|ccc|3.0|cccc| """ ), ), Data( table="", indent=0, header=headers, value=None, is_formatting_float=True, expected=dedent( """\ | a | b | c |dd | e | |---|---|---|---|---| """ ), ), Data( table="floating point", indent=0, header=headers, value=[ ["1", 123.09999999999999, "a", "1", 1], [2, 2.2000000000000002, "bb", "2.2", 2.2000000000000002], [3, 3.2999999999999998, "ccc", "3.2999999999999998", "cccc"], ], is_formatting_float=True, expected=dedent( """\ # floating point | a | b | c |dd | e | |--:|----:|---|--:|----| | 1|123.1|a |1.0| 1| | 2| 2.2|bb |2.2| 2.2| | 3| 3.3|ccc|3.3|cccc| """ ), ), Data( table="tablename", indent=1, header=headers, value=value_matrix, is_formatting_float=True, expected=dedent( """\ ## tablename | a | b | c |dd | e | |--:|----:|---|--:|----| | 1|123.1|a |1.0| 1| | 2| 2.2|bb |2.2| 2.2| | 3| 3.3|ccc|3.0|cccc| """ ), ), Data( table="", indent=0, header=headers, value=value_matrix_with_none, is_formatting_float=True, expected=dedent( """\ | a | b | c |dd | e | |--:|--:|---|--:|----| | 1| |a |1.0| | | |2.2| |2.2| 2.2| | 3|3.3|ccc| |cccc| | | | | | | """ ), ), Data( table="", indent=0, header=mix_header_list, value=mix_value_matrix, is_formatting_float=True, expected=dedent( """\ | i | f | c | if |ifc|bool| inf |nan|mix_num | time | |--:|---:|----|---:|---|----|--------|---|-------:|-------------------------| | 1|1.10|aa | 1.0| 1|X |Infinity|NaN| 1|2017-01-01T00:00:00 | | 2|2.20|bbb | 2.2|2.2| |Infinity|NaN|Infinity|2017-01-02 03:04:05+09:00| | 3|3.33|cccc|-3.0|ccc|X |Infinity|NaN| NaN|2017-01-01T00:00:00 | """ ), ), Data( table="formatting float 1", indent=0, header=headers, value=value_matrix, is_formatting_float=True, expected=dedent( """\ # formatting float 1 | a | b | c |dd | e | |--:|----:|---|--:|----| | 1|123.1|a |1.0| 1| | 2| 2.2|bb |2.2| 2.2| | 3| 3.3|ccc|3.0|cccc| """ ), ), Data( table="formatting float 2", indent=0, header=float_header_list, value=float_value_matrix, is_formatting_float=True, expected=dedent( """\ # formatting float 2 | a | b | c | |---:|----------:|----:| |0.01| 0.0012|0.000| |1.00| 99.9000|0.010| |1.20|999999.1230|0.001| """ ), ), Data( table="not formatting float 1", indent=0, header=headers, value=value_matrix, is_formatting_float=False, expected=dedent( """\ # not formatting float 1 | a | b | c |dd | e | |--:|----:|---|--:|----| | 1|123.1|a | 1| 1| | 2| 2.2|bb |2.2| 2.2| | 3| 3.3|ccc| 3|cccc| """ ), ), Data( table="not formatting float 2", indent=0, header=float_header_list, value=float_value_matrix, is_formatting_float=False, expected=dedent( """\ # not formatting float 2 | a | b | c | |---:|---------:|----:| |0.01| 0.00125| 0| | 1| 99.9| 0.01| | 1.2|999999.123|0.001| """ ), ), Data( table="", indent=0, header=["Name", "xUnit", "Source", "Remarks"], value=[ [ "Crotest", "", "[160]", "MIT License. A tiny and simple test framework for Crystal\nwith common assertions and no pollution into Object class.", "", ] ], is_formatting_float=True, expected=dedent( """\ | Name |xUnit|Source| Remarks | |-------|-----|------|--------------------------------------------------------------------------------------------------------------------| |Crotest| |[160] |MIT License. A tiny and simple test framework for Crystal with common assertions and no pollution into Object class.| """ ), ), Data( table="", indent=0, header=["姓", "名", "生年月日", "郵便番号", "住所", "電話番号"], value=[ ["山田", "太郎", "2001/1/1", "100-0002", "東京都千代田区皇居外苑", "03-1234-5678"], ["山田", "次郎", "2001/1/2", "251-0036", "神奈川県藤沢市江の島1丁目", "03-9999-9999"], ], is_formatting_float=True, expected=dedent( """\ | 姓 | 名 |生年月日|郵便番号| 住所 | 電話番号 | |----|----|--------|--------|--------------------------|------------| |山田|太郎|2001/1/1|100-0002|東京都千代田区皇居外苑 |03-1234-5678| |山田|次郎|2001/1/2|251-0036|神奈川県藤沢市江の島1丁目|03-9999-9999| """ ), ), Data( table="quoted values", indent=0, header=['"quote"', '"abc efg"'], value=[['"1"', '"abc"'], ['"-1"', '"efg"']], is_formatting_float=True, expected=dedent( """\ # quoted values |quote|abc efg| |----:|-------| | 1|abc | | -1|efg | """ ), ), Data( table="not str headers", indent=0, header=[None, 1, 0.1], value=[[None, 1, 0.1]], is_formatting_float=True, expected=dedent( """\ # not str headers | | 1 |0.1| |---|--:|--:| | | 1|0.1| """ ), ), Data( table="no uniform matrix", indent=0, header=["a", "b", "c"], value=[["a", 0], ["b", 1, "bb"], ["c", 2, "ccc", 0.1]], is_formatting_float=True, expected=dedent( """\ # no uniform matrix | a | b | c | |---|--:|---| |a | 0| | |b | 1|bb | |c | 2|ccc| """ ), ), Data( table="line breaks", indent=0, header=["a\nb", "\nc\n\nd\n", "e\r\nf"], value=[["v1\nv1", "v2\n\nv2", "v3\r\nv3"]], is_formatting_float=True, expected=dedent( """\ # line breaks | a b | c d | e f | |-----|-----|-----| |v1 v1|v2 v2|v3 v3| """ ), ), Data( table="empty header", indent=0, header=[], value=value_matrix, is_formatting_float=True, expected=dedent( """\ # empty header | A | B | C | D | E | |--:|----:|---|--:|----| | 1|123.1|a |1.0| 1| | 2| 2.2|bb |2.2| 2.2| | 3| 3.3|ccc|3.0|cccc| """ ), ), Data( table="vertical bar", indent=1, header=["a|b", "|c||d|"], value=[["|v1|v1|", "v2|v2"]], is_formatting_float=True, expected=r"""## vertical bar | a\|b |\|c\|\|d\|| |-------|------| |\|v1\|v1\||v2\|v2 | """, ), Data( table="mixed value types", indent=0, header=["data", "v"], value=[ [3.4375, 65.5397978633], [65.5397978633, 127.642095727], [189.74439359, 189.74439359], [10064.0097539, 10001.907456], ["next", 10250.3166474], ], is_formatting_float=True, expected=dedent( """\ # mixed value types | data | v | |---------|-------:| | 3.437| 65.54| | 65.540| 127.64| | 189.744| 189.74| |10064.010|10001.91| |next |10250.32| """ ), ), Data( table="list of dict", indent=0, header=["A", "B", "C"], value=[ {"A": 1}, {"B": 2.1, "C": "hoge"}, {"A": 0, "B": 0.1, "C": "foo"}, {}, {"A": -1, "B": -0.1, "C": "bar", "D": "extra"}, ], is_formatting_float=False, expected=dedent( """\ # list of dict | A | B | C | |--:|---:|----| | 1| | | | | 2.1|hoge| | 0| 0.1|foo | | | | | | -1|-0.1|bar | """ ), ), ] exception_test_data_list = [ Data( table="", indent=0, header=[], value=[], is_formatting_float=True, expected=ptw.EmptyTableDataError, ) ] table_writer_class = ptw.MarkdownTableWriter def trans_func(value): if value is None: return "" if value is True: return "X" if value is False: return "" return value class Test_MarkdownTableWriter_write_new_line(object): def test_normal(self, capsys): writer = table_writer_class() writer.write_null_line() out, _err = capsys.readouterr() assert out == "\n" class Test_MarkdownTableWriter_write_table(object): @pytest.mark.parametrize( ["table", "indent", "header", "value", "is_formatting_float", "expected"], [ [ data.table, data.indent, data.header, data.value, data.is_formatting_float, data.expected, ] for data in normal_test_data_list ], ) def test_normal(self, capsys, table, indent, header, value, is_formatting_float, expected): writer = table_writer_class() writer.table_name = table writer.set_indent_level(indent) writer.headers = header writer.value_matrix = value writer.is_formatting_float = is_formatting_float writer.register_trans_func(trans_func) writer.write_table() out, err = capsys.readouterr() print_test_result(expected=expected, actual=out, error=err) assert out == expected assert writer.dumps() == expected def test_normal_single_tabledata(self, capsys): writer = table_writer_class() writer.from_tabledata( TableData( "loader_mapping", ["Name", "Loader"], [ ["csv", "CsvTableFileLoader"], ["excel", "ExcelTableFileLoader"], ["html", "HtmlTableFileLoader"], ["markdown", "MarkdownTableFileLoader"], ["mediawiki", "MediaWikiTableFileLoader"], ["json", "JsonTableFileLoader"], ["Long Format Name", "Loader"], ], ) ) writer.write_table() expected = dedent( """\ # loader_mapping | Name | Loader | |----------------|------------------------| |csv |CsvTableFileLoader | |excel |ExcelTableFileLoader | |html |HtmlTableFileLoader | |markdown |MarkdownTableFileLoader | |mediawiki |MediaWikiTableFileLoader| |json |JsonTableFileLoader | |Long Format Name|Loader | """ ) out, err = capsys.readouterr() print_test_result(expected=expected, actual=out, error=err) assert out == expected def test_normal_multiple_write(self, capsys): writer = table_writer_class() writer.is_write_null_line_after_table = True writer.from_tabledata( TableData( "first", ["Name", "Loader"], [["csv", "CsvTableFileLoader"], ["excel", "ExcelTableFileLoader"]], ) ) writer.write_table() writer.from_tabledata( TableData("second", ["a", "b", "c"], [["1", "AA", "abc"], ["2", "BB", "zzz"]]) ) writer.write_table() expected = dedent( """\ # first |Name | Loader | |-----|--------------------| |csv |CsvTableFileLoader | |excel|ExcelTableFileLoader| # second | a | b | c | |--:|---|---| | 1|AA |abc| | 2|BB |zzz| """ ) out, err = capsys.readouterr() print_test_result(expected=expected, actual=out, error=err) assert out == expected def test_normal_style_align(self): writer = table_writer_class() writer.from_tabledata( TableData( "auto align", ["left", "right", "center", "auto", "auto", "None"], [ [0, "r", "center align", 0, "a", "n"], [11, "right align", "bb", 11, "auto", "none (auto)"], ], ) ) expected = dedent( """\ # auto align |left| right | center |auto|auto| None | |---:|-----------|------------|---:|----|-----------| | 0|r |center align| 0|a |n | | 11|right align|bb | 11|auto|none (auto)| """ ) out = writer.dumps() print_test_result(expected=expected, actual=out) assert out == expected writer.table_name = "specify alignment for each column manually" writer.styles = [ Style(align=Align.LEFT), Style(align=Align.RIGHT), Style(align=Align.CENTER), Style(align=Align.AUTO), Style(align=Align.AUTO), None, ] expected = dedent( """\ # specify alignment for each column manually |left| right | center |auto|auto| None | |----|----------:|:----------:|---:|----|-----------| |0 | r|center align| 0|a |n | |11 |right align| bb | 11|auto|none (auto)| """ ) out = writer.dumps() print_test_result(expected=expected, actual=out) assert out == expected # test for backward compatibility writer.styles = None writer.align_list = [Align.LEFT, Align.RIGHT, Align.CENTER, Align.AUTO, Align.AUTO, None] out = writer.dumps() print_test_result(expected=expected, actual=out) assert out == expected def test_normal_style_thousand_separator(self, capsys): writer = table_writer_class() writer.from_tabledata( TableData( "", ["none_format", "thousand_separator_i", "thousand_separator_f", "f", "wo_f"], [ [1000, 1234567, 1234567.8, 1234.5678, 1234567.8], [1000, 1234567, 1234567.8, 1234.5678, 1234567.8], ], ) ) writer.styles = [ Style(thousand_separator=ThousandSeparator.NONE), Style(thousand_separator=ThousandSeparator.COMMA), Style(thousand_separator=ThousandSeparator.COMMA), Style(thousand_separator=ThousandSeparator.SPACE), ] out = writer.dumps() expected = dedent( """\ |none_format|thousand_separator_i|thousand_separator_f| f | wo_f | |----------:|-------------------:|-------------------:|------:|--------:| | 1000| 1,234,567| 1,234,567.8|1 234.6|1234567.8| | 1000| 1,234,567| 1,234,567.8|1 234.6|1234567.8| """ ) print_test_result(expected=expected, actual=out) assert out == expected writer.styles = None writer.format_list = [ ptw.Format.NONE, ptw.Format.THOUSAND_SEPARATOR, ptw.Format.THOUSAND_SEPARATOR, ptw.Format.THOUSAND_SEPARATOR, ] out = writer.dumps() expected = dedent( """\ |none_format|thousand_separator_i|thousand_separator_f| f | wo_f | |----------:|-------------------:|-------------------:|------:|--------:| | 1000| 1,234,567| 1,234,567.8|1,234.6|1234567.8| | 1000| 1,234,567| 1,234,567.8|1,234.6|1234567.8| """ ) print_test_result(expected=expected, actual=out) assert out == expected def test_normal_style_font_size(self): writer = table_writer_class() writer.table_name = "style test: font size will not be affected" writer.headers = ["none", "empty_style", "tiny", "small", "medium", "large"] writer.value_matrix = [[111, 111, 111, 111, 111, 111], [1234, 1234, 1234, 1234, 1234, 1234]] writer.styles = [ None, Style(), Style(font_size=FontSize.TINY), Style(font_size=FontSize.SMALL), Style(font_size=FontSize.MEDIUM), Style(font_size=FontSize.LARGE), ] expected = dedent( """\ # style test: font size will not be affected |none|empty_style|tiny|small|medium|large| |---:|----------:|---:|----:|-----:|----:| | 111| 111| 111| 111| 111| 111| |1234| 1234|1234| 1234| 1234| 1234| """ ) out = writer.dumps() print_test_result(expected=expected, actual=out) assert out == expected def test_normal_style_font_weight(self): writer = table_writer_class() writer.table_name = "style test: bold" writer.headers = ["normal", "bold"] writer.value_matrix = [[11, 11], [123456, 123456]] writer.styles = [Style(font_weight="normal"), Style(font_weight="bold")] expected = dedent( """\ # style test: bold |normal| bold | |-----:|---------:| | 11| **11**| |123456|**123456**| """ ) out = writer.dumps() print_test_result(expected=expected, actual=out) assert out == expected def test_normal_style_mix(self): writer = table_writer_class() writer.from_tabledata(style_tabledata) writer.styles = styles expected = dedent( """\ # style test |none|empty|tiny|small|medium|large|null w/ bold| L bold |S italic|L bold italic| |---:|----:|---:|----:|-----:|----:|------------|-------:|-------:|------------:| | 111| 111| 111| 111| 111| 111| | **111**| _111_| _**111**_| |1234| 1234|1234| 1234| 1,234|1 234| |**1234**| _1234_| _**1234**_| """ ) out = writer.dumps() print_test_result(expected=expected, actual=out) assert out == expected def test_normal_set_style(self): writer = table_writer_class() writer.table_name = "set style method" writer.headers = ["normal", "style by idx", "style by header"] writer.value_matrix = [[11, 11, 11], [123456, 123456, 123456]] writer.set_style(1, Style(font_weight="bold", thousand_separator=",")) writer.set_style( "style by header", Style(align="center", font_weight="bold", thousand_separator=" ") ) expected = dedent( """\ # set style method |normal|style by idx|style by header| |-----:|-----------:|:-------------:| | 11| **11**| **11** | |123456| **123,456**| **123 456** | """ ) output = writer.dumps() print_test_result(expected=expected, actual=output) assert output == expected writer.table_name = "change style" writer.set_style(1, Style(align="right", font_style="italic")) writer.set_style("style by header", Style()) expected = dedent( """\ # change style |normal|style by idx|style by header| |-----:|-----------:|--------------:| | 11| _11_| 11| |123456| _123456_| 123456| """ ) output = writer.dumps() print_test_result(expected=expected, actual=output) assert output == expected def test_normal_ansi_color(self, capsys): writer = table_writer_class() writer.table_name = "ANCI escape sequence" writer.headers = ["colored_i", "colored_f", "colored_s", "wo_anci"] writer.value_matrix = [ [colored(111, "red"), colored(1.1, "green"), colored("abc", "blue"), "abc"], [colored(0, "red"), colored(0.12, "green"), colored("abcdef", "blue"), "abcdef"], ] writer.write_table() expected = dedent( """\ # ANCI escape sequence |colored_i|colored_f|colored_s|wo_anci| |--------:|--------:|---------|-------| | 111| 1.1|abc |abc | | 0| 0.12|abcdef |abcdef | """ ) out, err = capsys.readouterr() print_test_result(expected=expected, actual=out, error=err) _ansi_escape = re.compile(r"(\x9b|\x1b\[)[0-?]*[ -\/]*[@-~]", re.IGNORECASE) assert _ansi_escape.sub("", out) == expected def test_normal_margin_1(self, capsys): writer = table_writer_class() writer.from_tabledata(TableData("", headers, value_matrix)) writer.margin = 1 writer.write_table() expected = dedent( """\ | a | b | c | dd | e | |----:|------:|-----|----:|------| | 1 | 123.1 | a | 1.0 | 1 | | 2 | 2.2 | bb | 2.2 | 2.2 | | 3 | 3.3 | ccc | 3.0 | cccc | """ ) out, err = capsys.readouterr() print_test_result(expected=expected, actual=out, error=err) assert out == expected def test_normal_margin_2(self, capsys): writer = table_writer_class() writer.from_tabledata(TableData("", headers, value_matrix)) writer.margin = 2 writer.write_table() expected = dedent( """\ | a | b | c | dd | e | |------:|--------:|-------|------:|--------| | 1 | 123.1 | a | 1.0 | 1 | | 2 | 2.2 | bb | 2.2 | 2.2 | | 3 | 3.3 | ccc | 3.0 | cccc | """ ) out, err = capsys.readouterr() print_test_result(expected=expected, actual=out, error=err) assert out == expected def test_normal_value_map(self): writer = table_writer_class() writer.headers = ["a", "b"] writer.value_matrix = [["foo", True], ["bar", False]] writer.register_trans_func(trans_func) expected = dedent( """\ | a | b | |---|---| |foo|X | |bar| | """ ) output = writer.dumps() print_test_result(expected=expected, actual=output) assert output == expected def test_normal_avoid_overwrite_stream_by_dumps(self): writer = table_writer_class() writer.headers = ["a", "b"] writer.value_matrix = [["foo", "bar"]] writer.stream = six.StringIO() expected = dedent( """\ | a | b | |---|---| |foo|bar| """ ) output = writer.dumps() print_test_result(expected=expected, actual=output) assert output == expected print("--------------------") writer.write_table() output = writer.stream.getvalue() print_test_result(expected=expected, actual=output) assert output == expected @pytest.mark.skipif("six.PY2") def test_normal_escape_html_tag(self, capsys): writer = table_writer_class() writer.headers = ["no", "text"] writer.value_matrix = [[1, "<caption>Table 'formatting for Jupyter Notebook.</caption>"]] writer.is_escape_html_tag = True writer.write_table() expected = dedent( """\ |no | text | |--:|---------------------------------------------------------------------------| | 1|&lt;caption&gt;Table &#x27;formatting for Jupyter Notebook.&lt;/caption&gt;| """ ) out, err = capsys.readouterr() print_test_result(expected=expected, actual=out, error=err) assert out == expected @pytest.mark.skipif("six.PY2") def test_normal_escape_html_tag_from_tabledata(self, capsys): writer = table_writer_class() writer.from_tabledata( TableData( "", ["no", "text"], [[1, "<caption>Table 'formatting for Jupyter Notebook.</caption>"]], ) ) writer.is_escape_html_tag = True writer.write_table() expected = dedent( """\ |no | text | |--:|---------------------------------------------------------------------------| | 1|&lt;caption&gt;Table &#x27;formatting for Jupyter Notebook.&lt;/caption&gt;| """ ) out, err = capsys.readouterr() print_test_result(expected=expected, actual=out, error=err) assert out == expected @pytest.mark.parametrize( ["table", "indent", "header", "value", "expected"], [ [data.table, data.indent, data.header, data.value, data.expected] for data in exception_test_data_list ], ) def test_exception(self, table, indent, header, value, expected): writer = table_writer_class() writer.table_name = table writer.set_indent_level(indent) writer.headers = header writer.value_matrix = value with pytest.raises(expected): writer.write_table() class Test_MarkdownTableWriter_write_table_iter(object): @pytest.mark.parametrize( ["table", "header", "value", "expected"], [ [ "tablename", ["ha", "hb", "hc"], value_matrix_iter, dedent( """\ # tablename | ha | hb | hc | |---:|---:|---:| | 1| 2| 3| | 11| 12| 13| | 1| 2| 3| | 11| 12| 13| | 101| 102| 103| |1001|1002|1003| """ ), ], [ "mix length", ["string", "hb", "hc"], value_matrix_iter_1, dedent( """\ # mix length | string | hb | hc | |-----------------------------|----:|---:| |a b c d e f g h i jklmn | 2.1| 3| |aaaaa | 12.1| 13| |bbb | 2| 3| |cc | 12| 13| |a | 102| 103| | | 1002|1003| """ ), ], ], ) def test_normal(self, capsys, table, header, value, expected): writer = table_writer_class() writer.table_name = table writer.headers = header writer.value_matrix = value writer.iteration_length = len(value) writer.write_table_iter() out, err = capsys.readouterr() print_test_result(expected=expected, actual=out, error=err) assert out == expected @pytest.mark.parametrize( ["table", "header", "value", "expected"], [[data.table, data.header, data.value, data.expected] for data in exception_test_data_list], ) def test_exception(self, table, header, value, expected): writer = table_writer_class() writer.table_name = table writer.headers = header writer.value_matrix = value with pytest.raises(expected): writer.write_table_iter() class Test_MarkdownTableWriter_dump(object): def test_normal(self, tmpdir): test_filepath = str(tmpdir.join("test.sqlite")) writer = table_writer_class() writer.headers = ["a", "b"] writer.value_matrix = [["foo", "bar"]] writer.dump(test_filepath) expected = dedent( """\ | a | b | |---|---| |foo|bar| """ ) with open(test_filepath) as f: output = f.read() print_test_result(expected=expected, actual=output) assert output == expected class Test_MarkdownTableWriter_from_tablib(object): def test_normal_multiple_write(self, capsys): try: import tablib except ImportError: pytest.skip("requires tablib") data = tablib.Dataset() data.headers = ["a", "b", "c"] data.append(["1", "AA", "abc"]) data.append(["2", "BB", "zzz"]) writer = table_writer_class() writer.from_tablib(data) writer.write_table() expected = dedent( """\ | a | b | c | |--:|---|---| | 1|AA |abc| | 2|BB |zzz| """ ) out, err = capsys.readouterr() print_test_result(expected=expected, actual=out, error=err) assert out == expected class Test_MarkdownTableWriter_line_break_handling(object): @pytest.mark.parametrize( ["value", "expected"], [ [ ptw.LineBreakHandling.REPLACE, dedent( """\ |no | text | |--:|------------| | 1|first second| """ ), ], [ ptw.LineBreakHandling.ESCAPE, r"""|no | text | |--:|-------------| | 1|first\nsecond| """, ], [ "escape", r"""|no | text | |--:|-------------| | 1|first\nsecond| """, ], ], ) def test_normal_line(self, value, expected): writer = table_writer_class() writer.headers = ["no", "text"] writer.value_matrix = [[1, "first\nsecond"]] writer.line_break_handling = value out = writer.dumps() print_test_result(expected=expected, actual=out) assert out == expected @pytest.mark.skipif("SKIP_DATAFRAME_TEST is True") class Test_MarkdownTableWriter_from_dataframe(object): @pytest.mark.parametrize( ["add_index_column", "expected"], [ [ False, dedent( """\ # add_index_column: False | A | B | |--:|--:| | 1| 10| | 2| 11| """ ), ], [ True, dedent( """\ # add_index_column: True | | A | B | |---|--:|--:| |a | 1| 10| |b | 2| 11| """ ), ], ], ) def test_normal(self, tmpdir, add_index_column, expected): writer = table_writer_class() writer.table_name = "add_index_column: {}".format(add_index_column) df = pd.DataFrame({"A": [1, 2], "B": [10, 11]}, index=["a", "b"]) writer.from_dataframe(df, add_index_column=add_index_column) out = writer.dumps() print_test_result(expected=expected, actual=out) assert out == expected # pickle test df_pkl_filepath = str(tmpdir.join("df.pkl")) df.to_pickle(df_pkl_filepath) writer.from_dataframe(df_pkl_filepath, add_index_column=add_index_column) out = writer.dumps() print_test_result(expected=expected, actual=out) assert out == expected @pytest.mark.skipif("SKIP_DATAFRAME_TEST is True") class Test_MarkdownTableWriter_from_series(object): @pytest.mark.parametrize( ["add_index_column", "expected"], [ [ False, dedent( """\ # add_index_column: False |value | |-----:| |100.00| | 49.50| | 29.01| | 0.00| | 24.75| | 49.50| | 74.25| | 99.00| """ ), ], [ True, dedent( """\ # add_index_column: True | |value | |-----|-----:| |count|100.00| |mean | 49.50| |std | 29.01| |min | 0.00| |25% | 24.75| |50% | 49.50| |75% | 74.25| |max | 99.00| """ ), ], ], ) def test_normal(self, add_index_column, expected): writer = table_writer_class() writer.table_name = "add_index_column: {}".format(add_index_column) writer.from_series( pd.Series(list(range(100))).describe(), add_index_column=add_index_column ) out = writer.dumps() print_test_result(expected=expected, actual=out) assert out == expected
python
class Game: def __init__(self): self.is_active = True self.LEVEL_CLASSES = [Level_1, Level_2, Level_3] self.curr_level_index = 0 self.curr_level = self.LEVEL_CLASSES[self.curr_level_index]( self.go_to_next_level ) def go_to_next_level(self): self.curr_level_index += 1 self.curr_level = self.LEVEL_CLASSES[self.curr_level_index]( self.end_game if self.curr_level_index == len(self.LEVEL_CLASSES) - 1 else self.go_to_next_level ) def end_game(self): self.is_active = False def process_input(self, raw_input): # unintrusive cleansing & caps normalization cleaned_input = raw_input.lower().strip() return self.curr_level.process_cleaned_input(cleaned_input) class Level_1: def __init__(self, go_to_next_level): self.go_to_next_level = go_to_next_level self.LEVEL_NUMBER = 1 # things and actions should never be changed, if thing/action no longer exists # then set that as one of its properties e.g. phone_exists: False self.THINGS = {"room", "phone", "desk", "bed"} self.ACTIONS = { "look", "pickup", "approach", "answer", "sleep", "hit", "open", "help", "quit", "read", "draw", "place", "jump", } self.FUNCTIONS = { "bed": {"hit": self.hit_bed}, "phone": {"pickup": self.pickup_phone, "answer": self.answer_phone}, } self.SYNONYMS_FOR_ACTION = { "look": { "look", "look around", "see", "view", "survey", "observe", "observe around", "inspect", "scrutinize", "examine", "investigate", "check", "checkout", "review", "monitor", "search", "watch", "identify", "analyze", "peek", "describe", "find", }, "pickup": { "pickup", "pick up", "pick", "get", "take", "grab", "weild", "hold", "lift", }, "approach": {"approach", "go", "goto", "reach", "walk"}, "answer": {"answer", "respond", "talk"}, "sleep": {"sleep", "rest", "nap"}, "hit": { "hit", "kick", "smack", "slap", "punch", "pound", "fight", "headbutt", "attack", }, "open": {"open", "unlock", "enter"}, "help": {"help", "h"}, "quit": {"quit"}, "read": {"read"}, "draw": {"draw", "illustrate", "paint", "inscribe", "mark"}, "place": {"place", "put", "set", "lie"}, "jump": {"jump", "bounce"}, } self.SYNONYMS_FOR_THING = { "room": { "room", "floor", "wall", "walls", "ceiling", "space", "area", "environment", }, "phone": {"phone", "device", "cellphone"}, "desk": {"desk", "table"}, "bed": {"bed", "mattress", "sheets", "pillow"}, } # never delete a thing/action, just update thing_props = { "bed": {"wasHit": False}, } responses = { "room": { "look": ( "You can see a bed and a desk with a phone resting on top. " "There's nothing else." ), "pickup": "Don't be ridiculous.", "approach": "You're already in the room, man. No need.", "hit": "You kick and hit around the room. Nothing happens.", }, "phone": { "look": "A small cheap phone. It appears to be ringing.", "pickup": "You have taken the phone. It is still ringing.", "approach": "You have approached the phone.", "answer": ( "You answer it, the voice on the other line says 'You find " "yourself in a room.' As the voice speaks, the room around you" " starts to shift. You are now in a completely different room." ), "hit": "Why? Stop being so violent.", }, "desk": { "look": "A flimsy wooden desk.", "pickup": ( "Please. This desk is too heavy to pick up and take with you." ), "approach": "You have approached the desk.", "hit": "You hit the desk. That was pointless.", }, "bed": { "look": "The bed you woke up from. Not sure how you got here.", "pickup": "The bed's too big for that.", "approach": "You have approached the bed.", "sleep": "But you've just woke up. Get your head in the game, man!", "hit": "You attack and hit the bed mercilessly. Nothing happens.", "jump": ( "You jump on the bed for a bit, smiling and having a grand 'ol " "time. Wow that was fun." ), }, } inventory = set() self.state = { "thing_props": thing_props, "responses": responses, "inventory": inventory, } # all these dicts should include all things or actions assert self.THINGS == set(self.state["responses"].keys()) assert self.THINGS == set(self.SYNONYMS_FOR_THING.keys()) assert self.ACTIONS == set(self.SYNONYMS_FOR_ACTION.keys()) def process_cleaned_input(self, cleaned_input): # quickly return response for look shortcuts if cleaned_input in self.SYNONYMS_FOR_ACTION["look"] | {"l"}: return self.process_command("look", "room") # extracting verb and object out of input, then process verb object command input_words = cleaned_input.split() action, thing = self.extract_action_and_thing(input_words) return self.process_command(action, thing) def extract_action_and_thing(self, input_words): action, thing = "", "" action_Found, thing_Found = False, False # iterating through words to check for a direct match with any actions and # things available or their synonyms for input_word in input_words: if not action_Found: for action_key, synonyms in self.SYNONYMS_FOR_ACTION.items(): if input_word in synonyms: action = action_key action_Found = True if not thing_Found: for thing_key, synonyms in self.SYNONYMS_FOR_THING.items(): if input_word in synonyms: thing = thing_key thing_Found = True # print(f"ACTION:", action) # print(f"THING:", thing) return action, thing def process_command(self, action, thing): # if theres a game function for this input, do that, otherwise just get the # text resource try: do_action = self.FUNCTIONS[thing][action] except KeyError: return self.get_response_for_command(action, thing) else: return do_action() def get_response_for_command(self, action, thing): THING_HAS_NO_ACTION = f"You can't perform action '{action}' on the {thing}." response = None if action and thing: response = self.state["responses"][thing].get(action, THING_HAS_NO_ACTION) elif not action and thing: response = f"Not sure what you want to do to the {thing}." elif action and not thing: response = f"I can't perform action '{action}' on that." elif not action and not thing: response = ( "Couldn't find an eligible verb or object in your command.\n" "Example of a good command:\n" "hit desk\n" "Here, 'hit' is the verb and 'desk' is the object." ) return response + "\n" # // ---------------------------------- # SMART ACTIONS # this section has all the specific game actions that need to do things other than # just give back the string resource to the player def pickup_phone(self): response = self.get_response_for_command("pickup", "phone") responses, inventory = (self.state[k] for k in ("responses", "inventory")) if "phone" not in inventory: inventory.add("phone") # room responses["room"][ "look" ] = "You can see a bed and a desk. There's nothing else." # phone responses["phone"]["look"] = ( "A small cheap phone. It is ringing. Now that it is on your person, " "you can feel an unexplainable force emanating from it." ) responses["phone"]["pickup"] = "You already have the phone!" responses["phone"][ "approach" ] = "You can't approach something that's already on your person!" return response def answer_phone(self): response = self.get_response_for_command("answer", "phone") self.go_to_next_level() return response def hit_bed(self): response = self.get_response_for_command("hit", "bed") thing_props, responses = (self.state[k] for k in ("thing_props", "responses")) if not thing_props["bed"]["wasHit"]: thing_props["bed"]["wasHit"] = True responses["bed"]["hit"] = ( "You attack and hit the bed mercilessly. Nothing continues to happen. " "Do you need help?" ) return response class Level_2: def __init__(self, go_to_next_level): self.go_to_next_level = go_to_next_level self.LEVEL_NUMBER = 2 # things and actions should never be changed, if thing/action no longer exists # then set that as one of its properties e.g. phone_exists: False self.THINGS = {"room", "chalk", "note", "door"} self.ACTIONS = { "look", "pickup", "approach", "answer", "sleep", "hit", "open", "help", "quit", "read", "draw", "place", "jump", } self.SYNONYMS_FOR_ACTION = { "look": { "look", "look around", "see", "view", "survey", "observe", "observe around", "inspect", "scrutinize", "examine", "investigate", "check", "checkout", "review", "monitor", "search", "watch", "identify", "analyze", "peek", "describe", "find", }, "pickup": { "pickup", "pick up", "pick", "get", "take", "grab", "weild", "hold", "lift", }, "approach": {"approach", "go", "goto", "reach", "walk"}, "answer": {"answer", "respond", "talk"}, "sleep": {"sleep", "rest", "nap"}, "hit": { "hit", "kick", "smack", "slap", "punch", "pound", "fight", "headbutt", "attack", }, "open": {"open", "unlock", "enter"}, "help": {"help", "h"}, "quit": {"quit"}, "read": {"read"}, "draw": {"draw", "illustrate", "paint", "inscribe", "mark"}, "place": {"place", "put", "set", "lie"}, "jump": {"jump", "bounce"}, } self.SYNONYMS_FOR_THING = { "room": { "room", "floor", "wall", "walls", "ceiling", "space", "area", "environment", }, "chalk": {"chalk", "chalks", "chlak"}, "note": { "note", "paper", "message", "writing", "writings", "markings", "marks", "sticky", }, "door": {"door", "gate"}, } self.FUNCTIONS = { "chalk": {"pickup": self.pickup_chalk}, "door": {"draw": self.draw_door, "open": self.open_door}, } # never delete a thing/action, just update thing_props = {"door": {"exists": False}} responses = { "room": { "look": ( "Except for a piece of chalk you see rested on the center of " "the floor, this room is completely bare." ), "pickup": "Don't be ridiculous.", "approach": "You're already in the room, man. No need.", }, "chalk": { "look": ( "A normal piece of chalk. There is a sticky note attached to it." ), "pickup": "You have picked up the chalk.", "approach": "You have approached the chalk.", }, "note": { "look": ( "A sticky note with a message written on it:\nYOU'VE FOUND THE " "KEY. NOW FIND THE DOOR." ), "approach": "You have approached the note.", "read": "YOU'VE FOUND THE KEY. NOW FIND THE DOOR.", }, "door": { "look": ( "You try to look for a door, but alas. There is none to be found." ), "pickup": "Even if there was a door, that's quite silly.", "approach": "There is no door to approach.", "draw": "Can't draw a door without a writing utensil.", "open": "You can't open a non-existent door.", }, } inventory = set() self.state = { "thing_props": thing_props, "responses": responses, "inventory": inventory, } # all these dicts should include all things or actions assert self.THINGS == set(self.state["responses"].keys()) assert self.THINGS == set(self.SYNONYMS_FOR_THING.keys()) assert self.ACTIONS == set(self.SYNONYMS_FOR_ACTION.keys()) def process_cleaned_input(self, cleaned_input): # quickly return response for look shortcuts if cleaned_input in self.SYNONYMS_FOR_ACTION["look"] | {"l"}: return self.process_command("look", "room") # extracting verb and object out of input, then process verb object command input_words = cleaned_input.split() action, thing = self.extract_action_and_thing(input_words) return self.process_command(action, thing) def extract_action_and_thing(self, input_words): action, thing = "", "" action_Found, thing_Found = False, False # iterating through words to check for a direct match with any actions and # things available or their synonyms for input_word in input_words: if not action_Found: for action_key, synonyms in self.SYNONYMS_FOR_ACTION.items(): if input_word in synonyms: action = action_key action_Found = True if not thing_Found: for thing_key, synonyms in self.SYNONYMS_FOR_THING.items(): if input_word in synonyms: thing = thing_key thing_Found = True # print(f"ACTION:", action) # print(f"THING:", thing) return action, thing def process_command(self, action, thing): # if theres a game function for this input, do that, otherwise just get the # text resource try: do_action = self.FUNCTIONS[thing][action] except KeyError: return self.get_response_for_command(action, thing) else: return do_action() def get_response_for_command(self, action, thing): THING_HAS_NO_ACTION = f"You can't perform action '{action}' on the {thing}." response = None if action and thing: response = self.state["responses"][thing].get(action, THING_HAS_NO_ACTION) elif not action and thing: response = f"Not sure what you want to do to the {thing}." elif action and not thing: response = f"I can't perform action '{action}' on that." elif not action and not thing: response = ( "Couldn't find an eligible verb or object in your command.\n" "Example of a good command:\n" "hit desk\n" "Here, 'hit' is the verb and 'desk' is the object." ) return response + "\n" # // ---------------------------------- # SMART ACTIONS # this section has all the specific game actions that need to do things other than # just give back the string resource to the player def pickup_chalk(self): response = self.get_response_for_command("pickup", "chalk") responses, inventory = (self.state[k] for k in ("responses", "inventory")) if "chalk" not in inventory: inventory.add("chalk") # room responses["room"]["look"] = "The room is completely bare." # chalk responses["chalk"]["pickup"] = "You already have the chalk!" responses["chalk"][ "approach" ] = "No need to approach the chalk since you have it already." # note responses["note"][ "approach" ] = "No need to approach the note since you have it already." # door responses["door"]["draw"] = "You draw the door." return response def draw_door(self): response = self.get_response_for_command("draw", "door") thing_props, responses, inventory = ( self.state[k] for k in ("thing_props", "responses", "inventory") ) if not thing_props["door"]["exists"] and "chalk" in inventory: thing_props["door"]["exists"] = True # room responses["room"][ "look" ] = "The room is completely bare, except for a crudely drawn chalk door." # door responses["door"][ "look" ] = "A badly drawn, human-sized door drawn with chalk." responses["door"]["pickup"] = "You can't do that to the door silly." responses["door"]["approach"] = "You approach the door." responses["door"]["draw"] = "You've already drawn the door!" responses["door"][ "open" ] = "You try to open the door and somehow it works? You enter and are now in a completely different room." return response def open_door(self): response = self.get_response_for_command("open", "door") thing_props = self.state["thing_props"] if thing_props["door"]["exists"]: self.go_to_next_level() return response class Level_3: def __init__(self, end_game): self.end_game = end_game self.LEVEL_NUMBER = 3 # things and actions should never be changed, if thing/action no longer exists # then set that as one of its properties e.g. phone_exists: False self.THINGS = {"room", "wall", "table", "rack", "clock", "cube"} self.ACTIONS = { "look", "pickup", "approach", "answer", "sleep", "hit", "open", "help", "quit", "read", "draw", "place", "jump", } self.SYNONYMS_FOR_ACTION = { "look": { "look", "look around", "see", "view", "survey", "observe", "observe around", "inspect", "scrutinize", "examine", "investigate", "check", "checkout", "review", "monitor", "search", "watch", "identify", "analyze", "peek", "describe", "find", }, "pickup": { "pickup", "pick up", "pick", "get", "take", "grab", "weild", "hold", "lift", }, "approach": {"approach", "go", "goto", "reach", "walk"}, "answer": {"answer", "respond", "talk"}, "sleep": {"sleep", "rest", "nap"}, "hit": { "hit", "kick", "smack", "slap", "punch", "pound", "fight", "headbutt", "attack", }, "open": {"open", "unlock", "enter"}, "help": {"help", "h"}, "quit": {"quit"}, "read": {"read"}, "draw": {"draw", "illustrate", "paint", "inscribe", "mark"}, "place": {"place", "put", "set", "lie"}, "jump": {"jump", "bounce"}, } self.SYNONYMS_FOR_THING = { "room": {"room", "floor", "ceiling", "space", "area", "environment"}, "wall": { "wall", "walls", "marks", "markings", "writing", "writings", "drawing", "drawings", "symbol", "hint", "numbers", }, "table": {"table", "desk"}, "rack": {"rack", "triangle"}, "clock": {"clock", "circle", "circular"}, "cube": {"cube", "rubix", "square"}, } self.FUNCTIONS = {} # never delete a thing/action, just update thing_props = {} responses = { "room": { "look": ( "The north wall that's facing me has some strange " "writings/marks on it. There is a billiards table in the center " "of the room in front of you. There is a clock hanging on the same " "wall. There is a rubix cube lying on the floor." ), "pickup": "Don't be ridiculous.", "approach": "You're already in the room, man. No need.", }, "wall": { "look": ( "You see a clock hanging on the wall. Below that are some markings:" "\n3 -> 1 -> 4" ), "pickup": "Don't be ridiculous.", "approach": "You have approached the wall.", "hit": "You hit the wall. Completely useless.", "read": "3 -> 1 -> 4", }, "table": { "look": ( "An old no longer working billiards table. There is a triangle rack" " on it. It would probably be an ideal location to PLACE objects " "onto this table." ), "pickup": "Don't be silly.", "approach": "You have approached the table.", }, "rack": { "look": "A large triangle rack used to play pool.", "pickup": "You picked up the large triangle rack.", "approach": "You have approached the large triangle rack.", "place": ( "You need to have the rack on your person if you want to place it." ), }, "clock": { "look": "A medium-sized circular clock.", "pickup": "You picked up the medium-sized clock.", "approach": "You have approached the clock.", "place": ( "You need to have the clock on your person if you want to place it." ), }, "cube": { "look": ( "A small rubix cube. Unfortunately doesn't work anymore and " "might as well be a paperweight." ), "pickup": "You picked up the small rubix cube.", "approach": "You have approached the small rubix cube.", "place": ( "You need to have the cube on your person if you want to place it." ), }, } inventory = set() self.state = { "thing_props": thing_props, "responses": responses, "inventory": inventory, } # all these dicts should include all things or actions assert self.THINGS == set(self.state["responses"].keys()) assert self.THINGS == set(self.SYNONYMS_FOR_THING.keys()) assert self.ACTIONS == set(self.SYNONYMS_FOR_ACTION.keys()) def process_cleaned_input(self, cleaned_input): # quickly return response for look shortcuts if cleaned_input in self.SYNONYMS_FOR_ACTION["look"] | {"l"}: return self.process_command("look", "room") # extracting verb and object out of input, then process verb object command input_words = cleaned_input.split() action, thing = self.extract_action_and_thing(input_words) return self.process_command(action, thing) def extract_action_and_thing(self, input_words): action, thing = "", "" action_Found, thing_Found = False, False # iterating through words to check for a direct match with any actions and # things available or their synonyms for input_word in input_words: if not action_Found: for action_key, synonyms in self.SYNONYMS_FOR_ACTION.items(): if input_word in synonyms: action = action_key action_Found = True if not thing_Found: for thing_key, synonyms in self.SYNONYMS_FOR_THING.items(): if input_word in synonyms: thing = thing_key thing_Found = True # print(f"ACTION:", action) # print(f"THING:", thing) return action, thing def process_command(self, action, thing): # if theres a game function for this input, do that, otherwise just get the # text resource try: do_action = self.FUNCTIONS[thing][action] except KeyError: return self.get_response_for_command(action, thing) else: return do_action() def get_response_for_command(self, action, thing): THING_HAS_NO_ACTION = f"You can't perform action '{action}' on the {thing}." response = None if action and thing: response = self.state["responses"][thing].get(action, THING_HAS_NO_ACTION) elif not action and thing: response = f"Not sure what you want to do to the {thing}." elif action and not thing: response = f"I can't perform action '{action}' on that." elif not action and not thing: response = ( "Couldn't find an eligible verb or object in your command.\n" "Example of a good command:\n" "hit desk\n" "Here, 'hit' is the verb and 'desk' is the object." ) return response + "\n" # // ---------------------------------- # SMART ACTIONS # this section has all the specific game actions that need to do things other than # just give back the string resource to the player
python
#!/usr/bin/python from my_func3 import hello3 hello3()
python
#!/usr/bin/env python import rospy from std_msgs.msg import String from ros_rover.msg import Rover from numpy import interp from PanTilt import PanTilt pt = PanTilt() def callback(data): #rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.speed) pan=int(interp(data.camera_pan_axis,[-255,255],[-90,90])) tilt=int(interp(data.camera_tilt_axis,[-255,255],[-90,90])) ## Switch! # pt.panTilt(pan,tilt) pt.panTilt(-pan,tilt) ## Switch! #pt.increaseOffset(data.camera_tilt_button,data.camera_pan_button) pt.increaseOffset(-data.camera_pan_button,-data.camera_tilt_button) if (data.command=='CAMERA_CENTER'): pt.offset_pan=0 pt.offset_tilt=0 print "(%s,%s) Pan: %s Tilt: %s "%(data.camera_pan_button,data.camera_tilt_button,pan,tilt) def listener(): # In ROS, nodes are uniquely named. If two nodes with the same # name are launched, the previous one is kicked off. The # anonymous=True flag means that rospy will choose a unique # name for our 'listener' node so that multiple listeners can # run simultaneously. rospy.init_node('pantilt_listener', anonymous=True) rospy.Subscriber('chatter', Rover, callback,queue_size=1) rate = rospy.Rate(5) rate.sleep() rospy.spin() if __name__ == '__main__': listener()
python
# Copyright 2018 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """TIFFIODataset""" import tensorflow as tf from tensorflow_io.python.ops import core_ops class TIFFIODataset(tf.data.Dataset): """TIFFIODataset""" def __init__(self, filename, internal=True): if not internal: raise ValueError( "TIFFIODataset constructor is private; please use one " "of the factory methods instead (e.g., " "IODataset.from_pcap())" ) with tf.name_scope("TIFFIODataset"): content = tf.io.read_file(filename) _, dtype = core_ops.io_decode_tiff_info(content) # use dtype's rank to find out the number of elements dataset = tf.data.Dataset.range(tf.cast(tf.shape(dtype)[0], tf.int64)) dataset = dataset.map(lambda index: core_ops.io_decode_tiff(content, index)) self._dataset = dataset self._content = content super().__init__( self._dataset._variant_tensor ) # pylint: disable=protected-access def _inputs(self): return [] @property def element_spec(self): return self._dataset.element_spec
python
"""Trivial filter that adds an empty collection to the session.""" # pylint: disable=no-self-use,unused-argument from typing import TYPE_CHECKING, Any, Dict import dlite from oteapi.models import FilterConfig from pydantic import Field from pydantic.dataclasses import dataclass from oteapi_dlite.models import DLiteSessionUpdate if TYPE_CHECKING: from typing import Optional @dataclass class CreateCollectionStrategy: """Trivial filter that adds an empty collection to the session. **Registers strategies**: - `("filterType", "dlite/create-collection")` """ filter_config: FilterConfig # Find a better way to keep collections alive!!! # Need to be `Any`, because otherwise `pydantic` complains. collection_refs: Dict[str, Any] = Field( {}, description="A dictionary of DLite Collections.", ) def initialize( self, session: "Optional[Dict[str, Any]]" = None ) -> DLiteSessionUpdate: """Initialize.""" if session is None: raise ValueError("Missing session") if "collection_id" in session: raise KeyError("`collection_id` already exists in session.") coll = dlite.Collection() # Make sure that collection stays alive # It will never be deallocated... coll.incref() return DLiteSessionUpdate(collection_id=coll.uuid) def get(self, session: "Optional[Dict[str, Any]]" = None) -> DLiteSessionUpdate: """Execute the strategy.""" if session is None: raise ValueError("Missing session") return DLiteSessionUpdate(collection_id=session["collection_id"])
python
from ..serializers import ClienteSerializer from ..models import Cliente class ControllerCliente: serializer_class = ClienteSerializer def crearcliente(request): datosCliente= request.data try: ClienteNuevo = Cliente() ClienteNuevo.cliente = datosCliente['cliente'] ClienteNuevo.calle = datosCliente['calle'] ClienteNuevo.colonia = datosCliente['colonia'] ClienteNuevo.cp = datosCliente['cp'] ClienteNuevo.municipio = datosCliente['municipio'] ClienteNuevo.estado = datosCliente['estado'] ClienteNuevo.pais = datosCliente['pais'] ClienteNuevo.razon_social = datosCliente['razon_social'] ClienteNuevo.rfc = datosCliente['rfc'] ClienteNuevo.telefono = datosCliente['telefono'] ClienteNuevo.contacto = datosCliente['contacto'] ClienteNuevo.email = datosCliente['email'] ClienteNuevo.pagina_web = datosCliente['pagina_web'] ClienteNuevo.foto_cliente = datosCliente['foto_cliente'] ClienteNuevo.id_zona_horaria = datosCliente['id_zona_horaria'] ClienteNuevo.usar_inventario = datosCliente['usar_inventario'] ClienteNuevo.alertas_email = datosCliente['alertas_email'] ClienteNuevo.registro = datosCliente['registro'] ClienteNuevo.save() except Exception: return {"estatus":"Error"} return {"estatus":"Ok", 'nuevo_cliente': ClienteNuevo.cliente} def listarcliente(id_cliente=None): if id_cliente: try: queryset = Cliente.objects.get(id_cliente=id_cliente) except Cliente.DoesNotExist: return ({'result': 'No se encontró el cliente deseado'}) serializer = ClienteSerializer(queryset) return serializer.data else: queryset = Cliente.objects.all() serializer = ClienteSerializer(queryset, many=True) return serializer.data def modificarcliente(request,id_cliente=None): if id_cliente: datosCliente = request.data try: clienteModificar = Cliente.objects.get(id_cliente=id_cliente) except Cliente.DoesNotExist: return ({'result': 'No se encontró el cliente deseado'}) try: clienteModificar.cliente = datosCliente['cliente'] clienteModificar.calle = datosCliente['calle'] clienteModificar.colonia = datosCliente['colonia'] clienteModificar.cp = datosCliente['cp'] clienteModificar.municipio = datosCliente['municipio'] clienteModificar.estado = datosCliente['estado'] clienteModificar.pais = datosCliente['pais'] clienteModificar.razon_social = datosCliente['razon_social'] clienteModificar.rfc = datosCliente['rfc'] clienteModificar.telefono = datosCliente['telefono'] clienteModificar.contacto = datosCliente['contacto'] clienteModificar.email = datosCliente['email'] clienteModificar.pagina_web = datosCliente['pagina_web'] clienteModificar.foto_cliente = datosCliente['foto_cliente'] clienteModificar.id_zona_horaria = datosCliente['id_zona_horaria'] clienteModificar.usar_inventario = datosCliente['usar_inventario'] clienteModificar.alertas_email = datosCliente['alertas_email'] clienteModificar.registro = datosCliente['registro'] clienteModificar.save() clienteModificar.save() except Exception: return {"estatus":"Error"} return {"estatus":"Ok", 'cliente_modificado': clienteModificar.cliente} else: return {"result":"Ingrese el Id del cliente que desea modificar"}
python
from random import randint from typing import Tuple from pymetaheuristics.genetic_algorithm.types import Genome from pymetaheuristics.genetic_algorithm.exceptions import CrossOverException def single_point_crossover( g1: Genome, g2: Genome, **kwargs ) -> Tuple[Genome, Genome]: """Cut 2 Genomes on index p (randomly choosen) and swap its parts.""" if len(g1) == len(g2): length = len(g1) else: raise CrossOverException( "Genomes has to have the same length, got %d, %d" % ( len(g1), len(g2))) if length < 2: return g1, g2 p = randint(1, length - 1) return g1[0:p] + g2[p:length], g2[0:p] + g1[p:length] def pmx_single_point( g1: Genome, g2: Genome, **kwargs ) -> Tuple[Genome, Genome]: """ PMX is a crossover function which consider a Genome as a sequence of nom-repetitive genes through the Genome. So before swapping, checks if repetition is going to occur, and swap the pretitive gene with its partner on the other Genome and them swap with other gene on the same Genome. See more at https://user.ceng.metu.edu.tr/~ucoluk/research/publications/tspnew.pdf . This implementation suites very well the TSP problem. """ if len(g1) == len(g2): length = len(g1) else: raise CrossOverException( "Genomes has to have the same length, got %d, %d" % ( len(g1), len(g2))) if length < 2: return g1, g2 p = randint(1, length - 1) g1child = g1[:] for i in range(p): ans = g1child.index(g2[i]) g1child[ans] = g1child[i] g1child[i] = g2[i] g2child = g2[:] for i in range(p): ans = g2child.index(g1[i]) g2child[ans] = g2child[i] g2child[i] = g1[i] return g1child, g2child
python
from src.floyd_warshall import (floyd_warshall, floyd_warshall_with_path_reconstruction, reconstruct_path) def test_floyd_warshall(graph1): dist = floyd_warshall(graph1) assert dist[0][2] == 300 assert dist[0][3] == 200 assert dist[1][2] == 200 def test_floyd_warshall_with_path_reconstruction(graph1): dist, next_move = floyd_warshall_with_path_reconstruction(graph1) path = reconstruct_path(next_move, 0, 3) assert path == [0, 1, 3] path = reconstruct_path(next_move, 0, 2) assert path == [0, 1, 3, 2]
python
# Generated by Django 2.0.8 on 2019-08-18 19:16 import django.contrib.postgres.fields.jsonb from django.db import migrations class Migration(migrations.Migration): dependencies = [ ('tasks', '0022_auto_20190812_2145'), ] operations = [ migrations.AddField( model_name='task', name='metadata', field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default={}), ), ]
python
"Genedropping with IBD constraints" from pydigree.common import random_choice from pydigree.genotypes import AncestralAllele from .simulation import GeneDroppingSimulation from pydigree.exceptions import SimulationError from pydigree import paths from pydigree import Individual import collections class ConstrainedMendelianSimulation(GeneDroppingSimulation): """ Performs a gene-dropping simulation constrained to a specific IBD pattern """ def __init__(self, template=None, label=None, replications=1000, only=None): GeneDroppingSimulation.__init__(self, template=template, label=label, replications=replications, only=only) for ind in self.template.individuals: if ind.is_founder(): continue if not (ind.father.is_founder() or ind.mother.is_founder()): raise ValueError("ConstrainedMendelian only available" "for outbred pedigrees") def replicate(self, writeibd=False, verbose=False, replicatenumber=0): "Creates a replicate from the simulation" self.template.clear_genotypes() for x in self.template.founders(): x.label_genotypes() for ind in sorted(self.constraints['ibd'], key=lambda x: x.depth, reverse=True): if ind.has_genotypes(): # If the individual we're looking at has genotypes # already, we've seen them earlier while getting # genotypes for someone deeper in the pedigree continue constraints = self.constraints['ibd'][ind] # TODO: Multiple constraints per individual # Right now we're only using the first ([0]) constraint constraints = [(x[1], AncestralAllele(x[0], x[2])) for x in constraints] location, allele = constraints[0] ancestor = allele.ancestor descent_path = random_choice(paths(ancestor, ind)) for path_member in descent_path: if path_member.is_founder(): continue fa, mo = path_member.parents() if fa in descent_path: paternal_gamete = fa.constrained_gamete(constraints) else: paternal_gamete = fa.gamete() if mo in descent_path: maternal_gamete = mo.constrained_gamete(constraints) else: maternal_gamete = mo.gamete() genotypes = Individual.fertilize(paternal_gamete, maternal_gamete) path_member._set_genotypes(genotypes) # Get genotypes for everybody else that we're not constraining. for ind in self.template.individuals: ind.get_genotypes() if writeibd: self._writeibd(replicatenumber) # Now replace the label genotypes in founders with real ones. self.get_founder_genotypes() # Now replace the label genotypes in the nonfounders with the # genotypes of the founders if isinstance(self.only, collections.Callable): siminds = [x for x in self.template.nonfounders() if self.only(x)] else: siminds = self.template.nonfounders() for nf in siminds: nf.delabel_genotypes() # Predict phenotypes if self.trait: for ind in siminds: ind.predict_phenotype(self.trait) if verbose: for ind in siminds: print(ind, ind.get_genotype(location))
python
# The parameters were taken from the ReaxFF module in lammps: #! at1; at2; De(sigma); De(pi); De(pipi); p(be1); p(bo5); 13corr; p(bo6), p(ovun1); p(be2); p(bo3); p(bo4); n.u.; p(bo1); p(bo2) # 1 1 156.5953 100.0397 80.0000 -0.8157 -0.4591 1.0000 37.7369 0.4235 0.4527 -0.1000 9.2605 1.0000 -0.0750 6.8316 1.0000 0.0000 # 1 2 170.2316 0.0000 0.0000 -0.5931 0.0000 1.0000 6.0000 0.7140 5.2267 1.0000 0.0000 1.0000 -0.0500 6.8315 0.0000 0.0000 # 2 2 156.0973 0.0000 0.0000 -0.1377 0.0000 1.0000 6.0000 0.8240 2.9907 1.0000 0.0000 1.0000 -0.0593 4.8358 0.0000 0.0000 # 1 3 160.4802 105.1693 23.3059 -0.3873 -0.1613 1.0000 10.8851 1.0000 0.5341 -0.3174 7.0303 1.0000 -0.1463 5.2913 0.0000 0.0000 # 3 3 60.1463 176.6202 51.1430 -0.2802 -0.1244 1.0000 29.6439 0.9114 0.2441 -0.1239 7.6487 1.0000 -0.1302 6.2919 1.0000 0.0000 # 2 3 180.4373 0.0000 0.0000 -0.8074 0.0000 1.0000 6.0000 0.5514 1.2490 1.0000 0.0000 1.0000 -0.0657 5.0451 0.0000 0.0000 # atomID; ro(sigma); Val; atom mass; Rvdw; Dij; gamma; ro(pi); Val(e), alfa; gamma(w); Val(angle); p(ovun5); n.u.; chiEEM; etaEEM; n.u.; ro(pipi) ;p(lp2); Heat increment; p(boc4); p(boc3); p(boc5), n.u.; n.u.; p(ovun2); p(val3); n.u.; Val(boc); p(val5);n.u.;n.u.;n.u. # C 1.3825 4.0000 12.0000 1.9133 0.1853 0.9000 1.1359 4.0000 9.7602 2.1346 4.0000 33.2433 79.5548 5.8678 7.0000 0.0000 1.2104 0.0000 199.0303 8.6991 34.7289 13.3894 0.8563 0.0000 -2.8983 2.5000 1.0564 4.0000 2.9663 0.0000 0.0000 0.0000 # H 0.7853 1.0000 1.0080 1.5904 0.0419 1.0206 -0.1000 1.0000 9.3557 5.0518 1.0000 0.0000 121.1250 5.3200 7.4366 1.0000 -0.1000 0.0000 62.4879 1.9771 3.3517 0.7571 1.0698 0.0000 -15.7683 2.1488 1.0338 1.0000 2.8793 0.0000 0.0000 0.0000 # O 1.2477 2.0000 15.9990 1.9236 0.0904 1.0503 1.0863 6.0000 10.2127 7.7719 4.0000 36.9573 116.0768 8.5000 8.9989 2.0000 0.9088 1.0003 60.8726 20.4140 3.3754 0.2702 0.9745 0.0000 -3.6141 2.7025 1.0493 4.0000 2.9225 0.0000 0.0000 0.0000 num_to_atom_type = { 0 : "c" , 1 : "h" , 2 : "o" } atom_type_to_num = { "c" : 0 , "h" : 1 , "o" : 2 } # # Equilibrium distances for the atom types # r_s = [1.3825,0.7853,1.2477] r_pi = [1.1359,-0.1000,1.0863] r_pi2 = [1.2104,-0.1000,0.9088] # # Exponents etc. for calculating uncorrected bond order # pbo1 = [ [-0.0750,-0.0500,-0.1463] , [-0.0500,-0.0593,-0.0657] , [-0.1463,-0.0657,-0.1302] ] pbo2 = [ [6.8316,6.8315,5.2913] , [6.8315,4.8358,5.0451] , [5.2913,5.0451,6.2919] ] pbo3 = [ [-0.1000,1.0000,-0.3174] , [1.0000,1.0000,1.0000] , [-0.3174,1.0000,-0.1239] ] pbo4 = [ [9.2605,0.0000,7.0303] , [0.0000,0.0000,0.0000] , [7.0303,0.0000,7.6487] ] pbo5 = [ [-0.4591,0.0000,-0.1613] , [ 0.0000,0.0000, 0.0000] , [-0.1613,0.0000,-0.1244] ] pbo6 = [ [37.7369, 6.0000,10.8851] , [6.0000, 6.0000, 6.0000] , [10.8851, 6.0000,29.6439] ] # # Valency of the atoms (needed to correct for over coordination) # valency = [ 4.0, 1.0, 2.0 ] valency_val = [ 4.0, 1.0, 4.0 ] # # parameters to for over coordinaten correction # pboc4 = [8.6991, 1.9771, 20.4140] pboc3 = [34.7289, 3.3517, 3.3754] pboc5 = [13.3894, 0.7571, 0.2702] # # between which atoms the 1-3 over coordination should be corrected # v13cor = [ [1.0, 1.0, 1.0] , [1.0, 1.0, 1.0] , [1.0, 1.0, 1.0] ] # # between which atoms over coordination should be corrected # ovc = [ [1.0, 0.0, 0.0] , [0.0, 0.0, 0.0] , [0.0, 0.0, 1.0] ]
python
#codeby : Dileep #Write a Python program to simulate the SSTF program disk scheduling algorithms. num=int(input("Enter the Number:")) print("Enter the Queue:") requestqueue=list(map(int,input().split())) head_value=int(input("Head Value Starts at: ")) final=[] for i in requestqueue: emptylist=[] for j in requestqueue: if(j!=None and head_value!=None): emptylist.append(abs(head_value-j)) else: emptylist.append(float('inf')) final.append(min(emptylist)) head_value=requestqueue[emptylist.index(min(emptylist))] requestqueue[requestqueue.index(head_value)]=None print("Head Difference:") for i in final: print(i) print("Totoal Head Movements are:"+str(sum(final)))
python
# -*- coding: utf8 -*- from nose.tools import * from mbdata.api.tests import with_client, assert_json_response_equal @with_client def test_label_get(client): rv = client.get('/v1/label/get?id=ecc049d0-88a6-4806-a5b7-0f1367a7d6e1&include=area&include=ipi&include=isni') expected = { u"response": { u"status": { u"message": u"success", u"code": 0, u"version": u"1.0" }, u"label": { u"begin_date": { u"year": 1985, u"month": 6 }, u"name": u"\u30b9\u30bf\u30b8\u30aa\u30b8\u30d6\u30ea", u"area": { u"name": u"Japan" }, u"ipis": [ u"00173517959", u"00473554732" ], u"isnis": [ u"000000011781560X" ], u"type": u"Production", u"id": u"ecc049d0-88a6-4806-a5b7-0f1367a7d6e1" } } } assert_json_response_equal(rv, expected)
python
import datetime import logging import os import traceback import flask import google.auth from google.auth.transport import requests as grequests from google.oauth2 import id_token, credentials import googleapiclient.discovery from typing import Optional, NamedTuple from app.util.exceptions import BadPubSubTokenException IMPORT_SERVICE_SCOPES = [ "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/devstorage.read_write", "https://www.googleapis.com/auth/pubsub" ] class CachedCreds: def __init__(self, access_token: str, expire_str: str): self.creds = credentials.Credentials(access_token, scopes=IMPORT_SERVICE_SCOPES) self.expiry = self._google_expiretime_to_datetime(expire_str) @classmethod def _google_expiretime_to_datetime(cls, expire_time: str) -> datetime.datetime: """Google's documentation for the generateAccessToken endpoint describes the expireTime field as a timestamp in RFC3339 format, providing the example "2014-10-02T15:01:23.045123456Z" -- i.e. the time all the way to nanoseconds. In practice, the endpoint currently omits the nanoseconds entirely. Google verified this behaviour in a support ticket, unhelpfully adding "At some point in the future we may start supporting fractional times, and would not consider that a breaking change." Therefore we need to handle timestamps both with and without nanoseconds. Since this is just a token expiry, dropping the nanoseconds component will mean at worse we refresh the token (one second minus one nanosecond) early. https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/generateAccessToken https://console.cloud.google.com/support/cases/detail/21652153""" # if there are nanoseconds, everything left of the dot will be the time (with no Z, so we put it back). # if there aren't nanoseconds, there'll be no dot, so we don't need to reinstate the Z. trunc_time = expire_time.split('.')[0] if trunc_time[-1] != 'Z': trunc_time += 'Z' return datetime.datetime.strptime(trunc_time, "%Y-%m-%dT%H:%M:%SZ") _cached_isvc_creds: Optional[CachedCreds] = None def get_isvc_credential() -> credentials.Credentials: """Get a Google oauth2 Credentials object for the import service SA.""" return _get_isvc_cached_creds().creds def get_isvc_token() -> str: """Get an access token for the import service SA.""" return _get_isvc_cached_creds().creds.token def _get_isvc_cached_creds() -> CachedCreds: """Use the cached creds if it still exists and we have at least 5 minutes until it expires.""" if _cached_isvc_creds is not None and \ _cached_isvc_creds.expiry > datetime.datetime.utcnow() + datetime.timedelta(minutes=5): logging.info("using cached creds for import service SA") return _cached_isvc_creds else: logging.info("generating new creds for import service SA") return _update_isvc_creds() def _update_isvc_creds() -> CachedCreds: """The app engine SA has token creator on the import service SA""" token_response = _get_isvc_token_from_google() global _cached_isvc_creds _cached_isvc_creds = CachedCreds(token_response["accessToken"], token_response["expireTime"]) return _cached_isvc_creds def _get_isvc_token_from_google() -> dict: # create service account name email = os.environ.get('IMPORT_SVC_SA_EMAIL') name = 'projects/-/serviceAccounts/{}'.format(email) # create body for request body = { 'scope': IMPORT_SERVICE_SCOPES } credentials, project = google.auth.default() iam = googleapiclient.discovery.build('iamcredentials', 'v1', credentials=credentials) return iam.projects().serviceAccounts().generateAccessToken( name=name, body=body, ).execute() def verify_pubsub_jwt(request: flask.Request) -> None: """Verify that this request came from Cloud Pub/Sub. This looks for a secret token in a queryparam, then decodes the Bearer token and checks identity and audience. See here: https://cloud.google.com/pubsub/docs/push#using_json_web_tokens_jwts""" if request.args.get('token', '') != os.environ.get('PUBSUB_TOKEN'): logging.info("Bad Pub/Sub token") raise BadPubSubTokenException() bearer_token = request.headers.get('Authorization', '') token = bearer_token.split(' ', maxsplit=1)[1] try: claim = id_token.verify_oauth2_token(token, grequests.Request(), audience=os.environ.get('PUBSUB_AUDIENCE')) if claim['iss'] not in [ 'accounts.google.com', 'https://accounts.google.com' ]: # bad issuer logging.info("Bad issuer") raise BadPubSubTokenException() if claim['email'] != os.environ.get('PUBSUB_ACCOUNT'): logging.info("Incorrect email address") raise BadPubSubTokenException() except Exception as e: # eats all exceptions, including ones thrown by verify_oauth2_token if e.g. audience is wrong logging.info(traceback.format_exc()) raise BadPubSubTokenException()
python
from TunAugmentor import transformations def test_transform1(): assert transformations.transform1('mahdi')=='mahdi'
python
from sqlalchemy import Integer, String, Column, Float from api.db.base_class import BaseLayerTable from geoalchemy2 import Geometry class CommunityLocations(BaseLayerTable): """ https://catalogue.data.gov.bc.ca/dataset/first-nation-community-locations """ __tablename__ = 'fn_community_locations' # ogc_fid is an automatic primary key created by ogr2ogr when loading this dataset. ogc_fid = Column(Integer, primary_key=True, autoincrement=True) COMMUNITY_LOCATION_ID = Column(Integer) FIRST_NATION_BC_NAME = Column(String) FIRST_NATION_FEDERAL_NAME = Column(String) FIRST_NATION_FEDERAL_ID = Column(Integer) URL_TO_BC_WEBSITE = Column(String) URL_TO_FEDERAL_WEBSITE = Column(String) URL_TO_FIRST_NATION_WEBSITE = Column(String) MEMBER_ORGANIZATION_NAMES = Column(String) LANGUAGE_GROUP = Column(String) BC_REGIONAL_OFFICE = Column(String) MAPSHEET_NUMBER = Column(String) PREFERRED_NAME = Column(String) ALTERNATIVE_NAME_1 = Column(String) ALTERNATIVE_NAME_2 = Column(String) ADDRESS_LINE1 = Column(String) ADDRESS_LINE2 = Column(String) OFFICE_CITY = Column(String) OFFICE_PROVINCE = Column(String) OFFICE_POSTAL_CODE = Column(String) LOCATION_DESCRIPTION = Column(String) SITE_NAME = Column(String) SITE_NUMBER = Column(String) COMMENTS = Column(String) OBJECTID = Column(Integer) SE_ANNO_CAD_DATA = Column(String) fme_feature_type = Column(String) SHAPE = Column(Geometry('POINT', 4326), index=True) class TreatyAreas(BaseLayerTable): """ https://catalogue.data.gov.bc.ca/dataset/first-nations-treaty-areas """ __tablename__ = 'fn_treaty_areas' # ogc_fid is an automatic primary key created by ogr2ogr when loading this dataset. ogc_fid = Column(Integer, primary_key=True, autoincrement=True) TREATY_AREA_ID = Column(Integer) TREATY = Column(String) EFFECTIVE_DATE = Column(String) FIRST_NATION_NAME = Column(String) AREA_TYPE = Column(String) LAND_TYPE = Column(String) GEOGRAPHIC_LOCATION = Column(String) CHAPTER_REFERENCE = Column(String) APPENDIX_REFERENCE = Column(String) COMMENTS = Column(String) FEATURE_CODE = Column(String) SE_ANNO_CAD_DATA = Column(String) OBJECTID = Column(Integer) FEATURE_AREA_SQM = Column(Float(53)) FEATURE_LENGTH_M = Column(Float(53)) fme_feature_type = Column(String) SHAPE = Column(Geometry(srid=4326), index=True) class TreatyLands(BaseLayerTable): """ https://catalogue.data.gov.bc.ca/dataset/first-nations-treaty-lands """ __tablename__ = 'fn_treaty_lands' # ogc_fid is an automatic primary key created by ogr2ogr when loading this dataset. ogc_fid = Column(Integer, primary_key=True, autoincrement=True) TREATY_LAND_ID = Column(Integer) TREATY = Column(String) EFFECTIVE_DATE = Column(String) FIRST_NATION_NAME = Column(String) LAND_TYPE = Column(String) CHAPTER_REFERENCE = Column(String) APPENDIX_REFERENCE = Column(String) COMMENTS = Column(String) FEATURE_CODE = Column(String) OBJECTID = Column(Integer) SE_ANNO_CAD_DATA = Column(String) FEATURE_AREA_SQM = Column(Float(53)) FEATURE_LENGTH_M = Column(Float(53)) fme_feature_type = Column(String) SHAPE = Column(Geometry(srid=4326), index=True)
python
import csv def find_labels(xmap, contig_start_label, contig_end_label, contig_orientation): """ this does not account for split mapped molecules i.e., assumes one molecule ID per line in XMAP fix later """ # swap if -: #print(contig_start_label, contig_end_label) if contig_orientation == "-": contig_start_label,contig_end_label = contig_end_label,contig_start_label #print(contig_start_label, contig_end_label) print("mol_id"+"\t"+"five_prime_labels"+"\t"+"three_prime_labels") # cvs module: with open(xmap, 'r') as f: reader = csv.reader(f, delimiter='\t') xmap_lines = [line for line in reader if "#" not in line[0]] for x in xmap_lines: # tuples, replace paren, remove trailing comma, comma split, and every other ele (contig labels): contig_labels_only = x[13].replace("(", "").replace(")", ",")[:-1].split(',')[::2] contig_labels_only_int = [int(i) for i in contig_labels_only] # make int from str! # check that molecule crosses into SDs: if contig_orientation == "-": smaller_five_prime = [i for i in contig_labels_only_int if i > contig_start_label] # 5' of SDs larger_five_prime = [i for i in contig_labels_only_int if i <= contig_start_label] # into SDs smaller_three_prime = [i for i in contig_labels_only_int if i >= contig_end_label] # into SDs larger_three_prime = [i for i in contig_labels_only_int if i < contig_end_label] # 3' of SDs # if crosses either side of SDs, count labels outside SDs: if len(smaller_five_prime) and len(larger_five_prime) and len(smaller_three_prime) and len(larger_three_prime) > 1: print(x[1], len([i for i in contig_labels_only_int if i > contig_start_label]), len([i for i in contig_labels_only_int if i < contig_end_label])) elif len(smaller_five_prime) and len(larger_five_prime) > 1: print(x[1], len([i for i in contig_labels_only_int if i > contig_start_label]), len([i for i in contig_labels_only_int if i < contig_end_label])) elif len(smaller_three_prime) and len(larger_three_prime) > 1: print(x[1], len([i for i in contig_labels_only_int if i > contig_start_label]), len([i for i in contig_labels_only_int if i < contig_end_label])) # check that molecule crosses into SDs (note the change in < or > below): elif contig_orientation == "+": smaller_five_prime = [i for i in contig_labels_only_int if i < contig_start_label] # 5' of SDs larger_five_prime = [i for i in contig_labels_only_int if i >= contig_start_label] # into SDs smaller_three_prime = [i for i in contig_labels_only_int if i <= contig_end_label] # into SDs larger_three_prime = [i for i in contig_labels_only_int if i > contig_end_label] # 3' of SDs # if crosses either side of SDs, count labels outside SDs: if len(smaller_five_prime) and len(larger_five_prime) and len(smaller_three_prime) and len(larger_three_prime) > 1: print(x[1], len([i for i in contig_labels_only_int if i < contig_start_label]), len([i for i in contig_labels_only_int if i > contig_end_label])) elif len(smaller_five_prime) and len(larger_five_prime) > 1: print(x[1], len([i for i in contig_labels_only_int if i < contig_start_label]), len([i for i in contig_labels_only_int if i > contig_end_label])) elif len(smaller_three_prime) and len(larger_three_prime) > 1: print(x[1], len([i for i in contig_labels_only_int if i < contig_start_label]), len([i for i in contig_labels_only_int if i > contig_end_label])) return find_labels('results/11029B_initial_genome_check/11029B_fullContig211_molecules.xmap', 2214, 2253, "-")
python
#################### # Gui V3 16 Sept. 2017 Malacophonous ##################### ''' API for Gui v3 guiAPP guiWIN guiWID ''' from buildingblocks import guiRectangle,guiLines import random as r class Widget(): def __init__(self,_x,_y,_w,_h): self.x = _x self.y = _y self.w = _w self.h = _h self.children = [] self.parent = None self.toplevel = False self.visible = True self.batch = None self.visual = None self.group = None self.holdsFocus = True self.style = None self.pad = 3 def __repr__(self): return ('I am a {0} Widget at {1},{2} with a width {3} and height {4} in group {5}' .format(self.__class__,self.x,self.y,self.w,self.h,self.group)) def setVisibleTo(self,state,recurse = True): self.visible = state if state == True: self.visual = [guiRectangle(self.x,self.y,self.x+self.w,self.y+self.h, self.batch, [r.randint(0,255),r.randint(0,255),r.randint(0,255)]+[255])] elif state == False and self.visual is not None: #should work unless there is a disconnect between the self.visual and the actual draw batch item for component in self.visual: component.delete() self.visual = None if recurse: for child in self.children: child.setVisibleTo(state,recurse) def highlight(self): if not self.toplevel: pcolor = self.parent.visual[0].vertexlist.colors else: pcolor = [0,0,0,255] if self.visible: self.visual.append(guiLines(self.x-self.pad,self.y-self.pad,self.x+self.w+self.pad,self.y+self.h+self.pad,self.batch, [255 - pcolor[0],255-pcolor[1],255-pcolor[2],255])) def dehighlight(self): if self.visible: self.visual[-1].delete() def setStyleTo(self,style): pass def setGroupTo(self,group): self.group = group def setBatchTo(self,batch,recurse = True): self.batch = batch if recurse: for child in self.children: child.setBatchto(batch,recurse) def setParentTo(self,newparent): if self not in newparent.children: if self.parent is not None: # remove from old parent's children self.parent.children.remove(self) # set new parent as parent self.parent = newparent # add self to new parent's children newparent.children.append(self) else: print('{0} already parented to {1}'.format(self,self.parent)) def hitTest(self,x,y): for child in self.children: hit = child.hitTest(x,y) if hit is not None and hit.visible: return hit else: return self._hitFinal(x,y) def _hitFinal(self,x,y): if (0<x-self.x<self.w and 0<y-self.y<self.h and self.visible): return self def translate(self,dx,dy): right = self.x+dx+self.w left = self.x+dx top = self.y+dy+self.h bottom = self.y+dy if self.toplevel: px,py,pw,ph = (0,0,self.parent.width,self.parent.height) else: px,py,pw,ph = (self.parent.x,self.parent.y,self.parent.w,self.parent.h) if right >= px+pw: self.x = px+pw-self.w dx = 0 elif left <= px: self.x = px dx = 0 else: self.x = left if top >= py + ph: self.y = py + ph - self.h dy = 0 elif bottom <= py: self.y = py dy = 0 else: self.y = bottom if self.visible: self.visual[0].move(dx,dy) self.visual[1].move(dx,dy) for child in self.children: child._move(dx,dy) def _move(self,dx,dy): #only for use for a widget's children when that widget is being translated #this should cut down on conditional checks for translations of widgets with lots of children if self.visible: self.visual[0].move(dx,dy) for child in self.children: child._move(dx,dy) def gainedMouseOver(self,window): pass def lostMouseOver(self,window): pass def gainedFocus(self,window): @window.event def on_mouse_drag(x,y,dx,dy,buttons,modifiers): self.translate(dx,dy) self.highlight() def lostFocus(self,window): self.dehighlight()
python
# coding: utf-8 # ====================================================================== # DZI-IIIF # DeepZoom(dzi)形式のファイルをIIIF Image APIでアクセスできるようにする # ====================================================================== # 2020-05-21 Ver.0.1: Initial Version, No info.json handling. # 2020-05-22 Ver.0.2: Add info.json handling. # ====================================================================== # dziiiif_dzifile.py: DZI 形式ファイルの読み込みと加工 # ====================================================================== # * 20200518 メモ suzuki # 当分 Collection Type の Deep Zoom はサポートしない # root が Image のものだけサポートする # * 20200520 メモ suzuki # 画像処理モジュールとして Pillow のインストールが必要 # >pip install Pillow (管理者モードで実行) # グローバル変数の輸入 import dziiiif_common as glo # モジュールの輸入 import os import xml.etree.ElementTree as ET import re from PIL import Image import math import io import sys # XMLファイル(定義情報)の名前 dzi_xmlfile = 'dzc_output.xml' # 画像フォルダの名前 dzi_imgfiles = 'dzc_output_files' # タグ名から xml namespace 名を分離 def getxmlns(s): m = re.search(r'^\{[^\}]*\}', s) return m.group() #fed # タグ名からxml namespaceを除いた名前を求める def stripxmlns(s): m = re.search(r'[^\{\}]*$', s) return m.group(); #fed # XMLファイルの解析を試みる # root を返す def tryXMLparse(s): try: return ET.parse(s).getroot() except ET.ParseError: return ET.fromstring('<X xmlns="Illegal_XML_file"></X>') #yrt #fed # XMLファイルの読み込み def getxmlinfo(): xmlpath = os.path.join(glo.data_path, glo.identifier, dzi_xmlfile) # XMLファイルへのパスを生成 if os.path.isfile(xmlpath): root = tryXMLparse(xmlpath) # XMLファイルの読み込み glo.xmlns = getxmlns(root.tag) if (stripxmlns(root.tag) == 'Image'): # rootタグが Image なら必要なデータを読み出す a = root.attrib glo.dzi_tilesize = glo.readint(a['TileSize']) glo.dzi_overlap= glo.readint(a['Overlap']) glo.dzi_format= a['Format'] f = False for child in root: if (stripxmlns(child.tag) == 'Size'): a = child.attrib glo.dzi_w = glo.readint(a['Width']) glo.dzi_h = glo.readint(a['Height']) glo.dzi_maxlevel = math.ceil(max(math.log2(glo.dzi_w), math.log2(glo.dzi_h))) f = True else: pass #fi #rof if (not f): glo.change_status_at(glo.status_code.NOT_FOUND, 'dzifile.getxmlinfo; Size tag') else: glo.change_status_at(glo.status_code.NOT_FOUND, 'dzifile.getxmlinfo; Image tag') #fi else: glo.change_status_at(glo.status_code.NOT_FOUND, 'dzifile.getxmlinfo; XML file') #fi #fed # x を 0 <= x < dzi_w の範囲に収める def adjustX(x): return min(max(x, 0), glo.dzi_w - 1) #fed # y を 0 <= y < dzi_h の範囲に収める def adjustY(y): return min(max(y, 0), glo.dzi_h - 1) #fed # 切り取る画像領域の確定 def getregion(): if (glo.region == glo.region_mode.full): x1 = 0 y1 = 0 w = glo.dzi_w h = glo.dzi_h elif (glo.region == glo.region_mode.square): wh = min(glo.dzi_w, glo.dzi_h) # 短い方に合わせる x1 = int((glo.dzi_w - wh) / 2.0) y1 = int((glo.dzi_h - wh) / 2.0) w = wh h = wh elif (glo.region == glo.region_mode.pixel): x1 = adjustX(glo.region_x) y1 = adjustY(glo.region_y) x2 = adjustX(glo.region_x + glo.region_w) y2 = adjustY(glo.region_y + glo.region_h) w = max(x2 - x1, 0) h = max(y2 - y1, 0) elif (glo.region == glo.region_mode.percent): x = math.floor(dzi_w * glo.region_x / 100.0) y = math.floor(dzi_h * glo.region_y / 100.0) w = math.floor(dzi_w * glo.region_w / 100.0) h = math.floor(dzi_h * glo.region_h / 100.0) x1 = adjustX(x) y1 = adjustY(y) x2 = adjustX(x + w) y2 = adjustY(y + h) w = max(x2 - x1, 0) h = max(y2 - y1, 0) else: glo.change_status_at(glo.status_code.INTERNAL_SERVER_ERROR, 'dzifile.getregion') # 起こるはずないんだけど #fi if (w == 0 or h == 0): # 領域が0もしくは画像外 glo.change_status_at(glo.status_code.BAD_REQUEST, 'dzifile.getregion; w == 0 || h == 0') #fi glo.dzi_region_x = x1 glo.dzi_region_y = y1 glo.dzi_region_w = w glo.dzi_region_h = h #fed # 画像サイズの確定 def getsize(): if (glo.size == glo.size_mode.full or glo.size == glo.size_mode.max ): glo.outimage_w = glo.dzi_region_w glo.outimage_h = glo.dzi_region_h elif (glo.size == glo.size_mode.w_align): glo.outimage_w = glo.size_w glo.outimage_h = math.floor(glo.size_w * glo.dzi_region_h / glo.dzi_region_w) elif (glo.size == glo.size_mode.h_align): glo.outimage_w = math.floor(glo.size_h * glo.dzi_region_w / glo.dzi_region_h) glo.outimage_h = glo.size_h elif (glo.size == glo.size_mode.percent): glo.outimage_w = math.floor(glo.dzi_region_w * glo.size_percent / 100.0) glo.outimage_h = math.floor(glo.dzi_region_h * glo.size_percent / 100.0) elif (glo.size == glo.size_mode.wh): glo.outimage_w = glo.size_w glo.outimage_h = glo.size_h elif (glo.size == glo.size_mode.wh_align): dzi_ratio = glo.dzi_region_w / glo.dzi_region_h # 大きいほど縦長,小さいほど横長 size_ratio = glo.size_w / glo.size_h if (dzi_ratio >= size_ratio): # 画像の方が指定サイズより横長:幅を優先で納める glo.outimage_w = glo.size_w glo.outimage_h = math.floor(glo.size_w * glo.dzi_region_h / glo.dzi_region_w) else: # 画像の方が指定サイズより縦:高さを優先で納める glo.outimage_w = math.floor(glo.size_h * glo.dzi_region_w / glo.dzi_region_h) glo.outimage_h = glo.size_h #fi else: glo.change_status_at(glo.status_code.INTERNAL_SERVER_ERROR, 'dzifile.getsize') # 起こるはずないんだけど #fi if (glo.outimage_w == 0 or glo.outimage_h == 0): # 画像サイスが0 glo.change_status_at(glo.status_code.BAD_REQUEST, 'dzifile.getsize; outimage_w == 0 || outimage_h == 0') #fi if (glo.outimage_w > glo.wh_max or glo.outimage_h > glo.wh_max): # 画像サイスが制限を超えている glo.change_status_at(glo.status_code.BAD_REQUEST, 'dzifile.getsize; outimage_w > wh_max || outimage_h > wh_max') #fi #fed # 出力ファイルの生成 def makeoutputimage(): mag_ratio = max(glo.outimage_w / glo.dzi_region_w, glo.outimage_h / glo.dzi_region_h) # 拡大・縮小率 tileimage_index = math.floor(math.log2(1 / mag_ratio)) # 読み込むタイル画像の倍率(逆数のlog2の指数) tileimage_folder_id = str(glo.dzi_maxlevel - tileimage_index).strip() # タイル画像のフォルダ # タイル画像座標系における領域 scaledimage_x = math.floor(glo.dzi_region_x / (2 ** tileimage_index)) scaledimage_y = math.floor(glo.dzi_region_y / (2 ** tileimage_index)) scaledimage_w = math.floor(glo.dzi_region_w / (2 ** tileimage_index)) scaledimage_h = math.floor(glo.dzi_region_h / (2 ** tileimage_index)) # 読み込むタイル画像のインデックスと領域 tileimage_i1 = math.floor(scaledimage_x / glo.dzi_tilesize) tileimage_i2 = math.floor((scaledimage_x + scaledimage_w - 1) / glo.dzi_tilesize) tileimage_j1 = math.floor(scaledimage_y / glo.dzi_tilesize) tileimage_j2 = math.floor((scaledimage_y +scaledimage_h - 1) / glo.dzi_tilesize) tileimage_x = tileimage_i1 * glo.dzi_tilesize tileimage_y = tileimage_j1 * glo.dzi_tilesize tileimage_w = (tileimage_i2 - tileimage_i1 + 1) * glo.dzi_tilesize tileimage_h = (tileimage_j2 - tileimage_j1 + 1) * glo.dzi_tilesize # タイル画像から読み込む入力画像の領域 inimage_x = scaledimage_x - tileimage_x inimage_y = scaledimage_y - tileimage_y inimage_w = scaledimage_w inimage_h = scaledimage_h # 読み込むタイル画像 image1 = Image.new('RGB', (tileimage_w, tileimage_h), (0, 0, 0)) # 画像の読み込み for i in range(tileimage_i1, tileimage_i2 + 1): for j in range (tileimage_j1, tileimage_j2 + 1): inimage_fn = str(i).strip()+"_"+str(j).strip()+"."+glo.dzi_format # ファイル名 inimage_path = os.path.join(glo.data_path, glo.identifier, dzi_imgfiles, tileimage_folder_id, inimage_fn) # タイル画像ファイルへのパスを生成 glo.inimage_path = inimage_path if (os.path.isfile(inimage_path)): inimage = Image.open(inimage_path) x1 = glo.dzi_overlap if (i > tileimage_i1) else 0 y1 = glo.dzi_overlap if (j > tileimage_j1) else 0 x2 = x1 + glo.dzi_tilesize y2 = y1 + glo.dzi_tilesize cimg = inimage.crop((x1, y1, x2, y2)) image1.paste(cimg, ((i - tileimage_i1) * glo.dzi_tilesize, (j - tileimage_j1) * glo.dzi_tilesize)) else: glo.change_status_at(glo.status_code.NOT_FOUND, 'dzifile.makeoutputimage; DZI image file') break #fi #rof #rof # 読んだタイル画像から画像を切り出す if (glo.status == glo.status_code.OK): image2 = image1.crop((inimage_x, inimage_y, inimage_x + inimage_w, inimage_y + inimage_h)) glo.outimage = image2.resize((glo.outimage_w, glo.outimage_h)) o = io.BytesIO() glo.outimage.save(o, format=glo.format_PILstr[glo.format], quality=glo.outimage_quality) glo.outstream = o.getvalue() glo.outstream_size = len(glo.outstream) #fi #fed
python
# Returns the upper triangular part of a matrix (2-D tensor) # torch.triu(input, diagonal=0, *, out=None) → Tensor # The argument 'diagonal' controls which diagonal to consider. import torch source_tensor = torch.ones((10, 10)) # print(source_tensor) tensor = (torch.triu(source_tensor) == 1).transpose(0, 1) print(tensor) print(tensor.float().masked_fill(tensor == 0, float('-inf')).masked_fill(tensor == 1, float(0.0)))
python
import cant_utils as cu import numpy as np import matplotlib.pyplot as plt import glob import bead_util as bu import tkinter import tkinter.filedialog import os, sys from scipy.optimize import curve_fit import bead_util as bu from scipy.optimize import minimize_scalar as minimize import pickle as pickle import time #################################################### ####### Input parameters for data processing ####### TESTING = True ddict = bu.load_dir_file( "/home/charles/opt_lev_classy/scripts/cant_force/dir_file.txt" ) #print ddict respdir = 'Y' resp_axis = 1 # imaging response direction cant_axis = 1 # stage control axis straighten_axis = 2 # axis with coherent drive to straighten bin_size = 5 # um of cantilever travel load_charge_cal = True maxfiles = 1000 plot_forward_backward = False #True #subtract_background = True drivefreq = 18.0 cant_volts_to_um = 8.0 # 80 um / 10 V #fig_title = ('Force vs. Cantilever Position: %s Hz, %s - %s, ' + bead) % (drivefreq, gas, num) #dirs = [530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543] # 0 um sep dirs = [544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557] # 10 um sep #dirs = [558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571] # 20 um sep tf_path = './trans_funcs/Hout_20160808.p' step_cal_path = './calibrations/step_cal_20160808.p' thermal_cal_file_path = '/data/20160808/bead1/1_5mbar_zcool_final.h5' fcurve_path = '/home/charles/gravity/data/force_curves.p' force_curve_dic = pickle.load( open(fcurve_path, 'rb') ) # Identify Sep and Rbead def proc_dir(d): dv = ddict[d] dir_obj = cu.Data_dir(dv[0], [0,0,dv[-1]], dv[1]) dir_obj.load_dir(cu.diag_loader, maxfiles = maxfiles) dir_obj.load_H(tf_path) if load_charge_cal: dir_obj.load_step_cal(step_cal_path) else: dir_obj.charge_step_calibration = step_calibration dir_obj.gravity_signals = force_curve_dic dir_obj.calibrate_H() dir_obj.diagonalize_files(reconstruct_lowf=True, lowf_thresh=200., #plot_Happ=True, \ build_conv_facs=True, drive_freq=18.) amps = [] for fil_obj in dir_obj.fobjs: stagestuff = fil_obj.get_stage_settings(axis=straighten_axis) amp = stagestuff[2] * cant_volts_to_um amps.append(amp) uamps = np.unique(amps) if len(uamps) > 1: print('STUPIDITYERROR: Multiple dirve amplitudes in directory') newlist = [] for i in [0,1,2]: if i == straighten_axis: newlist.append(uamps[0]) else: newlist.append(0.0) dir_obj.drive_amplitude = newlist return dir_obj dir_objs = list(map(proc_dir, dirs)) colors_yeay = bu.get_color_map( len(dir_objs) ) f, axarr = plt.subplots(3,2,sharey='all',sharex='all',figsize=(10,12),dpi=100) for ind, obj in enumerate(dir_objs): col = colors_yeay[ind] cal_facs = obj.conv_facs obj.get_avg_force_v_pos(cant_axis = cant_axis, bin_size = bin_size) obj.get_avg_diag_force_v_pos(cant_axis = cant_axis, bin_size = bin_size) keys = list(obj.avg_force_v_pos.keys()) for key in keys: amp = obj.drive_amplitude[straighten_axis] if straighten_axis == 0: lab = 'X: ' elif straighten_axis == 1: lab = 'Y: ' elif straighten_axis == 2: lab = 'Z: ' lab = lab + str(amp) + ' um' for resp_axis in [0,1,2]: xdat = obj.avg_force_v_pos[key][resp_axis,0][0] ydat = (obj.avg_force_v_pos[key][resp_axis,0][1]) * cal_facs[resp_axis] errs = (obj.avg_force_v_pos[key][resp_axis,0][2]) * cal_facs[resp_axis] xdat_d = obj.avg_diag_force_v_pos[key][resp_axis,0][0] ydat_d = obj.avg_diag_force_v_pos[key][resp_axis,0][1] errs_d = obj.avg_diag_force_v_pos[key][resp_axis,0][2] xdatf = obj.avg_force_v_pos[key][resp_axis,1][0] xdatb = obj.avg_force_v_pos[key][resp_axis,-1][0] ydatf = (obj.avg_force_v_pos[key][resp_axis,1][1]) * cal_facs[resp_axis] ydatb = (obj.avg_force_v_pos[key][resp_axis,-1][1]) * cal_facs[resp_axis] errsf = (obj.avg_force_v_pos[key][resp_axis,1][2]) * cal_facs[resp_axis] errsb = (obj.avg_force_v_pos[key][resp_axis,-1][2]) * cal_facs[resp_axis] xdatf_d = obj.avg_diag_force_v_pos[key][resp_axis,1][0] xdatb_d = obj.avg_diag_force_v_pos[key][resp_axis,-1][0] ydatf_d = obj.avg_diag_force_v_pos[key][resp_axis,1][1] ydatb_d = obj.avg_diag_force_v_pos[key][resp_axis,-1][1] errsf_d = obj.avg_diag_force_v_pos[key][resp_axis,1][2] errsb_d = obj.avg_diag_force_v_pos[key][resp_axis,-1][2] offsetf = 0.0 offsetf_d = 0.0 offsetb = 0.0 offsetb_d = 0.0 offset = 0.0 offset_d = 0.0 if plot_forward_backward: axarr[resp_axis,0].errorbar(xdatf, (ydatf+offsetf)*1e15, errsf*1e15, \ label = lab, fmt='<-', ms=5, color = col, mew=0.0) axarr[resp_axis,1].errorbar(xdatf_d, (ydatf_d+offsetf_d)*1e15, errsf_d*1e15, \ label = lab, fmt='<-', ms=5, color = col, mew=0.0) axarr[resp_axis,0].errorbar(xdatb, (ydatb+offsetb)*1e15, errsb*1e15, \ fmt='>-', ms=5, color = col, mew=0.0) axarr[resp_axis,1].errorbar(xdatb_d, (ydatb_d+offsetb_d)*1e15, errsb_d*1e15, \ fmt='>-', ms=5, color = col, mew=0.0) else: axarr[resp_axis,0].errorbar(xdat, (ydat+offset)*1e15, errs*1e15, \ label = lab, fmt='.-', ms=10, color = col) axarr[resp_axis,1].errorbar(xdat_d, (ydat_d+offset_d)*1e15, errs_d*1e15, \ label = lab, fmt='.-', ms=10, color = col) arrs = [axarr,] for arr in arrs: arr[0,0].set_title('Raw Imaging Response') arr[0,1].set_title('Diagonalized Forces') for col in [0,1]: arr[2,col].set_xlabel('Distance from Cantilever [um]') arr[0,0].set_ylabel('X-direction Force [fN]') arr[1,0].set_ylabel('Y-direction Force [fN]') arr[2,0].set_ylabel('Z-direction Force [fN]') arr[0,0].legend(loc=0, numpoints=1, ncol=2, fontsize=9) plt.show()
python
import logging import time import cv2 as cv import numpy as np from scipy.sparse import lil_matrix from scipy.optimize import least_squares def project(points, camera_params, K, dist=np.array([])): """ Project 2D points using given camera parameters (R matrix and t vector), intrinsic matrix, and distortion parameters. :params points: 3D points to reproject :params camera_params: camera parameters (N x 12) with [:9] containing rotation matrix params and [9:12] containing translation vector parameters :params K: camera intrinsic matrix :params dist: distortion parameters (N x 5) :return points_proj: Numpy array of reprojected points """ points_proj = [] for idx in range(len(camera_params)): # idx applies to both points and cam_params, they are = length vectors R = camera_params[idx][:9].reshape(3, 3) rvec, _ = cv.Rodrigues(R) t = camera_params[idx][9:] pt = points[idx] pt = np.expand_dims(pt, axis=0) pt, _ = cv.projectPoints(pt, rvec, t, K, distCoeffs=dist) pt = np.squeeze(np.array(pt)) points_proj.append(pt) return np.array(points_proj) def fun(params, n_cameras, n_points, camera_indices, point_indices, points_2d, K): """ Main optimization function to minimize. Takes the difference between reprojected points and 2D points from views. :params params: numpy array of [camera parameters; 3D points] :params n_cameras: total number of cameras/views :params n_points: total number of 3D points :params camera_indices: numpy array of camera/view indicies corresponding with each 3D point :params point_indices: numpy array of indicies for 3D points corresponidng to each observation :params points_2d: 2D points corresponding to observations :params K: camera intrinsics matrix """ camera_params = params[:n_cameras * 12].reshape((n_cameras, 12)) points_3d = params[n_cameras * 12:].reshape((n_points, 3)) points_proj = project(points_3d[point_indices], camera_params[camera_indices], K) return (points_proj - points_2d).ravel() def bundle_adjustment_sparsity(n_cameras, n_points, camera_indices, point_indices): """ Handles the sparsity for Jacobian. :params n_cameras: total number of cameras/views :params n_points: total number of 3D points :params camera_indices: numpy array of camera/view indicies corresponding with each 3D point :params point_indices: numpy array of indicies for 3D points corresponidng to each observation """ m = camera_indices.size * 2 n = n_cameras * 12 + n_points * 3 A = lil_matrix((m, n), dtype=int) i = np.arange(camera_indices.size) for s in range(12): A[2 * i, camera_indices * 12 + s] = 1 A[2 * i + 1, camera_indices * 12 + s] = 1 for s in range(3): A[2 * i, n_cameras * 12 + point_indices * 3 + s] = 1 A[2 * i + 1, n_cameras * 12 + point_indices * 3 + s] = 1 return A class BundleAdjustment: def __init__(self, wpSet, K, dist, completed_views): self.completed_views = completed_views self.wpSet = wpSet self.points_3d = wpSet.world_points self.points_2d = [] self.point_indices = [] self.camera_indices = [] self.view_idx = {} self.camera_params = [] self.focal_len = (K[0, 0] + K[1, 1]) / 2 self.dist = dist[0][:2] self.K = K self.correspondences = wpSet.correspondences self.n_cameras = None self.n_points = None def view2idx(self): """ Takes in a list of views and converts them to indices. For each 2D point, a view index is assigned.""" for view in self.completed_views: if view.id not in self.view_idx: self.view_idx[view.id] = len(self.view_idx) rot_vec = np.squeeze(view.rotation) # 1 x 9 params = np.concatenate((rot_vec, view.translation.reshape((1, 3))), axis=None).tolist() # print(view.name, params) self.camera_params.append(params) self.camera_params = np.array(self.camera_params) for i, row in self.correspondences.iterrows(): self.points_2d.append(row['FeatureIndex'][0]) self.camera_indices.append(self.view_idx[row['ViewId'][0]]) self.points_2d.append(row['FeatureIndex'][1]) self.camera_indices.append(self.view_idx[row['ViewId'][1]]) self.point_indices.append(i) self.point_indices.append(i) self.camera_indices = np.array(self.camera_indices) self.point_indices = np.array(self.point_indices) self.points_2d = np.array(self.points_2d) self.points_3d = np.array(self.points_3d) self.n_points = self.points_3d.shape[0] self.n_cameras = self.camera_params.shape[0] logging.info(f"Number of views processed: {self.n_cameras}.") logging.info(f"Number of 3D points processed: {self.n_points}.") np.savez('optimize_data', camera_params=self.camera_params, points_3d=self.points_3d, camera_indices=self.camera_indices, point_indices=self.point_indices, points_2d=self.points_2d) def optimize(self): """ Performs optimization on reprojection error function by updating poses and 3D points. :return poses: (N x 12) numpy array containing optimized pose information with [:9] containing rotation matrix params and [9:12] containing translation vector parameters :return points_3d: optimized 3D points """ self.view2idx() x0 = np.hstack((self.camera_params.ravel(), self.points_3d.ravel())) print(len(self.camera_params.ravel()), len(self.points_3d.ravel())) fun(x0, self.n_cameras, self.n_points, self.camera_indices, self.point_indices, self.points_2d, self.K) A = bundle_adjustment_sparsity(self.n_cameras, self.n_points, self.camera_indices, self.point_indices) t0 = time.time() res = least_squares(fun, x0, jac_sparsity=A, verbose=2, x_scale='jac', ftol=1e-4, method='trf', xtol=1e-12, args=(self.n_cameras, self.n_points, self.camera_indices, self.point_indices, self.points_2d, self.K)) t1 = time.time() logging.info(f"Optimized {self.n_points} in {t1-t0} seconds.") points_3d = res.x[self.n_cameras * 12:].reshape(self.n_points, 3) poses = res.x[:self.n_cameras * 12].reshape(self.n_cameras, 12) return poses, points_3d
python
# Copyright (c) 2010-2019 openpyxl import pytest from openpyxl.xml.functions import fromstring, tostring from openpyxl.tests.helper import compare_xml @pytest.fixture def Marker(): from ..marker import Marker return Marker class TestMarker: def test_ctor(self, Marker): marker = Marker(symbol=None, size=5) xml = tostring(marker.to_tree()) expected = """ <marker> <symbol val="none"/> <size val="5"/> <spPr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> <a:ln> <a:prstDash val="solid" /> </a:ln> </spPr> </marker> """ diff = compare_xml(xml, expected) assert diff is None, diff def test_from_xml(self, Marker): src = """ <marker> <symbol val="square"/> <size val="5"/> </marker> """ node = fromstring(src) marker = Marker.from_tree(node) assert marker == Marker(symbol="square", size=5) @pytest.fixture def DataPoint(): from ..marker import DataPoint return DataPoint class TestDataPoint: def test_ctor(self, DataPoint): dp = DataPoint(idx=9) xml = tostring(dp.to_tree()) expected = """ <dPt> <idx val="9"/> <spPr> <a:ln xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> <a:prstDash val="solid"/> </a:ln> </spPr> </dPt> """ diff = compare_xml(xml, expected) assert diff is None, diff def test_from_xml(self, DataPoint): src = """ <dPt> <idx val="9"/> <marker> <symbol val="triangle"/> <size val="5"/> </marker> <bubble3D val="0"/> </dPt> """ node = fromstring(src) dp = DataPoint.from_tree(node) assert dp.idx == 9 assert dp.bubble3D is False
python
from telegram import InlineKeyboardButton def generate_buttons(labels): buttons = [[InlineKeyboardButton(labels[0], callback_data=labels[0]), InlineKeyboardButton(labels[1], callback_data=labels[1])], [InlineKeyboardButton(labels[2], callback_data=labels[2]), InlineKeyboardButton(labels[3], callback_data=labels[3])]] return buttons question = {0:"Qual'è l'unità di misura della tensione elettrica?", 1:"Qual'è il modulo del numero complesso 4+i3 ?", 2:"Qual'è l'unità di misura della corrente elettrica?", 3:"Qual'è l'unità di misura della potenza?", 4:"Qual'è la derivata di sin(x) rispetto a x?", 5:"Sia f(x)=k1*x + k2. Qual'è la derivata di f(x) rispetto a x?", 6:"Quanto vale il valor medio di sin(x) calcolato su un periodo?", 7:"Qual'è l'unità di misura del campo elettrico?", 8:"Qual'è l'unità di misura del campo magnetico?", 9:"Qual'è l'unità di misura della frequenza?", 10:"Come si calcola l'energia cinetica?", 11:"Se f è una funzione lineare ed f(x1) vale 5. Quanto vale f(5 x1)?", 12:"Sia f una funzione periodica. Il suo integrale su un periodo vale 10. Quanto vale l'integrale su tre periodi?", 13:"20°C, espressi in kelvin, sono pari a:", 14:"Durante la fase di evaporazione, la temperatura dell'acqua:", 15:"Il numero complesso 5-i3 in quale quadrante si trova?", 16:"Il numero complesso -5-i5 ha fase pari a:", 17:"Due vettori hannno modulo pari a 5 e sono sfasati di 180 gradi. Il modulo della loro somma vale:", 18:"Due vettori hannno modulo pari a 5 e sono sfasati di 90 gradi. Il modulo della loro somma vale:", 19:"Un corpo solido ha un peso di 9810 N. Quanto vale, approssimativamente, la sua massa?"} answer = {0:generate_buttons(['metri (m)', 'joule (J)', 'volt (V)', 'newton (N)']), 1:generate_buttons(['16', '5', '9', '25']), 2:generate_buttons(['ampere (A)', 'watt (W)', 'Farad (F)', 'weber (Wb)']), 3:generate_buttons(['watt (W)', 'joule (J)', 'pascal (Pa)', 'kelvin (K)']), 4:generate_buttons(['log(x)', '1/tan(x)', '-cos(x)', 'cos(x)']), 5:generate_buttons(['k2+k1', 'k1', 'k2', 'k1*k2']), 6:generate_buttons(['0', '1', 'infinito', '-1']), 7:generate_buttons(['V', 'kg', 'm/V', 'V/m']), 8:generate_buttons(['rad', 'A/m', 'm2', 'Hz']), 9:generate_buttons(['m', 'Wb', 'Hz', '°C']), 10:generate_buttons(['mv', '0.5 m v^2', 'm g h', 'v^2/2']), 11:generate_buttons(['1', '5', '25', '0.5']), 12:generate_buttons(['10', '3.3', '15', '30']), 13:generate_buttons(['-273.15 K', '293.15 K','10000 K','-20 K']), 14:generate_buttons(['aumenta', 'è costante', 'diminuisce', 'vale 100 K']), 15:generate_buttons(['primo', 'secondo', 'terzo', 'quarto']), 16:generate_buttons(['0 gradi', '-135 gradi', '90 gradi', '-pi/2']), 17:generate_buttons(['10', 'indefinito', '5/sqrt(2)', '0']), 18:generate_buttons(['0', '5/sqrt(2)', '10', '5*sqrt(2)']), 19:generate_buttons(['100 kg', '1 tonnellata', '10 kg', '1000 g'])} right_answer = {0:"volt (V)", 1:"5", 2:"ampere (A)", 3:"watt (W)", 4:"cos(x)", 5:"k1", 6:"0", 7:"V/m", 8:"A/m", 9:"Hz", 10:"0.5 m v^2", 11:"25", 12:"30", 13:"293.15 K", 14:"è costante", 15:"quarto", 16:"-135 gradi", 17:"0", 18:"5*sqrt(2)", 19:"1 tonnellata"}
python
import base64 from authlib.common.encoding import to_bytes, to_unicode import fence.utils def create_basic_header(username, password): """ Create an authorization header from the username and password according to RFC 2617 (https://tools.ietf.org/html/rfc2617). Use this to send client credentials in the authorization header. """ text = "{}:{}".format(username, password) auth = to_unicode(base64.b64encode(to_bytes(text))) return {"Authorization": "Basic " + auth} def create_basic_header_for_client(oauth_client): """ Wrap ``create_basic_header`` to make a header for the client. """ return create_basic_header(oauth_client.client_id, oauth_client.client_secret)
python
# ywo queue to stack import queue class ArrayQueue(object): def __init__(self): self.queue1 = queue.Queue(5) self.help = queue.Queue(5) def push(self, data): """模仿压栈""" if self.queue1.full() == True: raise RuntimeError('the stack is full') self.queue1.put(data) def pop(self): """模仿弹栈""" if self.queue1.empty(): raise RuntimeError('the stack is empty') while self.queue1.qsize() != 1: self.help.put(self.queue1.get()) result = self.queue1.get() temp = self.queue1 self.queue1 = self.help self.help = temp return result if __name__ == '__main__': q = ArrayQueue() for i in range(5): q.push(i) for i in range(5): print(q.pop()) # q.push(3)
python
''' Written by Jinsung Yoon Date: Jul 9th 2018 (Revised Oct 19th 2018) Generative Adversarial Imputation Networks (GAIN) Implementation on MNIST Reference: J. Yoon, J. Jordon, M. van der Schaar, "GAIN: Missing Data Imputation using Generative Adversarial Nets," ICML, 2018. Paper Link: http://medianetlab.ee.ucla.edu/papers/ICML_GAIN.pdf Appendix Link: http://medianetlab.ee.ucla.edu/papers/ICML_GAIN_Supp.pdf Contact: [email protected] ''' #%% Packages import matplotlib matplotlib.use('agg') import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import numpy as np import os from tqdm import tqdm import tflib as lib import tflib.plot import math import pandas as pd def calcNMSE(truth, pred): mse = np.sum(np.abs(truth-pred), axis=1) truthNorm = np.sum(truth, axis=1) print(mse.shape, truthNorm.shape, min(mse), min(truthNorm)) nmse = mse/truthNorm print(np.mean(nmse), np.median(nmse)) return(np.mean(nmse), np.median(nmse)) def preprocessData(X, maxZerosInCell, maxZerosInGene): cellSums = (X==0).sum(axis=1) selectedCells = cellSums <= maxZerosInCell geneSums = (X==0).sum(axis=0) selectedGenes = geneSums <= maxZerosInGene #print(cellSums, geneSums, maxZerosInCell, maxZerosInGene) #print(geneSums, np.sum(selectedGenes)) selectedCellsIdxs = np.array([i for i, v in enumerate(selectedCells) if v]) selectedGenesIdxs = np.array([i for i, v in enumerate(selectedGenes) if v]) #print(selectedCellsIdxs, selectedGenesIdxs) X_f = X[selectedCellsIdxs[:, None], selectedGenesIdxs] X_f_log = np.log(X_f+1) #print("==============") #print(X[:5, :5]) #print(X_f[:5, :5]) #print(X_f_log[:5, :5]) maxPerCell = X_f_log.max(axis=1) print(np.min(X), np.max(X), np.min(maxPerCell), np.max(maxPerCell)) #print(maxPerCell[:5], len(maxPerCell)) X_f_log_norm = X_f_log/maxPerCell[:,None] return(X_f_log_norm, selectedGenes, selectedCells, maxPerCell) def preprocessData2(X, maxZerosInCell, maxZerosInGene): cellSums = (X==0).sum(axis=1) selectedCells = cellSums <= maxZerosInCell geneSums = (X==0).sum(axis=0) selectedGenes = geneSums <= maxZerosInGene #print(cellSums, geneSums, maxZerosInCell, maxZerosInGene) #print(geneSums, np.sum(selectedGenes)) selectedCellsIdxs = np.array([i for i, v in enumerate(selectedCells) if v]) selectedGenesIdxs = np.array([i for i, v in enumerate(selectedGenes) if v]) #print(selectedCellsIdxs, selectedGenesIdxs) X_f = X[selectedCellsIdxs[:, None], selectedGenesIdxs] s = X_f.sum(axis=1) s_m = np.median(s) s_ = s/s_m X_f_norm = X_f/s_[:,None] X_f_norm_log = np.log(X_f_norm+1) #print("==============") #print(X[:5, :5]) #print(X_f[:5, :5]) #print(X_f_log[:5, :5]) return(X_f_norm_log, selectedGenes, selectedCells, s_) def nonZeroMean(v): meu = 0 if np.count_nonzero(v) == 0 else np.median(v[v!=0]).round(decimals=2) return(meu) def getMask(X, alpha, sparsity=101): geneAvgs = pd.DataFrame(X).apply(nonZeroMean) geneSums = (X==0).sum(axis=0) maxNZerosInGene = sparsity*X.shape[0]/100 #print(geneAvgs) #print(geneAvgs) mask = np.ones(X.shape) #X_bar = mask*geneAvgs[None,:] mask[(geneAvgs[None,:] > alpha) & (geneSums <= maxNZerosInGene) & (X == 0)] = 0 return(mask, geneAvgs) def transformBack(X_, X, M): X_ = M*X+(1-M)*X_ X_t = np.transpose(X_) return(X_t) def transformBackAll(X_, X, M, filteredGenes, filteredCells, maxPerCell): X_ = M*X+(1-M)*X_ print(np.min(X_), np.max(X_)) X_ = X_*maxPerCell[:,None] print(np.min(X_), np.max(X_)) X_ = np.exp(X_)-1 print(np.min(X_), np.max(X_)) X_t = np.transpose(X_) return(X_t) # Rows are genes, Cols are cells data_suffix = 'PBMC'#'dropout_index_5_seed_20000' out_suffix = 'PBMC'#'5_20000' data = pd.read_csv('simulation_data/simulation_data_'+data_suffix+'.csv', delimiter=',', header=None) data_full = pd.read_csv('simulation_data/simulation_data_'+data_suffix+'_logn_true.csv', delimiter=',', header=None) data_full = data_full.T.to_numpy() #data = data.loc[1:10,1:6].T #print(data.to_numpy) data = data.to_numpy()#[:1000,:] print("Data with ", data.shape[0], " cells, and ", data.shape[1], " genes") #print(data[:5, :5]) maxZerosInCell = 95*data.shape[1]/100 maxZerosInGene = 95*data.shape[0]/100 small_zero_thre = 2#0.6#0.7 data_f, filteredGenes, filteredCells, maxPerCell = preprocessData2(data, maxZerosInCell, maxZerosInGene) selectedCellsIdxs = np.array([i for i, v in enumerate(filteredCells) if v]) selectedGenesIdxs = np.array([i for i, v in enumerate(filteredGenes) if v]) print(len(selectedCellsIdxs), len(selectedGenesIdxs), selectedCellsIdxs, selectedGenesIdxs) data_full = data_full[selectedGenesIdxs[:, None], selectedCellsIdxs] np.savetxt('imputation_gain_data/'+out_suffix+'_selectedGenes.csv', selectedGenesIdxs+1 , delimiter=',', fmt="%d") #print(data_f, filteredGenes, filteredCells, maxPerCell) mask, geneAvgs = getMask(data_f, small_zero_thre, 101) #data_f = data_f.to_numpy() print("Impute Matrix After Preprocessing ", data_f.shape) #print(mask.shape) print(data[:5, :5]) print(data_f[:5, :5]) print(mask[:5, :5]) np.savetxt('imputation_gain_data/'+out_suffix+'_logn.csv', data_f, delimiter=',', fmt="%f") #np.savetxt('imputation_gain_data/'+out_suffix+'_mask.csv', mask, delimiter=',', fmt="%f") #mask2 = pd.read_csv('simulation_data/mask.csv', delimiter=',', header=None).T.to_numpy() print("Mask Dim ", mask.shape) #print(mask[:5, :5]) #res = mask2*2+mask #np.savetxt('imputation_gain_data/'+out_suffix+'_mask_diff.csv', res, delimiter=',', fmt="%i") #np.savetxt('imputation_gain_data/'+out_suffix+'_mask_avgs.csv', geneAvgs, delimiter=',', fmt="%.3f") #idxs = np.where(res[1,]==2) #print(geneAvgs[idxs[0]], data_f[1, idxs[0]]) #exit(0) #%% System Parameters # 1. Mini batch size mb_size = 128 # 3. Hint rate p_hint = 0.9 # 4. Loss Hyperparameters alpha = 5 # 5. Imput Dim (Fixed) Dim = data_f.shape[1] # Mask Vector and Hint Vector Generation def sample_M(m, n, p): A = np.random.uniform(0., 1., size = [m, n]) B = A > p C = 1.*B #C[:,7150:7155] = 0 return C def sample_M_bias(m, n, p, probs): #probs = probs/sum(probs) num = int(p*n) l = np.array([np.random.choice(n, num, False, probs) for i in range(m)]) rows = np.repeat(range(m), num) cols = l.reshape(-1) #print(l, rows, cols) mask = np.ones((m, n)) mask[rows, cols] = 0 return(mask) ###################### ## TensorFlow ###################### #%% Necessary Functions # 1. Xavier Initialization Definition def xavier_init(size): in_dim = size[0] xavier_stddev = 1. / tf.sqrt(in_dim / 2.) return tf.random_normal(shape = size, stddev = xavier_stddev) # 2. Plot (4 x 4 subfigures) def plot(samples): fig = plt.figure(figsize = (5,5)) gs = gridspec.GridSpec(5,5) gs.update(wspace=0.05, hspace=0.05) for i, sample in enumerate(samples): ax = plt.subplot(gs[i]) plt.axis('off') ax.set_xticklabels([]) ax.set_yticklabels([]) ax.set_aspect('equal') plt.imshow(sample.reshape(298,11), cmap='Greys_r') return fig ''' GAIN Consists of 3 Components - Generator - Discriminator - Hint Mechanism ''' #%% GAIN Architecture #%% 1. Input Placeholders # 1.1. Data Vector X = tf.placeholder(tf.float32, shape = [None, Dim]) # 1.2. Mask Vector M = tf.placeholder(tf.float32, shape = [None, Dim]) # 1.3. Hint vector H = tf.placeholder(tf.float32, shape = [None, Dim]) # 1.4. Random Noise Vector Z = tf.placeholder(tf.float32, shape = [None, Dim]) NZ = tf.placeholder(tf.float32, shape = [None, Dim]) #%% 2. Discriminator D_W1 = tf.Variable(xavier_init([Dim*2, 256])) # Data + Hint as inputs D_b1 = tf.Variable(tf.zeros(shape = [256])) D_W2 = tf.Variable(xavier_init([256, 128])) D_b2 = tf.Variable(tf.zeros(shape = [128])) D_W3 = tf.Variable(xavier_init([128, Dim])) D_b3 = tf.Variable(tf.zeros(shape = [Dim])) # Output is multi-variate theta_D = [D_W1, D_W2, D_W3, D_b1, D_b2, D_b3] #%% 3. Generator G_W1 = tf.Variable(xavier_init([Dim*2, 256])) # Data + Mask as inputs (Random Noises are in Missing Components) G_b1 = tf.Variable(tf.zeros(shape = [256])) EMB_SIZE=128 G_W2 = tf.Variable(xavier_init([256, EMB_SIZE])) G_b2 = tf.Variable(tf.zeros(shape = [EMB_SIZE])) G_W3 = tf.Variable(xavier_init([EMB_SIZE, Dim])) G_b3 = tf.Variable(tf.zeros(shape = [Dim])) theta_G = [G_W1, G_W2, G_W3, G_b1, G_b2, G_b3] #%% GAIN Function #%% 1. Generator def generator(x,z,m): inp = m * x + (1-m) * z # Fill in random noise on the missing values inputs = tf.concat(axis = 1, values = [inp,m]) # Mask + Data Concatenate G_h1 = tf.nn.relu(tf.matmul(inputs, G_W1) + G_b1) G_h2 = tf.nn.relu(tf.matmul(G_h1, G_W2) + G_b2) #G_prob = tf.nn.sigmoid(tf.matmul(G_h2, G_W3) + G_b3) # [0,1] normalized Output G_prob = tf.nn.relu(tf.matmul(G_h2, G_W3) + G_b3) # [0,1] normalized Output return G_prob, G_h2 #%% 2. Discriminator def discriminator(x, m, g, h): inp = m * x + (1-m) * g # Replace missing values to the imputed values inputs = tf.concat(axis = 1, values = [inp,h]) # Hint + Data Concatenate D_h1 = tf.nn.relu(tf.matmul(inputs, D_W1) + D_b1) D_h2 = tf.nn.relu(tf.matmul(D_h1, D_W2) + D_b2) D_logit = tf.matmul(D_h2, D_W3) + D_b3 D_prob = tf.nn.sigmoid(D_logit) # [0,1] Probability Output return D_prob #%% 3. Others # Random sample generator for Z def sample_Z(m, n): return np.random.uniform(0., 1., size = [m, n]) def sample_idx(m, n): A = np.random.permutation(m) idx = A[:n] return idx #%% Structure G_sample, G_embed = generator(X,Z,M) D_prob = discriminator(X, M, G_sample, H) #%% Loss NZC = 1#NZ#+1-NZ D_loss1 = -tf.reduce_mean(M * NZC * tf.log(D_prob + 1e-8) + (1-M) * NZC * tf.log(1. - D_prob + 1e-8)) * 2 G_loss1 = -tf.reduce_mean((1-M) * NZC * tf.log(D_prob + 1e-8)) / tf.reduce_mean(tf.maximum(1-M * NZC, 1) ) MSE_train_loss = tf.reduce_mean( NZC * (M * X - M * G_sample)**2) / tf.reduce_mean(tf.maximum(M * NZC, 1) ) D_loss = D_loss1 G_loss = G_loss1 + alpha * MSE_train_loss #%% MSE Performance metric MSE_test_loss = tf.reduce_mean(((1-M) * NZC * X - (1-M) * NZC * G_sample)**2) / tf.reduce_mean(tf.maximum((1-M) * NZC, 1) ) #%% Solver D_solver = tf.train.AdamOptimizer().minimize(D_loss, var_list=theta_D) G_solver = tf.train.AdamOptimizer().minimize(G_loss, var_list=theta_G) # Sessions os.environ["CUDA_VISIBLE_DEVICES"]="0" saver = tf.train.Saver() sess = tf.Session() sess.run(tf.global_variables_initializer()) #%% # Output Initialization if not os.path.exists('imputation_gain_data/'): os.makedirs('imputation_gain_data/') # Iteration Initialization trainX = data_f trainM = mask testX = trainX[:,:] testM = trainM[:,:] Train_No = trainX.shape[0] geneProbs = np.sum(trainM) #print([nonZeroMean(trainX[:,i]) for i in range(7150, 7155)]) print("NZeros=", np.count_nonzero(trainX)/(trainX.shape[0]*trainX.shape[1]*1.0), np.count_nonzero(testX)/(trainX.shape[0]*trainX.shape[1]*1.0)) print("NZeros=", np.count_nonzero(1-testM)/(trainX.shape[0]*trainX.shape[1]*1.0), np.count_nonzero(trainX*(1-testM))*1.0/np.count_nonzero(1-testM)) cutoff_i = 0 cutoffs = [2, 1, 0.75, 0.5, 0.5, 0.5] sparsity = [101, 101, 101, 60, 85, 90] maxIters = [2000, 10000, 25000, 70000, 70000, 70000] maxIters = [1000, 3000, 6000, 70000, 70000, 70000] percent_nonzero = 0#np.count_nonzero(trainX*(1-np.transpose(testM)))*1.0/np.count_nonzero(1-testM) #%% Start Iterations for it in tqdm(range(70020)): #%% Inputs mb_idx = sample_idx(Train_No, mb_size) X_mb = trainX[mb_idx,:] Z_mb = sample_Z(mb_size, Dim) M_mb = trainM[mb_idx,:] H_mb1 = sample_M(mb_size, Dim, 1-p_hint) H_mb = M_mb * H_mb1 + 0.5 * (1-H_mb1) New_X_mb = M_mb * X_mb + (1-M_mb) * Z_mb # Missing Data Introduce _, D_loss_curr = sess.run([D_solver, D_loss1], feed_dict = {X: X_mb, M: M_mb, Z: New_X_mb, H: H_mb, NZ:(X_mb > 0)*1}) _, G_loss_curr, MSE_train_loss_curr, MSE_test_loss_curr = sess.run([G_solver, G_loss1, MSE_train_loss, MSE_test_loss], feed_dict = {X: X_mb, M: M_mb, Z: New_X_mb, H: H_mb, NZ:(X_mb > 0)*1}) #%% Intermediate Losses if it % 100000 == 0: print('Iter: {}'.format(it)) print('Train_loss: {:.4}'.format(MSE_train_loss_curr)) print('Test_loss: {:.4}'.format(MSE_test_loss_curr)) print() #%% Output figure if it % 1000 == 0 and it > 1: #if it == 3000 or it == 25000 or it == 50000 or it == 70000: preds, MSE_train_loss_curr, MSE_test_loss_curr, d_pr = sess.run([G_sample, MSE_train_loss, MSE_test_loss, D_prob], feed_dict = {X: testX, M: testM, Z: testM * testX + (1-testM) * sample_Z(testM.shape[0], Dim), H: testM, NZ:(testX > 0)*1}) imp = transformBack(preds, testX, testM) print(imp.shape, testM.shape, testX.shape, data_full.shape) #print([nonZeroMean(imp[:,i]) for i in range(7150, 7155)]) mse = ((data_full-imp)**2).mean(axis=None) mse_preds = ((data_full-np.transpose(preds))**2).mean(axis=None) mse_masked = (((data_full-imp)*(1-np.transpose(testM)))**2).mean(axis=None) print("threshold:", cutoff_i, cutoffs[cutoff_i], maxIters[cutoff_i], sparsity[cutoff_i]) print("MSE=", mse) nz = (1-testM).sum(axis=1) print("AvgNImputsPerCell=", np.min(nz), np.max(nz), np.median(nz)) print("NZeros=", np.count_nonzero(imp)/(imp.shape[0]*imp.shape[1]*1.0), np.count_nonzero(testX)/(imp.shape[0]*imp.shape[1]*1.0)) print("NZeros=", np.count_nonzero(1-testM)/(imp.shape[0]*imp.shape[1]*1.0), np.count_nonzero(imp*(1-np.transpose(testM)))*1.0/np.count_nonzero(1-testM)) percent_nonzero = np.count_nonzero(imp*(1-np.transpose(testM)))*1.0/np.count_nonzero(1-testM) #np.savetxt('imputation_gain_data/'+'/gain_preds_pr.txt', d_pr, delimiter=',', fmt="%f") lib.plot.plot('imputation_gain_data/'+'/loss', "MSE_train", MSE_train_loss_curr) lib.plot.plot('imputation_gain_data/'+'/loss', "MSE_test", MSE_test_loss_curr) lib.plot.plot('imputation_gain_data/'+'/MSE', "MSE", mse) lib.plot.plot('imputation_gain_data/'+'/MSE', "MSE preds", mse_preds) lib.plot.plot('imputation_gain_data/'+'/MSE', "MSE imp only", mse_masked) lib.plot.plot('imputation_gain_data/'+'/NZeros', "NZeros_imp", np.count_nonzero(imp)/(imp.shape[0]*imp.shape[1]*1.0)) lib.plot.plot('imputation_gain_data/'+'/NZeros', "NZeros_masked_imp", np.count_nonzero(imp*(1-np.transpose(testM)))*1.0/np.count_nonzero(1-testM)) lib.plot.flush() if it % 5000 == 0 and it > 1: #imp_final = transformBackAll(preds, testX, testM, filteredGenes, filteredCells, maxPerCell) np.savetxt('imputation_gain_data/'+'/gain_'+out_suffix+'_transformed.csv', imp, delimiter=',', fmt="%f") #np.savetxt('imputation_gain_data/'+'/gain_'+out_suffix+"_"+str(it)+'.csv', imp_final, delimiter=',', fmt="%f") if percent_nonzero > 0.95 or it > maxIters[cutoff_i]: np.savetxt('imputation_gain_data/'+'/gain_'+out_suffix+'_'+str(it)+'_'+str(cutoffs[cutoff_i])+'_transformed.csv', imp, delimiter=',', fmt="%f") cutoff_i += 1 mask, geneAvgs = getMask(data_f, cutoffs[cutoff_i], sparsity[cutoff_i]) trainM = mask testM = trainM[:,:] trainX = np.transpose(imp) testX = trainX[:,:] percent_nonzero = 0 print("\n=========================\nNew Cutoff : ", cutoffs[cutoff_i]) print("NZeros=", np.count_nonzero(1-testM)/(imp.shape[0]*imp.shape[1]*1.0), np.count_nonzero(imp*(1-np.transpose(testM)))*1.0/np.count_nonzero(1-testM)) lib.plot.flush() if it == 100000:#1000: mask, geneAvgs = getMask(data_f, 1)#0.4) trainM = mask testM = trainM[:,:] trainX = np.transpose(imp) testX = trainX[:,:] if it == 250000:#25000: mask, geneAvgs = getMask(data_f, 0.75)#0.25) trainM = mask testM = trainM[:,:] trainX = np.transpose(imp) testX = trainX[:,:] if it == 500000:#50000: mask, geneAvgs = getMask(data_f, 0.5)#0.2) trainM = mask testM = trainM[:,:] trainX = np.transpose(imp) testX = trainX[:,:] lib.plot.tick() #if it % 20000 == 1: # saver.save(sess, "imputation_gain_data/gain_model", global_step=it)
python
import datetime import names from django.contrib.auth import get_user_model from usersetting.models import UserSetting User = get_user_model() def initialize_usersetting(email): while(True): try: nickname = names.get_first_name() UserSetting.objects.get(nickname=nickname) except: break enlisted_date = datetime.datetime.now().strftime("%Y-%m-%d") delisted_date = (datetime.datetime.now() + datetime.timedelta(days=548)).strftime("%Y-%m-%d") promotion1_date = (datetime.datetime.now() + datetime.timedelta(days=60)).strftime("%Y-%m-%d") promotion2_date = (datetime.datetime.now() + datetime.timedelta(days=240)).strftime("%Y-%m-%d") promotion3_date = (datetime.datetime.now() + datetime.timedelta(days=400)).strftime("%Y-%m-%d") UserSetting.objects.create(email=User.objects.get(email=email), nickname=nickname, major='army', type='soldier', enlisted_date=enlisted_date, delisted_date=delisted_date, promotion1_date=promotion1_date, promotion2_date=promotion2_date, promotion3_date=promotion3_date)
python
import numpy as np import pandas as pd def create_rolling_ts( input_data, lookback=5, return_target=True, apply_datefeatures=True, return_np_array=False ): """ Make flat data by using pd.concat instead, pd.concat([df1, df2]). Slow function. Save data as preprocessed? """ x = [] y = [] rows = len(input_data) features = input_data.copy() target = input_data.copy() for i in range(rows - lookback): """Create embeddings for the date-features""" if apply_datefeatures: rolling_features = date_features(features.iloc[i: i + lookback]) else: rolling_features = features.iloc[i: i + lookback] rolling_target = target.iloc[i + lookback: i + lookback + 1] x.append(rolling_features) y.append(rolling_target) if return_np_array: x = np.array(x) y = np.array(y) if return_target: return x, y return x def date_features(df): if isinstance(df, pd.core.series.Series): df = pd.DataFrame(df, index=df.index) df.loc[:, 'day_of_year'] = df.index.dayofyear df.loc[:, 'month'] = df.index.month df.loc[:, 'day_of_week'] = df.index.day df.loc[:, 'hour'] = df.index.hour return df def split_data(data, train_size, valid_size): """ Implement data based splitting. Do normalization. """ train_size = int(len(data) * train_size) valid_size = int(train_size + len(data) * valid_size) try: train_set = data.iloc[: train_size] valid_set = data.iloc[train_size: valid_size] test_set = data.iloc[valid_size: ] return train_set, valid_set, test_set except Exception as e: print(f'Exception from _split_data: {e}') def square(i): return i ** 2 def flatten(data): """Make data n X 1 dimensional""" return data.reshape(data.shape[0], -1) def is_pandas(df): return isinstance(df, (pd.core.frame.DataFrame, pd.core.series.Series)) """ EXPERIMENTAL """ # transform a time series dataset into a supervised learning dataset def series_to_supervised(data, n_in=1, n_out=1, dropnan=True): n_vars = 1 if type(data) is list else data.shape[1] df = pd.DataFrame(data) cols = list() # input sequence (t-n, ... t-1) for i in range(n_in, 0, -1): cols.append(df.shift(i)) # forecast sequence (t, t+1, ... t+n) for i in range(0, n_out): cols.append(df.shift(-i)) # put it all together agg = pd.concat(cols, axis=1) # drop rows with NaN values if dropnan: agg.dropna(inplace=True) return agg.values # split a univariate dataset into train/test sets def train_test_split(data, n_test): return data[:-n_test, :], data[-n_test:, :] # walk-forward validation for univariate data def walk_forward_validation(data, n_test): predictions = list() # split dataset train, test = train_test_split(data, n_test) # seed history with training dataset history = [x for x in train] # step over each time-step in the test set for i in range(len(test)): # split test row into input and output columns testX, testy = test[i, :-1], test[i, -1] # fit model on history and make a prediction yhat = xgboost_forecast(history, testX) # store forecast in list of predictions predictions.append(yhat) # add actual observation to history for the next loop history.append(test[i]) # summarize progress print('>expected=%.1f, predicted=%.1f' % (testy, yhat)) # estimate prediction error error = mean_absolute_error(test[:, -1], predictions) return error, test[:, -1], predictions
python
import time def merge(data, low, high, middle, drawData, timetick): color=[] for i in range (len(data)): color.append('sky blue') left = data[low:middle+1] right = data[middle+1:high+1] i = 0 j = 0 for k in range(low, high+1): if i< len(left) and j<len(right): if left[i] <= right[j] : data[k] = left[i] i += 1 else: data[k]=right[j] j+=1 elif i<len(left): data[k]=left[i] i += 1 else: data[k]=right[j] j += 1 for p in range (len(data)): if p==low + i or p==middle + 1 + j : color[p] = 'yellow' else: color[p] = 'sky blue' drawData(data, color) time.sleep(timetick) def merge_sort(data, low, high, drawData, timetick): if low < high: middle=(low + high)//2 merge_sort(data, low,middle, drawData, timetick) merge_sort(data, middle+1, high, drawData, timetick) merge(data, low, high, middle, drawData, timetick) drawData(data, ['sky blue' for x in range (len(data))]) time.sleep(timetick)
python
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery from pyrogram import Client, emoji from datetime import datetime, timedelta from shamil.voicechat import mp @Client.on_callback_query() async def cb_handler(client: Client, query: CallbackQuery): if query.data == "replay": group_call = mp.group_call if not mp.playlist: return group_call.restart_playout() await mp.update_start_time() start_time = mp.start_time playlist = mp.playlist if not start_time: await query.edit_message_text(f"{emoji.PLAY_BUTTON} **Nothing Playing!**") return utcnow = datetime.utcnow().replace(microsecond=0) if mp.msg.get('current') is not None: playlist=mp.playlist if not playlist: pl = f"{emoji.NO_ENTRY} **Empty Playlist!**" else: if len(playlist) == 1: pl = f"{emoji.REPEAT_SINGLE_BUTTON} **Playlist**:\n" else: pl = f"{emoji.PLAY_BUTTON} **Playlist**:\n" pl += "\n".join([ f"**{i}**. **{x.audio.title}**" for i, x in enumerate(playlist) ]) await mp.msg['current'].delete() mp.msg['current'] = await playlist[0].reply_text( f"{pl}\n\n{emoji.PLAY_BUTTON} {utcnow - start_time} / " f"{timedelta(seconds=playlist[0].audio.duration)}", parse_mode="Markdown", reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton("Replay", callback_data="replay"), InlineKeyboardButton("Pause", callback_data="pause"), InlineKeyboardButton("Skip", callback_data="skip") ], ] ) ) elif query.data == "pause": mp.group_call.pause_playout() await mp.update_start_time(reset=True) playlist = mp.playlist if not playlist: pl = f"{emoji.NO_ENTRY} **Empty Playlist!**" else: if len(playlist) == 1: pl = f"{emoji.REPEAT_SINGLE_BUTTON} **Playlist**:\n" else: pl = f"{emoji.PLAY_BUTTON} **Playlist**:\n" pl += "\n".join([ f"**{i}**. **{x.audio.title}**" for i, x in enumerate(playlist) ]) reply = await query.edit_message_text(f"{emoji.PLAY_OR_PAUSE_BUTTON} **Paused Playing 🤐!**\n\n{pl}", reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton("Replay", callback_data="replay"), InlineKeyboardButton("Resume️", callback_data="resume"), InlineKeyboardButton("Skip", callback_data="skip") ], ] ) ) elif query.data == "resume": mp.group_call.resume_playout() playlist=mp.playlist if not playlist: pl = f"{emoji.NO_ENTRY} **Empty Playlist!**" else: if len(playlist) == 1: pl = f"{emoji.REPEAT_SINGLE_BUTTON} **Playlist**:\n" else: pl = f"{emoji.PLAY_BUTTON} **Playlist**:\n" pl += "\n".join([ f"**{i}**. **{x.audio.title}**" for i, x in enumerate(playlist) ]) await query.edit_message_text(f"{emoji.PLAY_OR_PAUSE_BUTTON} **Resumed Playing 🤗!**\n\n{pl}", reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton("Replay", callback_data="replay"), InlineKeyboardButton("Pause", callback_data="pause"), InlineKeyboardButton("Skip", callback_data="skip") ], ] ) ) elif query.data=="skip": playlist = mp.playlist await mp.skip_current_playing() if not playlist: pl = f"{emoji.NO_ENTRY} **Empty Playlist!**" else: if len(playlist) == 1: pl = f"{emoji.REPEAT_SINGLE_BUTTON} **Playlist**:\n" else: pl = f"{emoji.PLAY_BUTTON} **Playlist**:\n" pl += "\n".join([ f"**{i}**. **{x.audio.title}**" for i, x in enumerate(playlist) ]) try: await query.edit_message_text(f"⏭ **Skipped Track 🤷‍♀️!**\n\n{pl}", reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton("Replay", callback_data="replay"), InlineKeyboardButton("Pause", callback_data="pause"), InlineKeyboardButton("Skip", callback_data="skip") ], ] ) ) except: pass elif query.data=="help": await query.edit_message_text("🙋‍♂️ **Hi Bruh**, \nIts Me Samantha Here 👋🏻 ...", reply_markup=InlineKeyboardMarkup( [ [ InlineKeyboardButton("Close", callback_data="close"), ], ] ) ) elif query.data=="close": await query.message.delete()
python
#!/usr/bin/env python import yaml import json def main(): my_list = range(8) my_list.append('0 through 7 are cool numbers') my_list.append({}) my_list[-1]['subnet_mask'] = '255.255.255.0' my_list[-1]['gateway'] = '192.168.1.1' with open("first_yaml_file.yml", "w") as f: f.write(yaml.dump(my_list, default_flow_style=False)) with open("first_json_file.json", "w") as g: json.dump(my_list, g) if __name__ == "__main__": main()
python
import os import glob import sys class CAPSProfile: def __init__(self): self.streams = [] # List of strings, e.g. XY.ABCD.*.* self.stations = [] # List of network, station tuples, e.g. (XY,ABCD) self.oldStates = [] # Content of old state file ''' Plugin handler for the CAPS plugin. ''' class SeedlinkPluginHandler: # Create defaults def __init__(self): self.profiles = {} def push(self, seedlink): try: maxTimeDiff = float(seedlink._get('plugins.caps.maxTimeDiff', False)) except: maxTimeDiff = 86400 inOrder = "" try: if seedlink._get('plugins.caps.inOrder', False): inOrder = " --in-order" except: pass # Check and set defaults try: address = seedlink.param('sources.caps.address') except KeyError: address = "localhost:18002" try: streams = [chaId.strip() for chaId in seedlink.param( 'sources.caps.streams').split(',')] except KeyError: seedlink.setParam('sources.caps.streams', "*.*") streams = ["*.*"] try: encoding = seedlink.param('sources.caps.encoding') except KeyError: seedlink.setParam('sources.caps.encoding', "STEIM2") # parse address URL and create capsId of form: # host[.port][_user] addressFormatError = "Error: invalid address format, expected " \ "[[caps|capss]://][user:pass@]host[:port]" # protocol toks = address.split("://") if len(toks) > 2: raise Exception(addressFormatError) elif len(toks) == 2: protocol = toks[0] address = toks[1] if protocol != "caps" and protocol != "capss": raise Exception(addressFormatError) else: protocol = "caps" # authentication toks = address.split("@") if len(toks) > 2: raise Exception(addressFormatError) elif len(toks) == 2: capsId = "%s_%s" % (toks[1].replace( ":", "."), toks[0].split(":")[0]) else: capsId = address.replace(":", ".") address = "%s://%s" % (protocol, address) if capsId not in self.profiles: profile = CAPSProfile() self.profiles[capsId] = profile else: profile = self.profiles[capsId] for chaId in streams: toks = chaId.split('.') if len(toks) != 2: raise Exception( "Error: invalid stream format, expected [LOC.CHA]") streamID = seedlink.net + "." + seedlink.sta + "." + chaId profile.streams.append(streamID) profile.stations.append((seedlink.net, seedlink.sta)) log = os.path.join(seedlink.config_dir, "caps2sl.%s.state" % capsId) streamsFile = os.path.join( seedlink.config_dir, "caps2sl.%s.req" % capsId) seedlink.setParam('sources.caps.address', address) seedlink.setParam('sources.caps.log', log) seedlink.setParam('sources.caps.streamsFile', streamsFile) seedlink.setParam('sources.caps.maxTimeDiff', maxTimeDiff) seedlink.setParam('sources.caps.inOrder', inOrder) seedlink.setParam('seedlink.caps.id', capsId) return capsId def flush(self, seedlink): # Populate request file per address for id, profile in self.profiles.items(): caps2slreq = os.path.join( seedlink.config_dir, "caps2sl.%s.req" % id) fd = open(caps2slreq, "w") for streamId in profile.streams: fd.write("%s\n" % streamId) fd.close() try: caps2slstate = os.path.join(seedlink.config_dir, "caps2sl.%s.state" % id) # Read existing state file fd = open(caps2slstate, "r") profile.oldStates = [line.strip() for line in fd.readlines()] except: pass # Delete all existing state files for fl in glob.glob(os.path.join(seedlink.config_dir, "caps2sl.*.state")): try: os.remove(fl) except: sys.stderr.write("Failed to remove old state file: %s\n" % str(fl)) # Clean up state file to contain only configured stations for id, profile in self.profiles.items(): caps2slstate = os.path.join(seedlink.config_dir, "caps2sl.%s.state" % id) newStates = [] for (net, sta) in profile.stations: for line in profile.oldStates: if line.startswith(net + "." + sta + "."): newStates.append(line) if len(newStates) > 0: fd = open(caps2slstate, "w") for line in newStates: fd.write(line + "\n") fd.close()
python
#!/usr/bin/env python # -*- coding: utf-8 -*- import unittest import time import xlrd from public import saveScreenshot from core import LoginPage from test import support from public.log import logger from public.pyse import Pyse class LoginTest(unittest.TestCase): def setUp(self): self.driver = Pyse("chrome") self.driver.wait(10) self.url = "http://dxttest.dxtmobile.com/dxtyhch/a/login" self.verificationErrors = [] def action_login(self, case_id='case_0000', case_summary=u'正确验证', username=u'73103741刘婷', password=u'000000'): login_page = LoginPage.LoginPage(self.driver, self.url, u"萃花销售助手 登录") login_page.iopen() logger.info(u"======== 【" + case_id + u"】" + case_summary + u" ========") logger.info("username:"+username+" password:"+password) # 调用用户名输入组件 login_page.type_username(username) login_page.type_password(password) login_page.submit() time.sleep(3) saveScreenshot.saveScreenshot(self.driver, u"登录") try: assert (self.driver.get_title() == u"萃花销售助手"), u"登录成功" logger.info(u"登录成功") except: logger.info(u"登录失败") @staticmethod def getTestFunc(case_id, case_summary, username, password): def func(self): self.action_login(case_id, case_summary, username, password) return func def tearDown(self): self.driver.close() self.assertEqual([], self.verificationErrors) def __generateTestCases(): data = xlrd.open_workbook(r"./data/login_126mail_data.xls") # 通过索引顺序获取Excel表 table = data.sheets()[0] for args in range(1, table.nrows): txt = table.row_values(args) setattr(LoginTest, 'test_login_%s' % txt[1], LoginTest.getTestFunc(*txt)) __generateTestCases() if __name__ == '__main__': support.run_unittest(LoginTest)
python
import os import pprint import random import collections from ai_list_common import ( ListInterface, ListBase, ListTypeNgram, TestRunnerCheckNgram, ) class NgramList(ListTypeNgram, ListBase, ListInterface): def check_num_of_gram(self, num_of_gram): if super().check_num_of_gram(num_of_gram): return if num_of_gram < 1: err = f'num_of_gram: {num_of_gram}' raise self.InvalidNumOfNgramError(err) def make_starting_token_list(self): keywords = self.config.KEYWORDS_NGRAM[:] random.shuffle(keywords) self.starting_token_list = [keywords[0][1]] def make_token_list(self): txt = self.text_target ngram_list = [] num_max = len(txt) - (self.num_of_gram - 1) for i in range(num_max): sta = i end = i + self.num_of_gram ngram_list.append(txt[sta:end]) self.token_list = ngram_list msg = 'list_size: {}'.format(len(ngram_list)) self.logger.i(msg) def make_type_name_list(self, txt): pass def make_morph_list(self, txt): pass def update_starting_token_list(self): pass def update_token_list(self): pass def _test_starting_token_list(self): tokens_start = self.get_starting_token_list() max = len(tokens_start) - 1 msg_fmt = 'start chrs: {}'.format if max > 10: msg = msg_fmt(tokens_start[0:10]) else: msg = msg_fmt(tokens_start) self.logger.i(msg) def _test_token_list(self): tokens = self.get_token_list() max = len(tokens) msg_fmt = 'ngram list: {}'.format if max > 10: msg = msg_fmt(tokens[0:10]) else: msg = msg_fmt(tokens) self.logger.i(msg) def _test_token_counts(self): tokens = self.get_token_list() path_name = '_gram_counts.txt' path = os.path.join(self.config.PATH_TMP, path_name) if self.num_of_gram == 1: with open(path, mode='w', encoding='utf-8') as file: file counts = collections.Counter(tokens) with open(path, mode='a', encoding='utf-8') as file: pprint.pprint(counts, stream=file) msg = 'Making counts file is Done!' self.logger.i(msg) if __name__ == '__main__': TestRunnerCheckNgram(NgramList)
python
# -*- coding: utf-8 -*- from __future__ import absolute_import import os import six from aiida import orm from aiida.common.lang import classproperty from aiida.plugins import factories from aiida_quantumespresso.calculations import BasePwCpInputGenerator class PwCalculation(BasePwCpInputGenerator): """`CalcJob` implementation for the pw.x code of Quantum ESPRESSO.""" _automatic_namelists = { 'scf': ['CONTROL', 'SYSTEM', 'ELECTRONS'], 'nscf': ['CONTROL', 'SYSTEM', 'ELECTRONS'], 'bands': ['CONTROL', 'SYSTEM', 'ELECTRONS'], 'relax': ['CONTROL', 'SYSTEM', 'ELECTRONS', 'IONS'], 'md': ['CONTROL', 'SYSTEM', 'ELECTRONS', 'IONS'], 'vc-md': ['CONTROL', 'SYSTEM', 'ELECTRONS', 'IONS', 'CELL'], 'vc-relax': ['CONTROL', 'SYSTEM', 'ELECTRONS', 'IONS', 'CELL'], } # Keywords that cannot be set by the user but will be set by the plugin _blocked_keywords = [ ('CONTROL', 'pseudo_dir'), ('CONTROL', 'outdir'), ('CONTROL', 'prefix'), ('SYSTEM', 'ibrav'), ('SYSTEM', 'celldm'), ('SYSTEM', 'nat'), ('SYSTEM', 'ntyp'), ('SYSTEM', 'a'), ('SYSTEM', 'b'), ('SYSTEM', 'c'), ('SYSTEM', 'cosab'), ('SYSTEM', 'cosac'), ('SYSTEM', 'cosbc'), ] _use_kpoints = True # Not using symlink in pw to allow multiple nscf to run on top of the same scf _default_symlink_usage = False @classproperty def xml_filepaths(cls): """Return a list of XML output filepaths relative to the remote working directory that should be retrieved.""" filepaths = [] for filename in cls.xml_filenames: filepath = os.path.join(cls._OUTPUT_SUBFOLDER, '{}.save'.format(cls._PREFIX), filename) filepaths.append(filepath) return filepaths @classmethod def define(cls, spec): # yapf:disable super(PwCalculation, cls).define(spec) spec.input('metadata.options.parser_name', valid_type=six.string_types, default='quantumespresso.pw') spec.input('kpoints', valid_type=orm.KpointsData, help='kpoint mesh or kpoint path') spec.input('hubbard_file', valid_type=orm.SinglefileData, required=False, help='SinglefileData node containing the output Hubbard parameters from a HpCalculation') spec.output('output_parameters', valid_type=orm.Dict, help='The `output_parameters` output node of the successful calculation.') spec.output('output_structure', valid_type=orm.StructureData, required=False, help='The `output_structure` output node of the successful calculation if present.') spec.output('output_trajectory', valid_type=orm.TrajectoryData, required=False) spec.output('output_band', valid_type=orm.BandsData, required=False, help='The `output_band` output node of the successful calculation if present.') spec.output('output_kpoints', valid_type=orm.KpointsData, required=False) spec.output('output_atomic_occupations', valid_type=orm.Dict, required=False) spec.default_output_node = 'output_parameters' # Unrecoverable errors: resources like the retrieved folder or its expected contents are missing spec.exit_code(200, 'ERROR_NO_RETRIEVED_FOLDER', message='The retrieved folder data node could not be accessed.') spec.exit_code(201, 'ERROR_NO_RETRIEVED_TEMPORARY_FOLDER', message='The retrieved temporary folder could not be accessed.') spec.exit_code(210, 'ERROR_OUTPUT_STDOUT_MISSING', message='The retrieved folder did not contain the required stdout output file.') spec.exit_code(220, 'ERROR_OUTPUT_XML_MISSING', message='The retrieved folder did not contain the required required XML file.') spec.exit_code(221, 'ERROR_OUTPUT_XML_MULTIPLE', message='The retrieved folder contained multiple XML files.') # Unrecoverable errors: required retrieved files could not be read, parsed or are otherwise incomplete spec.exit_code(300, 'ERROR_OUTPUT_FILES', message='Both the stdout and XML output files could not be read or parsed.') spec.exit_code(310, 'ERROR_OUTPUT_STDOUT_READ', message='The stdout output file could not be read.') spec.exit_code(311, 'ERROR_OUTPUT_STDOUT_PARSE', message='The stdout output file could not be parsed.') spec.exit_code(312, 'ERROR_OUTPUT_STDOUT_INCOMPLETE', message='The stdout output file was incomplete.') spec.exit_code(320, 'ERROR_OUTPUT_XML_READ', message='The XML output file could not be read.') spec.exit_code(321, 'ERROR_OUTPUT_XML_PARSE', message='The XML output file could not be parsed.') spec.exit_code(322, 'ERROR_OUTPUT_XML_FORMAT', message='The XML output file has an unsupported format.') spec.exit_code(350, 'ERROR_UNEXPECTED_PARSER_EXCEPTION', message='The parser raised an unexpected exception.') # Significant errors but calculation can be used to restart spec.exit_code(400, 'ERROR_OUT_OF_WALLTIME', message='The calculation stopped prematurely because it ran out of walltime.') spec.exit_code(410, 'ERROR_ELECTRONIC_CONVERGENCE_NOT_REACHED', message='The electronic minimization cycle did not reach self-consistency.') spec.exit_code(500, 'ERROR_IONIC_CONVERGENCE_NOT_REACHED', message='The ionic minimization cycle did not converge for the given thresholds.') spec.exit_code(501, 'ERROR_IONIC_CONVERGENCE_REACHED_EXCEPT_IN_FINAL_SCF', message='Then ionic minimization cycle converged but the thresholds are exceeded in the final SCF.') spec.exit_code(502, 'ERROR_IONIC_CYCLE_EXCEEDED_NSTEP', message='The ionic minimization cycle did not converge after the maximum number of steps.') spec.exit_code(510, 'ERROR_IONIC_CYCLE_ELECTRONIC_CONVERGENCE_NOT_REACHED', message='The electronic minimization cycle failed during an ionic minimization cycle.') spec.exit_code(511, 'ERROR_IONIC_CONVERGENCE_REACHED_FINAL_SCF_FAILED', message='The ionic minimization cycle converged, but electronic convergence was not reached in the ' 'final SCF.') spec.exit_code(520, 'ERROR_IONIC_CYCLE_BFGS_HISTORY_FAILURE', message='The ionic minimization cycle terminated prematurely because of two consecutive failures in the ' 'BFGS algorithm.') spec.exit_code(521, 'ERROR_IONIC_CYCLE_BFGS_HISTORY_AND_FINAL_SCF_FAILURE', message='The ionic minimization cycle terminated prematurely because of two consecutive failures in the ' 'BFGS algorithm and electronic convergence failed in the final SCF.') spec.exit_code(531, 'ERROR_CHARGE_IS_WRONG', message='The electronic minimization cycle did not reach self-consistency.') spec.exit_code(541, 'ERROR_SYMMETRY_NON_ORTHOGONAL_OPERATION', message='The variable cell optimization broke the symmetry of the k-points.') @classproperty def input_file_name_hubbard_file(cls): """ The relative file name of the file containing the Hubbard parameters if they should be read from file instead of specified in the input file cards. Requires the aiida-quantumespresso-hp plugin to be installed """ try: HpCalculation = factories.CalculationFactory('quantumespresso.hp') except Exception: raise RuntimeError('this is determined by the aiida-quantumespresso-hp plugin but it is not installed') return HpCalculation.input_file_name_hubbard_file @classmethod def input_helper(cls, *args, **kwargs): """ Validate if the keywords are valid Quantum ESPRESSO pw.x keywords, and also helps in preparing the input parameter dictionary in a 'standardized' form (e.g., converts ints to floats when required, or if the flag flat_mode is specified, puts the keywords in the right namelists). This function calls :py:func:`aiida_quantumespresso.calculations.helpers.pw_input_helper`, see its docstring for further information. """ from . import helpers return helpers.pw_input_helper(*args, **kwargs)
python
# -*- coding: utf-8 -*- from com.pnfsoftware.jeb.client.api import IClientContext from com.pnfsoftware.jeb.core import IRuntimeProject from com.pnfsoftware.jeb.core.units import IUnit from com.pnfsoftware.jeb.core.units.code import IDecompilerUnit, DecompilationOptions, DecompilationContext from com.pnfsoftware.jeb.core.units.code.android import IDexUnit, IDexDecompilerUnit from com.pnfsoftware.jeb.core.units.code.java import IJavaMethod, IJavaTry from com.pnfsoftware.jeb.core.util import DecompilerHelper def displayTree(e, level=0): dispatch(e,level) if e: elts = e.getSubElements() for e in elts: displayTree(e, level+1) def dispatch(ele,level): if isinstance(ele,IJavaTry): print "---------- try body start -----------" print ele.getTryBody() # try块语句序列 print "---------- try body end -------------" print "CatchCount >>> " , ele.getCatchCount() # catch块个数 for idx in range(ele.getCatchCount()): print "" print "" print "---------- catch body start -----------",idx print "Type >>> ",ele.getCatchType(idx) # catch块括号内异常类型 print "Identifier >>> ",ele.getCatchIdentifier(idx) # catch块括号内标识符 print "catch body >>> " print ele.getCatchBody(idx) # catch块语句序列 print "---------- catch body end -------------",idx print "" print "" print "finally body >>>",ele.getFinallyBody() # final块语句序列 exit(0) pass else: pass # IJavaTry def Test(ctx): assert isinstance(ctx,IClientContext) input_path = r"D:\tmp\2\project\about_dex_diff\code\jsq\jsq.dex" sign = "Lnet/cavas/show/af;->a(Lorg/apache/http/client/HttpClient;Ljava/util/Queue;)V" unit = ctx.open(input_path); assert isinstance(unit,IUnit) prj = ctx.getMainProject(); assert isinstance(prj,IRuntimeProject) dexUnit = prj.findUnit(IDexUnit); assert isinstance(dexUnit,IDexUnit) dexDecompilerUnit = DecompilerHelper.getDecompiler(dexUnit); assert isinstance(dexDecompilerUnit,IDexDecompilerUnit) opt = DecompilationOptions.Builder().newInstance().flags(IDecompilerUnit.FLAG_NO_DEFERRED_DECOMPILATION).build() bool = dexDecompilerUnit.decompileAllClasses(DecompilationContext(opt)) print(bool) javaMethod = dexDecompilerUnit.getMethod(sign,False); assert isinstance(javaMethod,IJavaMethod) print("---------------- tree ----------------") displayTree(javaMethod) ''' 目标代码: final class af implements Runnable { private void a(HttpClient arg6, Queue arg7) { String v0 = (String)arg7.poll(); if(this.b != null && v0 != null) { HttpPost v1 = new HttpPost(this.b.replace(" ", "%20")); v1.setEntity(new EntityTemplate(new ag(this, v0))); try { HttpResponse v0_4 = arg6.execute(((HttpUriRequest)v1)); c.a("offer", Integer.valueOf(v0_4.getStatusLine().getStatusCode())); if(v0_4.getStatusLine().getStatusCode() == 200) { this.a(arg6, arg7); return; } } catch(ClientProtocolException v0_3) { try { c.c(d.a, "Caught ClientProtocolException in PingUrlRunnable"); return; label_35: c.c(d.a, "Caught IOException in PingUrlRunnable"); return; } catch(Throwable v0_1) { throw v0_1; } } catch(IOException v0_2) { goto label_35; return; } catch(Throwable v0_1) { throw v0_1; } } } } ''' ''' 输出: True ---------------- tree ---------------- ---------- try body start ----------- org.apache.http.HttpResponse v0_4 = execute(arg6, ((org.apache.http.client.methods.HttpUriRequest), v1)) a("offer", valueOf(getStatusCode(getStatusLine(v0_4)))) If@-2003461530 ---------- try body end ------------- CatchCount >>> 3 ---------- catch body start ----------- 0 Type >>> org.apache.http.client.ClientProtocolException Identifier >>> v0_3 catch body >>> Try@1198833152 ---------- catch body end ------------- 0 ---------- catch body start ----------- 1 Type >>> java.io.IOException Identifier >>> v0_2 catch body >>> goto label_35 return ---------- catch body end ------------- 1 ---------- catch body start ----------- 2 Type >>> java.lang.Throwable Identifier >>> v0_1 catch body >>> throw v0_1 ---------- catch body end ------------- 2 finally body >>> None '''
python
#!/usr/bin/env python # # ---------------------------------------------------------------------- # # Brad T. Aagaard, U.S. Geological Survey # Charles A. Williams, GNS Science # Matthew G. Knepley, University of Chicago # # This code was developed as part of the Computational Infrastructure # for Geodynamics (http://geodynamics.org). # # Copyright (c) 2010-2017 University of California, Davis # # See COPYING for license information. # # ---------------------------------------------------------------------- # ## @file pylith/problems/ImplicitLgDeform.py ## ## @brief Python ImplicitLgDeform object for solving equations using ## an implicit formulation with rigid body motions and small strains. ## ## Factory: pde_formulation from Implicit import Implicit # ImplicitLgDeform class class ImplicitLgDeform(Implicit): """ Python ImplicitLgDeform object for solving equations using an implicit formulation with rigid body motions and small strains. Factory: pde_formulation. """ class Inventory(Implicit.Inventory): """ Python object for managing ImplicitLgDeform facilities and properties. Provide appropriate solver for small strains as the default. """ ## @class Inventory ## Python object for managing ExplicitLumped facilities and properties. ## ## \b Properties ## @li None ## ## \b Facilities ## @li \b solver Algebraic solver. import pyre.inventory from SolverNonlinear import SolverNonlinear solver = pyre.inventory.facility("solver", family="solver", factory=SolverNonlinear) solver.meta['tip'] = "Algebraic solver." # PUBLIC METHODS ///////////////////////////////////////////////////// def __init__(self, name="implicitlgdeform"): """ Constructor. """ Implicit.__init__(self, name) return def elasticityIntegrator(self): """ Get integrator for elastic material. """ from pylith.feassemble.ElasticityImplicitLgDeform import ElasticityImplicitLgDeform return ElasticityImplicitLgDeform() # PRIVATE METHODS //////////////////////////////////////////////////// def _configure(self): """ Set members based using inventory. """ Implicit._configure(self) self.solver = self.inventory.solver return # FACTORIES //////////////////////////////////////////////////////////// def pde_formulation(): """ Factory associated with ImplicitLgDeform. """ return ImplicitLgDeform() # End of file
python
# coding=utf-8 import sys from enum import Enum from typing import List class VarType(Enum): INVALID = -1 EXIT = 0 OPERATION = 1 CONDITION = 2 SUBROUTINE = 3 START = 4 END = 5 SELECTED = 6 # 包含选择的条件语句 即yes分支或者no分支 class ConnectType(Enum): NONE = 0, NORMAL = 1, YSE = 2, NO = 3, LEFT = 4, RIGHT = 5, TOP = 6, BOTTOM = 7 class Var: def __init__(self, num: int, varType: VarType, info: str, select: str = "N/A"): self.num = num self.varType = varType self.info = info self.select = select def copy(self): return Var(self.num, self.varType, self.info, self.select) def toDef(self): if self.varType == VarType.CONDITION: return f"cnd{self.num}=>condition: {self.info}" elif self.varType == VarType.OPERATION: return f"opt{self.num}=>operation: {self.info}" elif self.varType == VarType.SUBROUTINE: return f"sub{self.num}=>subroutine: {self.info}" elif self.varType == VarType.START: return f"st=>start: 开始" elif self.varType == VarType.END: return f"ed=>end: 结束" def toConnectName(self): if self.varType == VarType.CONDITION: return f"cnd{self.num}" elif self.varType == VarType.OPERATION: return f"opt{self.num}" elif self.varType == VarType.SUBROUTINE: return f"sub{self.num}" elif self.varType == VarType.START: return f"st" elif self.varType == VarType.END: return f"ed" if self.varType == VarType.SELECTED: return f"cnd{self.num}({self.select})" class Node: def __init__(self, info: str, varType: VarType, connectType: ConnectType): self.info = info self.varType = varType self.connectType = connectType class VarTable: # 特殊节点, 输出时进行特出处理, 因此只需要保证类型正确即可 NoneVar = Var(-1, VarType.INVALID, "") # 无效节点 StartVar = Var(0, VarType.START, "") # 开始节点 EndVar = Var(1, VarType.END, "") # 结束节点 def __init__(self): self.table: List[Var] = [self.StartVar, self.EndVar] self.currentID = 1 def addVar(self, info, varType: VarType) -> Var: self.currentID += 1 var = Var(self.currentID, varType, info) self.table.append(var) return var def getVarByNode(self, node: Node) -> Var: if node.varType == VarType.START: return self.StartVar.copy() elif node.varType == VarType.END: return self.EndVar.copy() for v in self.table: if v.info == node.info and v.varType == node.varType: return v.copy() return self.addVar(node.info, node.varType) def getVarByID(self, ID: int): return self.table[ID] def getVarNum(self): return len(self.table) def genCode(self, f): for var in self.table: f.write(var.toDef()) f.write("\n") class ConnectTable: def __init__(self): self.length = 15 self.graph: List[List[ConnectType]] = [[ConnectType.NONE for col in range(self.length)] for row in range(self.length)] self.ConnectNameDict = { ConnectType.YSE: "yes", ConnectType.NO: "no", ConnectType.LEFT: "left", ConnectType.RIGHT: "right", ConnectType.TOP: "top", ConnectType.BOTTOM: "bottom" } def resize(self, toSize: int): differ = toSize - self.length for row in self.graph: for i in range(differ): row.append(ConnectType.NONE) self.length = toSize def addConnect(self, head: int, tail: int, conType: ConnectType): if head >= self.length or tail >= self.length: self.resize(max(head, tail)) self.graph[head][tail] = conType def getNameByCon(self, con: ConnectType): return self.ConnectNameDict[con] def genCode(self, varTable: VarTable, f): code = [] self.DFS(varTable, 0, code) for c in self.reduceCode(code): f.write(c) def DFS(self, varTable: VarTable, row, code): for col in range(len(self.graph[row])): con = self.graph[row][col] if con != ConnectType.NONE: if con == ConnectType.NORMAL: name = f"{varTable.getVarByID(row).toConnectName()}" else: name = f"{varTable.getVarByID(row).toConnectName()}({self.getNameByCon(con)})" code.append(name) code.append("->") code.append(f"{varTable.getVarByID(col).toConnectName()}") code.append("\n") self.DFS(varTable, col, code) @staticmethod def reduceCode(code: List[str]): newCode = [] length = len(code) i = 0 while i < length: if code[i] != "\n": newCode.append(code[i]) elif i + 1 < length and code[i - 1] == code[i + 1]: newCode.append("->") i += 2 else: newCode.append("\n") i += 1 # 由于部分分支节点需要多次遍历, 因此不能在深度优先算法中直接将遍历过的路径重置 # 否则分支节点只会出现一次 lineCode = [] s = "" for c in newCode: s += c if c == "\n": lineCode.append(s) s = "" return frozenset(lineCode) def checkIntegrity(self, varTable: VarTable): """检查每个节点是否有入度, 每个条件节点是否有两个分支""" length = varTable.getVarNum() node = 2 # 跳过start和end节点 while node < length: self.checkReference(node, varTable) if varTable.getVarByID(node).varType == VarType.CONDITION: self.checkBranch(node, varTable) node += 1 def checkBranch(self, node, varTable: VarTable): yNode = False nNode = False for col in range(len(self.graph)): if self.graph[node][col] == ConnectType.YSE: yNode = True elif self.graph[node][col] == ConnectType.NO: nNode = True if not yNode: raise CheckException(f"Node ({varTable.getVarByID(node).info}) is missing a yes branch") elif not nNode: raise CheckException(f"Node ({varTable.getVarByID(node).info}) is missing a no branch") def checkReference(self, node, varTable: VarTable): referenced = False for row in range(len(self.graph)): if self.graph[row][node] != ConnectType.NONE: referenced = True break if not referenced: raise CheckException(f"Node ({varTable.getVarByID(node).info}) is not referenced by any node") class Line: def __init__(self, num: int, value: str): self.num = num self.value = value NoneLine = Line(0, "NoneLine") class Parser: connectNameDict = { "y": ConnectType.YSE, "n": ConnectType.NO, "l": ConnectType.LEFT, "r": ConnectType.RIGHT, "t": ConnectType.TOP, "b": ConnectType.BOTTOM } def __init__(self, filename: str): self.varTable: VarTable = VarTable() self.connectTable: ConnectTable = ConnectTable() self.filename = filename self.currentLine = NoneLine def compile(self, filename: str = "flowOutput"): try: self.parseFile() # 代码生成前, 先检查关系完整性 self.connectTable.checkIntegrity(self.varTable) self.genCode(filename) print("Compile Finish.\n0 Error 0 Warning.") finally: pass # except Exception as e: # sys.stderr.write("Compile Failed.\n") # sys.stderr.write(str(e)) def parseFile(self): with open(self.filename, "r", encoding="utf8") as f: num = 0 for line in f.readlines(): num += 1 if not line.isspace(): self.parseLine(Line(num, line.replace("\n", ""))) def parseLine(self, line: Line): self.currentLine = line nodes = line.value.split() if len(nodes) < 2: raise CheckException(f"Error: Line {line.num}: The num of nodes less than 2") for i in range(len(nodes) - 1): node = self.parseNode(nodes[i]) varFst: Var = self.varTable.getVarByNode(node) varSnd: Var = self.varTable.getVarByNode(self.parseNode(nodes[i + 1])) self.connectTable.addConnect(varFst.num, varSnd.num, node.connectType) def parseNode(self, varStr: str) -> Node: # 只要此部分代码被执行, 在if中定义的变量离开if语句依然有效 if varStr[0] == '<': varType = VarType.CONDITION elif varStr[0] == '[': varType = VarType.OPERATION elif varStr[0] == '{': varType = VarType.SUBROUTINE elif varStr == "st": return Node("", VarType.START, ConnectType.NORMAL) elif varStr == "ed": return Node("", VarType.END, ConnectType.NORMAL) else: raise CheckException(f"Undefined type of {varStr}") varStr = self.removeBrackets(varStr) # 移除两端的括号 if ":" in varStr: info, typename = varStr.split(":") return Node(info, varType, self.connectNameDict[typename]) else: return Node(varStr, varType, ConnectType.NORMAL) @staticmethod def removeBrackets(s: str): colonIdx = s.find(":") if colonIdx != -1: s = s[1:colonIdx - 1] + s[colonIdx:] else: s = s[1:-1] return s def genCode(self, filename: str): with open(filename, "w", encoding="utf8") as f: self.varTable.genCode(f) f.write("\n") self.connectTable.genCode(self.varTable, f) class CheckException(Exception): def __init__(self, info): Exception.__init__(self, info) if __name__ == "__main__": print(sys.argv) if len(sys.argv) >= 2: parser = Parser(sys.argv[1]) parser.compile(sys.argv[1] + "_out") else: parser = Parser("flowTest/hashMapMain") parser.compile("flowOutput") # 优化方案 # 分支节点选择最长的路径作为向下的路径 # 即从分支节点出发, 到两个分支的交汇点, 选择路径最长的分支 # 但是如果某个路径为0, 则依然选择0路径为向下路径
python
def lcm(*values): values = set([abs(int(v)) for v in values]) if values and 0 not in values: n = n0 = max(values) values.remove(n) while any( n % m for m in values ): n += n0 return n return 0 lcm(-6, 14) 42 lcm(2, 0) 0 lcm(12, 18) 36 lcm(12, 18, 22) 396
python
from GTDLambda import * from TileCoder import * import numpy class DirectStepsToLeftDemon(GTDLambda): def __init__(self, featureVectorLength, alpha): GTDLambda.__init__(self, featureVectorLength, alpha) def gamma(self, state, observation): encoder = observation['encoder'] if (encoder == -1): return 0 elif (encoder == 1023): #This represents the extreme position return 0 else: return 1 def rho(self, action): if (action == 2): #our policy is to always move left. #ie. how many steps if we were to go directly to the left. return 1 else: return 0 def test(): d = DirectStepsToLeftDemon(8*8*8, 0.1/8) numTilings = 8 numTiles = 8 encoderPosition = 600 speed = 100 firstState = firstState = tileCode(numTilings, numTilings * numTiles * numTiles, [((encoderPosition-510.0)/(1023.0-510.0)) * numTiles, ((speed + 200.0) / 400.0) * numTiles]) encoderPosition = 1023 speed = 0 secondState = firstState = tileCode(numTilings, numTilings * numTiles * numTiles, [((encoderPosition-510.0)/(1023.0-510.0)) * numTiles, ((speed + 200.0) / 400.0) * numTiles]) d.learn(firstState, 2, secondState, 1023)
python
import os import numpy as np from tqdm import tqdm from PIL import Image from imagededup.methods import PHash def run(root: str): phasher = PHash() img = Image.open("/home/huakun/datasets/meinv/anime/IMG_0903.PNG") size = np.asarray(img.size) scale = 0.1 new_size = (size * scale).astype(int) img.resize(new_size).resize(size).save("/home/huakun/datasets/meinv/anime/IMG_0903-to-remove.PNG") encodings = phasher.encode_images(image_dir=root) duplicates = phasher.find_duplicates(encoding_map=encodings) removed = set() file_removed = [] for key, value in tqdm(duplicates.items()): if len(value): if key in removed: continue else: for v in value: file_2_remove = f"{root}/{v}" file_removed.append(file_2_remove) os.remove(file_2_remove) removed.add(v) if __name__ == '__main__': import fire fire.Fire(run)
python
#!python """ Keras implementation of CapsNet in Hinton's paper Dynamic Routing Between Capsules. The current version maybe only works for TensorFlow backend. Actually it will be straightforward to re-write to TF code. Adopting to other backends should be easy, but I have not tested this. Usage: python capsulenet.py python capsulenet.py --epochs 50 python capsulenet.py --epochs 50 --routings 3 ... ... Result: Validation accuracy > 99.5% after 20 epochs. Converge to 99.66% after 50 epochs. About 110 seconds per epoch on a single GTX1070 GPU card Author: Xifeng Guo, E-mail: `[email protected]`, Github: `https://github.com/XifengGuo/CapsNet-Keras` This is a forked version for training on audio spectrograms. """ import numpy as np import os import tensorflow as tf import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import argparse from tensorflow.keras import callbacks from tensorflow.keras import layers, models, optimizers from tensorflow.keras import backend as K from tensorflow.keras.utils import to_categorical, normalize, multi_gpu_model from capsulelayers import CapsuleLayer, PrimaryCap, Length, Mask from utils import plot_log, MetricCallback from sklearn.metrics import confusion_matrix, recall_score, accuracy_score from sklearn.preprocessing import LabelBinarizer K.set_image_data_format('channels_last') def CapsNet(input_shape, n_class, routings): """ A Capsule Network on MNIST. :param input_shape: data shape, 3d, [width, height, channels] :param n_class: number of classes :param routings: number of routing iterations :return: Two Keras Models, the first one used for training, and the second one for evaluation. `eval_model` can also be used for training. """ x = layers.Input(shape=input_shape) # Layer 1: Just a conventional Conv2D layer conv1 = layers.Conv2D(filters=256, kernel_size=9, strides=1, padding='valid', activation='relu', name='conv1')(x) # Layer 2: Conv2D layer with `squash` activation, then reshape to [None, num_capsule, dim_capsule] primarycaps = PrimaryCap(conv1, dim_capsule=8, n_channels=32, kernel_size=9, strides=2, padding='valid') # Layer 3: Capsule layer. Routing algorithm works here. digitcaps = CapsuleLayer(num_capsule=n_class, dim_capsule=16, routings=routings, name='digitcaps')(primarycaps) # Layer 4: This is an auxiliary layer to replace each capsule with its length. Just to match the true label's shape. # If using tensorflow, this will not be necessary. :) out_caps = Length(name='capsnet')(digitcaps) # Decoder network. y = layers.Input(shape=(n_class, )) masked_by_y = Mask()( [digitcaps, y] ) # The true label is used to mask the output of capsule layer. For training masked = Mask( )(digitcaps) # Mask using the capsule with maximal length. For prediction # Shared Decoder model in training and prediction decoder = models.Sequential(name='decoder') decoder.add(layers.Dense(512, activation='relu', input_dim=16 * n_class)) decoder.add(layers.Dense(1024, activation='relu')) decoder.add(layers.Dense(np.prod(input_shape), activation='sigmoid')) decoder.add(layers.Reshape(target_shape=input_shape, name='out_recon')) # Models for training and evaluation (prediction) train_model = models.Model([x, y], [out_caps, decoder(masked_by_y)]) eval_model = models.Model(x, [out_caps, decoder(masked)]) # manipulate model noise = layers.Input(shape=(n_class, 16)) noised_digitcaps = layers.Add()([digitcaps, noise]) masked_noised_y = Mask()([noised_digitcaps, y]) manipulate_model = models.Model([x, y, noise], decoder(masked_noised_y)) return train_model, eval_model, manipulate_model def margin_loss(y_true, y_pred): """ Margin loss for Eq.(4). When y_true[i, :] contains not just one `1`, this loss should work too. Not test it. :param y_true: [None, n_classes] :param y_pred: [None, num_capsule] :return: a scalar loss value. """ L = y_true * K.square(K.maximum(0., 0.9 - y_pred)) + \ 0.5 * (1 - y_true) * K.square(K.maximum(0., y_pred - 0.1)) return K.mean(K.sum(L, 1)) def train(model, eval_model, data, args): """ Training a CapsuleNet :param model: the CapsuleNet model :param data: a tuple containing training and testing data, like `((x_train, y_train), (x_test, y_test))` :param args: arguments :return: The trained model """ # unpacking the data (x_train, y_train), (x_test, y_test), classes = data print("x_train {}, y_train {}, x_test {}, y_test {}".format( x_train.shape, y_train.shape, x_test.shape, y_test.shape)) # callbacks log = callbacks.CSVLogger(args.save_dir + '/log.csv') tb = callbacks.TensorBoard(log_dir=args.save_dir + '/tensorboard-logs', batch_size=args.batch_size, histogram_freq=int(args.debug)) checkpoint = callbacks.ModelCheckpoint(args.save_dir + '/weights-{epoch:02d}.h5', monitor='val_rec_macro', mode='max', save_best_only=True, save_weights_only=True, verbose=1) lr_decay = callbacks.LearningRateScheduler( schedule=lambda epoch: args.lr * (args.lr_decay**epoch)) if os.path.isfile(args.save_dir + '/trained_model.h5'): model.load_weights(args.save_dir + '/trained_model.h5') # compile the model model.compile(optimizer=optimizers.Adam(lr=args.lr), loss=[margin_loss, 'mse'], loss_weights=[1., args.lam_recon], metrics={'capsnet': 'accuracy'}) mc = MetricCallback(validation_data=((x_test, y_test), (y_test, x_test)), labels=classes, batch_size=args.batch_size) model.fit([x_train, y_train], [y_train, x_train], batch_size=args.batch_size, epochs=args.epochs, validation_data=[[x_test, y_test], [y_test, x_test]], callbacks=[mc, log, tb, checkpoint, lr_decay], shuffle=True) model.save_weights(args.save_dir + '/trained_model.h5') print('Trained model saved to \'%s/trained_model.h5\'' % args.save_dir) plot_log(args.save_dir + '/log.csv', show=True) y_pred = eval_model.predict( x_test, batch_size=args.batch_size)[0].astype("float32") acc = accuracy_score(y_test, y_pred) cm = confusion_matrix(y_test, y_pred) recall = recall_score(y_test, y_pred, average="macro") print("Accuracy: {:.2f}%".format(acc * 100)) print("Recall score: {:.2f}%".format(recall * 100)) print("Confusion matrix:\n{}".format(cm)) return model def test(model, data, args): x_test, y_test = data y_pred, x_recon = model.predict(x_test, batch_size=100) print('-' * 30 + 'Begin: test' + '-' * 30) acc = accuracy_score(y_test, y_pred) cm = confusion_matrix(y_test, y_pred) recall = recall_score(y_test, y_pred, average="macro") print("Accuracy: {:.2f}%".format(acc * 100)) print("Recall score: {:.2f}%".format(recall * 100)) print("Confusion matrix:\n{}".format(cm)) def load_audiodata(args): #1 load training data x_train = np.load(args.data_train) #x_train = (x_train - min_train) / (max_train - min_train) y_train = np.load(args.labels_train) lb = LabelBinarizer() y_train = lb.fit_transform(y_train) x_train = x_train.reshape(-1, x_train.shape[1], x_train.shape[2], 1).astype("float32") #2 load test data x_test = np.load(args.data_test) #x_test = (x_test - min_train) / (max_train - min_train) y_test = np.load(args.labels_test) y_test = lb.transform(y_test) x_test = x_test.reshape(-1, x_test.shape[1], x_test.shape[2], 1).astype("float32") print("Training dataset {}x{}x{}x{} .. labels {}".format( x_train.shape[0], x_train.shape[1], x_train.shape[2], x_train.shape[3], y_train.shape)) print("Test dataset {}x{}x{}x{} .. labels {}".format( x_test.shape[0], x_test.shape[1], x_test.shape[2], x_test.shape[3], y_test.shape)) assert not np.any(np.isnan(x_train)) assert not np.any(np.isnan(x_test)) return x_train, y_train, x_test, y_test, lb.classes_ if __name__ == "__main__": # setting the hyper parameters parser = argparse.ArgumentParser( description="Capsule Network on 3D Audio data.") parser.add_argument('--epochs', default=50, type=int) parser.add_argument('--batch-size', default=10, type=int) parser.add_argument('--lr', default=0.001, type=float, help="Initial learning rate") parser.add_argument( '--lr-decay', default=0.9, type=float, help= "The value multiplied by lr at each epoch. Set a larger value for larger epochs" ) parser.add_argument('--lam-recon', default=0.392, type=float, help="The coefficient for the loss of decoder") parser.add_argument( '-r', '--routings', default=3, type=int, help="Number of iterations used in routing algorithm. should > 0") parser.add_argument('--debug', action='store_true', help="Save weights by TensorBoard") parser.add_argument('--save_dir', default='./result') parser.add_argument('-t', '--testing', action='store_true', help="Test the trained model on testing dataset") parser.add_argument( '-w', '--weights', default=None, help="The path of the saved weights. Should be specified when testing") parser.add_argument('-tr', '--data-train', default=None, help="Training dataset numpy file") parser.add_argument('-l-tr', '--labels-train', default=None, help="Training labels numpy file") parser.add_argument('-te', '--data-test', default=None, help="Test dataset numpy file") parser.add_argument('-l-te', '--labels-test', default=None, help="Test labels numpy file") args = parser.parse_args() print(args) if not os.path.exists(args.save_dir): os.makedirs(args.save_dir) # load data and define model x_train, y_train, x_test, y_test, classes = load_audiodata(args) model, eval_model, manipulate_model = CapsNet( input_shape=x_train.shape[1:], n_class=int(y_train.shape[1]), routings=args.routings) model.summary() # train or test if args.weights is not None: # init the model weights with provided one model.load_weights(args.weights) if not args.testing: train(model=model, eval_model=eval_model, data=((x_train, y_train), (x_test, y_test), classes), args=args) else: # as long as weights are given, will run testing if args.weights is None: print( 'No weights are provided. Will test using random initialized weights.' ) test(model=eval_model, data=(x_test, y_test), args=args)
python
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """A tf.learn implementation of tensor_forest (extremely random forests).""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from tensorflow.contrib import framework as contrib_framework from tensorflow.contrib.framework import deprecated_arg_values from tensorflow.contrib.learn.python.learn import evaluable from tensorflow.contrib.learn.python.learn import trainable from tensorflow.contrib.learn.python.learn.estimators import estimator from tensorflow.contrib.learn.python.learn.utils import export from tensorflow.contrib.tensor_forest.client import eval_metrics from tensorflow.contrib.tensor_forest.data import data_ops from tensorflow.contrib.tensor_forest.python import tensor_forest from tensorflow.python.framework import dtypes from tensorflow.python.ops import control_flow_ops from tensorflow.python.ops import math_ops from tensorflow.python.ops import state_ops from tensorflow.python.platform import tf_logging as logging from tensorflow.python.training import session_run_hook KEYS_NAME = 'keys' LOSS_NAME = 'rf_training_loss' def _assert_float32(tensors): """Assert all tensors are float32. Args: tensors: `Tensor` or `dict` of `Tensor` objects. Raises: TypeError: if any tensor is not float32. """ if not isinstance(tensors, dict): tensors = [tensors] else: tensors = tensors.values() for tensor in tensors: if tensor.dtype.base_dtype != dtypes.float32: raise TypeError('Expected dtype=float32, %s.' % tensor) class TensorForestLossHook(session_run_hook.SessionRunHook): """Monitor to request stop when loss stops decreasing.""" def __init__(self, early_stopping_rounds): self.early_stopping_rounds = early_stopping_rounds self.min_loss = None self.last_step = -1 # self.steps records the number of steps for which the loss has been # non-decreasing self.steps = 0 def before_run(self, run_context): return session_run_hook.SessionRunArgs( {'global_step': contrib_framework.get_global_step(), 'current_loss': run_context.session.graph.get_operation_by_name( LOSS_NAME).outputs[0]}) def after_run(self, run_context, run_values): current_loss = run_values.results['current_loss'] current_step = run_values.results['global_step'] self.steps += 1 # Gaurd against the global step going backwards, which might happen # if we recover from something. if self.last_step == -1 or self.last_step > current_step: logging.info('TensorForestLossHook resetting last_step.') self.last_step = current_step self.steps = 0 return if self.min_loss is None or current_loss < self.min_loss: self.min_loss = current_loss self.steps = 0 if self.steps > self.early_stopping_rounds: logging.info('TensorForestLossHook requesting stop.') run_context.request_stop() def get_model_fn(params, graph_builder_class, device_assigner, weights_name=None, keys_name=None): """Return a model function given a way to construct a graph builder.""" def _model_fn(features, targets): """Function that returns predictions, training loss, and training op.""" weights = None keys = None if weights_name and weights_name in features: weights = features.pop(weights_name) if keys_name and keys_name in features: keys = features.pop(keys_name) processed_features, spec = data_ops.ParseDataTensorOrDict(features) _assert_float32(processed_features) if targets is not None: targets = data_ops.ParseLabelTensorOrDict(targets) _assert_float32(targets) graph_builder = graph_builder_class(params, device_assigner=device_assigner) inference = {eval_metrics.INFERENCE_PROB_NAME: graph_builder.inference_graph(processed_features, data_spec=spec)} if not params.regression: inference[eval_metrics.INFERENCE_PRED_NAME] = math_ops.argmax( inference[eval_metrics.INFERENCE_PROB_NAME], 1) if keys: inference[KEYS_NAME] = keys # targets might be None if we're doing prediction (which brings up the # question of why we force everything to adhere to a single model_fn). training_loss = None training_graph = None if targets is not None: training_loss = graph_builder.training_loss(processed_features, targets, data_spec=spec, name=LOSS_NAME) training_graph = control_flow_ops.group( graph_builder.training_graph( processed_features, targets, data_spec=spec, input_weights=weights), state_ops.assign_add(contrib_framework.get_global_step(), 1)) # Put weights back in if weights is not None: features[weights_name] = weights return (inference, training_loss, training_graph) return _model_fn class TensorForestEstimator(evaluable.Evaluable, trainable.Trainable): """An estimator that can train and evaluate a random forest.""" def __init__(self, params, device_assigner=None, model_dir=None, graph_builder_class=tensor_forest.RandomForestGraphs, config=None, weights_name=None, keys_name=None, feature_engineering_fn=None, early_stopping_rounds=100): self.params = params.fill() self.graph_builder_class = graph_builder_class self.early_stopping_rounds = early_stopping_rounds self._estimator = estimator.Estimator( model_fn=get_model_fn(params, graph_builder_class, device_assigner, weights_name=weights_name, keys_name=keys_name), model_dir=model_dir, config=config, feature_engineering_fn=feature_engineering_fn) def evaluate( self, x=None, y=None, input_fn=None, feed_fn=None, batch_size=None, steps=None, metrics=None, name=None): """See evaluable.Evaluable.""" return self._estimator.evaluate( input_fn=input_fn, x=x, y=y, feed_fn=feed_fn, batch_size=batch_size, steps=steps, metrics=metrics, name=name) def fit(self, x=None, y=None, input_fn=None, steps=None, batch_size=None, monitors=None, max_steps=None): """See trainable.Trainable.""" if not monitors: monitors = [TensorForestLossHook(self.early_stopping_rounds)] self._estimator.fit(input_fn=input_fn, x=x, y=y, batch_size=batch_size, steps=steps, monitors=monitors, max_steps=max_steps) @deprecated_arg_values( estimator.AS_ITERABLE_DATE, estimator.AS_ITERABLE_INSTRUCTIONS, as_iterable=False) def predict_proba( self, x=None, input_fn=None, batch_size=None, outputs=None, as_iterable=True): """Returns prediction probabilities for given features (classification). Args: x: features. input_fn: Input function. If set, x and y must be None. batch_size: Override default batch size. outputs: list of `str`, name of the output to predict. If `None`, returns all. as_iterable: If True, return an iterable which keeps yielding predictions for each example until inputs are exhausted. Note: The inputs must terminate if you want the iterable to terminate (e.g. be sure to pass num_epochs=1 if you are using something like read_batch_features). Returns: Numpy array of predicted probabilities (or an iterable of predicted probabilities if as_iterable is True). Raises: ValueError: If both or neither of x and input_fn were given. """ results = self._estimator.predict( x=x, input_fn=input_fn, batch_size=batch_size, outputs=outputs, as_iterable=as_iterable) if as_iterable: return (x[eval_metrics.INFERENCE_PROB_NAME] for x in results) else: return results[eval_metrics.INFERENCE_PROB_NAME] @deprecated_arg_values( estimator.AS_ITERABLE_DATE, estimator.AS_ITERABLE_INSTRUCTIONS, as_iterable=False) def predict( self, x=None, input_fn=None, axis=None, batch_size=None, outputs=None, as_iterable=True): """Returns predictions for given features. Args: x: features. input_fn: Input function. If set, x must be None. axis: Axis on which to argmax (for classification). Last axis is used by default. batch_size: Override default batch size. outputs: list of `str`, name of the output to predict. If `None`, returns all. as_iterable: If True, return an iterable which keeps yielding predictions for each example until inputs are exhausted. Note: The inputs must terminate if you want the iterable to terminate (e.g. be sure to pass num_epochs=1 if you are using something like read_batch_features). Returns: Numpy array of predicted classes or regression values (or an iterable of predictions if as_iterable is True). """ results = self._estimator.predict( x=x, input_fn=input_fn, batch_size=batch_size, outputs=outputs, as_iterable=as_iterable) predict_name = (eval_metrics.INFERENCE_PROB_NAME if self.params.regression else eval_metrics.INFERENCE_PRED_NAME) if as_iterable: return (x[predict_name] for x in results) else: return results[predict_name] @deprecated_arg_values( estimator.AS_ITERABLE_DATE, estimator.AS_ITERABLE_INSTRUCTIONS, as_iterable=False) def predict_with_keys( self, x=None, input_fn=None, axis=None, batch_size=None, outputs=None, as_iterable=True): """Same as predict but also returns the example keys.""" results = self._estimator.predict( x=x, input_fn=input_fn, batch_size=batch_size, outputs=outputs, as_iterable=as_iterable) predict_name = (eval_metrics.INFERENCE_PROB_NAME if self.params.regression else eval_metrics.INFERENCE_PRED_NAME) if as_iterable: return ((x[predict_name], x.get(KEYS_NAME, None)) for x in results) else: return results[predict_name], results.get(KEYS_NAME, None) def export(self, export_dir, input_fn, signature_fn=None, default_batch_size=1): """See BaseEstimator.export.""" # Reset model function with basic device assigner. # Servo doesn't support distributed inference # but it will try to respect device assignments if they're there. # pylint: disable=protected-access orig_model_fn = self._estimator._model_fn self._estimator._model_fn = get_model_fn( self.params, self.graph_builder_class, tensor_forest.RandomForestDeviceAssigner()) result = self._estimator.export( export_dir=export_dir, use_deprecated_input_fn=True, signature_fn=(signature_fn or (export.regression_signature_fn if self.params.regression else export.classification_signature_fn_with_prob)), default_batch_size=default_batch_size, prediction_key=eval_metrics.INFERENCE_PROB_NAME) self._estimator._model_fn = orig_model_fn # pylint: enable=protected-access return result
python
import configparser import collections import os import json import copy from .utils import parse_timedelta from .scrape import get_all_scrapers import argparse # Configuration handling class AltJobOptions(collections.UserDict): """ Wrap argparse and configparser objects into one configuration dict object """ def __init__(self): # overwriting arguments parser1 = argparse.ArgumentParser( # Turn off help, so we print all options in response to -h add_help=False) # CLI only arguments parser1.add_argument('-c','--config_file', help='configuration file(s). Default locations will be checked and loaded if file exists: `~/.alt_job/alt_job.conf`, `~/alt_job.conf` or `./alt_job.conf`', metavar='<File path>', nargs='+') parser1.add_argument('-t','--template_conf', action='store_true', help='print a template config file and exit. ') parser1.add_argument('-V','--version', action='store_true', help='print Alt Job version and exit. ') args1, remaining_argv = parser1.parse_known_args() if args1.config_file: config_file = ConfigurationFile(files=args1.config_file) else: config_file = ConfigurationFile() # Determine enlabled scrapers config_file['alt_job']['enabled_scrapers'] = [ w for w in get_all_scrapers() if w in config_file ] # Determine the default arguments defaults_args=config_file['alt_job'] # Parse rest of arguments # Don't suppress add_help here so it will handle -h parser2 = argparse.ArgumentParser( # Inherit options from config_parser parents=[parser1], description="""Atl Job scrapes a bunch of green/social/alternative websites to send digest of new job postings by email. Also generates an Excel file with job postings informations.""", prog='python3 -m alt_job', formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) parser2.set_defaults(**defaults_args) # Arguments that overwrites [alt_job] config values parser2.add_argument("-x", "--xlsx_output", metavar='<File path>', help='Write all NEW jobs to Excel file') parser2.add_argument("-s", "--enabled_scrapers", metavar='<Website>', help="List of enabled scrapers. By default it's all scrapers configured in config file(s)", nargs='+') parser2.add_argument("-j", "--jobs_datafile", metavar='<File path>', help="""JSON file to store ALL jobs data. Default is '~/jobs.json'. Use 'null' keyword to disable the storage of the datafile, all jobs will be considered as new and will be loaded""") parser2.add_argument("--workers", metavar='<Number>', help="Number of websites to scrape asynchronously", type=int) parser2.add_argument("--full", "--load_all_jobs",action="store_true", help="Load the full job description page to parse additionnal data. This settings is applied to all scrapers") parser2.add_argument("--all", "--load_all_new_pages",action="store_true", help="Load new job listing pages until older jobs are found. This settings is applied to all scrapers") parser2.add_argument("--quick", "--no_load_all_jobs", action='store_true', help='Do not load the full job description page to parse additionnal data (Much more faster). This settings is applied to all scrapers') parser2.add_argument("--first", "--no_load_all_new_pages", action='store_true', help='Load only the first job listing page. This settings is applied to all scrapers') parser2.add_argument("--mailto", metavar="<Email>", help='Emails to notify of new job postings', nargs='+') parser2.add_argument("--log_level", metavar='<String>', help='Alt job log level. Exemple: DEBUG') parser2.add_argument("--scrapy_log_level", metavar='<String>', help='Scrapy log level. Exemple: DEBUG') args2 = parser2.parse_args(remaining_argv) # Update 'alt_job' section witll all parsed arguments config_file['alt_job'].update(vars(args2)) config_file['alt_job'].update(vars(args1)) if args2.full and args2.quick: raise ValueError("Incompatible options: --full is enable with --quick") if args2.full and args2.quick: raise ValueError("Incompatible options: --all is enable with --first") # Overwriting load_all_new_pages and load_full_jobs if passed --first or --quick if args2.first: for website in [ k for k in config_file.keys() if k in get_all_scrapers() ]: config_file[website]['load_all_new_pages']=False if args2.quick: for website in [ k for k in config_file.keys() if k in get_all_scrapers() ]: config_file[website]['load_full_jobs']=False if args2.full: for website in [ k for k in config_file.keys() if k in get_all_scrapers() ]: config_file[website]['load_full_jobs']=True if args2.all: for website in [ k for k in config_file.keys() if k in get_all_scrapers() ]: config_file[website]['load_all_new_pages']=True self.data=config_file # Config default values DEFAULT_CONFIG={ 'alt_job':{ 'log_level':'INFO', 'scrapy_log_level':'ERROR', 'jobs_datafile':'', 'workers':5, 'smtphost':'', 'mailfrom':'', 'smtpuser':'', 'smtppass':'', 'smtpport':'587', 'smtptls':'Yes', 'mailto':'[]' } } BOOL_VALUES=['use_google_cache', 'smtptls', 'load_full_jobs', 'load_all_new_pages', 'attach_jobs_description'] JSON_VALUES=['mailto', 'start_urls'] INT_VALUES=['smtpport', 'workers'] class ConfigurationFile(collections.UserDict): '''Build config dict from file. Parse the config file(s) and return dict config. Return a tuple (config dict, read files list). The dict returned contain all possible config values. Default values are applied if not specified in the file(s) or string. ''' def __init__(self, data=None, files=None, string=None): super().__init__(data) self.files=files if files else [] # Init config parser self.parser=configparser.ConfigParser() # Load default configuration self.parser.read_dict(DEFAULT_CONFIG) if string: self.parser.read_string(string) else: if not self.files: self.files=find_config_files() if not self.files: print("Could not find default config: `~/.alt_job/alt_job.conf`, `~/alt_job.conf` or `./alt_job.conf`") else: for f in self.files: try : with open(f,'r') as fp: self.parser.read_file(fp) except (FileNotFoundError, OSError) as err : raise ValueError("Could not read config %s. Make sure the file exists and you have correct access right."%(f)) from err self.data=copy.deepcopy(self.parser._sections) self.data['alt_job']['config_file']=self.files # casting int, booleans and json data sctructure for scraper in self.data: for config_option in self.data[scraper]: # List of BOOL config values if config_option in BOOL_VALUES: self.data[scraper][config_option]=getbool(self.parser, scraper, config_option) # list of JSON config values if config_option in JSON_VALUES: self.data[scraper][config_option]=getjson(self.parser, scraper, config_option) # List of INT config values if config_option in INT_VALUES: self.data[scraper][config_option]=getint(self.parser, scraper, config_option) def getjson(conf, section, key): '''Return json loaded structure from a configparser object. Empty list if the loaded value is null. Arguments: - `conf`: configparser object - `section`: config section - `key`: alt_job config key ''' try: loaded=json.loads(conf.get(section, key)) return loaded if loaded else [] except ValueError as err: raise ValueError("Could not read JSON value in config file for key '{}' and string: '{}'".format(key, conf.get(section,key))) from err def getbool(conf, section, key): '''Return bool value from a configparser object. Arguments: - `conf`: configparser object - `section`: config section - `key`: alt_job config key ''' try: return conf.getboolean(section, key) except ValueError as err: raise ValueError("Could not read boolean value in config file for key '{}' and string '{}'. Must be Yes/No".format(key, conf.get(section,key))) from err def getint(conf, section, key): '''Return int value from a configparser object. Arguments: - `conf`: configparser object - `section`: config section - `key`: alt_job config key ''' try: return conf.getint(section, key) except ValueError as err: raise ValueError("Could not read int value in config file for key '{}' and string '{}'. Must be an integer".format(key, conf.get(section,key))) from err def find_files(env_location, potential_files, default_content="", create=False): '''Find existent files based on folders name and file names. Arguments: - `env_location`: list of environment variable to use as a base path. Exemple: ['HOME', 'XDG_CONFIG_HOME', 'APPDATA', 'PWD'] - `potential_files`: list of filenames. Exemple: ['.alt_job/alt_job.conf', 'alt_job.conf'] - `default_content`: Write default content if the file does not exist - `create`: Create the file in the first existing env_location with default content if the file does not exist ''' potential_paths=[] existent_files=[] # build potential_paths of config file for env_var in env_location: if env_var in os.environ: for file_path in potential_files: potential_paths.append(os.path.join(os.environ[env_var],file_path)) # If file exist, add to list for p in potential_paths: if os.path.isfile(p): existent_files.append(p) # If no file foud and create=True, init new template config if len(existent_files)==0 and create: os.makedirs(os.path.dirname(potential_paths[0]), exist_ok=True) with open(potential_paths[0],'w') as config_file: config_file.write(default_content) print("Init new file: %s"%(p)) existent_files.append(potential_paths[0]) return(existent_files) def find_config_files(create=False): ''' Returns the location of existing `alt_job.conf` file at `./alt_job.conf` and/or `~/alt_job.conf` or under `~/.alt_job/` folder ''' files=['.alt_job/alt_job.conf', 'alt_job.conf'] env=['HOME', 'XDG_CONFIG_HOME', 'APPDATA', 'PWD'] return(find_files(env, files)) # Configuration template ------------------------- TEMPLATE_FILE=""" [alt_job] ##### General config ##### # Logging log_level=INFO scrapy_log_level=ERROR # Jobs data file, default is ~/jobs.json # jobs_datafile=/home/user/Jobs/jobs-mtl.json # Asynchronous workers, number of site to scan at the same time # Default to 5. # workers=10 ##### Mail sender ##### # Email server settings smtphost=smtp.gmail.com [email protected] [email protected] smtppass=password smtpport=587 smtptls=Yes # Email notif settings mailto=["[email protected]"] ##### Scrapers ##### # Scraper name [goodwork.ca] # URL to start the scraping, required for all scrapers url=https://www.goodwork.ca/jobs.php?prov=QC [cdeacf.ca] url=http://cdeacf.ca/recherches?f%5B0%5D=type%3Aoffre_demploi # Load full jobs details: If supported by the scraper, # this will follow each job posting link in listing and parse full job description. # turn on to parse all job informations # Default to False! load_full_jobs=True [arrondissement.com] url=https://www.arrondissement.com/tout-list-emplois/ # Load all new pages: If supported by the scraper, # this will follow each "next page" links and parse next listing page # until older (in database) job postings are found. # Default to False! load_all_new_pages=True [chantier.qc.ca] url=https://chantier.qc.ca/decouvrez-leconomie-sociale/offres-demploi load_full_jobs=Yes # Disabled scraper # [engages.ca] # url=https://www.engages.ca/emplois?search%5Bkeyword%5D=&search%5Bjob_sector%5D=&search%5Bjob_city%5D=Montr%C3%A9al [enviroemplois.org] # Multiple start URLs crawl start_urls=["https://www.enviroemplois.org/offres-d-emploi?sector=&region=6&job_kind=&employer=", "https://www.enviroemplois.org/offres-d-emploi?sector=&region=3&job_kind=&employer="] """
python
import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from multi_var_gradient_decent import LinearRegressionUsingGD from mpl_toolkits.mplot3d import axes3d from sklearn.metrics import mean_squared_error, r2_score def create_mesh_grid(start, end): theta_1 = np.linspace(start, end, 30) theta_2 = np.linspace(start, end, 30) theta_1, theta_2 = np.meshgrid(theta_1, theta_2) return theta_1, theta_2 def plot_result(x1, x2, y, regression_model): fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(x1, x2, y, c='b', marker='o') ax.set_xlabel('\u03B81') ax.set_ylabel('\u03B82') ax.set_zlabel('h(\u03B81, \u03B82)') X1, X2 = create_mesh_grid(0, 1) w = regression_model.w Z = w[0][0] + w[1][0] * X1 + w[2][0] * X2 ax.plot_wireframe(X1, X2, Z, color='red') plt.show() def plot_cost_function_2d(theta_1, theta_2, cost): fig, ax = plt.subplots(1, 1) ax.contourf(theta_1, theta_2, cost, levels=[0, 1, 2, 4, 6, 8, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], cmap=cm.coolwarm, antialiased=True) plt.xlabel('\u03B81') plt.ylabel('\u03B82') plt.show() def get_cost_function(theta_1, theta_2, x1, y, points_n): m = theta_1.shape[0] cost = np.zeros([theta_1.shape[0], theta_1.shape[1]]) for i in range(points_n): residuals = ((theta_1 * x1[i] + theta_2) - y[i]) ** 2 cost += residuals cost = cost / (2 * m) return cost def plot_cost_function(x, y, points_n): theta_1, theta_2, = create_mesh_grid(-5, 15) cost = get_cost_function(theta_1, theta_2, x, y, points_n) plot_cost_function_2d(theta_1, theta_2, cost) def plot_raw_data(x1, x2, y): fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(x1, x2, y, c='b', marker='o') plt.xlabel('\u03B80') plt.ylabel('\u03B81') ax.set_zlabel('h(\u03B81, \u03B82)') plt.show() def create_data(points_n): np.random.seed(0) x = np.random.rand(points_n, 2) x1 = np.sort(x[:, 0].reshape(points_n, 1)) x2 = np.sort(x[:, 1].reshape(points_n, 1)) y = 2 + 3 * x1 + np.random.rand(points_n, 1) + 6 * x2 + np.random.rand(points_n, 1) return x, x1, x2, y def main(): points_n = 100 x, x1, x2, y = create_data(points_n) plot_raw_data(x1, x2, y) plot_cost_function(x1, y, points_n) plot_cost_function(x2, y, points_n) # Model initialization regression_model = LinearRegressionUsingGD(lr=0.05, n_iterations=10000) # Fit the data(train the model) regression_model.fit(x, y) # Predict y_predicted = regression_model.predict(x) # model evaluation rmse = mean_squared_error(y, y_predicted) r2 = r2_score(y, y_predicted) # For sci-kit learn implementation: print('Weights:', regression_model.w) print('Root mean squared error: ', rmse) print('R2 score: ', r2) # plot plot_result(x1, x2, y, regression_model) if __name__ == '__main__': main()
python
import mxnet as mx import utils from model_utils import validate_model from gluon_zoo import save_mobilenet1_0 from from_tensorflow import tf_dump_model from os import path def test_tf_resnet50_v1(): sym_path = "./data/tf_resnet50_v1.json" prm_path = "./data/tf_resnet50_v1.params" # if not path.exists(sym_path) or not path.exists(prm_path): if True: tf_dump_model("resnet50_v1") ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] validate_model(sym_path, prm_path, ctx) def test_tf_mobilenet(): sym_path = "./data/tf_mobilenet.json" prm_path = "./data/tf_mobilenet.params" # if not path.exists(sym_path) or not path.exists(prm_path): if True: tf_dump_model("mobilenet") ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] validate_model(sym_path, prm_path, ctx) def test_mobilenet1_0(): sym_path = "./data/mobilenet1_0.json" prm_path = "./data/mobilenet1_0.params" if not path.exists(sym_path) or not path.exists(prm_path): save_mobilenet1_0() ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] validate_model(sym_path, prm_path, ctx, iter_num=999999, dump_model=True) def test_mobilenet_v2_1_0(): sym_path = "./data/mobilenetv2_1.0.json" prm_path = "./data/mobilenetv2_1.0.params" ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] validate_model(sym_path, prm_path, ctx) def test_tf_inceptionv3(): sym_path = "./data/tf_inception_v3.json" prm_path = "./data/tf_inception_v3.params" if not path.exists(sym_path) or not path.exists(prm_path): tf_dump_model("inception_v3") ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] # validate_model(sym_path, prm_path, ctx, input_size=299, dump_model=True) validate_model(sym_path, prm_path, ctx, input_size=299, iter_num=99999999) def test_alexnet(): sym_path = "./data/alexnet.json" prm_path = "./data/alexnet.params" ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] # validate_model(sym_path, prm_path, batch_size=700, ctx=ctx, dump_model=True) validate_model(sym_path, prm_path, batch_size=700, ctx=ctx, iter_num=9999999) def test_cifar10_resnet20_v1(): sym_path = "./data/cifar_resnet20_v1.json" prm_path = "./data/cifar_resnet20_v1.params" ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] # validate_model(sym_path, prm_path, ctx, input_size=32, # ds_name='cifar10', dump_model=True) validate_model(sym_path, prm_path, ctx, input_size=32, ds_name='cifar10', iter_num=9999999) def test_resnet(suffix): sym_path = "./data/resnet" + suffix + ".json" prm_path = "./data/resnet" + suffix + ".params" ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] # validate_model(sym_path, prm_path, ctx, lambd=16, dump_model=True) validate_model(sym_path, prm_path, ctx, lambd=16, iter_num=999999) def test_densenet161(): sym_path = "./data/densenet161.json" prm_path = "./data/densenet161.params" ctx = [mx.gpu(int(i)) for i in "1,2,3,4,5".split(',') if i.strip()] # validate_model(sym_path, prm_path, ctx, batch_size=16, dump_model=True) validate_model(sym_path, prm_path, ctx, batch_size=16, iter_num=9999999) def test_qd10_resnetv1_20(): sym_path = "./data/quick_raw_qd_animal10_2_cifar_resnet20_v2.json" prm_path = "./data/quick_raw_qd_animal10_2_cifar_resnet20_v2.params" ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] # validate_model(sym_path, prm_path, ctx, num_channel=1, # input_size=28, ds_name='quickdraw', dump_model=True) validate_model(sym_path, prm_path, ctx, num_channel=1, input_size=28, ds_name='quickdraw', iter_num=999999) def test_shufflenet_v1(): sym_path = "./data/shufflenet_v1.json" prm_path = "./data/shufflenet_v1.params" ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] # validate_model(sym_path, prm_path, ctx, dump_model=True) validate_model(sym_path, prm_path, ctx, iter_num=9999999) def test_squeezenet(): sym_path = "./data/squeezenet1.0.json" prm_path = "./data/squeezenet1.0.params" ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] # validate_model(sym_path, prm_path, ctx, batch_size=60, dump_model=True) validate_model(sym_path, prm_path, ctx, batch_size=60, iter_num=9999999) def test_vgg19(): sym_path = "./data/vgg19.json" prm_path = "./data/vgg19.params" ctx = [mx.gpu(int(i)) for i in "3".split(',') if i.strip()] # validate_model(sym_path, prm_path, ctx, dump_model=True) validate_model(sym_path, prm_path, ctx, iter_num=999999) def test_quickdraw(): sym_path = "./data/quickdraw_wlt_augmentation_epoch-4-0.8164531394275162.json" prm_path = "./data/quickdraw_wlt_augmentation_epoch-4-0.8164531394275162.params" ctx = [mx.gpu(int(i)) for i in "4".split(',') if i.strip()] # validate_model(sym_path, prm_path, ctx, input_size=28, num_channel=1, # ds_name="quickdraw", dump_model=True) validate_model(sym_path, prm_path, ctx, input_size=28, num_channel=1, ds_name="quickdraw", iter_num=9999999) def test_trec(): sym_path = "./data/trec.json" prm_path = "./data/trec.params" ctx = [mx.gpu(int(i)) for i in "3".split(',') if i.strip()] validate_model(sym_path, prm_path, ctx, ds_name="trec", input_shape=(38, 16), input_prec=16, dump_model=True, dump_shape=(38, 1)) # validate_model(sym_path, prm_path, ctx, ds_name="trec", # input_shape=(38, 16), iter_num=999999) if __name__ == '__main__': utils.log_init() # test_mobilenet1_0() ''' 2020-01-10 15:34:15 top1: 70.76% --> 63.08% top5: 89.97% --> 85.02% Iteration: 3123 Total Sample: 49984 ''' # test_mobilenet_v2_1_0() # 73% --> 0% # test_tf_inceptionv3() ''' 2020-01-10 16:08:03 top1: 55.57% --> 53.74% top5: 77.56% --> 76.01% Iteration: 3123 Total Sample: 49984 ''' # test_alexnet() ''' 2020-01-10 16:23:24 top1: 55.92% --> 55.15% top5: 78.74% --> 78.20% Iteration: 70 Total Sample: 49700 ''' # test_cifar10_resnet20_v1() ''' 2020-01-10 16:37:35 top1: 92.88% --> 92.83% top5: 99.78% --> 99.75% Iteration: 623 Total Sample: 9984 ''' # test_resnet("50_v1") ''' 2020-01-10 17:04:50 top1: 77.38% --> 75.81% top5: 93.58% --> 93.06% Iteration: 3123 Total Sample: 49984 ''' # test_resnet("18_v1") ''' 2020-01-10 16:55:48 top1: 70.94% --> 70.14% top5: 89.92% --> 89.54% Iteration: 3123 Total Sample: 49984 ''' # test_resnet("50_v1d_0.86") # not valid: Pooling count_include_pad:True # test_resnet("18_v1b_0.89") ''' 2020-01-10 17:00:43 top1: 67.20% --> 63.82% top5: 87.45% --> 85.60% Iteration: 3123 Total Sample: 49984 ''' # test_resnet("50_v2") ''' 2020-01-10 17:29:01 top1: 77.15% --> 74.13% top5: 93.44% --> 91.76% Iteration: 3123 Total Sample: 49984 ''' # test_densenet161() ''' 2020-01-10 20:33:58 top1: 77.61% --> 77.32% top5: 93.82% --> 93.62% Iteration: 3127 Total Sample: 49984 ''' # test_qd10_resnetv1_20() ''' 2020-01-10 17:57:44 top1: 85.72% --> 85.73% top5: 98.71% --> 98.70% Iteration: 17330 Total Sample: 277296 ''' # test_shufflenet_v1() ''' 2020-01-10 17:34:01 top1: 63.48% --> 60.38% top5: 85.11% --> 82.88% Iteration: 3123 Total Sample: 49984 ''' # test_squeezenet() ''' 2020-01-10 17:26:18 top1: 57.20% --> 54.49% top5: 80.03% --> 77.86% Iteration: 832 Total Sample: 49980 ''' # test_vgg19() ''' 2020-01-10 17:40:53 top1: 74.12% --> 73.68% top5: 91.77% --> 91.66% Iteration: 3123 Total Sample: 49984 ''' # test_quickdraw() ''' 2020-01-10 16:39:51 top1: 81.66% --> 81.57% top5: 98.22% --> 98.20% Iteration: 17330 Total Sample: 277296 ''' test_trec() ''' 2020-01-10 top1: --> top5: --> Iteration: Total Sample: ''' # TODO: test # test_tf_mobilenet() # 0% --> 0%, maybe due to pad # test_tf_resnet50_v1() # 0% --> 0%
python
import click import reader from reader._cli import setup_logging def make_add_response_headers_middleware(wsgi_app, headers): def wsgi_app_wrapper(environ, start_response): def start_response_wrapper(status, response_headers, exc_info=None): response_headers.extend(headers) return start_response(status, response_headers, exc_info) return wsgi_app(environ, start_response_wrapper) return wsgi_app_wrapper @click.command() @click.pass_obj @click.option('-h', '--host', default='localhost', help="The interface to bind to.") @click.option('-p', '--port', default=8080, type=int, help="The port to bind to.") @click.option( '--plugin', multiple=True, envvar=reader._APP_PLUGIN_ENVVAR, help="Import path to a web app plug-in. Can be passed multiple times.", ) @click.option('-v', '--verbose', count=True) def serve(config, host, port, plugin, verbose): """Start a local HTTP reader server.""" setup_logging(verbose) from werkzeug.serving import run_simple from . import create_app if plugin: config['app']['plugins'] = dict.fromkeys(plugin) # TODO: remove this once we make debug_storage a storage_arg config['default']['reader'].pop('debug_storage', None) app = create_app(config) app.wsgi_app = make_add_response_headers_middleware( app.wsgi_app, [('Referrer-Policy', 'same-origin')], ) run_simple(host, port, app)
python
from collections import defaultdict """ Block """ class Block: def __init__(self, author, round, payload, qc, id, txn_id): self.author = author self.round = round self.payload = payload self.qc = qc self.id = id self.txn_id = txn_id self.children = [] #Parent id is redundant. Get from qc.vote info.parent id #Also not needed
python
try: from webdriver_manager.chrome import ChromeDriverManager except: raise ImportError("'webdriver-manager' package not installed") try: from selenium.webdriver.common.keys import Keys from selenium import webdriver except: raise ImportError("'selenium' package not installed") from bs4 import BeautifulSoup import pandas as pd import time usr_agent = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Accept-Encoding': 'none', 'Accept-Language': 'en-US,en;q=0.8', 'Connection': 'keep-alive', } def scrappi(url, n_pages): chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') chrome_options.headless = True driver = webdriver.Chrome(ChromeDriverManager().install(), options = chrome_options) driver.create_options() driver.get(url) for _ in range(n_pages): driver.find_element_by_tag_name('body').send_keys(Keys.END) time.sleep(3) html = driver.page_source soup = BeautifulSoup(html, 'html.parser') videos = soup.find_all("div", {"id": "dismissible"}) lst = [] for video in videos: dictionary = {} dictionary['Title'] = video.find("a", {"id": "video-title"}).text dictionary['Video_url'] = "https://www.youtube.com/" + video.find("a", {"id": "video-title"})['href'] meta = video.find("div", {"id": "metadata-line"}).find_all('span') dictionary['Views'] = meta[0].text dictionary['Days'] = meta[1].text lst.append(dictionary) return pd.DataFrame(lst)
python
import requests user = 'alexey' password = 'styagaylo' base_url = 'http://httpbin.org/' def test_my_first_api(): r = requests.post(base_url + 'post', data={'user': user, 'password': password}) assert r.status_code == 200, "Unexpected status code: {}".format(r.status_code) assert r.json()['url'] == base_url + 'post', "Unexpected url: {}".format(r.json()['url']) assert r.json()['form']['user'] == user, "Unexpected user: {}".format(r.json()['form']['user']) assert r.json()['form']['password'] == password, "Unexpected password: {}".format(r.json()['form']['password'])
python
import plotly.graph_objects as go import dash import dash_core_components as dcc import dash_html_components as html from matplotlib import pylab import matplotlib.pyplot as plt import networkx as nx # read results from the file docs = eval(open('final_out_dupe_detect.txt', 'r').read()) final_docs = [[i[0], list(set(i[2]) & set(i[3]))] for i in docs] # initiate graph G = nx.Graph() duplicate_count = 2 # change this to see documents with more than this number of duplicates nodes = list(set([i[0] for i in final_docs if len(i[1]) > duplicate_count] + [j for i in final_docs for j in i[1] if len(i[1]) > duplicate_count])) edges = [] for i in final_docs: for j in i[1]: if i[0] != j and len(i[1]) > duplicate_count: edges.append((i[0], j)) G.add_nodes_from(nodes) for e in edges: G.add_edge(*e) print(nx.info(G)) def save_graph(graph, file_name): # initialze Figure plt.figure(num=None, figsize=(20, 20), dpi=80) plt.axis('off') fig = plt.figure(1) pos = nx.spring_layout(graph) nx.draw_networkx_nodes(graph, pos) nx.draw_networkx_edges(graph, pos) nx.draw_networkx_labels(graph, pos) cut = 1.00 xmax = cut * max(xx for xx, yy in pos.values()) ymax = cut * max(yy for xx, yy in pos.values()) plt.xlim(0, xmax) plt.ylim(0, ymax) plt.savefig(file_name) plt.show() pylab.close() del fig # save_graph(G,'out.png') pos=nx.spring_layout(G) edge_x = [] edge_y = [] for edge in G.edges(): x0, y0 = pos[edge[0]] x1, y1 = pos[edge[1]] edge_x.append(x0) edge_x.append(x1) edge_x.append(None) edge_y.append(y0) edge_y.append(y1) edge_y.append(None) edge_trace = go.Scatter( x=edge_x, y=edge_y, line=dict(width=0.5, color='#888'), hoverinfo='none', mode='lines') node_x = [] node_y = [] for node in G.nodes(): x, y = pos[node] node_x.append(x) node_y.append(y) node_trace = go.Scatter( x=node_x, y=node_y, mode='markers', hoverinfo='text', marker=dict( showscale=True, # colorscale options # 'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' | # 'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' | # 'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' | colorscale='YlGnBu', reversescale=True, color=[], size=10, colorbar=dict( thickness=15, title='Node Connections', xanchor='left', titleside='right' ), line_width=2)) node_adjacencies = [] node_text = [] for node, adjacencies in enumerate(G.adjacency()): node_adjacencies.append(len(adjacencies[1])) node_text.append(str(adjacencies[0])+' - # of connections: '+str(len(adjacencies[1]))+" <a href='https://plotly.com>grdg</a>") node_trace.marker.color = node_adjacencies node_trace.text = node_text fig = go.FigureWidget(data=[edge_trace, node_trace], layout=go.Layout( title='<br>Duplicate Detection Results', titlefont_size=16, showlegend=False, hovermode='closest', margin=dict(b=20,l=5,r=5,t=40), annotations=[ dict( text="Python code: <a href='https://plotly.com/ipython-notebooks/network-graphs/'> https://plotly.com/ipython-notebooks/network-graphs/</a>", showarrow=False, xref="paper", yref="paper", x=0.005, y=-0.002 ) ], xaxis=dict(showgrid=False, zeroline=False, showticklabels=True), yaxis=dict(showgrid=False, zeroline=False, showticklabels=True)) ) def update_point(trace, points, selector): print('points.poin_inds') fig.data[0].on_click(update_point) app = dash.Dash() app.layout = html.Div([ dcc.Graph(figure=fig) ]) app.run_server(debug=True, use_reloader=False)
python
import re import os import math import subprocess from MadGraphControl.MadGraphUtils import * nevents = 10000 mode = 0 mass=0.500000e+03 channel="mumu" gsmuL=-1 gseL=-1 gbmuL=-1 gbeL=-1 JOname = runArgs.jobConfig[0] matches = re.search("M([0-9]+).*\.py", JOname) if matches is None: raise RuntimeError("Cannot find mass string.") else: mass = float(matches.group(1)) if "sbLQmumu" in JOname: channel="mumu" gsmuL=1.0 gseL=0.0 gbmuL=1.0 gbeL=0.0 elif "sbLQee" in JOname: channel="ee" gsmuL=0.0 gseL=1.0 gbmuL=0.0 gbeL=1.0 else: raise RuntimeError("Cannot find coupling string.") test=[999999] fcard = open('proc_card_mg5.dat','w') if runArgs.runNumber in test and channel=="mumu": fcard.write(""" import model VectorLQ_U1_UFO\n define p = p b b~ define j = j b b~ generate p p > mu+ mu- NP==2 output VectorLQSingleProduction""") fcard.close() elif runArgs.runNumber in test and channel=="ee": fcard.write(""" import model VectorLQ_U1_UFO\n define p = p b b~ define j = j b b~ generate p p > e+ e- NP==2 output VectorLQSingleProduction""") fcard.close() else: raise RuntimeError("runNumber %i not recognised in these jobOptions."%runArgs.runNumber) beamEnergy = -999 if hasattr(runArgs, 'ecmEnergy'): beamEnergy = runArgs.ecmEnergy / 2. else: raise RuntimeError("No center of mass energy found.") process_dir = new_process() extras = {'pdlabel': "'lhapdf'", 'ktdurham': '1.0'} try: os.remove('run_card.dat') except OSError: pass build_run_card(run_card_old=get_default_runcard(proc_dir=process_dir), run_card_new='run_card.dat', nevts=nevents, rand_seed=runArgs.randomSeed, beamEnergy=beamEnergy, extras=extras) if os.path.exists("param_card.dat"): os.remove("param_card.dat") param_card_name = 'MadGraph_param_card_SingleVectorLQ_U1_DrellYan.py' param_card = subprocess.Popen(['get_files', '-data', param_card_name]) param_card.wait() if not os.access(param_card_name, os.R_OK): print 'ERROR: Could not get param card' elif os.access('param_card.dat',os.R_OK): print 'ERROR: Old param card in the current directory. Dont want to clobber it. Please move it first.' else: oldcard = open(param_card_name, 'r') newcard = open('param_card.dat', 'w') for line in oldcard: if 'mLQ' in line: newcard.write(' 9000002 %e # mLQ\n' % (mass)) elif 'gbmuL' in line: newcard.write(' 2 %e # gbmuL\n' % (gbmuL)) elif 'gbeL' in line: newcard.write(' 3 %e # gbeL\n' % (gbeL)) elif 'gsmuL' in line: newcard.write(' 4 %e # gsmuL\n' % (gsmuL)) elif 'gseL' in line: newcard.write(' 5 %e # gseL\n' % (gseL)) else: newcard.write(line) oldcard.close() newcard.close() print_cards() runName = 'run_01' process_dir = new_process() generate(run_card_loc='run_card.dat', param_card_loc='param_card.dat', mode=mode, proc_dir=process_dir, run_name=runName) arrange_output(run_name=runName, proc_dir=process_dir, outputDS=runName + '._00001.events.tar.gz', lhe_version=3, saveProcDir=True) include("MC15JobOptions/Pythia8_A14_NNPDF23LO_EvtGen_Common.py") include("MC15JobOptions/Pythia8_MadGraph.py") #### Shower evgenConfig.description = 'Single-production vector LQ to DrellYan' evgenConfig.keywords+=['BSM', 'exotic', 'leptoquark', 'scalar'] evgenConfig.generators+=["MadGraph", "Pythia8", "EvtGen"] evgenConfig.process = 'pp -> ll' evgenConfig.contact = ["Etienne Dreyer <[email protected]>"] evgenConfig.inputfilecheck = runName runArgs.inputGeneratorFile=runName+'._00001.events.tar.gz'
python
# coding: utf-8 import sys import utils # python .github/workflows/scripts/override_version.py example/tools/analyzer_plugin/pubspec.yaml 1.1.0 pubspec_yaml = sys.argv[1] version = sys.argv[2] utils.override_version(pubspec_yaml, version)
python
"""Define a version number for turboPy""" VERSION = ('2020', '10', '14') __version__ = '.'.join(map(str, VERSION))
python
from django.db import models # Create your models here. class TimeStampedModel(models.Model): created_at = models.DateTimeField(_(""), auto_now=False, auto_now_add=True) updated_at = models.DateTimeField(_(""), auto_now=True, auto_now_add=False) class Meta: abstract = True class Image(TimeStampedModel): file = models.ImageField() location = models.CharField(max_length=140) caption = models.TextField() class Comment(TimeStampedModel): text = models.TextField()
python
# # Copyright 2018 Jaroslav Chmurny # # # This file is part of Python Sudoku Sandbox. # # Python Sudoku Sandbox is free software developed for educational and # experimental purposes. It is licensed under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with the # License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """ This module provides classes supporting implementation of search algorithms. Search algorithm implementations should use the SearchSupport class which is more or less a facade encapsulating the functionality provided by this module. They should not use the other classes (except of the CandidateQueryMode enum) directly. """ from collections import deque, OrderedDict from enum import Enum, unique from logging import getLogger from grid import Grid, CellStatus _logger = getLogger() @unique class _ExclusionOutcome(Enum): """ Defines possible outcomes of an exclusion, for instance an exclusion of a candidate value for a single undefined cell. The meaning of particular enum elements is the following: * UNAMBIGUOUS_CANDIDATE_FOUND indicates that after the exclusion of a candidate, there is only single applicable candidate remaining. This outcome inidcates that an unambiguous candidate has been found by the exclusion. * UNAMBIGUOUS_CANDIDATE_NOT_FOUND indicates that the exclusion has not identified an unambiguous candidate. This value is to be used in several situations, for instance if two or more applicable candidates are still remaining after the exclusion, or if the exclusion of a candidate has not changed the set of candidates as the candidate was already excluded. This enum is internal, there is no need to use it directly in other modules. """ UNAMBIGUOUS_CANDIDATE_FOUND = 1 UNAMBIGUOUS_CANDIDATE_NOT_FOUND = 2 @unique class CandidateQueryMode(Enum): """ Defines options how value exclusion logic can provide candidates for an undefined cell. The meaning of particular enum elements is the following: * FIRST_UNDEFINED_CELL indicates that the candidates for the first undefined cell are to be returned, regardless of how many candidates are applicable to the first undefined cell. * UNDEFINED_CELL_WITH_LEAST_CANDIDATES indicates that the candidates for the undefined cell with least applicable candidates are to be returned. """ FIRST_UNDEFINED_CELL = 1 UNDEFINED_CELL_WITH_LEAST_CANDIDATES = 2 class _BaseCandidateInfo: """ Internal base class providing functionality common to UnambiguousCandidate and CandidateList classes. """ def __init__(self, row, column): self._row = row self._column = column @property def cell_address(self): """ The coordinates of the cell the candidate information carried by this object is applicable to. Returns: Tuple representing the above mentioned coordinates. The first element of the tuple is the row, the second element is the column. Zero corresponds to the first row or column, eight corresponds to the last row or column. """ return (self._row, self._column) def __eq__(self, other): if type(self) is not type(other): return False return self._row == other._row and self._column == other._column def __repr__(self): return "[row; column] = [{0}; {1}]".format(self._row, self._column) class UnambiguousCandidate(_BaseCandidateInfo): """ Immutable structure carrying information about an unambiguous candidate for an undefined cell. Besides the only applicable candidate value, this structure also carries the address (i.e. the row and the column) of the concerned cell. """ def __init__(self, row, column, value): super().__init__(row, column) self._value = value @property def value(self): """ The only candidate value applicable to the cell with the coordinates carried by this object. Returns: All canidate values applicable to the concerned cell at the time this object has been created. An empty tuple is returned if no candiate value is applicable to the concerned cell. """ return self._value def __eq__(self, other): return super().__eq__(other) and self._value == other._value def __repr__(self): type_name = type(self).__name__ return "{0}({1}, value = {2})".format(type_name, super().__repr__(), self._value) class CandidateList(_BaseCandidateInfo): """ Simple structure carrying all candidate values applicable to a single undefined cell. Besides the applicable candidate values, this structure also carries the address (i.e. the row and the column) of the concerned cell. """ def __init__(self, row, column, values): super().__init__(row, column) self._values = tuple(values) @property def values(self): """ Returns a tuple with all candidate values applicable to the cell with the coordinates carried by this candidate list. Returns: All canidate values applicable to the concerned cell at the time this object has been created. An empty tuple is returned if no candiate value is applicable to the concerned cell. """ return self._values def __len__(self): return len(self._values) def __eq__(self, other): return super().__eq__(other) and sorted(self._values) == sorted(other._values) def __repr__(self): type_name = type(self).__name__ return "{0}({1}, values = {2})".format(type_name, super().__repr__(), self._values) class _CellPeers: """ Internal helper class that creates a list of peers for every single cell contained in a Sudoku grid. For a cell, the peers are other cells contained in the same row, in the same column, or in the same region. Peers play a vital role in the exclusion logic provided by this module. """ @staticmethod def create(): result = [] for row in range(0, 9): cells_in_row = [tuple(_CellPeers.__create_for_single_cell(row, column)) for column in range(0, 9)] result.append(tuple(cells_in_row)) return tuple(result) @staticmethod def __create_for_single_cell(row, column): peers_in_row = [(row, c) for c in range(0, 9) if c != column] peers_in_column = [(r, column) for r in range(0, 9) if r != row] topmost_row = 3 * (row // 3) leftmost_column = 3 * (column // 3) peers_in_region = [(r, c) for r in range(topmost_row, topmost_row + 3) for c in range(leftmost_column, leftmost_column + 3)] peers_in_region.remove((row, column)) return OrderedDict((address, True) for address in (peers_in_row + peers_in_column + peers_in_region)).keys() class _CandidateValues: """ Internal helper that keeps track of applicable candidate values for a single cell. An instance of this class is to be updated whenever one of the peers of the cell corresponding to the instance of this class is updated. For better understanding, let's assume the following example. An instance of this class corresponds to an undefined cell. The row containing the cell contains another undefined cells, and the value of one of them is set to 5. The above mentioned instance of this class has to be updated via the exclude_value method as the value 5 is not applicable anymore. """ def __init__(self, bitmask = 0b111111111, applicable_value_count = 9): self._bitmask = bitmask self._applicable_value_count = applicable_value_count def clear(self): self._bitmask = 0 self._applicable_value_count = 0 def get_applicable_value_count(self): return self._applicable_value_count def exclude_value(self, value): _logger.debug("Going to exclude the value %d, bitmask before exclusion = %s", value, format(self._bitmask, "b")) value_mask = 1 << (value - 1) if self._bitmask & value_mask == value_mask: self._bitmask ^= value_mask _logger.debug("Bitmask after exclusion = %s", format(self._bitmask, "b")) self._applicable_value_count -= 1 if self._applicable_value_count == 1: return _ExclusionOutcome.UNAMBIGUOUS_CANDIDATE_FOUND return _ExclusionOutcome.UNAMBIGUOUS_CANDIDATE_NOT_FOUND def get_applicable_values(self): result = [value for value in range(1, 10) if self._bitmask & (1 << (value - 1))] return tuple(result) def get_single_remaining_applicable_value(self): if self._applicable_value_count != 1: message = "Cannot provide single remaining applicable value ({0} candidates remaining)." raise RuntimeError(message.format(self._applicable_value_count)) for value in range(1, 10): if self._bitmask == (1 << (value - 1)): return value def is_applicable(self, value): value_mask = 1 << (value - 1) return self._bitmask & value_mask == value_mask def copy(self): """ Creates and returns a deep copy of this object. """ return _CandidateValues(self._bitmask, self._applicable_value_count) class _CandidateValueExclusionLogic: """ Logic responsible for exclusion of candidate values inapplicable to particular cells. For instance, if the value of a cell is set to 5, the value 5 is excluded for all cells within the same row, column, and region. If a single candidate value remains applicable to a cell, that value is considered as unambiguous candidate for that cell. This class is an internal helper which should not be used directly by other modules. """ __cell_peers = _CellPeers.create() def __init__(self, original_exclusion_logic = None): if original_exclusion_logic is None: self._candidates = _CandidateValueExclusionLogic.__create_candidates_from_scratch() else: self._candidates = _CandidateValueExclusionLogic.__create_candidates_from_other_instance(original_exclusion_logic) @staticmethod def __create_candidates_from_scratch(): rows = [] for row in range(0, 9): rows.append(tuple([_CandidateValues() for column in range(0, 9)])) return tuple(rows) @staticmethod def __create_candidates_from_other_instance(original_exclusion_logic): rows = [] for row in range(0, 9): rows.append(tuple([original_exclusion_logic._candidates[row][column].copy() for column in range(0, 9)])) return tuple(rows) @staticmethod def create_from(grid): """ Creates and returns a new CandidateValueExclusionLogic instance. Before returning the above mentioned instance, candidate value exclusion is performed reflecting the predefined and completed cells of the given grid. Args: grid: Returns: The created CandidateValueExclusionLogic instance. """ exclusion_logic = _CandidateValueExclusionLogic() for (row, column) in Grid.get_all_cell_addresses(): if grid.get_cell_status(row, column) is not CellStatus.UNDEFINED: value = grid.get_cell_value(row, column) exclusion_logic.apply_and_exclude_cell_value(row, column, value) return exclusion_logic def apply_and_exclude_cell_value(self, row, column, value): """ Applies the given cell value to the cell with the given coordinates and excludes the given cell value for the peers of the cell with the coordinates. Args: row (int): The row coordinate of the cell the given value is to be applied to. Zero corresponds to the first row, eight corresponds to the last row. column (int) The column coordinate of the cell the given value is to be applied to. Zero corresponds to the first column, eight corresponds to the last column. value (int): The value for the given cell. Returns: List of UnambiguousCandidate instances, one for each of those peers of the concerned cell for which just a single applicable candidate value has remained after the exclusion. None is returned if there is no such peer. """ _logger.debug("Going to apply candidate value %d to cell [%d, %d]", value, row, column) self._candidates[row][column].clear() result = None for cell in _CandidateValueExclusionLogic.__cell_peers[row][column]: row, column = cell _logger.debug("Going to exclude candidate value %d for cell [%d, %d]", value, row, column) exclusion_outcome = self._candidates[row][column].exclude_value(value) _logger.debug("Exclusion outcome = %s", exclusion_outcome) if exclusion_outcome is _ExclusionOutcome.UNAMBIGUOUS_CANDIDATE_FOUND: result = result if result is not None else [] candidate_list = UnambiguousCandidate(row, column, self._candidates[row][column].get_single_remaining_applicable_value()) result.append(candidate_list) return result def get_undefined_cell_candidates(self, mode): """ Returns a list of candidate values applicable to one of the undefined cells. Args: mode: Determines which the undefined cell for which the candidate values are to be provided. Returns: New CandidateList instance carrying the applicable candidate values as well as the address of the undefined cell the candidate values are applicable to. Raises: ValueError: If unexpected mode is received. """ if mode is CandidateQueryMode.FIRST_UNDEFINED_CELL: return self.__get_candidates_for_first_undefined_cell() elif mode is CandidateQueryMode.UNDEFINED_CELL_WITH_LEAST_CANDIDATES: return self.__get_candidates_for_undefined_cell_with_least_candidates() message = "Unexpected candidate query mode {0}".format(mode) raise ValueError(message) def __get_candidates_for_first_undefined_cell(self): for (row, column) in Grid.get_all_cell_addresses(): if self._candidates[row][column].get_applicable_value_count() > 0: values = self._candidates[row][column].get_applicable_values() return CandidateList(row, column, values) return None def __get_candidates_for_undefined_cell_with_least_candidates(self): candidate_list = None for (row, column) in Grid.get_all_cell_addresses(): count_for_current_cell = self._candidates[row][column].get_applicable_value_count() if count_for_current_cell == 0: continue if candidate_list is None or count_for_current_cell < len(candidate_list): candidate_list = CandidateList(row, column, self._candidates[row][column].get_applicable_values()) return candidate_list def is_applicable(self, unambiguous_candidate): """ Verifies whether the given unambiguous candidate is applicable. Args: unambiguous_candidate: The unambiguous candidate to be verified. Returns: True if and only of the candidate value carried by the given candidate object is applicable to the cell with the coordinates carried by the given candidate object. False if the concerned cell is not empty, or if the concerned cell value is already present in the row, column, or region containing the concerned cell. """ row, column = unambiguous_candidate.cell_address value = unambiguous_candidate.value return self._candidates[row][column].is_applicable(value) def get_applicable_value_count(self, row, column): """ Returns the number of candidate values applicable to the cell with the given coordinates. Args: row (int): The row coordinate of the cell for which the number of applicable candidate values is to be returned. Zero corresponds to the first row, eight corresponds to the last row. column (int): The column coordinate of the cell for which the number of candidate values is to be returned. Zero corresponds to the first column, eight corresponds to the last column. """ return self._candidates[row][column].get_applicable_value_count() def copy(self): """ Creates and returns a deep copy of this object. """ return _CandidateValueExclusionLogic(self) class _RegionCandidateCells: """ Keeps track of cells within a region where a particular value is applicable. """ __row_peers = {0: 0b111111000, 1: 0b111000111, 2: 0b000111111} __column_peers = {0: 0b110110110, 1: 0b101101101, 2: 0b011011011} def __init__(self, topmost_row, leftmost_column, value, bitmask = 0b111111111, applicable_cell_count = 9): self._topmost_row = topmost_row self._leftmost_column = leftmost_column self._value = value self._bitmask = bitmask self._applicable_cell_count = applicable_cell_count def apply_and_exclude_cell_value(self, row, column, value): _logger.debug("Going to apply/exclude value %d for [%d, %d]", value, row, column) row_within_region, column_within_region = self.__get_cell_coordinates_within_this_region(row, column) _logger.debug("Cell address within region [%d, %d]", row_within_region, column_within_region) if (row_within_region, column_within_region) == (-1, -1): # cell not contained in this region, and neither the row, nor the column # containing the cell is crossing this region => nothing to be excluded _logger.debug("Ignoring region starting at [%d, %d] for the value %d", self._topmost_row, self._leftmost_column, self._value) return _ExclusionOutcome.UNAMBIGUOUS_CANDIDATE_NOT_FOUND if row_within_region in [0, 1, 2] and column_within_region not in [0, 1, 2]: _logger.debug("Row is crossing this region") # cell not contained in this region, but the row containing the cell is # crossing this region; depending on the value, we have to exclude either # nothing, or all peers of the cell if value != self._value: _logger.debug("Ignoring the value %d (my value is %d)", value, self._value) return _ExclusionOutcome.UNAMBIGUOUS_CANDIDATE_NOT_FOUND peers_mask = _RegionCandidateCells.__row_peers[row_within_region] _logger.debug("Peers mask (row) = %s, current status = %s", format(peers_mask, 'b'), format(self._bitmask, 'b')) self._bitmask = self._bitmask & peers_mask _logger.debug("New status = %s", format(self._bitmask, 'b')) return self.__update_applicable_value_count() if column_within_region in [0, 1, 2] and row_within_region not in [0, 1, 2]: _logger.debug("Column is crossing this region") # cell not contained in this region, but the column containing the cell is # crossing this region; depending on the value, we have to exclude either # nothing, or all peers of the cell if value != self._value: _logger.debug("Ignoring the value %d (my value is %d)", value, self._value) return _ExclusionOutcome.UNAMBIGUOUS_CANDIDATE_NOT_FOUND peers_mask = _RegionCandidateCells.__column_peers[column_within_region] _logger.debug("Peers mask (column) = %s, current status = %s", format(peers_mask, 'b'), format(self._bitmask, 'b')) self._bitmask = self._bitmask & peers_mask _logger.debug("New status = %s", format(self._bitmask, 'b')) return self.__update_applicable_value_count() # cell contained in this region; depending on the value, we have to exclude eihter # a single cell, or the entire region if self._value == value: _logger.debug("Excluding complete region") self._bitmask = 0 self._applicable_cell_count = 0 return _ExclusionOutcome.UNAMBIGUOUS_CANDIDATE_NOT_FOUND _logger.debug("Excluding single cell") cell_mask = 1 << (3 * row_within_region + column_within_region) cell_mask = 0b111111111 ^ cell_mask self._bitmask = self._bitmask & cell_mask _logger.debug("New status = %s", format(self._bitmask, 'b')) return self.__update_applicable_value_count() def __get_cell_coordinates_within_this_region(self, row, column): row_within_region, column_within_region = (-1, -1) if (3 * (row // 3)) == self._topmost_row: row_within_region = row - self._topmost_row if (3 * (column // 3)) == self._leftmost_column: column_within_region = column - self._leftmost_column return (row_within_region, column_within_region) def __update_applicable_value_count(self): new_count = 0 for shift in range(0, 9): mask = 1 << shift if self._bitmask & mask == mask: new_count += 1 _logger.debug("Going to update the value count from %d to %d", self._applicable_cell_count, new_count) result = _ExclusionOutcome.UNAMBIGUOUS_CANDIDATE_NOT_FOUND if new_count == 1 and self._applicable_cell_count > new_count: result = _ExclusionOutcome.UNAMBIGUOUS_CANDIDATE_FOUND self._applicable_cell_count = new_count return result def get_single_remaining_applicable_cell(self): if self._applicable_cell_count != 1: message = "Cannot provide single remaining applicable cell ({0} candidates remaining)." raise RuntimeError(message.format(self._applicable_value_count)) _logger.debug("Remaining bitmask = %s", format(self._bitmask, 'b')) for i in range(0, 9): mask = 1 << i if self._bitmask & mask == mask: row = self._topmost_row + (i // 3) column = self._leftmost_column + (i % 3) result = UnambiguousCandidate(row, column, self._value) _logger.debug("%s will be returned", result) return result _logger.debug("None will be returned") def copy(self): """ Creates and returns a deep copy of this object. """ return _RegionCandidateCells(self._topmost_row, self._leftmost_column, self._value, self._bitmask, self._applicable_cell_count) class _RegionGrid: """ Helper class supporting candidate cell exclusion. Single instance of this class aggregates 9 instances of _RegionCandidateCells. """ def __init__(self, value, regions = None): if regions is None: self._regions = tuple([_RegionCandidateCells(row, column, value) for row in [0, 3, 6] for column in [0, 3, 6]]) else: self._regions = regions def apply_and_exclude_cell_value(self, row, column, value): result = None for region in self._regions: exclusion_outcome = region.apply_and_exclude_cell_value(row, column, value) if exclusion_outcome is _ExclusionOutcome.UNAMBIGUOUS_CANDIDATE_FOUND: result = result if result is not None else [] candidate = region.get_single_remaining_applicable_cell() result.append(candidate) return result def copy(self): """ Creates and returns a deep copy of this object. """ regions_copy = tuple([single_region.copy() for single_region in self._regions]) return _RegionGrid(None, regions_copy) class _CandidateCellExclusionLogic: """ Logic responsible for exclusion of candidate cells where a particular value is not applicable. The exclusion leads to identification of the only cell within a region where a value is applicable. For such a cell, the value is considered as unambiguous candidate value. This class is an internal helper that should not be used directly by other modules. """ def __init__(self, original_exclusion_logic = None): if original_exclusion_logic is None: self._region_grids = tuple([_RegionGrid(value) for value in range(1, 10)]) else: self._region_grids = tuple([grid.copy() for grid in original_exclusion_logic._region_grids]) def apply_and_exclude_cell_value(self, row, column, value): """ Applies the given cell value to the cell with the given coordinates and excludes all peers of the given cell as candidate cells for the given value. Args: row (int): The row coordinate of the cell the given value is to be applied to. Zero corresponds to the first row, eight corresponds to the last row. column (int) The column coordinate of the cell the given value is to be applied to. Zero corresponds to the first column, eight corresponds to the last column. value (int): The value for the given cell. Returns: List of UnambiguousCandidate instances, one for each of those cells which have been identified as unambiguous candidate cells with any region for any value. None is returned if the exclusion has not led to any cell being identified as unambiguous candidate cell. """ _logger.debug("Going to apply & exclude the value %d for the cell [%d, %d]", value, row, column) result = None for grid in self._region_grids: partial_result = grid.apply_and_exclude_cell_value(row, column, value) if partial_result is not None: result = result if result is not None else [] result += partial_result return result def copy(self): """ Creates and returns a deep copy of this object. """ return _CandidateCellExclusionLogic(self) class _ExclusionLogic: """ Composite that aggregates and coordinates _CandidateValueExclusionLogic and _CandidateCellExclusionLogic. This class is an internal helper that should not be used directly by other modules. """ def __init__(self, candidate_value_exclusion = None, candidate_cell_exclusion = None): if candidate_value_exclusion is None: candidate_value_exclusion = _CandidateValueExclusionLogic() self._candidate_value_exclusion = candidate_value_exclusion if candidate_cell_exclusion is None: candidate_cell_exclusion = _CandidateCellExclusionLogic() self._candidate_cell_exclusion = candidate_cell_exclusion def apply_and_exclude_cell_value(self, row, column, value): """ Applies the given cell value to the cell with the given coordinates and excludes the given cell value for the peers of the cell with the coordinates. Args: row (int): The row coordinate of the cell the given value is to be applied to. Zero corresponds to the first row, eight corresponds to the last row. column (int) The column coordinate of the cell the given value is to be applied to. Zero corresponds to the first column, eight corresponds to the last column. value (int): The value for the given cell. Returns: List of UnambiguousCandidate instances, one for each of those cells for which just a single applicable candidate value has remained after the exclusion. None is returned if there is no such peer. """ _logger.debug("Going to apply & exclude the value %d for the cell [%d, %d]", value, row, column) result = None list = self._candidate_value_exclusion.apply_and_exclude_cell_value(row, column, value) if list is not None: _logger.debug("There are %d candidates from candidate value exclusion", len(list)) result = [] result.extend(list) list = self._candidate_cell_exclusion.apply_and_exclude_cell_value(row, column, value) if list is not None: _logger.debug("There are %d candidates from candidate cell exclusion", len(list)) result = [] if result is None else result result.extend(list) return result def is_applicable(self, unambiguous_candidate): """ Verifies whether the given unambiguous candidate is applicable. Args: unambiguous_candidate: The unambiguous candidate to be verified. Returns: True if and only of the candidate value carried by the given candidate object is applicable to the cell with the coordinates carried by the given candidate object. False if the concerned cell is not empty, or if the concerned cell value is already present in the row, column, or region containing the concerned cell. """ return self._candidate_value_exclusion.is_applicable(unambiguous_candidate) def get_applicable_value_count(self, row, column): """ Returns the number of candidate values applicable to the cell with the given coordinates. Args: row (int): The row coordinate of the cell for which the number of applicable candidate values is to be returned. Zero corresponds to the first row, eight corresponds to the last row. column (int): The column coordinate of the cell for which the number of candidate values is to be returned. Zero corresponds to the first column, eight corresponds to the last column. """ return self._candidate_value_exclusion.get_applicable_value_count(row, column) def get_undefined_cell_candidates(self, mode): """ Returns a list of candidate values applicable to one of the undefined cells. Args: mode: Determines which the undefined cell for which the candidate values are to be provided. Returns: New CandidateList instance carrying the applicable candidate values as well as the address of the undefined cell the candidate values are applicable to. None is returned if there is no undefined cell, or no candidate is applicable to any of the undefined cells. """ return self._candidate_value_exclusion.get_undefined_cell_candidates(mode) def copy(self): """ Creates and returns a deep copy of this object. """ return _ExclusionLogic(self._candidate_value_exclusion.copy(), self._candidate_cell_exclusion.copy()) class SearchSupport: """ Facade encapsulating the functionality provided by this module. An instance of this class coordinates a grid with exclusion logic keeping track of applicable candidate values for the grid. """ def __init__(self, grid = None, original = None): """ Initializer allowing to create a new instance of this class either based on the given Grid, or as a clone of the given SearchSupport instance. In any of the two cases, use only one of the two arguments - the other must be None. Args: grid: The grid the new search support is to be based on. If you want to create a copy of an existing search support, use None. original: Original search support that is to be cloned. Use None if you want to create a new search support based on a grid. """ if SearchSupport.__is_ordinary_constructor(grid, original): self.__init_from_scratch(grid) elif SearchSupport.__is_copy_constructor(grid, original): self.__init_from_other_instance(original) else: message = "Invalid arguments. Exactly one of the two arguments is expected." raise ValueError(message) @staticmethod def __is_ordinary_constructor(grid, original): return original is None and isinstance(grid, Grid) def __init_from_scratch(self, grid): self._exclusion_logic = _ExclusionLogic() self._candidate_queue = deque() self._grid = grid for (row, column) in Grid.get_all_cell_addresses(): if grid.get_cell_status(row, column) is CellStatus.PREDEFINED: value = grid.get_cell_value(row, column) candidate_list = self._exclusion_logic.apply_and_exclude_cell_value(row, column, value) if candidate_list is not None: self._candidate_queue.extend(candidate_list) @staticmethod def __is_copy_constructor(grid, original): return grid is None and isinstance(original, SearchSupport) def __init_from_other_instance(self, original): self._exclusion_logic = original._exclusion_logic.copy() self._candidate_queue = deque() self._grid = original._grid.copy() @property def grid(self): """ Provides a clone of the underlying grid. """ return self._grid.copy() def has_completed_grid(self): """ Verifies whether the underlying grid is already completed. Returns: True if and only if none of the cells of the underlying grid is empty; False if the underlying grid contains at least one empty value. """ return self._grid.is_complete() def set_cell_value(self, row, column, value): """ Sets the cell with the given coordinates to the given value, assumed the cell with the given coordinates is empty (i.e. its value is undefined). Subsequently, excludes the given value from applicable candidate values for the peers of the given cell. If the exclusion identifies unambiguous candidate(s) for any undefined cell(s), the unambiguous candidates are retained so that they can be provided by the get_unambiguous_candidate method. Args: row (int): The row coordinate of the cell whose value is to be set. Zero corresponds to the first row, eight corresponds to the last row. column (int) The column coordinate of the cell whose value is to be set. Zero corresponds to the first column, eight corresponds to the last column. value (int): The new value for the given cell. Raises: ValueError If the given cell has already a value, regardless of whether the value was defined in the original puzzle or completed during the search. """ self._grid.set_cell_value(row, column, value) candidate_list = self._exclusion_logic.apply_and_exclude_cell_value(row, column, value) _logger.info("Assignment [%d, %d] = %d completed, outcome of exclusion is %s", row, column, value, candidate_list) if candidate_list is not None: self._candidate_queue.extend(candidate_list) def has_empty_cells_without_applicable_candidates(self): """ Verifies whether the underlying grid contains at least one undefined cell for which all nine values have been already excluded (i.e. no candidate value is applicable to the cell). Returns: True if and only if the underlying grid contains at least one undefined cell for which all nine values have been already excluded. False if at least one candidate value is applicable to each undefined cell of underlying grid. """ for (row, column) in Grid.get_all_cell_addresses(): cell_status = self._grid.get_cell_status(row, column) if cell_status is not CellStatus.UNDEFINED: continue if self._exclusion_logic.get_applicable_value_count(row, column) == 0: _logger.info("Cell [%d, %d] undefined, but there are no applicable candidates", row, column) return True return False def get_unambiguous_candidate(self): """ Returns the next unambiguous candidate identified by one of the former invocations of the set_cell_value method. None is returned if there is no such unambiguous candidate. """ while len(self._candidate_queue) > 0: candidate = self._candidate_queue.popleft() _logger.debug("Candidate taken from queue: %s", candidate) if self._exclusion_logic.is_applicable(candidate): _logger.debug("Candidate still applicable, going to return it") return candidate else: _logger.debug("Candidate not applicable anymore, cannot return it") return None def get_undefined_cell_candidates(self, mode): """ Returns candidate values applicable to one of the undefined cells of the underlying grid. Args: mode: One of the elements of the CandidateQueryMode enum determining which of the undefined cells of the underlying grid is to be taken into account. """ result = self._exclusion_logic.get_undefined_cell_candidates(mode) if result: _logger.info("Undefined cell candidates found (mode = %s): %s", mode, result) row, column = result.cell_address assert self._grid.get_cell_status(row, column) is CellStatus.UNDEFINED else: _logger.debug("No undefined cell candidates, returning None") return result def copy(self): """ Creates and returns a deep copy of this object. """ return SearchSupport(original = self)
python
# _ __ # | |/ /___ ___ _ __ ___ _ _ ® # | ' </ -_) -_) '_ \/ -_) '_| # |_|\_\___\___| .__/\___|_| # |_| # # Keeper Commander # Copyright 2022 Keeper Security Inc. # Contact: [email protected] # import abc import json import logging from typing import Optional, List, Set, Tuple from google.protobuf import message from .params import KeeperParams from .proto import enterprise_pb2 as proto from . import api, utils, crypto def query_enterprise(params): # type: (KeeperParams) -> None if not params.enterprise_loader: params.enterprise_loader = _EnterpriseLoader() params.enterprise_loader.load(params) def _to_key_type(key_type): # type: (proto.EncryptedKeyType) -> str if key_type == proto.KT_ENCRYPTED_BY_DATA_KEY: return 'encrypted_by_data_key' if key_type == proto.KT_ENCRYPTED_BY_PUBLIC_KEY: return 'encrypted_by_public_key' if key_type == proto.KT_ENCRYPTED_BY_DATA_KEY_GCM: return 'encrypted_by_data_key_gcm' if key_type == proto.KT_ENCRYPTED_BY_PUBLIC_KEY_ECC: return 'encrypted_by_public_key_ecc' return 'no_key' class EnterpriseInfo(object): def __init__(self): self._tree_key = b'' self._rsa_key = b'' self._ec_key = b'' self._enterprise_name = '' @property def tree_key(self): return self._tree_key @property def rsa_key(self): return self._rsa_key @property def ec_key(self): return self._ec_key @property def enterprise_name(self): return self._enterprise_name class _EnterpriseLoader(object): def __init__(self): super(_EnterpriseLoader, self).__init__() self._enterprise = EnterpriseInfo() self._continuationToken = b'' self._data_types = { # type: dict[int, _EnterpriseDataParser] proto.NODES: _EnterpriseNodeEntity(self._enterprise), proto.USERS: _EnterpriseUserEntity(self._enterprise), proto.TEAMS: _EnterpriseTeamEntity(self._enterprise), proto.ROLES: _EnterpriseRoleEntity(self._enterprise), proto.LICENSES: _EnterpriseLicenseEntity(self._enterprise), proto.QUEUED_TEAMS: _EnterpriseQueuedTeamEntity(self._enterprise), proto.SCIMS: _EnterpriseScimEntity(self._enterprise), proto.SSO_SERVICES: _EnterpriseSsoServiceEntity(self._enterprise), proto.BRIDGES: _EnterpriseBridgeEntity(self._enterprise), proto.EMAIL_PROVISION: _EnterpriseEmailProvisionEntity(self._enterprise), proto.TEAM_USERS: _EnterpriseTeamUserEntity(self._enterprise), proto.QUEUED_TEAM_USERS: _EnterpriseQueuedTeamUserEntity(self._enterprise), proto.ROLE_USERS: _EnterpriseRoleUserEntity(self._enterprise), proto.ROLE_TEAMS: _EnterpriseRoleTeamEntity(self._enterprise), proto.MANAGED_NODES: _EnterpriseManagedNodeEntity(self._enterprise), proto.ROLE_PRIVILEGES: _EnterpriseRolePrivilegeEntity(self._enterprise), proto.ROLE_ENFORCEMENTS: _EnterpriseRoleEnforcements(self._enterprise), proto.MANAGED_COMPANIES: _EnterpriseManagedCompanyEntity(self._enterprise), proto.DEVICES_REQUEST_FOR_ADMIN_APPROVAL: _EnterpriseAdminApprovalRequestEntity(self._enterprise), } teams = self._data_types[proto.TEAMS] if isinstance(teams, _EnterpriseEntity): teams.register_link('team_uid', self._data_types[proto.TEAM_USERS]) teams.register_link('team_uid', self._data_types[proto.ROLE_TEAMS]) users = self._data_types[proto.USERS] if isinstance(teams, _EnterpriseEntity): users.register_link('enterprise_user_id', self._data_types[proto.TEAM_USERS]) users.register_link('enterprise_user_id', self._data_types[proto.ROLE_USERS]) roles = self._data_types[proto.ROLES] if isinstance(roles, _EnterpriseEntity): users.register_link('role_id', self._data_types[proto.ROLE_TEAMS]) users.register_link('role_id', self._data_types[proto.ROLE_USERS]) users.register_link('role_id', self._data_types[proto.MANAGED_NODES]) @property def enterprise(self): return self._enterprise def load(self, params): # type: (KeeperParams) -> None if params.enterprise is None: params.enterprise = {} self._continuationToken = b'' if not self._enterprise.tree_key: rq = proto.GetEnterpriseDataKeysRequest() rs = api.communicate_rest(params, rq, 'enterprise/get_enterprise_data_keys', rs_type=proto.GetEnterpriseDataKeysResponse) if rs.treeKey: encrypted_tree_key = utils.base64_url_decode(rs.treeKey.treeKey) if rs.treeKey.keyTypeId == proto.ENCRYPTED_BY_DATA_KEY: self._enterprise._tree_key = crypto.decrypt_aes_v1(encrypted_tree_key, params.data_key) elif rs.treeKey.keyTypeId == proto.ENCRYPTED_BY_PUBLIC_KEY: if len(encrypted_tree_key) == 60: self._enterprise._tree_key = crypto.decrypt_aes_v2(encrypted_tree_key, params.msp_tree_key) else: self._enterprise._tree_key = api.decrypt_rsa(rs.treeKey.treeKey, params.rsa_key) params.enterprise['unencrypted_tree_key'] = self._enterprise.tree_key if rs.enterpriseKeys: keys = {} if rs.enterpriseKeys.rsaEncryptedPrivateKey: self._enterprise._rsa_key = \ api.decrypt_aes_plain(rs.enterpriseKeys.rsaEncryptedPrivateKey, self._enterprise.tree_key) keys['rsa_public_key'] = utils.base64_url_encode(rs.enterpriseKeys.rsaPublicKey) keys['rsa_encrypted_private_key'] = \ utils.base64_url_encode(rs.enterpriseKeys.rsaEncryptedPrivateKey) if rs.enterpriseKeys.eccEncryptedPrivateKey: self._enterprise._ec_key = \ api.decrypt_aes_plain(rs.enterpriseKeys.eccEncryptedPrivateKey, self._enterprise.tree_key) keys['ecc_public_key'] = utils.base64_url_encode(rs.enterpriseKeys.eccPublicKey) keys['ecc_encrypted_private_key'] = \ utils.base64_url_encode(rs.enterpriseKeys.eccEncryptedPrivateKey) params.enterprise['keys'] = keys entities = set() while True: rq = proto.EnterpriseDataRequest() if self._continuationToken: rq.continuationToken = self._continuationToken rs = api.communicate_rest(params, rq, 'enterprise/get_enterprise_data_for_user', rs_type=proto.EnterpriseDataResponse) if rs.cacheStatus == proto.CLEAR: for d in self._data_types.values(): d.clear(params) if not self._enterprise.enterprise_name and rs.generalData: self._enterprise._enterprise_name = rs.generalData.enterpriseName params.enterprise['enterprise_name'] = self._enterprise.enterprise_name for ed in rs.data: entities.add(ed.entity) parser = self._data_types.get(ed.entity) if parser: parser.parse(params, ed) self._continuationToken = rs.continuationToken if not rs.hasMore: break if proto.MANAGED_NODES in entities: self.load_missing_role_keys(params) if not entities.isdisjoint([proto.MANAGED_NODES, proto.NODES, proto.ROLE_USERS]): if 'user_root_nodes' in params.enterprise: del params.enterprise['user_root_nodes'] if 'user_managed_nodes' in params.enterprise: del params.enterprise['user_managed_nodes'] @staticmethod def load_missing_role_keys(params): # type: (KeeperParams) -> None nodes = set() if 'managed_nodes' in params.enterprise: for mn in params.enterprise['managed_nodes']: nodes.add(mn['role_id']) if len(nodes) > 0: roles = set() if 'role_keys' in params.enterprise: for rk in params.enterprise['role_keys']: roles.add(rk['role_id']) if 'role_keys2' in params.enterprise: for rk in params.enterprise['role_keys2']: roles.add(rk['role_id']) nodes.difference_update(roles) if len(nodes) > 0: rq = proto.GetEnterpriseDataKeysRequest() rq.roleId.extend(nodes) rs = api.communicate_rest(params, rq, 'enterprise/get_enterprise_data_keys', rs_type=proto.GetEnterpriseDataKeysResponse) if len(rs.roleKey) > 0: if 'role_keys' not in params.enterprise: params.enterprise['role_keys'] = [] for rk1 in rs.roleKey: params.enterprise['role_keys'].append({ 'role_id': rk1.roleId, 'encrypted_key': utils.base64_url_encode(rk1.encryptedKey), 'key_type': _to_key_type(rk1.keyType) }) if len(rs.reEncryptedRoleKey) > 0: if 'role_keys2' not in params.enterprise: params.enterprise['role_keys2'] = [] for rk2 in rs.reEncryptedRoleKey: params.enterprise['role_keys2'].append({ 'role_id': rk2.role_id, 'role_key': utils.base64_url_encode(rk2.encryptedRoleKey), }) class _EnterpriseDataParser(abc.ABC): def __init__(self, enterprise): # type: (EnterpriseInfo) -> None self.enterprise = enterprise @abc.abstractmethod def parse(self, params, enterprise_data, **kwargs): # type: (KeeperParams, proto.EnterpriseData, dict) -> None pass @abc.abstractmethod def get_entity_type(self): pass @abc.abstractmethod def get_keeper_entity_name(self): # type: () -> str pass @abc.abstractmethod def to_keeper_entity(self, proto_entity, keeper_entity): pass def get_entities(self, params, create_if_absent=True): # type: (KeeperParams, bool) -> Optional[List] name = self.get_keeper_entity_name() if name not in params.enterprise: if not create_if_absent: return None params.enterprise[name] = [] return params.enterprise[name] def clear(self, params): # type: (KeeperParams) -> None entities = self.get_entities(params, create_if_absent=False) if entities: entities.clear() class _EnterpriseEntity(_EnterpriseDataParser): def __init__(self, enterprise): # type: (EnterpriseInfo) -> None super(_EnterpriseEntity, self).__init__(enterprise) self._links = [] # type: List[Tuple[str, _EnterpriseLink]] @abc.abstractmethod def get_keeper_entity_id(self, proto_entity): # type: (dict) -> any pass @abc.abstractmethod def get_proto_entity_id(self, proto_entity): # type: (message.Message) -> any pass @staticmethod def fix_data(d): idx = d.rfind(b'}') if idx < len(d) - 1: d = d[:idx+1] return d def register_link(self, keeper_entity_id_name, parser): # type: (str, _EnterpriseDataParser) -> None if isinstance(parser, _EnterpriseLink): self._links.append((keeper_entity_id_name, parser)) def parse(self, params, enterprise_data, **kwargs): # type: (KeeperParams, proto.EnterpriseData, dict) -> None if not enterprise_data.data: return entities = self.get_entities(params) entity_map = {self.get_keeper_entity_id(x): x for x in entities} entity_type = self.get_entity_type() deleted_entities = set() for entityData in enterprise_data.data: entity = entity_type() entity.ParseFromString(entityData) entity_id = self.get_proto_entity_id(entity) if enterprise_data.delete: if entity_id in entity_map: entity_map.pop(entity_id) deleted_entities.add(entity_id) else: keeper_entity = entity_map.get(entity_id) if not keeper_entity: keeper_entity = {} entity_map[entity_id] = keeper_entity self.to_keeper_entity(entity, keeper_entity) entities.clear() entities.extend(entity_map.values()) if len(deleted_entities) > 0: for keeper_entity_id_name, link in self._links: link.cascade_delete(params, keeper_entity_id_name, deleted_entities) class _EnterpriseLink(_EnterpriseDataParser): @abc.abstractmethod def get_keeper_entity1_id(self, proto_entity): # type: (dict) -> any pass @abc.abstractmethod def get_keeper_entity2_id(self, proto_entity): # type: (dict) -> any pass @abc.abstractmethod def get_proto_entity1_id(self, proto_entity): # type: (message.Message) -> any pass @abc.abstractmethod def get_proto_entity2_id(self, proto_entity): # type: (message.Message) -> any pass def cascade_delete(self, params, keeper_entity_id, deleted_entities): # type: (KeeperParams, str, Set) -> None entities = self.get_entities(params, create_if_absent=False) if not entities: return to_keep = [x for x in entities if keeper_entity_id not in x or x[keeper_entity_id] not in deleted_entities] if len(to_keep) < len(entities): entities.clear() entities.extend(to_keep) def parse(self, params, enterprise_data, **kwargs): # type: (KeeperParams, proto.EnterpriseData, dict) -> None entities = self.get_entities(params) entity_map = { '{0}:{1}'.format(self.get_keeper_entity1_id(x), self.get_keeper_entity2_id(x)): x for x in entities } entity_type = self.get_entity_type() for entityData in enterprise_data.data: entity = entity_type() entity.ParseFromString(entityData) entity1_id = self.get_proto_entity1_id(entity) entity2_id = self.get_proto_entity2_id(entity) key = '{0}:{1}'.format(entity1_id, entity2_id) if enterprise_data.delete: if key in entity_map: entity_map.pop(key) else: keeper_entity = entity_map.get(key) if not keeper_entity: keeper_entity = {} entity_map[key] = keeper_entity self.to_keeper_entity(entity, keeper_entity) entities.clear() entities.extend(entity_map.values()) def get_entities(self, params, create_if_absent=True): # type: (KeeperParams, bool) -> Optional[List] name = self.get_keeper_entity_name() if name not in params.enterprise: if not create_if_absent: return None params.enterprise[name] = [] return params.enterprise[name] def _set_or_remove(obj, key, value): # type: (dict, str, any) -> None if value is not None: obj[key] = value else: if key in obj: obj.pop(key) class _EnterpriseNodeEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.Node, dict) -> None _set_or_remove(keeper_entity, 'node_id', proto_entity.nodeId) _set_or_remove(keeper_entity, 'parent_id', proto_entity.parentId if proto_entity.parentId > 0 else None) _set_or_remove(keeper_entity, 'bridge_id', proto_entity.bridgeId if proto_entity.bridgeId > 0 else None) _set_or_remove(keeper_entity, 'scim_id', proto_entity.scimId if proto_entity.scimId > 0 else None) _set_or_remove(keeper_entity, 'license_id', proto_entity.licenseId if proto_entity.licenseId > 0 else None) _set_or_remove(keeper_entity, 'encrypted_data', proto_entity.encryptedData) _set_or_remove(keeper_entity, 'duo_enabled', True if proto_entity.duoEnabled else None) _set_or_remove(keeper_entity, 'rsa_enabled', True if proto_entity.rsaEnabled else None) _set_or_remove(keeper_entity, 'sso_service_provider_id', proto_entity.ssoServiceProviderId if proto_entity.ssoServiceProviderId > 0 else None) _set_or_remove(keeper_entity, 'restrict_visibility', proto_entity.restrictVisibility if proto_entity.restrictVisibility else None) data = {} if 'encrypted_data' in keeper_entity: try: data_json = api.decrypt_data(keeper_entity['encrypted_data'], self.enterprise.tree_key) data_json = self.fix_data(data_json) data.update(json.loads(data_json.decode('utf-8'))) except Exception as e: logging.warning('Decrypt encryption data error: %s', e) elif 'parent_id' not in keeper_entity: data['displayname'] = self.enterprise.enterprise_name keeper_entity['data'] = data def get_keeper_entity_id(self, entity): # type: (dict) -> any return entity.get('node_id') def get_proto_entity_id(self, entity): # type: (proto.Node) -> any return entity.nodeId def get_entity_type(self): return proto.Node def get_keeper_entity_name(self): # type: () -> str return 'nodes' class _EnterpriseUserEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.User, dict) -> None _set_or_remove(keeper_entity, 'enterprise_user_id', self.get_proto_entity_id(proto_entity)) _set_or_remove(keeper_entity, 'node_id', proto_entity.nodeId) _set_or_remove(keeper_entity, 'username', proto_entity.username) _set_or_remove(keeper_entity, 'encrypted_data', proto_entity.encryptedData) _set_or_remove(keeper_entity, 'key_type', proto_entity.keyType) _set_or_remove(keeper_entity, 'status', proto_entity.status) _set_or_remove(keeper_entity, 'lock', proto_entity.lock) _set_or_remove(keeper_entity, 'user_id', proto_entity.userId) _set_or_remove(keeper_entity, 'account_share_expiration', proto_entity.accountShareExpiration if proto_entity.accountShareExpiration > 0 else None) _set_or_remove(keeper_entity, 'full_name', proto_entity.fullName if proto_entity.fullName else None) _set_or_remove(keeper_entity, 'job_title', proto_entity.jobTitle if proto_entity.jobTitle else None) data = {} encrypted_data = keeper_entity.get('encrypted_data') if encrypted_data: if keeper_entity.get('key_type') == 'no_key': data['displayname'] = encrypted_data else: try: data_json = api.decrypt_data(encrypted_data, self.enterprise.tree_key) data_json = self.fix_data(data_json) data.update(json.loads(data_json.decode('utf-8'))) except Exception as e: logging.warning('Decrypt User data error: %s', e) elif 'full_name' in proto_entity: data['displayname'] = proto_entity['full_name'] keeper_entity['data'] = data def get_keeper_entity_id(self, entity): # type: (dict) -> any return entity.get('enterprise_user_id') def get_proto_entity_id(self, entity): # type: (proto.User) -> any return entity.enterpriseUserId def get_entity_type(self): return proto.User def get_keeper_entity_name(self): # type: () -> str return 'users' class _EnterpriseTeamEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.Team, dict) -> None _set_or_remove(keeper_entity, 'team_uid', self.get_proto_entity_id(proto_entity)) _set_or_remove(keeper_entity, 'name', proto_entity.name) _set_or_remove(keeper_entity, 'node_id', proto_entity.nodeId) _set_or_remove(keeper_entity, 'restrict_edit', proto_entity.restrictEdit) _set_or_remove(keeper_entity, 'restrict_sharing', proto_entity.restrictShare) _set_or_remove(keeper_entity, 'restrict_view', proto_entity.restrictView) _set_or_remove(keeper_entity, 'encrypted_data', proto_entity.encryptedData) _set_or_remove(keeper_entity, 'encrypted_team_key', proto_entity.encryptedTeamKey) def get_keeper_entity_id(self, entity): # type: (dict) -> any return entity.get('team_uid') def get_proto_entity_id(self, entity): # type: (proto.Team) -> any return utils.base64_url_encode(entity.teamUid) def get_entity_type(self): return proto.Team def get_keeper_entity_name(self): # type: () -> str return 'teams' class _EnterpriseRoleEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.Role, dict) -> None _set_or_remove(keeper_entity, 'role_id', self.get_proto_entity_id(proto_entity)) _set_or_remove(keeper_entity, 'node_id', proto_entity.nodeId) _set_or_remove(keeper_entity, 'encrypted_data', proto_entity.encryptedData) _set_or_remove(keeper_entity, 'visible_below', proto_entity.visibleBelow) _set_or_remove(keeper_entity, 'new_user_inherit', proto_entity.newUserInherit) _set_or_remove(keeper_entity, 'key_type', proto_entity.keyType) _set_or_remove(keeper_entity, 'role_type', proto_entity.roleType) data = {} encrypted_data = keeper_entity.get('encrypted_data') if encrypted_data: try: data_json = api.decrypt_data(encrypted_data, self.enterprise.tree_key) data_json = self.fix_data(data_json) data.update(json.loads(data_json.decode('utf-8'))) except Exception as e: logging.warning('Decrypt encryption data error: %s', e) keeper_entity['data'] = data def get_keeper_entity_id(self, entity): # type: (dict) -> any return entity.get('role_id') def get_proto_entity_id(self, entity): # type: (proto.Role) -> any return entity.roleId def get_entity_type(self): return proto.Role def get_keeper_entity_name(self): # type: () -> str return 'roles' class _EnterpriseLicenseEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.License, dict) -> None _set_or_remove(keeper_entity, 'enterprise_license_id', proto_entity.enterpriseLicenseId) _set_or_remove(keeper_entity, 'name', proto_entity.name) _set_or_remove(keeper_entity, 'paid', proto_entity.paid) _set_or_remove(keeper_entity, 'number_of_seats', proto_entity.numberOfSeats) _set_or_remove(keeper_entity, 'expiration', proto_entity.expiration) _set_or_remove(keeper_entity, 'license_key_id', proto_entity.licenseKeyId if proto_entity.licenseKeyId > 0 else None) _set_or_remove(keeper_entity, 'product_type_id', proto_entity.productTypeId if proto_entity.productTypeId > 0 else None) _set_or_remove(keeper_entity, 'seats_allocated', proto_entity.seatsAllocated) _set_or_remove(keeper_entity, 'seats_pending', proto_entity.seatsPending) _set_or_remove(keeper_entity, 'tier', proto_entity.tier) _set_or_remove(keeper_entity, 'file_plan', proto_entity.filePlanTypeId if proto_entity.filePlanTypeId > 0 else None) _set_or_remove(keeper_entity, 'max_gb', int(proto_entity.maxBytes / 1024 / 1024 / 1024) if proto_entity.filePlanTypeId > 0 else None) _set_or_remove(keeper_entity, 'storage_expiration', proto_entity.storageExpiration if proto_entity.storageExpiration > 0 else None) _set_or_remove(keeper_entity, 'lic_status', proto_entity.licenseStatus) msp_pool = None if proto_entity.mspPool: msp_pool = [{ 'product_id': x.productId, 'seats': x.seats, 'availableSeats': x.availableSeats, 'stash': x.stash } for x in proto_entity.mspPool] _set_or_remove(keeper_entity, 'msp_pool', msp_pool) if proto_entity.managedBy and proto_entity.managedBy.enterpriseId > 0: _set_or_remove(keeper_entity, 'managed_by', { 'enterprise_id': proto_entity.managedBy.enterpriseId, 'enterprise_name': proto_entity.managedBy.enterpriseName, }) if proto_entity.addOns: _set_or_remove(keeper_entity, 'add_ons', [{ 'name': x.name, 'enabled': x.enabled, 'is_trial': x.isTrial, 'created': x.created, 'expiration': x.expiration, } for x in proto_entity.addOns]) _set_or_remove(keeper_entity, 'next_billing_date', proto_entity.nextBillingDate if proto_entity.nextBillingDate > 0 else None) def get_keeper_entity_id(self, entity): # type: (dict) -> any return entity.get('enterprise_license_id') def get_proto_entity_id(self, entity): # type: (proto.License) -> any return entity.enterpriseLicenseId def get_entity_type(self): return proto.License def get_keeper_entity_name(self): # type: () -> str return 'licenses' class _EnterpriseQueuedTeamEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.QueuedTeam, dict) -> None _set_or_remove(keeper_entity, 'team_uid', self.get_proto_entity_id(proto_entity)) _set_or_remove(keeper_entity, 'name', proto_entity.name) _set_or_remove(keeper_entity, 'node_id', proto_entity.nodeId) _set_or_remove(keeper_entity, 'encrypted_data', proto_entity.encryptedData) def get_keeper_entity_id(self, entity): # type: (dict) -> any return entity.get('team_uid') def get_proto_entity_id(self, entity): # type: (proto.QueuedTeam) -> any return utils.base64_url_encode(entity.teamUid) def get_entity_type(self): return proto.QueuedTeam def get_keeper_entity_name(self): # type: () -> str return 'queued_teams' class _EnterpriseScimEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.Scim, dict) -> None _set_or_remove(keeper_entity, 'scim_id', self.get_proto_entity_id(proto_entity)) _set_or_remove(keeper_entity, 'node_id', proto_entity.nodeId) _set_or_remove(keeper_entity, 'status', proto_entity.status) _set_or_remove(keeper_entity, 'last_synced', proto_entity.lastSynced if proto_entity.lastSynced > 0 else None) _set_or_remove(keeper_entity, 'role_prefix', proto_entity.rolePrefix) _set_or_remove(keeper_entity, 'unique_groups', proto_entity.uniqueGroups) def get_keeper_entity_id(self, entity): # type: (dict) -> any return entity.get('scim_id') def get_proto_entity_id(self, entity): # type: (proto.Scim) -> any return entity.scimId def get_entity_type(self): return proto.Scim def get_keeper_entity_name(self): # type: () -> str return 'scims' class _EnterpriseTeamUserEntity(_EnterpriseLink): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.TeamUser, dict) -> None _set_or_remove(keeper_entity, 'team_uid', self.get_proto_entity1_id(proto_entity)) _set_or_remove(keeper_entity, 'enterprise_user_id', proto_entity.enterpriseUserId) user_type = 0 if proto_entity.userType == 'USER' else 1 if proto_entity.userType == 'ADMIN' else 2 _set_or_remove(keeper_entity, 'user_type', user_type) def get_keeper_entity1_id(self, entity): # type: (dict) -> any return entity.get('team_uid') def get_keeper_entity2_id(self, entity): # type: (dict) -> any return entity.get('enterprise_user_id') def get_proto_entity1_id(self, entity): # type: (proto.TeamUser) -> any return utils.base64_url_encode(entity.teamUid) def get_proto_entity2_id(self, entity): # type: (proto.TeamUser) -> any return entity.enterpriseUserId def get_entity_type(self): return proto.TeamUser def get_keeper_entity_name(self): # type: () -> str return 'team_users' class _EnterpriseRoleUserEntity(_EnterpriseLink): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.RoleUser, dict) -> None _set_or_remove(keeper_entity, 'role_id', self.get_proto_entity1_id(proto_entity)) _set_or_remove(keeper_entity, 'enterprise_user_id', proto_entity.enterpriseUserId) def get_keeper_entity1_id(self, entity): # type: (dict) -> any return entity.get('role_id') def get_keeper_entity2_id(self, entity): # type: (dict) -> any return entity.get('enterprise_user_id') def get_proto_entity1_id(self, entity): # type: (proto.RoleUser) -> any return entity.roleId def get_proto_entity2_id(self, entity): # type: (proto.RoleUser) -> any return entity.enterpriseUserId def get_entity_type(self): return proto.RoleUser def get_keeper_entity_name(self): # type: () -> str return 'role_users' class _EnterpriseRoleTeamEntity(_EnterpriseLink): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.RoleTeam, dict) -> None _set_or_remove(keeper_entity, 'role_id', self.get_proto_entity1_id(proto_entity)) _set_or_remove(keeper_entity, 'team_uid', self.get_proto_entity2_id(proto_entity)) def get_keeper_entity1_id(self, entity): # type: (dict) -> any return entity.get('role_id') def get_keeper_entity2_id(self, entity): # type: (dict) -> any return entity.get('team_uid') def get_proto_entity1_id(self, entity): # type: (proto.RoleTeam) -> any return entity.role_id def get_proto_entity2_id(self, entity): # type: (proto.RoleTeam) -> any return utils.base64_url_encode(entity.teamUid) def get_entity_type(self): return proto.RoleTeam def get_keeper_entity_name(self): # type: () -> str return 'role_teams' class _EnterpriseManagedNodeEntity(_EnterpriseLink): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.ManagedNode, dict) -> None _set_or_remove(keeper_entity, 'role_id', self.get_proto_entity1_id(proto_entity)) _set_or_remove(keeper_entity, 'managed_node_id', self.get_proto_entity2_id(proto_entity)) _set_or_remove(keeper_entity, 'cascade_node_management', proto_entity.cascadeNodeManagement) def get_keeper_entity1_id(self, entity): # type: (dict) -> any return entity.get('role_id') def get_keeper_entity2_id(self, entity): # type: (dict) -> any return entity.get('managed_node_id') def get_proto_entity1_id(self, entity): # type: (proto.ManagedNode) -> any return entity.roleId def get_proto_entity2_id(self, entity): # type: (proto.ManagedNode) -> any return entity.managedNodeId def get_entity_type(self): return proto.ManagedNode def get_keeper_entity_name(self): # type: () -> str return 'managed_nodes' class _EnterpriseRolePrivilegeEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.RolePrivilege, dict) -> None _set_or_remove(keeper_entity, 'role_id', proto_entity.roleId) _set_or_remove(keeper_entity, 'managed_node_id', proto_entity.managedNodeId) _set_or_remove(keeper_entity, 'privilege', proto_entity.privilegeType) def get_keeper_entity_id(self, entity): # type: (dict) -> any return '{0}:{1}:{2}'.format(entity.get('role_id'), entity.get('managed_node_id'), entity.get('privilege')) def get_proto_entity_id(self, entity): # type: (proto.RolePrivilege) -> any return '{0}:{1}:{2}'.format(entity.roleId, entity.managedNodeId, entity.privilegeType) def get_entity_type(self): return proto.RolePrivilege def get_keeper_entity_name(self): # type: () -> str return 'role_privileges' class _EnterpriseRoleEnforcements(_EnterpriseDataParser): def parse(self, params, enterprise_data, **kwargs): # type: (KeeperParams, proto.EnterpriseData, dict) -> None entities = self.get_entities(params) entity_map = {x['role_id']: x for x in entities} entity_type = self.get_entity_type() for entityData in enterprise_data.data: entity = entity_type() entity.ParseFromString(entityData) role_id = entity.roleId enforcement_type = entity.enforcementType if enterprise_data.delete: if role_id in entity_map: enforcements = entity_map[role_id]['enforcements'] if enforcement_type in enforcements: enforcements.pop(enforcement_type) else: keeper_entity = entity_map.get(role_id) if not keeper_entity: keeper_entity = { 'role_id': role_id, 'enforcements': {} } entity_map[role_id] = keeper_entity enforcements = keeper_entity['enforcements'] enforcements[enforcement_type] = entity.value entities.clear() entities.extend(entity_map.values()) def get_entity_type(self): return proto.RoleEnforcement def get_keeper_entity_name(self): # type: () -> str return 'role_enforcements' def to_keeper_entity(self, proto_entity, keeper_entity): pass class _EnterpriseManagedCompanyEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.ManagedCompany, dict) -> None _set_or_remove(keeper_entity, 'mc_enterprise_id', proto_entity.mcEnterpriseId) _set_or_remove(keeper_entity, 'mc_enterprise_name', proto_entity.mcEnterpriseName) _set_or_remove(keeper_entity, 'msp_node_id', proto_entity.mspNodeId) _set_or_remove(keeper_entity, 'number_of_seats', proto_entity.numberOfSeats) _set_or_remove(keeper_entity, 'number_of_users', proto_entity.numberOfUsers) _set_or_remove(keeper_entity, 'product_id', proto_entity.productId) _set_or_remove(keeper_entity, 'paused', proto_entity.isExpired) _set_or_remove(keeper_entity, 'tree_key', proto_entity.treeKey if proto_entity.treeKey else None) _set_or_remove(keeper_entity, 'tree_key_role', proto_entity.tree_key_role) _set_or_remove(keeper_entity, 'file_plan_type', proto_entity.filePlanType) if proto_entity.addOns: _set_or_remove(keeper_entity, 'add_ons', [{ 'name': x.name, 'enabled': x.enabled, 'is_trial': x.isTrial, 'created': x.created, 'expiration': x.expiration, } for x in proto_entity.addOns]) def get_keeper_entity_id(self, entity): # type: (dict) -> any return entity.get('mc_enterprise_id') def get_proto_entity_id(self, entity): # type: (proto.ManagedCompany) -> any return entity.mcEnterpriseId def get_entity_type(self): return proto.ManagedCompany def get_keeper_entity_name(self): # type: () -> str return 'managed_companies' class _EnterpriseQueuedTeamUserEntity(_EnterpriseDataParser): def parse(self, params, enterprise_data, **kwargs): # type: (KeeperParams, proto.EnterpriseData, dict) -> None entities = self.get_entities(params) entity_map = {x['team_uid']: x for x in entities} entity_type = self.get_entity_type() for entityData in enterprise_data.data: entity = entity_type() entity.ParseFromString(entityData) team_uid = utils.base64_url_encode(entity.teamUid) if enterprise_data.delete: if team_uid in entity_map: users = entity_map[team_uid]['users'] # type: set users.difference_update(entity.users) else: keeper_entity = entity_map.get(team_uid) if not keeper_entity: keeper_entity = { 'team_uid': team_uid, 'users': set() } entity_map[team_uid] = keeper_entity users = keeper_entity['users'] users.update(entity.users) entities.clear() entities.extend(entity_map.values()) def get_entity_type(self): return proto.QueuedTeamUser def get_keeper_entity_name(self): # type: () -> str return 'queued_team_users' def to_keeper_entity(self, proto_entity, keeper_entity): pass class _EnterpriseAdminApprovalRequestEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.DeviceRequestForAdminApproval, dict) -> None _set_or_remove(keeper_entity, 'enterprise_user_id', proto_entity.enterpriseUserId) _set_or_remove(keeper_entity, 'encrypted_device_token', utils.base64_url_encode(proto_entity.encryptedDeviceToken)) _set_or_remove(keeper_entity, 'device_id', proto_entity.deviceId) _set_or_remove(keeper_entity, 'device_public_key', utils.base64_url_encode(proto_entity.devicePublicKey)) _set_or_remove(keeper_entity, 'device_name', proto_entity.deviceName) _set_or_remove(keeper_entity, 'client_version', proto_entity.clientVersion) _set_or_remove(keeper_entity, 'device_type', proto_entity.deviceType) _set_or_remove(keeper_entity, 'date', proto_entity.date) _set_or_remove(keeper_entity, 'ip_address', proto_entity.ipAddress) _set_or_remove(keeper_entity, 'location', proto_entity.location) _set_or_remove(keeper_entity, 'email', proto_entity.email) def get_keeper_entity_id(self, entity): # type: (dict) -> any return '{0}:{1}'.format(entity.get('enterprise_user_id'), entity.get('device_id')) def get_proto_entity_id(self, entity): # type: (proto.DeviceRequestForAdminApproval) -> any return '{0}:{1}'.format(entity.enterpriseUserId, entity.deviceId) def get_entity_type(self): return proto.DeviceRequestForAdminApproval def get_keeper_entity_name(self): # type: () -> str return 'devices_request_for_admin_approval' class _EnterpriseSsoServiceEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.SsoService, dict) -> None _set_or_remove(keeper_entity, 'sso_service_provider_id', self.get_proto_entity_id(proto_entity)) _set_or_remove(keeper_entity, 'node_id', proto_entity.nodeId) _set_or_remove(keeper_entity, 'name', proto_entity.name) _set_or_remove(keeper_entity, 'sp_url', proto_entity.sp_url) _set_or_remove(keeper_entity, 'invite_new_users', proto_entity.inviteNewUsers) _set_or_remove(keeper_entity, 'active', proto_entity.active) _set_or_remove(keeper_entity, 'is_cloud', proto_entity.isCloud) def get_keeper_entity_id(self, entity): # type: (dict) -> any return entity.get('sso_service_provider_id') def get_proto_entity_id(self, entity): # type: (proto.SsoService) -> any return entity.ssoServiceProviderId def get_entity_type(self): return proto.SsoService def get_keeper_entity_name(self): # type: () -> str return 'sso_services' class _EnterpriseBridgeEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.Bridge, dict) -> None _set_or_remove(keeper_entity, 'bridge_id', self.get_proto_entity_id(proto_entity)) _set_or_remove(keeper_entity, 'node_id', proto_entity.nodeId) _set_or_remove(keeper_entity, 'wan_ip_enforcement', proto_entity.wanIpEnforcement) _set_or_remove(keeper_entity, 'lan_ip_enforcement', proto_entity.lanIpEnforcement) _set_or_remove(keeper_entity, 'status', proto_entity.status) def get_keeper_entity_id(self, entity): # type: (dict) -> any return entity.get('bridge_id') def get_proto_entity_id(self, entity): # type: (proto.Bridge) -> any return entity.bridgeId def get_entity_type(self): return proto.Bridge def get_keeper_entity_name(self): # type: () -> str return 'bridges' class _EnterpriseEmailProvisionEntity(_EnterpriseEntity): def to_keeper_entity(self, proto_entity, keeper_entity): # type: (proto.EmailProvision, dict) -> None _set_or_remove(keeper_entity, 'id', self.get_proto_entity_id(proto_entity)) _set_or_remove(keeper_entity, 'node_id', proto_entity.nodeId) _set_or_remove(keeper_entity, 'domain', proto_entity.domain) _set_or_remove(keeper_entity, 'method', proto_entity.method) def get_keeper_entity_id(self, entity): # type: (dict) -> any return entity.get('id') def get_proto_entity_id(self, entity): # type: (proto.EmailProvision) -> any return entity.id def get_entity_type(self): return proto.EmailProvision def get_keeper_entity_name(self): # type: () -> str return 'email_provision'
python
""" Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Example: Input: [1,2,3] Output: 3 Explanation: Only three moves are needed (remember each move increments two elements): [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4] """ class Solution(object): def minMoves(self, nums): """ :type nums: List[int] :rtype: int """ nums.sort() t, c, p = [], 0, None for n in reversed(nums): if p is None: c += 1 else: if n == p: c += 1 else: t.append((p, c)) c = 1 p = n if p is not None: t.append((p, c)) r = 0 p, s = None, 0 for n, c in t: if p is None: s = c p = n continue r += (p - n) * s p = n s += c return r
python
from __future__ import annotations import os import warnings from datetime import datetime from pathlib import Path from typing import ( Any, Dict, List, Optional, Tuple, Union, Sequence, Iterable, ) import pydantic from .managers import ManagerQueryBody, ComputeManager from .metadata_models import QueryMetadata, UpdateMetadata from .molecules import Molecule, MoleculeIdentifiers, MoleculeQueryBody, MoleculeModifyBody from .permissions import ( UserInfo, RoleInfo, is_valid_username, is_valid_password, is_valid_rolename, ) from .records import ( RecordStatusEnum, PriorityEnum, RecordQueryBody, RecordModifyBody, RecordDeleteURLParameters, RecordUndeleteURLParameters, AllRecordTypes, AllDataModelTypes, ) from .records.gridoptimization import ( GridoptimizationKeywords, GridoptimizationAddBody, GridoptimizationRecord, ) from .records.optimization import ( OptimizationProtocols, OptimizationRecord, OptimizationQueryBody, OptimizationQCInputSpecification, OptimizationInputSpecification, OptimizationAddBody, ) from .records.singlepoint import ( SinglepointRecord, SinglepointAddBody, SinglepointQueryBody, SinglepointDriver, SinglepointProtocols, ) from .records.torsiondrive import ( TorsiondriveKeywords, TorsiondriveAddBody, TorsiondriveRecord, TorsiondriveQueryBody, ) from .serverinfo import ( AccessLogQueryParameters, AccessLogQuerySummaryParameters, ErrorLogQueryParameters, ServerStatsQueryParameters, DeleteBeforeDateParameters, ) from .base_models import ( CommonGetURLParametersName, CommonGetProjURLParameters, CommonGetURLParameters, CommonDeleteURLParameters, ) from .cache import PortalCache from .client_base import PortalClientBase, PortalRequestError from .keywords import KeywordSet from .metadata_models import InsertMetadata, DeleteMetadata, UndeleteMetadata from .utils import make_list, make_str # TODO : built-in query limit chunking, progress bars, fs caching and invalidation class PortalClient(PortalClientBase): def __init__( self, address: str = "api.qcarchive.molssi.org:443", username: Optional[str] = None, password: Optional[str] = None, verify: bool = True, cache: Optional[Union[str, Path]] = None, max_memcache_size: Optional[int] = 1000000, ) -> None: """Initializes a PortalClient instance from an address and verification information. Parameters ---------- address The IP and port of the FractalServer instance ("192.168.1.1:8888") username The username to authenticate with. password The password to authenticate with. verify Verifies the SSL connection with a third party server. This may be False if a FractalServer was not provided a SSL certificate and defaults back to self-signed SSL keys. cache Path to directory to use for cache. If None, only in-memory caching used. max_memcache_size Number of items to hold in client's memory cache. Increase this value to improve performance for repeated calls, at the cost of higher memory usage. """ PortalClientBase.__init__(self, address, username, password, verify) self._cache = PortalCache(self, cachedir=cache, max_memcache_size=max_memcache_size) def __repr__(self) -> str: """A short representation of the current PortalClient. Returns ------- str The desired representation. """ ret = "PortalClient(server_name='{}', address='{}', username='{}', cache='{}')".format( self.server_name, self.address, self.username, self.cache ) return ret def _repr_html_(self) -> str: output = f""" <h3>PortalClient</h3> <ul> <li><b>Server: &nbsp; </b>{self.server_name}</li> <li><b>Address: &nbsp; </b>{self.address}</li> <li><b>Username: &nbsp; </b>{self.username}</li> <li><b>Cache: &nbsp; </b>{self.cache}</li> </ul> """ # postprocess due to raw spacing above return "\n".join([substr.strip() for substr in output.split("\n")]) def recordmodel_from_datamodel(self, data: Sequence[Optional[AllDataModelTypes]]) -> List[Optional[AllRecordTypes]]: record_init = [ {"client": self, "record_type": d.record_type, "raw_data": d} if d is not None else None for d in data ] return pydantic.parse_obj_as(List[Optional[AllRecordTypes]], record_init) @property def cache(self): if self._cache.cachedir is not None: return os.path.relpath(self._cache.cachedir) else: return None def _get_with_cache(self, func, id, missing_ok, entity_type, include=None): str_id = make_str(id) ids = make_list(str_id) # pass through the cache first # remove any ids that were found in cache # if `include` filters passed, don't use cache, just query DB, as it's often faster # for a few fields if include is None: cached = self._cache.get(ids, entity_type=entity_type) else: cached = {} for i in cached: ids.remove(i) # if all ids found in cache, no need to go further if len(ids) == 0: if isinstance(id, list): return [cached[i] for i in str_id] else: return cached[str_id] # molecule getting does *not* support "include" if include is None: payload = { "data": {"id": ids}, } else: if "id" not in include: include.append("id") payload = { "meta": {"include": include}, "data": {"id": ids}, } results, to_cache = func(payload) # we only cache if no field filtering was done if include is None: self._cache.put(to_cache, entity_type=entity_type) # combine cached records with queried results results.update(cached) # check that we have results for all ids asked for missing = set(make_list(str_id)) - set(results.keys()) if missing and not missing_ok: raise KeyError(f"No objects found for `id`: {missing}") # order the results by input id list if isinstance(id, list): ordered = [results.get(i, None) for i in str_id] else: ordered = results.get(str_id, None) return ordered # TODO - needed? def _query_cache(self): pass def get_server_information(self) -> Dict[str, Any]: """Request general information about the server Returns ------- : Server information. """ # Request the info, and store here for later use return self._auto_request("get", "v1/information", None, None, Dict[str, Any], None, None) ############################################################## # Molecules ############################################################## def get_molecules( self, id: Union[int, Sequence[int]], missing_ok: bool = False, ) -> Union[Optional[Molecule], List[Optional[Molecule]]]: """Obtains molecules from the server via molecule ids Parameters ---------- id An id or list of ids to query. missing_ok If True, return ``None`` for ids that were not found on the server. If False, raise ``KeyError`` if any ids were not found on the server. Returns ------- : The requested molecules, in the same order as the requested ids. If given a list of ids, the return value will be a list. Otherwise, it will be a single Molecule. """ url_params = {"id": make_list(id), "missing_ok": missing_ok} mols = self._auto_request( "get", "v1/molecule", None, CommonGetURLParameters, List[Optional[Molecule]], None, url_params ) if isinstance(id, Sequence): return mols else: return mols[0] # TODO: we would like more fields to be queryable via the REST API for mols # e.g. symbols/elements. Unless these are indexed might not be performant. # TODO: what was paginate: bool = False for? def query_molecules( self, molecule_hash: Optional[Union[str, Iterable[str]]] = None, molecular_formula: Optional[Union[str, Iterable[str]]] = None, identifiers: Optional[Dict[str, Union[str, Iterable[str]]]] = None, limit: Optional[int] = None, skip: int = 0, ) -> List[Molecule]: """Query molecules by attributes. All matching molecules, up to the lower of `limit` or the server's maximum result count, will be returned. The return list will be in an indeterminate order Parameters ---------- molecule_hash Queries molecules by hash molecular_formula Queries molecules by molecular formula Molecular formulas are not order-sensitive (e.g. "H2O == OH2 != Oh2"). identifiers Additional identifiers to search for (smiles, etc) limit The maximum number of Molecules to query. skip The number of Molecules to skip in the query, used during pagination """ if limit is not None and limit > self.api_limits["get_molecules"]: warnings.warn(f"Specified limit of {limit} is over the server limit. Server limit will be used") limit = min(limit, self.api_limits["get_molecules"]) query_body = { "molecule_hash": make_list(molecule_hash), "molecular_formula": make_list(molecular_formula), "limit": limit, "skip": skip, } if identifiers is not None: query_body["identifiers"] = {k: make_list(v) for k, v in identifiers.items()} meta, molecules = self._auto_request( "post", "v1/molecule/query", MoleculeQueryBody, None, Tuple[QueryMetadata, List[Molecule]], query_body, None ) return meta, molecules def add_molecules(self, molecules: Sequence[Molecule]) -> Tuple[InsertMetadata, List[int]]: """Add molecules to the server. Parameters molecules A list of Molecules to add to the server. Returns ------- : A list of Molecule ids in the same order as the `molecules` parameter. """ if len(molecules) > self.api_limits["add_molecules"]: raise RuntimeError( f"Cannot add {len(molecules)} molecules - over the limit of {self.api_limits['add_molecules']}" ) mols = self._auto_request( "post", "v1/molecule", List[Molecule], None, Tuple[InsertMetadata, List[int]], make_list(molecules), None, ) return mols def modify_molecule( self, id: int, name: Optional[str] = None, comment: Optional[str] = None, identifiers: Optional[Union[Dict[str, Any], MoleculeIdentifiers]] = None, overwrite_identifiers: bool = False, ) -> UpdateMetadata: """ Modify molecules on the server This is only capable of updating the name, comment, and identifiers fields (except molecule_hash and molecular formula). If a molecule with that id does not exist, an exception is raised Parameters ---------- id Molecule ID of the molecule to modify name New name for the molecule. If None, name is not changed. comment New comment for the molecule. If None, comment is not changed identifiers A new set of identifiers for the molecule overwrite_identifiers If True, the identifiers of the molecule are set to be those given exactly (ie, identifiers that exist in the DB but not in the new set will be removed). Otherwise, the new set of identifiers is merged into the existing ones. Note that molecule_hash and molecular_formula are never removed. Returns ------- : Metadata about the modification/update. """ body = { "name": name, "comment": comment, "identifiers": identifiers, "overwrite_identifiers": overwrite_identifiers, } return self._auto_request("patch", f"v1/molecule/{id}", MoleculeModifyBody, None, UpdateMetadata, body, None) def delete_molecules(self, id: Union[int, Sequence[int]]) -> DeleteMetadata: """Deletes molecules from the server This will not delete any keywords that are in use Parameters ---------- id An id or list of ids to query. Returns ------- : Metadata about what was deleted """ url_params = {"id": make_list(id)} return self._auto_request( "delete", "v1/molecule", None, CommonDeleteURLParameters, DeleteMetadata, None, url_params ) ############################################################## # Keywords ############################################################## def get_keywords( self, keywords_id: Union[int, Sequence[int]], missing_ok: bool = False, ) -> Union[Optional[KeywordSet], List[Optional[KeywordSet]]]: """Obtains keywords from the server via keyword ids Parameters ---------- keywords_id An id or list of ids to query. missing_ok If True, return ``None`` for ids that were not found on the server. If False, raise ``KeyError`` if any ids were not found on the server. Returns ------- : The requested keywords, in the same order as the requested ids. If given a list of ids, the return value will be a list. Otherwise, it will be a single KeywordSet. """ url_params = {"id": make_list(keywords_id), "missing_ok": missing_ok} if len(url_params["id"]) > self.api_limits["get_keywords"]: raise RuntimeError( f"Cannot get {len(url_params['id'])} keywords - over the limit of {self.api_limits['get_keywords']}" ) keywords = self._auto_request( "get", "v1/keyword", None, CommonGetURLParameters, List[Optional[KeywordSet]], None, url_params ) if isinstance(keywords_id, Sequence): return keywords else: return keywords[0] def add_keywords(self, keywords: Sequence[KeywordSet]) -> Union[List[int], Tuple[InsertMetadata, List[int]]]: """Adds keywords to the server This function is not expected to be used by end users Parameters ---------- keywords A KeywordSet or list of KeywordSet to add to the server. full_return If True, return additional metadata about the insertion. The return will be a tuple of (metadata, ids) Returns ------- : A list of KeywordSet ids that were added or existing on the server, in the same order as specified in the keywords parameter. If full_return is True, this function will return a tuple containing metadata and the ids. """ if len(keywords) > self.api_limits["add_molecules"]: raise RuntimeError( f"Cannot add {len(keywords)} keywords - over the limit of {self.api_limits['add_keywords']}" ) return self._auto_request( "post", "v1/keyword", List[KeywordSet], None, Tuple[InsertMetadata, List[int]], make_list(keywords), None ) def _delete_keywords(self, keywords_id: Union[int, Sequence[int]]) -> DeleteMetadata: """Deletes keywords from the server This will not delete any keywords that are in use Parameters ---------- keywords_id An id or list of ids to query. Returns ------- : Metadata about what was deleted """ url_params = {"id": make_list(keywords_id)} return self._auto_request( "delete", "v1/keyword", None, CommonDeleteURLParameters, DeleteMetadata, None, url_params ) ############################################################## # General record functions ############################################################## def get_records( self, record_id: Union[int, Sequence[int]], missing_ok: bool = False, *, include_task: bool = False, include_service: bool = False, include_outputs: bool = False, include_comments: bool = False, ) -> Union[List[Optional[AllRecordTypes]], Optional[AllRecordTypes]]: """Get result records by id.""" if isinstance(record_id, Sequence) and not record_id: return [] url_params = {"id": make_list(record_id), "missing_ok": missing_ok} if len(url_params["id"]) > self.api_limits["get_records"]: raise RuntimeError( f"Cannot get {len(url_params['id'])} records - over the limit of {self.api_limits['get_records']}" ) include = set() # We must add '*' so that all the default fields are included if include_task: include |= {"*", "task"} if include_service: include |= {"*", "service"} if include_outputs: include |= {"*", "compute_history.*", "compute_history.outputs"} if include_comments: include |= {"*", "comments"} if include: url_params["include"] = include record_data = self._auto_request( "get", "v1/record", None, CommonGetProjURLParameters, List[Optional[AllDataModelTypes]], None, url_params, ) records = self.recordmodel_from_datamodel(record_data) if isinstance(record_id, Sequence): return records else: return records[0] def query_records( self, record_id: Optional[Iterable[int]] = None, record_type: Optional[Iterable[str]] = None, manager_name: Optional[Iterable[str]] = None, status: Optional[Iterable[RecordStatusEnum]] = None, created_before: Optional[datetime] = None, created_after: Optional[datetime] = None, modified_before: Optional[datetime] = None, modified_after: Optional[datetime] = None, limit: int = None, skip: int = 0, *, include_task: bool = False, include_service: bool = False, include_outputs: bool = False, include_comments: bool = False, ) -> Tuple[QueryMetadata, List[AllRecordTypes]]: if limit is not None and limit > self.api_limits["get_records"]: warnings.warn(f"Specified limit of {limit} is over the server limit. Server limit will be used") limit = min(limit, self.api_limits["get_records"]) query_data = { "record_id": make_list(record_id), "record_type": make_list(record_type), "manager_name": make_list(manager_name), "status": make_list(status), "created_before": created_before, "created_after": created_after, "modified_before": modified_before, "modified_after": modified_after, "limit": limit, "skip": skip, } include = set() # We must add '*' so that all the default fields are included if include_task: include |= {"*", "task"} if include_service: include |= {"*", "service"} if include_outputs: include |= {"*", "compute_history.*", "compute_history.outputs"} if include_comments: include |= {"*", "comments"} if include: query_data["include"] = include meta, record_data = self._auto_request( "post", "v1/record/query", RecordQueryBody, None, Tuple[QueryMetadata, List[AllDataModelTypes]], query_data, None, ) return meta, self.recordmodel_from_datamodel(record_data) def cancel_records(self, record_id: Union[int, Sequence[int]]) -> UpdateMetadata: body_data = {"record_id": make_list(record_id), "status": RecordStatusEnum.cancelled} return self._auto_request("patch", "v1/record", RecordModifyBody, None, UpdateMetadata, body_data, None) def reset_records(self, record_id: Union[int, Sequence[int]]) -> UpdateMetadata: body_data = {"record_id": make_list(record_id), "status": RecordStatusEnum.waiting} return self._auto_request("patch", "v1/record", RecordModifyBody, None, UpdateMetadata, body_data, None) def delete_records( self, record_id: Union[int, Sequence[int]], soft_delete=True, delete_children: bool = True ) -> DeleteMetadata: url_params = {"record_id": make_list(record_id), "soft_delete": soft_delete, "delete_children": delete_children} return self._auto_request( "delete", "v1/record", None, RecordDeleteURLParameters, DeleteMetadata, None, url_params ) def undelete_records(self, record_id: Union[int, Sequence[int]]) -> UndeleteMetadata: url_params = {"record_id": make_list(record_id)} return self._auto_request( "post", "v1/record/undelete", None, RecordUndeleteURLParameters, UndeleteMetadata, None, url_params ) def modify_records( self, record_id: Union[int, Sequence[int]], new_tag: Optional[str] = None, new_priority: Optional[PriorityEnum] = None, delete_tag: bool = False, ) -> UpdateMetadata: body_data = { "record_id": make_list(record_id), "tag": new_tag, "priority": new_priority, "delete_tag": delete_tag, } return self._auto_request("patch", "v1/record", RecordModifyBody, None, UpdateMetadata, body_data, None) def add_comment(self, record_id: Union[int, Sequence[int]], comment: str) -> UpdateMetadata: """ Adds a comment to records Parameters ---------- record_id The record or records to add the comments to comment The comment string to add. You username will be added automatically Returns ------- : Metadata about which records were updated """ body_data = { "record_id": make_list(record_id), "comment": comment, } return self._auto_request("patch", "v1/record", RecordModifyBody, None, UpdateMetadata, body_data, None) ############################################################## # Singlepoint calculations ############################################################## def add_singlepoints( self, molecules: Union[int, Molecule, List[Union[int, Molecule]]], program: str, driver: str, method: str, basis: Optional[str], keywords: Optional[Union[KeywordSet, Dict[str, Any], int]] = None, protocols: Optional[Union[SinglepointProtocols, Dict[str, Any]]] = None, tag: Optional[str] = None, priority: PriorityEnum = PriorityEnum.normal, ) -> Tuple[InsertMetadata, List[int]]: """ Adds a "single" compute to the server. Parameters ---------- molecules The Molecules or Molecule ids to compute with the above methods program The computational program to execute the result with (e.g., "rdkit", "psi4"). driver The primary result that the compute will acquire {"energy", "gradient", "hessian", "properties"} method The computational method to use (e.g., "B3LYP", "PBE") basis The basis to apply to the computation (e.g., "cc-pVDZ", "6-31G") keywords The KeywordSet ObjectId to use with the given compute priority The priority of the job {"HIGH", "MEDIUM", "LOW"}. Default is "MEDIUM". protocols Protocols for store more or less data per field tag The computational tag to add to your compute, managers can optionally only pull based off the string tags. These tags are arbitrary, but several examples are to use "large", "medium", "small" to denote the size of the job or "project1", "project2" to denote different projects. Returns ------- : A list of record ids (one per molecule) that were added or existing on the server, in the same order as specified in the molecules.keywords parameter """ body_data = { "molecules": make_list(molecules), "specification": { "program": program, "driver": driver, "method": method, "basis": basis, }, "tag": tag, "priority": priority, } if isinstance(keywords, dict): # Turn this into a keyword set keywords = KeywordSet(values=keywords) # If these are None, then let the pydantic models handle the defaults if keywords is not None: body_data["specification"]["keywords"] = keywords if protocols is not None: body_data["specification"]["protocols"] = protocols if len(body_data["molecules"]) > self.api_limits["add_records"]: raise RuntimeError( f"Cannot add {len(body_data['molecules'])} records - over the limit of {self.api_limits['add_records']}" ) return self._auto_request( "post", "v1/record/singlepoint", SinglepointAddBody, None, Tuple[InsertMetadata, List[int]], body_data, None ) def get_singlepoints( self, record_id: Union[int, Sequence[int]], missing_ok: bool = False, *, include_task: bool = False, include_outputs: bool = False, include_comments: bool = False, include_molecule: bool = False, include_wavefunction: bool = False, ) -> Union[Optional[SinglepointRecord], List[Optional[SinglepointRecord]]]: url_params = {"id": make_list(record_id), "missing_ok": missing_ok} include = set() # We must add '*' so that all the default fields are included if include_task: include |= {"*", "task"} if include_outputs: include |= {"*", "compute_history.*", "compute_history.outputs"} if include_comments: include |= {"*", "comments"} if include_molecule: include |= {"*", "molecule"} if include_wavefunction: include |= {"*", "wavefunction"} if include: url_params["include"] = include if len(url_params["id"]) > self.api_limits["get_records"]: raise RuntimeError( f"Cannot get {len(url_params['id'])} records - over the limit of {self.api_limits['get_records']}" ) record_data = self._auto_request( "get", "v1/record/singlepoint", None, CommonGetProjURLParameters, List[Optional[SinglepointRecord._DataModel]], None, url_params, ) records = self.recordmodel_from_datamodel(record_data) if isinstance(record_id, Sequence): return records else: return records[0] def query_singlepoints( self, record_id: Optional[Iterable[int]] = None, manager_name: Optional[Iterable[str]] = None, status: Optional[Iterable[RecordStatusEnum]] = None, created_before: Optional[datetime] = None, created_after: Optional[datetime] = None, modified_before: Optional[datetime] = None, modified_after: Optional[datetime] = None, program: Optional[Iterable[str]] = None, driver: Optional[Iterable[SinglepointDriver]] = None, method: Optional[Iterable[str]] = None, basis: Optional[Iterable[Optional[str]]] = None, keywords_id: Optional[Iterable[int]] = None, molecule_id: Optional[Iterable[int]] = None, limit: Optional[int] = None, skip: int = 0, *, include_task: bool = False, include_outputs: bool = False, include_comments: bool = False, include_molecule: bool = False, include_wavefunction: bool = False, ) -> Tuple[QueryMetadata, List[SinglepointRecord]]: """Queries SinglepointRecords from the server.""" if limit is not None and limit > self.api_limits["get_records"]: warnings.warn(f"Specified limit of {limit} is over the server limit. Server limit will be used") limit = min(limit, self.api_limits["get_records"]) query_data = { "record_id": make_list(record_id), "manager_name": make_list(manager_name), "status": make_list(status), "program": make_list(program), "driver": make_list(driver), "method": make_list(method), "basis": make_list(basis), "keywords_id": make_list(keywords_id), "molecule_id": make_list(molecule_id), "created_before": created_before, "created_after": created_after, "modified_before": modified_before, "modified_after": modified_after, "limit": limit, "skip": skip, } include = set() # We must add '*' so that all the default fields are included if include_task: include |= {"*", "task"} if include_outputs: include |= {"*", "compute_history.*", "compute_history.outputs"} if include_comments: include |= {"*", "comments"} if include_molecule: include |= {"*", "molecule"} if include_wavefunction: include |= {"*", "wavefuntion"} if include: query_data["include"] = include meta, record_data = self._auto_request( "post", "v1/record/singlepoint/query", SinglepointQueryBody, None, Tuple[QueryMetadata, List[SinglepointRecord._DataModel]], query_data, None, ) return meta, self.recordmodel_from_datamodel(record_data) ############################################################## # Optimization calculations ############################################################## def add_optimizations( self, initial_molecules: Union[int, Molecule, List[Union[int, Molecule]]], program: str, qc_specification: OptimizationQCInputSpecification, keywords: Optional[Union[KeywordSet, Dict[str, Any], int]] = None, protocols: Optional[OptimizationProtocols] = None, tag: Optional[str] = None, priority: PriorityEnum = PriorityEnum.normal, ) -> Tuple[InsertMetadata, List[int]]: """ Adds optimization calculations to the server """ body_data = { "initial_molecules": make_list(initial_molecules), "specification": { "program": program, "qc_specification": qc_specification, }, "tag": tag, "priority": priority, } # If these are None, then let the pydantic models handle the defaults if keywords is not None: body_data["specification"]["keywords"] = keywords if protocols is not None: body_data["specification"]["protocols"] = protocols if len(body_data["initial_molecules"]) > self.api_limits["add_records"]: raise RuntimeError( f"Cannot get {len(body_data['initial_molecules'])} records - over the limit of {self.api_limits['add_records']}" ) return self._auto_request( "post", "v1/record/optimization", OptimizationAddBody, None, Tuple[InsertMetadata, List[int]], body_data, None, ) def get_optimizations( self, record_id: Union[int, Sequence[int]], missing_ok: bool = False, *, include_task: bool = False, include_outputs: bool = False, include_comments: bool = False, include_initial_molecule: bool = False, include_final_molecule: bool = False, include_trajectory: bool = False, ) -> Union[Optional[OptimizationRecord], List[Optional[OptimizationRecord]]]: url_params = {"id": make_list(record_id), "missing_ok": missing_ok} include = set() # We must add '*' so that all the default fields are included if include_task: include |= {"*", "task"} if include_outputs: include |= {"*", "compute_history.*", "compute_history.outputs"} if include_comments: include |= {"*", "comments"} if include_initial_molecule: include |= {"*", "initial_molecule"} if include_final_molecule: include |= {"*", "final_molecule"} if include_trajectory: include |= {"*", "trajectory"} if include: url_params["include"] = include if len(url_params["id"]) > self.api_limits["get_records"]: raise RuntimeError( f"Cannot get {len(url_params['id'])} records - over the limit of {self.api_limits['get_records']}" ) record_data = self._auto_request( "get", "v1/record/optimization", None, CommonGetProjURLParameters, List[Optional[OptimizationRecord._DataModel]], None, url_params, ) records = self.recordmodel_from_datamodel(record_data) if isinstance(record_id, Sequence): return records else: return records[0] def query_optimizations( self, record_id: Optional[Iterable[int]] = None, manager_name: Optional[Iterable[str]] = None, status: Optional[Iterable[RecordStatusEnum]] = None, created_before: Optional[datetime] = None, created_after: Optional[datetime] = None, modified_before: Optional[datetime] = None, modified_after: Optional[datetime] = None, program: Optional[Iterable[str]] = None, singlepoint_program: Optional[Iterable[str]] = None, singlepoint_method: Optional[Iterable[str]] = None, singlepoint_basis: Optional[Iterable[Optional[str]]] = None, singlepoint_keywords_id: Optional[Iterable[int]] = None, initial_molecule_id: Optional[Iterable[int]] = None, final_molecule_id: Optional[Iterable[int]] = None, limit: Optional[int] = None, skip: int = 0, *, include_task: bool = False, include_outputs: bool = False, include_comments: bool = False, include_initial_molecule: bool = False, include_final_molecule: bool = False, include_trajectory: bool = False, ) -> Tuple[QueryMetadata, List[OptimizationRecord]]: """Queries OptimizationRecords from the server.""" if limit is not None and limit > self.api_limits["get_records"]: warnings.warn(f"Specified limit of {limit} is over the server limit. Server limit will be used") limit = min(limit, self.api_limits["get_records"]) query_data = { "record_id": make_list(record_id), "manager_name": make_list(manager_name), "status": make_list(status), "program": make_list(program), "singlepoint_program": make_list(singlepoint_program), "singlepoint_method": make_list(singlepoint_method), "singlepoint_basis": make_list(singlepoint_basis), "singlepoint_keywords_id": make_list(singlepoint_keywords_id), "initial_molecule_id": make_list(initial_molecule_id), "final_molecule_id": make_list(final_molecule_id), "created_before": created_before, "created_after": created_after, "modified_before": modified_before, "modified_after": modified_after, "limit": limit, "skip": skip, } include = set() # We must add '*' so that all the default fields are included if include_task: include |= {"*", "task"} if include_outputs: include |= {"*", "compute_history.*", "compute_history.outputs"} if include_comments: include |= {"*", "comments"} if include_initial_molecule: include |= {"*", "initial_molecule"} if include_final_molecule: include |= {"*", "final_molecule"} if include_trajectory: include |= {"*", "trajectory"} if include: query_data["include"] = include meta, record_data = self._auto_request( "post", "v1/record/optimization/query", OptimizationQueryBody, None, Tuple[QueryMetadata, List[OptimizationRecord._DataModel]], query_data, None, ) return meta, self.recordmodel_from_datamodel(record_data) ############################################################## # Torsiondrive calculations ############################################################## def add_torsiondrives( self, initial_molecules: List[List[Union[int, Molecule]]], program: str, optimization_specification: OptimizationInputSpecification, keywords: Union[TorsiondriveKeywords, Dict[str, Any]], tag: Optional[str] = None, priority: PriorityEnum = PriorityEnum.normal, ) -> Tuple[InsertMetadata, List[int]]: """ Adds torsiondrive calculations to the server """ body_data = { "initial_molecules": initial_molecules, "specification": { "program": program, "optimization_specification": optimization_specification, "keywords": keywords, }, "as_service": True, "tag": tag, "priority": priority, } if len(body_data["initial_molecules"]) > self.api_limits["add_records"]: raise RuntimeError( f"Cannot get {len(body_data['initial_molecules'])} records - over the limit of {self.api_limits['add_records']}" ) return self._auto_request( "post", "v1/record/torsiondrive", TorsiondriveAddBody, None, Tuple[InsertMetadata, List[int]], body_data, None, ) def get_torsiondrives( self, record_id: Union[int, Sequence[int]], missing_ok: bool = False, *, include_task: bool = False, include_service: bool = False, include_outputs: bool = False, include_comments: bool = False, include_initial_molecules: bool = False, include_optimizations: bool = False, ) -> Union[Optional[TorsiondriveRecord], List[Optional[TorsiondriveRecord]]]: url_params = {"id": make_list(record_id), "missing_ok": missing_ok} include = set() # We must add '*' so that all the default fields are included if include_task: include |= {"*", "task"} if include_service: include |= {"*", "service"} if include_outputs: include |= {"*", "compute_history.*", "compute_history.outputs"} if include_comments: include |= {"*", "comments"} if include_initial_molecules: include |= {"*", "initial_molecules"} if include_optimizations: include |= {"*", "optimizations"} if include: url_params["include"] = include if len(url_params["id"]) > self.api_limits["get_records"]: raise RuntimeError( f"Cannot get {len(url_params['id'])} records - over the limit of {self.api_limits['get_records']}" ) record_data = self._auto_request( "get", "v1/record/torsiondrive", None, CommonGetProjURLParameters, List[Optional[TorsiondriveRecord._DataModel]], None, url_params, ) records = self.recordmodel_from_datamodel(record_data) if isinstance(record_id, Sequence): return records else: return records[0] def query_torsiondrives( self, record_id: Optional[Iterable[int]] = None, manager_name: Optional[Iterable[str]] = None, status: Optional[Iterable[RecordStatusEnum]] = None, created_before: Optional[datetime] = None, created_after: Optional[datetime] = None, modified_before: Optional[datetime] = None, modified_after: Optional[datetime] = None, program: Optional[Iterable[str]] = None, optimization_program: Optional[Iterable[str]] = None, singlepoint_program: Optional[Iterable[str]] = None, singlepoint_method: Optional[Iterable[str]] = None, singlepoint_basis: Optional[Iterable[Optional[str]]] = None, singlepoint_keywords_id: Optional[Iterable[int]] = None, initial_molecule_id: Optional[Iterable[int]] = None, limit: Optional[int] = None, skip: int = 0, *, include_task: bool = False, include_service: bool = False, include_outputs: bool = False, include_comments: bool = False, include_initial_molecules: bool = False, include_optimizations: bool = False, ) -> Tuple[QueryMetadata, List[TorsiondriveRecord]]: """Queries torsiondrive records from the server.""" if limit is not None and limit > self.api_limits["get_records"]: warnings.warn(f"Specified limit of {limit} is over the server limit. Server limit will be used") limit = min(limit, self.api_limits["get_records"]) query_data = { "record_id": make_list(record_id), "manager_name": make_list(manager_name), "status": make_list(status), "program": make_list(program), "optimization_program": make_list(optimization_program), "singlepoint_program": make_list(singlepoint_program), "singlepoint_method": make_list(singlepoint_method), "singlepoint_basis": make_list(singlepoint_basis), "singlepoint_keywords_id": make_list(singlepoint_keywords_id), "initial_molecule_id": make_list(initial_molecule_id), "created_before": created_before, "created_after": created_after, "modified_before": modified_before, "modified_after": modified_after, "limit": limit, "skip": skip, } include = set() # We must add '*' so that all the default fields are included if include_task: include |= {"*", "task"} if include_service: include |= {"*", "service"} if include_outputs: include |= {"*", "compute_history.*", "compute_history.outputs"} if include_comments: include |= {"*", "comments"} if include_initial_molecules: include |= {"*", "initial_molecules"} if include_optimizations: include |= {"*", "optimizations"} if include: query_data["include"] = include meta, record_data = self._auto_request( "post", "v1/record/torsiondrive/query", TorsiondriveQueryBody, None, Tuple[QueryMetadata, List[TorsiondriveRecord._DataModel]], query_data, None, ) return meta, self.recordmodel_from_datamodel(record_data) ############################################################## # Grid optimization calculations ############################################################## def add_gridoptimizations( self, initial_molecules: Union[int, Molecule, Sequence[Union[int, Molecule]]], program: str, optimization_specification: OptimizationInputSpecification, keywords: Union[GridoptimizationKeywords, Dict[str, Any]], tag: Optional[str] = None, priority: PriorityEnum = PriorityEnum.normal, ) -> Tuple[InsertMetadata, List[int]]: """ Adds gridoptimization calculations to the server """ body_data = { "initial_molecules": initial_molecules, "specification": { "program": program, "optimization_specification": optimization_specification, "keywords": keywords, }, "tag": tag, "priority": priority, } if len(body_data["initial_molecules"]) > self.api_limits["add_records"]: raise RuntimeError( f"Cannot get {len(body_data['initial_molecules'])} records - over the limit of {self.api_limits['add_records']}" ) return self._auto_request( "post", "v1/record/gridoptimization", GridoptimizationAddBody, None, Tuple[InsertMetadata, List[int]], body_data, None, ) def get_gridoptimizations( self, record_id: Union[int, Sequence[int]], missing_ok: bool = False, *, include_service: bool = False, include_outputs: bool = False, include_comments: bool = False, include_initial_molecule: bool = False, include_starting_molecule: bool = False, include_optimizations: bool = False, ) -> Union[Optional[GridoptimizationRecord], List[Optional[GridoptimizationRecord]]]: url_params = {"id": make_list(record_id), "missing_ok": missing_ok} include = set() # We must add '*' so that all the default fields are included if include_service: include |= {"*", "service"} if include_outputs: include |= {"*", "compute_history.*", "compute_history.outputs"} if include_comments: include |= {"*", "comments"} if include_initial_molecule: include |= {"*", "initial_molecule"} if include_starting_molecule: include |= {"*", "starting_molecule"} if include_optimizations: include |= {"*", "optimizations"} if include: url_params["include"] = include if len(url_params["id"]) > self.api_limits["get_records"]: raise RuntimeError( f"Cannot get {len(url_params['id'])} records - over the limit of {self.api_limits['get_records']}" ) record_data = self._auto_request( "get", "v1/record/gridoptimization", None, CommonGetProjURLParameters, List[Optional[GridoptimizationRecord._DataModel]], None, url_params, ) records = self.recordmodel_from_datamodel(record_data) if isinstance(record_id, Sequence): return records else: return records[0] def query_gridoptimizations( self, record_id: Optional[Iterable[int]] = None, manager_name: Optional[Iterable[str]] = None, status: Optional[Iterable[RecordStatusEnum]] = None, created_before: Optional[datetime] = None, created_after: Optional[datetime] = None, modified_before: Optional[datetime] = None, modified_after: Optional[datetime] = None, program: Optional[Iterable[str]] = None, optimization_program: Optional[Iterable[str]] = None, singlepoint_program: Optional[Iterable[str]] = None, singlepoint_method: Optional[Iterable[str]] = None, singlepoint_basis: Optional[Iterable[Optional[str]]] = None, singlepoint_keywords_id: Optional[Iterable[int]] = None, initial_molecule_id: Optional[Iterable[int]] = None, limit: Optional[int] = None, skip: int = 0, *, include_task: bool = False, include_service: bool = False, include_outputs: bool = False, include_comments: bool = False, include_initial_molecule: bool = False, include_optimizations: bool = False, ) -> Tuple[QueryMetadata, List[GridoptimizationRecord]]: """Queries torsiondrive records from the server.""" if limit is not None and limit > self.api_limits["get_records"]: warnings.warn(f"Specified limit of {limit} is over the server limit. Server limit will be used") limit = min(limit, self.api_limits["get_records"]) query_data = { "record_id": make_list(record_id), "manager_name": make_list(manager_name), "status": make_list(status), "program": make_list(program), "optimization_program": make_list(optimization_program), "singlepoint_program": make_list(singlepoint_program), "singlepoint_method": make_list(singlepoint_method), "singlepoint_basis": make_list(singlepoint_basis), "singlepoint_keywords_id": make_list(singlepoint_keywords_id), "initial_molecule_id": make_list(initial_molecule_id), "created_before": created_before, "created_after": created_after, "modified_before": modified_before, "modified_after": modified_after, "limit": limit, "skip": skip, } include = set() # We must add '*' so that all the default fields are included if include_task: include |= {"*", "task"} if include_service: include |= {"*", "service"} if include_outputs: include |= {"*", "compute_history.*", "compute_history.outputs"} if include_comments: include |= {"*", "comments"} if include_initial_molecule: include |= {"*", "initial_molecule"} if include_optimizations: include |= {"*", "optimizations"} if include: query_data["include"] = include meta, record_data = self._auto_request( "post", "v1/record/gridoptimization/query", TorsiondriveQueryBody, None, Tuple[QueryMetadata, List[GridoptimizationRecord._DataModel]], query_data, None, ) return meta, self.recordmodel_from_datamodel(record_data) ############################################################## # Managers ############################################################## def get_managers( self, name: Union[str, Sequence[str]], missing_ok: bool = False, ) -> Union[Optional[ComputeManager], List[Optional[ComputeManager]]]: """Obtains manager information from the server via name Parameters ---------- name A manager name or list of names missing_ok If True, return ``None`` for managers that were not found on the server. If False, raise ``KeyError`` if any managers were not found on the server. Returns ------- : The requested managers, in the same order as the requested ids. If given a list of ids, the return value will be a list. Otherwise, it will be a single manager. """ url_params = {"name": make_list(name), "missing_ok": missing_ok} managers = self._auto_request( "get", "v1/manager", None, CommonGetURLParametersName, List[Optional[ComputeManager]], None, url_params ) if isinstance(name, Sequence): return managers else: return managers[0] def query_managers( self, id: Optional[Union[int, Iterable[int]]] = None, name: Optional[Union[str, Iterable[str]]] = None, cluster: Optional[Union[str, Iterable[str]]] = None, hostname: Optional[Union[str, Iterable[str]]] = None, status: Optional[Union[RecordStatusEnum, Iterable[RecordStatusEnum]]] = None, modified_before: Optional[datetime] = None, modified_after: Optional[datetime] = None, include_log: bool = False, limit: Optional[int] = None, skip: int = 0, ) -> Tuple[QueryMetadata, Dict[str, Any]]: """Obtains information about compute managers attached to this Fractal instance Parameters ---------- id ID assigned to the manager (this is not the UUID. This should be used very rarely). name Queries the managers name cluster Queries the managers cluster hostname Queries the managers hostname status Queries the manager's status field modified_before Query for managers last modified before a certain time modified_after Query for managers last modified after a certain time include_log If True, include the log entries for the manager limit The maximum number of managers to query skip The number of managers to skip in the query, used during pagination Returns ------- : Metadata about the query results, and a list of dictionaries with information matching the specified query. """ if limit is not None and limit > self.api_limits["get_managers"]: warnings.warn(f"Specified limit of {limit} is over the server limit. Server limit will be used") limit = min(limit, self.api_limits["get_managers"]) query_body = { "id": make_list(id), "name": make_list(name), "cluster": make_list(cluster), "hostname": make_list(hostname), "status": make_list(status), "modified_before": modified_before, "modified_after": modified_after, "limit": limit, "skip": skip, } if include_log: query_body["include"] = ["*", "log"] return self._auto_request( "post", "v1/manager/query", ManagerQueryBody, None, Tuple[QueryMetadata, List[ComputeManager]], query_body, None, ) ############################################################## # Server statistics and logs ############################################################## def query_server_stats( self, before: Optional[datetime] = None, after: Optional[datetime] = None, limit: Optional[int] = None, skip: int = 0, ) -> Tuple[QueryMetadata, List[Dict[str, Any]]]: """Obtains individual entries in the server stats logs""" if limit is not None and limit > self.api_limits["get_server_stats"]: warnings.warn(f"Specified limit of {limit} is over the server limit. Server limit will be used") limit = min(limit, self.api_limits["get_server_stats"]) url_params = {"before": before, "after": after, "limit": limit, "skip": skip} return self._auto_request( "get", "v1/server_stats", None, ServerStatsQueryParameters, Tuple[QueryMetadata, List[Dict[str, Any]]], None, url_params, ) def delete_server_stats(self, before: datetime): url_params = {"before": before} return self._auto_request("delete", "v1/server_stats", None, DeleteBeforeDateParameters, int, None, url_params) def query_access_log( self, access_type: Optional[Union[str, Iterable[str]]] = None, access_method: Optional[Union[str, Iterable[str]]] = None, before: Optional[datetime] = None, after: Optional[datetime] = None, limit: Optional[int] = None, skip: int = 0, ) -> Tuple[QueryMetadata, List[Dict[str, Any]]]: """Obtains individual entries in the access logs""" if limit is not None and limit > self.api_limits["get_access_logs"]: warnings.warn(f"Specified limit of {limit} is over the server limit. Server limit will be used") limit = min(limit, self.api_limits["get_access_logs"]) url_params = { "access_type": make_list(access_type), "access_method": make_list(access_method), "before": before, "after": after, "limit": limit, "skip": skip, } return self._auto_request( "get", "v1/access", None, AccessLogQueryParameters, Tuple[QueryMetadata, List[Dict[str, Any]]], None, url_params, ) def delete_access_log(self, before: datetime): url_params = {"before": before} return self._auto_request("delete", "v1/access", None, DeleteBeforeDateParameters, int, None, url_params) def query_error_log( self, id: Optional[Union[int, Iterable[int]]] = None, username: Optional[Union[str, Iterable[str]]] = None, before: Optional[datetime] = None, after: Optional[datetime] = None, limit: Optional[int] = None, skip: int = 0, ) -> Tuple[QueryMetadata, Dict[str, Any]]: """Obtains individual entries in the error logs""" if limit is not None and limit > self.api_limits["get_error_logs"]: warnings.warn(f"Specified limit of {limit} is over the server limit. Server limit will be used") limit = min(limit, self.api_limits["get_error_logs"]) url_params = { "id": make_list(id), "username": make_list(username), "before": before, "after": after, "limit": limit, "skip": skip, } return self._auto_request( "get", "v1/server_error", None, ErrorLogQueryParameters, Tuple[QueryMetadata, List[Dict[str, Any]]], None, url_params, ) def delete_error_log(self, before: datetime): url_params = {"before": before} return self._auto_request("delete", "v1/server_error", None, DeleteBeforeDateParameters, int, None, url_params) def query_access_summary( self, group_by: str = "day", before: Optional[datetime] = None, after: Optional[datetime] = None, ) -> Dict[str, Any]: """Obtains daily summaries of accesses Parameters ---------- group_by How to group the data. Valid options are "user", "hour", "day", "country", "subdivision" before Query for log entries with a timestamp before a specific time after Query for log entries with a timestamp after a specific time """ url_params = { "group_by": group_by, "before": before, "after": after, } return self._auto_request( "get", "v1/access/summary", None, AccessLogQuerySummaryParameters, Dict[str, Any], None, url_params ) ############################################################## # User & role management ############################################################## def list_roles(self) -> List[RoleInfo]: """ List all user roles on the server """ return self._auto_request("get", "v1/role", None, None, List[RoleInfo], None, None) def get_role(self, rolename: str) -> RoleInfo: """ Get information about a role on the server """ is_valid_rolename(rolename) return self._auto_request("get", f"v1/role/{rolename}", None, None, RoleInfo, None, None) def add_role(self, role_info: RoleInfo) -> None: """ Adds a role with permissions to the server If not successful, an exception is raised. """ is_valid_rolename(role_info.rolename) return self._auto_request("post", "v1/role", RoleInfo, None, None, role_info, None) def modify_role(self, role_info: RoleInfo) -> RoleInfo: """ Modifies the permissions of a role on the server If not successful, an exception is raised. Returns ------- : A copy of the role as it now appears on the server """ is_valid_rolename(role_info.rolename) return self._auto_request("put", f"v1/role/{role_info.rolename}", RoleInfo, None, RoleInfo, role_info, None) def delete_role(self, rolename: str) -> None: """ Deletes a role from the server This will not delete any role to which a user is assigned Will raise an exception on error Parameters ---------- rolename Name of the role to delete """ is_valid_rolename(rolename) return self._auto_request("delete", f"v1/role/{rolename}", None, None, None, None, None) def list_users(self) -> List[UserInfo]: """ List all user roles on the server """ return self._auto_request("get", "v1/user", None, None, List[UserInfo], None, None) def get_user(self, username: Optional[str] = None, as_admin: bool = False) -> UserInfo: """ Get information about a user on the server If the username is not supplied, then info about the currently logged-in user is obtained Parameters ---------- username The username to get info about as_admin If True, then fetch the user from the admin user management endpoint. This is the default if requesting a user other than the currently logged-in user Returns ------- : Information about the user """ if username is None: username = self.username if username is None: raise RuntimeError("Cannot get user - not logged in?") # Check client side so we can bail early is_valid_username(username) if username != self.username: as_admin = True if as_admin is False: # For the currently logged-in user, use the "me" endpoint. The other endpoint is # restricted to admins uinfo = self._auto_request("get", f"v1/me", None, None, UserInfo, None, None) if uinfo.username != self.username: raise RuntimeError( f"Inconsistent username - client is {self.username} but logged in as {uinfo.username}" ) else: uinfo = self._auto_request("get", f"v1/user/{username}", None, None, UserInfo, None, None) return uinfo def add_user(self, user_info: UserInfo, password: Optional[str] = None) -> str: """ Adds a user to the server Parameters ---------- user_info Info about the user to add password The user's password. If None, then one will be generated Returns ------- : The password of the user (either the same as the supplied password, or the server-generated one) """ is_valid_username(user_info.username) is_valid_rolename(user_info.role) if password is not None: is_valid_password(password) if user_info.id is not None: raise RuntimeError("Cannot add user when user_info contains an id") return self._auto_request( "post", "v1/user", Tuple[UserInfo, Optional[str]], None, str, (user_info, password), None ) def modify_user(self, user_info: UserInfo, as_admin: bool = False) -> UserInfo: """ Modifies a user on the server The user is determined by the username field of the input UserInfo, although the id and username are checked for consistency. Depending on the current user's permissions, some fields may not be updatable. Parameters ---------- user_info Updated information for a user as_admin If True, then attempt to modify fields that are only modifiable by an admin (enabled, role). This is the default if requesting a user other than the currently logged-in user. Returns ------- : The updated user information as it appears on the server """ is_valid_username(user_info.username) is_valid_rolename(user_info.role) if as_admin or (user_info.username != self.username): url = f"v1/user/{user_info.username}" else: url = "v1/me" return self._auto_request("put", url, UserInfo, None, UserInfo, user_info, None) def change_user_password(self, username: Optional[str] = None, new_password: Optional[str] = None) -> str: """ Change a users password If the username is not specified, then the current logged-in user is used. If the password is not specified, then one is automatically generated by the server. Parameters ---------- username The name of the user whose password to change. If None, then use the currently logged-in user new_password Password to change to. If None, let the server generate one. Returns ------- : The new password (either the same as the supplied one, or the server generated one """ if username is None: username = self.username is_valid_username(username) if new_password is not None: is_valid_password(new_password) if username == self.username: url = "v1/me/password" else: url = f"v1/user/{username}/password" return self._auto_request("put", url, Optional[str], None, str, new_password, None) def delete_user(self, username: str) -> None: is_valid_username(username) if username == self.username: raise RuntimeError("Cannot delete your own user!") return self._auto_request("delete", f"v1/user/{username}", None, None, None, None, None)
python
import sys import jinja2 import tdclient import tdclient.version from .version import __version__ import logging logger = logging.getLogger(__name__) class Context(object): '''High-level wrapper for tdclient.Client.''' def __init__(self, module=None, config=None): if config is None: config = {} self.module = module # tdclient self.client = self.get_client(apikey=config.get('apikey'), endpoint=config.get('endpoint')) # jinja2 if 'template_loader' in config: self.template_loader = config['template_loader'] elif self.module: self.template_loader = jinja2.PackageLoader(self.module, 'templates') else: self.template_loader = jinja2.FileSystemLoader('templates') def get_client(self, apikey=None, endpoint=None): kwargs = {} if apikey is not None: kwargs['apikey'] = apikey if endpoint is not None: if not endpoint.endswith('/'): endpoint = endpoint + '/' kwargs['endpoint'] = endpoint if 'user_agent' not in kwargs: versions = [ "tdclient/{0}".format(tdclient.version.__version__), "Python/{0}.{1}.{2}.{3}.{4}".format(*list(sys.version_info)), ] kwargs['user_agent'] = "pytd/{0} ({1})".format(__version__, ' '.join(versions)) return tdclient.Client(**kwargs) @property def apikey(self): return self.client.api.apikey @property def endpoint(self): return self.client.api.endpoint def query(self, *args, **kwargs): from pytd.query import Query return Query(self, *args, **kwargs)
python
import agama mass_unit = (1.0/4.3)*(10.0**(6.0)) agama.setUnits(mass=mass_unit, length=1, velocity=1) pot = agama.Potential(type='Spheroid', gamma=1.0, beta=3.1, scaleRadius=2.5, outerCutoffRadius=15.0) df = agama.DistributionFunction(type='QuasiSpherical',potential=pot) model = agama.GalaxyModel(pot,df) M = model.sample(10000) print(M[0][9999,0]) agama.writeSnapshot('test_snapshot.snp',M,'n')
python
# Developed for the LSST System Integration, Test and Commissioning Team. # This product includes software developed by the LSST Project # (http://www.lsst.org). # See the LICENSE file at the top-level directory of this distribution # for details of code ownership. # # Use of this source code is governed by a 3-clause BSD-style # license that can be found in the LICENSE file. __all__ = ["NON_CONFIG_CSCS", "OFFLINE_CSCS"] NON_CONFIG_CSCS = [ "ATArchiver", "ATHeaderService", "ATMCS", "ATPneumatics", "ATPtg", "CCHeaderService", "CCArchiver", "DSM", "LinearStage", "MTPtg", "MTRotator", "ScriptQueue" ] OFFLINE_CSCS = [ "ATCamera", "CCCamera", ]
python
# Import the toolkit specific version. from pyface.toolkit import toolkit_object TaskWindowBackend = toolkit_object( 'tasks.task_window_backend:TaskWindowBackend')
python
# Based on # https://www.paraview.org/Wiki/Python_Programmable_Filter#Generating_Data_.28Programmable_Source.29 #This script generates a helix curve. #This is intended as the script of a 'Programmable Source' def _helix(self, numPts): import math #numPts = 80 # Points along Helix length = 8.0 # Length of Helix rounds = 3.0 # Number of times around #Get a vtk.PolyData object for the output pdo = self.GetPolyDataOutput() #This will store the points for the Helix newPts = vtk.vtkPoints() for i in range(0, numPts): #Generate the Points along the Helix x = i*length/numPts y = math.sin(i*rounds*2*math.pi/numPts) z = math.cos(i*rounds*2*math.pi/numPts) #Insert the Points into the vtkPoints object #The first parameter indicates the reference. #value for the point. Here we add them sequentially. #Note that the first point is at index 0 (not 1). newPts.InsertPoint(i, x,y,z) #Add the points to the vtkPolyData object #Right now the points are not associated with a line - #it is just a set of unconnected points. We need to #create a 'cell' object that ties points together #to make a curve (in this case). This is done below. #A 'cell' is just an object that tells how points are #connected to make a 1D, 2D, or 3D object. pdo.SetPoints(newPts) #Make a vtkPolyLine which holds the info necessary #to create a curve composed of line segments. This #really just hold constructor data that will be passed #to vtkPolyData to add a new line. aPolyLine = vtk.vtkPolyLine() #Indicate the number of points along the line aPolyLine.GetPointIds().SetNumberOfIds(numPts) for i in range(0,numPts): #Add the points to the line. The first value indicates #the order of the point on the line. The second value #is a reference to a point in a vtkPoints object. Depends #on the order that Points were added to vtkPoints object. #Note that this will not be associated with actual points #until it is added to a vtkPolyData object which holds a #vtkPoints object. aPolyLine.GetPointIds().SetId(i, i) #Allocate the number of 'cells' that will be added. We are just #adding one vtkPolyLine 'cell' to the vtkPolyData object. pdo.Allocate(1, 1) #Add the poly line 'cell' to the vtkPolyData object. pdo.InsertNextCell(aPolyLine.GetCellType(), aPolyLine.GetPointIds()) def wrapper(**kwargs): import sys import paraview.simple as pvs # create a new 'Programmable Source' programmableSource1 = pvs.ProgrammableSource() # https://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3 if sys.version_info[0] < 3: programmableSource1.Script = "kwargs="+str(kwargs)+";execfile('" + __file__ + "',globals(),locals())" else: programmableSource1.Script = "kwargs="+str(kwargs)+";exec(open('" + __file__ + "').read())" programmableSource1.ScriptRequestInformation = '' programmableSource1.PythonPath = '' # get active view renderView1 = pvs.GetActiveViewOrCreate('RenderView') # show data in view programmableSource1Display = pvs.Show(programmableSource1, renderView1) def helix(n=10): wrapper(n=n) if 'kwargs' in vars(): _helix(self, kwargs['n'])
python
from django.shortcuts import get_object_or_404, render from django.http import HttpResponseRedirect, HttpResponse from django.contrib.auth import authenticate, login, logout from Propylaea.forms import LoginForm, SignUpForm from django.template import loader from django.contrib.auth.decorators import login_required def SignUpV(request): signedUp = False # Signed up flag if request.method == 'POST': # If the request is type of POST then proccess data form1 = SignUpForm(request.POST)# Create 2 form instances and populate them with data from the POST request if form1.is_valid(): # Check whether it's valid try: # Save main user form first and hash password user = form1.save(commit=False) user.set_password(user.password) user.save() # Change sign up flag to true signedUp = True return HttpResponseRedirect('/demoscopesis/') except: signedUp = False else: template = loader.get_template('error.html') context = { "errorType": "409", "errorMessage": "Your prefered credentials were received but your account was not created. Please try again with a different username.", "redirectTo": "/user/signup" } return HttpResponse(template.render(context, request)) else: # If request is not POST create empty forms #form1 = SignUpForm() template = loader.get_template('Propylaea/login.html') context = { 'SignUpForm': SignUpForm, 'LoginForm': LoginForm, } return HttpResponse(template.render(context, request)) #return HttpResponseRedirect('/Demoscopesis/Demoscopesis') #return render(request, 'Propylaea/login_register.html', {'SignUpForm': SignUpForm, 'LoginForm': LoginForm}) def LogIn(request): if request.method == 'POST': # If the request is type of POST then proccess data #forml = LoginForm(request.POST) #if forml.is_valid(): #email = request.POST.get('email') username = request.POST.get('username') password = request.POST.get('password') #email = request.POST['email'] #password = request.POST['password'] # Authenticate #user = authenticate(email=email, password=password) user = authenticate(username=username, password=password) if user is not None: # User valid if user.is_active: login(request, user) return HttpResponseRedirect('/demoscopesis/') else: return HttpResponse("Your account is disabled.") else: #return HttpResponse("Invalid login details supplied")#: {0}, {1}".format(email, password)) template = loader.get_template('error.html') context = { "errorType": "401", "errorMessage": "You are not authorized to login. Please check your credentials or register an account", "redirectTo": "/user/login" } return HttpResponse(template.render(context, request)) else: template = loader.get_template('Propylaea/login.html') context = { 'LoginForm': LoginForm, 'SignUpForm': SignUpForm, } return HttpResponse(template.render(context, request)) #return render_to_response('Propylaea/login_register.html', {}, context) @login_required(login_url='/user/login/') def UsrLogout(request): # Since we know the user is logged in, we can now just log them out. logout(request) # Take the user back to the homepage. return HttpResponseRedirect('/user/login/')
python
#!/usr/bin/env python import numpy as np import h5py as h import matplotlib import matplotlib.pyplot as plt import sys, os, re, shutil, subprocess, time from optparse import OptionParser parser = OptionParser() parser.add_option("-f", "--file", action="store", type="string", dest="inputFile", help="Input H5 file with TOF data", metavar="FILENAME", default="") parser.add_option("-m", "--motor", action="store", type="string", dest="motorName", help="Motorname to plot TOF data against (default: injectory)", metavar="NAME", default="injectory") parser.add_option("-r", "--run", action="store", type="int", dest="runNumber", help="Run number with TOF data", metavar="NUMBER", default=0) parser.add_option("-l", "--level", action="store", type="int", dest="outputLevel", help="Output level in input H5 file (default: 3)", metavar="NUMBER", default=3) (options, args) = parser.parse_args() ######################################################## original_dir = os.getcwd() + '/' work_dir = "/asap3/flash/gpfs/bl1/2017/data/11001733/processed/hummingbird_tof/" if options.inputFile != '' or options.runNumber != 0: # open input cxidb file if options.inputFile != '': print "Reading TOF data from %s%s ..." % (work_dir, options.inputFile) f = h.File(work_dir + options.inputFile, "r") else: fname = "r%04d_ol%d.h5" % (options.runNumber, options.outputLevel) print "Reading TOF data from %s%s ..." % (work_dir, fname) f = h.File(work_dir + fname, "r") gmdPath = "/entry_1/FEL/gmd" tdPath = "/entry_1/detector_2/data" injectorPath = "/entry_1/motors/%s" % options.motorName # sanity check for p in [gmdPath, tdPath, injectorPath]: if (not f.get(p)): print "\t'%s' does not exist, aborting..." % (p) sys.exit(1) td = f[tdPath][:] print "\tfound %d time traces with %d bins" % (td.shape[0], td.shape[1]) gmd = f[gmdPath][:] gmd_is_nan = np.isnan(gmd) gmd_is_not_nan = np.abs(gmd_is_nan.astype(np.int) - 1).astype(np.bool) gmd_without_nan = gmd[gmd_is_not_nan] print "\tfound %d gmd values (including %d NaNs) between %.2f and %.2f (%.2f +/- %.2f)" % (gmd.shape[0], gmd_is_nan.sum(), np.nanmin(gmd), np.nanmax(gmd), np.nanmean(gmd), np.nanstd(gmd)) injector = f[injectorPath][:] injector_unique = np.sort(list(set(injector))) print "\tfound %d injector (%d unique) values between %.2f and %.2f (%.2f +/- %.2f)" % (injector.shape[0], injector_unique.shape[0], injector.min(), injector.max(), injector.mean(), injector.std()) #print injector_unique # histogram gmd energies gmd_bins = np.arange(np.floor(gmd_without_nan.min()) - 1., np.ceil(gmd_without_nan.max()) + 3., 1.) - 0.5 gmd_hist, gmd_bins = np.histogram(gmd_without_nan, bins=gmd_bins) gmd_bins_center = np.array([(gmd_bins[i] + gmd_bins[i + 1])/2 for i in range(len(gmd_bins) - 1)]) # histogram injector values injector_delta = 0.05 injector_bins = np.arange(injector.min() - injector_delta, injector.max() + 3*injector_delta, injector_delta) - injector_delta/2. injector_hist, injector_bins = np.histogram(injector, bins=injector_bins) injector_bins_center = np.array([(injector_bins[i] + injector_bins[i + 1])/2 for i in range(len(injector_bins) - 1)]) # plot histogram fig = plt.figure(num=1, figsize=(11.5, 5.0), dpi=100, facecolor='w', edgecolor='k') fig.suptitle("Histograms") ax1 = fig.add_subplot(121) ax1.set_title("GMD energies (uJ)") ax1.set_xlabel("GMD (uJ)") ax1.set_ylabel("number of hits") ax1.plot(gmd_bins_center, gmd_hist) ax2 = fig.add_subplot(122) ax2.set_title("%s positions (mm)" % options.motorName) ax2.set_xlabel("%s (mm)" % options.motorName) ax2.set_ylabel("number of hits") ax2.plot(injector_bins_center, injector_hist) plt.show(block=False) while True: try: gmd_low = np.float(input("Enter lower limit for GMD energies (uJ): ")) ax1.axvline(x=gmd_low, color='k', linestyle='--') plt.draw() gmd_high = np.float(input("Enter upper limit for GMD energies (uJ): ")) ax1.axvline(x=gmd_high, color='k', linestyle='--') plt.draw() break except ValueError as err: print err gmd[gmd_is_nan] = -1 data_to_use = gmd_is_not_nan & (gmd >= gmd_low) & (gmd <= gmd_high) print "\taveraging %d / %d traces (%.1f%%)" % (data_to_use.sum(), len(data_to_use), data_to_use.sum()*100./len(data_to_use)) td_avg = np.zeros((injector_unique.shape[0], td.shape[1])) n = 0 for p in injector_unique: td_avg[n] = np.mean(td[data_to_use & (injector == p)], axis=0) n += 1 # plot TOF data fig = plt.figure(num=2, figsize=(11.5, 5.0), dpi=100, facecolor='w', edgecolor='k') fig.suptitle("TOF") ax1 = fig.add_subplot(121) ax1.set_title("TOF traces") ax1.set_xlabel("flight time (arb. u.)") ax1.set_ylabel("ion trace (mV)") ax1.plot(np.mean(td[data_to_use], axis=0), 'k', label="selected") ax1.plot(np.mean(td, axis=0), 'k--', label="all") cmap = plt.get_cmap('plasma') colors = [cmap(n) for n in np.linspace(0, 1, len(injector_unique))] n = 0 for p in injector_unique: ax1.plot(td_avg[n] - (n + 1)*100, color=colors[n], label="%.2f mm" % p) n += 1 #plt.legend(loc='best') #plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.legend(loc=4) ax2 = fig.add_subplot(122) ax2.set_title("TOF trace vs %s" % options.motorName) #ax2.imshow(td_avg, interpolation='nearest', origin='lower', extent=[0, td_avg.shape[1], injector_unique[0], injector_unique[-1]], aspect="auto") im = ax2.imshow(td_avg, interpolation='nearest', origin='lower', extent=[0, td_avg.shape[1], injector_bins[1], injector_bins[-3]], aspect="auto", cmap=plt.get_cmap("viridis")) plt.colorbar(im, pad=0.01) plt.show() else: print "No input file or run specified, aborting..." sys.exit(1)
python
import numpy as np import math # from mxnet import nd from mxnet.gluon import nn class Augmentation(nn.HybridBlock): def __init__(self, angle_range, zoom_range, translation_range, target_shape, orig_shape, batch_size, aspect_range = None, relative_angle = 0, relative_scale = (1, 1), relative_translation = 0): super().__init__() self._angle_range = tuple(map(lambda x : x / 180 * math.pi, angle_range) ) self._scale_range = zoom_range try: translation_range = tuple(translation_range) if len(translation_range) != 2: raise ValueError('expect translation range to have shape [2,], but got {}'.format(translation_range)) except TypeError: translation_range = (-translation_range, translation_range) self._translation_range = tuple(map(lambda x : x * 2, translation_range)) self._target_shape = np.array(target_shape) self._orig_shape = np.array(orig_shape) self._batch_size = batch_size self._unit = np.flip(self._target_shape - 1, axis=0).reshape([2,1]) / np.flip(self._orig_shape - 1, axis=0).reshape([1,2]) self._relative_scale = relative_scale self._relative_angle = tuple(map(lambda x : x / 180 * math.pi * relative_angle, angle_range) ) self._relative_translation = (-relative_translation * 2, relative_translation * 2) self._aspect_range = aspect_range def _get_relative_transform(self, F): aspect_ratio = (self._target_shape[0] - 1) / (self._target_shape[1] - 1) rotation = F.random.uniform(*self._relative_angle, shape=(self._batch_size)) scale = F.random.uniform(*self._relative_scale, shape=(self._batch_size)) affine_params = [scale * rotation.cos(), scale * -rotation.sin() * aspect_ratio, F.zeros_like(scale), scale * rotation.sin() / aspect_ratio, scale * rotation.cos(), F.zeros_like(scale), F.zeros_like(scale), F.zeros_like(scale), F.ones_like(scale)] affine = F.reshape(F.stack(*affine_params, axis=1), [0, 3, 3]) return affine def hybrid_forward(self, F, img1, img2): rotation = F.random.uniform(*self._angle_range, shape=(self._batch_size)) scale = F.random.uniform(*self._scale_range, shape=(self._batch_size)) if self._aspect_range is not None: aspect_ratio = F.random.uniform(*self._aspect_range, shape=(self._batch_size)) else: aspect_ratio = 1 pad_x, pad_y = 1 - scale * self._unit[0, 0], 1 - scale * self._unit[1, 1] translation_x = F.random.uniform(-1, 1, shape=(self._batch_size,)) * pad_x + F.random.uniform(*self._translation_range, shape=(self._batch_size)) translation_y = F.random.uniform(-1, 1, shape=(self._batch_size,)) * pad_y + F.random.uniform(*self._translation_range, shape=(self._batch_size)) affine_params = [scale * aspect_ratio * rotation.cos() * self._unit[0, 0], scale * aspect_ratio * -rotation.sin() * self._unit[1, 0], translation_x, scale * rotation.sin() * self._unit[0, 1], scale * rotation.cos() * self._unit[1, 1], translation_y] affine_params = F.stack(*affine_params, axis=1) rel_affine = self._get_relative_transform(F) affine_2 = F.reshape(F.batch_dot(F.reshape(affine_params, [0, 2, 3]), rel_affine), [0, 6]) rel_translation = [F.zeros((self._batch_size,)), F.zeros((self._batch_size,)), F.random.uniform(*self._relative_translation, shape=(self._batch_size,)), F.zeros((self._batch_size,)), F.zeros((self._batch_size,)), F.random.uniform(*self._relative_translation, shape=(self._batch_size))] rel_translation = F.stack(*rel_translation, axis = 1) affine_2 = affine_2 + rel_translation grid = F.GridGenerator(data=affine_params, transform_type='affine', target_shape=list(self._target_shape)) img1 = F.BilinearSampler(data=img1, grid=grid) grid_2 = F.GridGenerator(data=affine_2, transform_type='affine', target_shape=list(self._target_shape)) img2 = F.BilinearSampler(data=img2, grid=grid_2) return img1, img2 ''' class ChromaticBrightnessAugmentation(nn.HybridBlock): def __init__(self, brightness = 0.5, batch_size = 1, **kwargs): super().__init__(**kwargs) self.brightness = brightness self.batch_size = batch_size def hybrid_forward(self, F, img): aug = img alpha = 1.0 + F.random.uniform(-self.brightness, self.brightness, shape = (self.batch_size, 1, 1, 1)) aug = F.broadcast_mul(aug, alpha) return aug class ChromaticContrastAugmentation(nn.HybridBlock): def __init__(self, contrast = 0.5, batch_size = 1, **kwargs): super().__init__(**kwargs) self.contrast = contrast self.coefficient = [0.299, 0.587, 0.114] self.batch_size = batch_size def hybrid_forward(self, F, img): aug = img alpha = 1.0 + F.random.uniform(-self.contrast, self.contrast, shape = (self.batch_size, 1, 1, 1)) gray = F.concat(*[img.slice_axis(axis = 1, begin = k, end = k + 1) * self.coefficient[k] for k in range(3)], dim = 1) mean = F.mean(gray, keepdims = True, axis = (1, 2, 3)) gray = 3.0 * (1.0 - alpha) * mean aug = F.broadcast_mul(aug, alpha) aug = F.broadcast_add(aug, gray) return aug ''' class ChromaticSHAugmentation(nn.HybridBlock): def __init__(self, saturation = 0.5, hue = 0.5, batch_size = 1, **kwargs): super().__init__(**kwargs) self.saturation = saturation self.hue = hue self.matrix_yiq = [ [ 0.299, 0.587, 0.114], [ 0.596, -0.274, -0.321], [ 0.211, -0.523, -0.311]] self.matrix_rgb = [ [ 1. , 0.956, 0.621], [ 1. , -0.272, -0.647], [ 1. , -1.107, 1.705]] self.batch_size = batch_size def hybrid_forward(self, F, img): aug = img alpha = 1.0 + F.random.uniform(-self.saturation, self.saturation, shape = (self.batch_size, 1, 1, 1)) theta = F.random.uniform(-self.hue * np.pi, self.hue * np.pi, shape = (self.batch_size, 1, 1, 1)) su = alpha * F.cos(theta) sw = alpha * F.sin(theta) matrix = [ [0.299 + 0.701 * su + 0.168 * sw, 0.587 - 0.587 * su + 0.330 * sw, 0.114 - 0.114 * su - 0.497 * sw], [0.299 - 0.299 * su - 0.328 * sw, 0.587 + 0.413 * su + 0.035 * sw, 0.114 - 0.114 * su + 0.292 * sw], [0.299 - 0.300 * su + 1.250 * sw, 0.587 - 0.588 * su - 1.050 * sw, 0.114 + 0.886 * su - 0.203 * sw]] aug = F.concat(*[sum([F.broadcast_mul(aug.slice_axis(axis = 1, begin = j, end = j + 1), matrix[i][j]) for j in range(3)]) for i in range(3)], dim = 1) return aug ''' class ChromaticGammaAugmentation(nn.HybridBlock): def __init__(self, gamma = (0.7, 1.5), batch_size = 1, **kwargs): super().__init__(**kwargs) self.gamma_min, self.gamma_max = gamma self.batch_size = batch_size def hybrid_forward(self, F, img): aug = img alpha = F.random.uniform(self.gamma_min, self.gamma_max, shape = (self.batch_size, 1, 1, 1)) aug = F.broadcast_power(aug, alpha) return aug class ChromaticEigenAugmentation(nn.HybridBlock): def __init__(self, batch_size = 1, **kwargs): super().__init__(**kwargs) self.batch_size = batch_size def hybrid_forward(self, F, img): spin_angle = F.random.uniform(low = -np.pi, high = np.pi, shape = (self.batch_size, 3, 1, 1)) cos_ = [F.cos(spin_angle).slice_axis(axis = 1, begin = k, end = k + 1) for k in range(3)] sin_ = [F.sin(spin_angle).slice_axis(axis = 1, begin = k, end = k + 1) for k in range(3)] spin_matrix = [ [ cos_[0] * cos_[1], sin_[1] * cos_[2] + sin_[0] * cos_[1] * sin_[2], sin_[1] * sin_[2] - sin_[0] * cos_[1] * cos_[2]], [- cos_[0] * sin_[1], cos_[1] * cos_[2] - sin_[0] * sin_[1] * sin_[2], cos_[1] * sin_[2] + sin_[0] * sin_[1] * cos_[2]], [ sin_[0] , - cos_[0] * sin_[2] , cos_[0] * cos_[2] ]] aug = F.concat(*[sum([F.broadcast_mul(img.slice_axis(axis = 1, begin = j, end = j + 1), spin_matrix[i][j]) for j in range(3)]) for i in range(3)], dim = 1) return aug class ChromaticComposeAugmentation(nn.Block): def __init__(self, brightness = 0.2, contrast = 0.5, saturation = 0.5, hue = 0.5, gamma = (0.7, 1.5), batch_size = 1, **kwargs): super().__init__(**kwargs) self.brightness = brightness self.contrast = contrast self.saturation = saturation self.hue = hue self.gamma = gamma self.batch_size = batch_size self.aug_brightness = ChromaticBrightnessAugmentation(self.brightness, self.batch_size) self.aug_contrast = ChromaticContrastAugmentation(self.contrast, self.batch_size) self.aug_sh = ChromaticSHAugmentation(self.saturation, self.hue, self.batch_size) self.augs = [self.aug_brightness, self.aug_contrast, self.aug_sh] self.Gamma = ChromaticGammaAugmentation(self.gamma, self.batch_size) def forward(self, img1, img2): aug = nd.concat(img1, img2, dim = 2) augs = random.sample(self.augs, 3) for aug_op in augs: aug = aug_op(aug) aug = aug.clip(0, 1) aug = self.Gamma(aug) return nd.split(aug, axis = 2, num_outputs = 2) ''' class ColorAugmentation(nn.HybridBlock): def __init__(self, contrast_range, brightness_sigma, channel_range, batch_size, shape, noise_range, saturation, hue, gamma_range = None, eigen_aug = False, **kwargs): super().__init__(**kwargs) self._contrast_range = contrast_range self._brightness_sigma = brightness_sigma self._channel_range = channel_range self._batch_size = batch_size self._shape = shape self._noise_range = noise_range self._gamma_range = gamma_range self._eigen_aug = eigen_aug self._saturation = saturation self._hue = hue def hybrid_forward(self, F, img1, img2): contrast = F.random.uniform(*self._contrast_range, shape=(self._batch_size, 1, 1, 1)) + 1 brightness = F.random.normal(scale=self._brightness_sigma, shape=(self._batch_size, 1, 1, 1)) channel = F.random.uniform(*self._channel_range, shape=(self._batch_size, 3, 1, 1)) noise_sigma = F.random.uniform(*self._noise_range) if self._gamma_range is not None: gamma = F.random.uniform(*self._gamma_range, shape = (self._batch_size, 1, 1, 1)) contrast = contrast.repeat(repeats=3, axis=1) brightness = brightness.repeat(repeats=3, axis=1) alpha = 1.0 + F.random.uniform(-self._saturation, self._saturation, shape = (self._batch_size, 1, 1, 1)) theta = F.random.uniform(-self._hue * np.pi, self._hue * np.pi, shape = (self._batch_size, 1, 1, 1)) su = alpha * F.cos(theta) sw = alpha * F.sin(theta) sh_matrix = [ [0.299 + 0.701 * su + 0.168 * sw, 0.587 - 0.587 * su + 0.330 * sw, 0.114 - 0.114 * su - 0.497 * sw], [0.299 - 0.299 * su - 0.328 * sw, 0.587 + 0.413 * su + 0.035 * sw, 0.114 - 0.114 * su + 0.292 * sw], [0.299 - 0.300 * su + 1.250 * sw, 0.587 - 0.588 * su - 1.050 * sw, 0.114 + 0.886 * su - 0.203 * sw]] if self._eigen_aug: spin_angle = F.random.uniform(low = -np.pi, high = np.pi, shape = (self._batch_size, 3, 1, 1)) cos_ = [F.cos(spin_angle).slice_axis(axis = 1, begin = k, end = k + 1) for k in range(3)] sin_ = [F.sin(spin_angle).slice_axis(axis = 1, begin = k, end = k + 1) for k in range(3)] spin_matrix = [ [ cos_[0] * cos_[1], sin_[1] * cos_[2] + sin_[0] * cos_[1] * sin_[2], sin_[1] * sin_[2] - sin_[0] * cos_[1] * cos_[2]], [-cos_[0] * sin_[1], cos_[1] * cos_[2] - sin_[0] * sin_[1] * sin_[2], cos_[1] * sin_[2] + sin_[0] * sin_[1] * cos_[2]], [ sin_[0] ,-cos_[0] * sin_[2] , cos_[0] * cos_[2] ]] ret = [] for img in (img1, img2): aug = img aug = F.concat(*[sum([F.broadcast_mul(aug.slice_axis(axis = 1, begin = j, end = j + 1), sh_matrix[i][j]) for j in range(3)]) for i in range(3)], dim = 1) noise = F.random.normal(scale=1, shape=(self._batch_size, 3) + tuple(self._shape)) aug = aug + noise * noise_sigma mean = F.mean(aug, keepdims=True, axis=(2,3)) aug = F.broadcast_minus(aug, mean) aug = F.broadcast_mul(aug, contrast * channel) if self._eigen_aug: aug = F.concat(*[sum([F.broadcast_mul(aug.slice_axis(axis = 1, begin = j, end = j + 1), spin_matrix[i][j]) for j in range(3)]) for i in range(3)], dim = 1) aug = F.broadcast_add(aug, mean * channel + brightness) aug = F.clip(aug, 0, 1) if self._gamma_range is not None: aug = F.broadcast_power(aug, F.exp(gamma)) ret.append(aug) return ret class GeometryAugmentation(nn.HybridBlock): def __init__(self, angle_range, zoom_range, translation_range, target_shape, orig_shape, batch_size, aspect_range = None, relative_angle=None, relative_scale=None, relative_translation=None): super().__init__() self._angle_range = tuple(map(lambda x : x / 180 * math.pi, angle_range) ) self._scale_range = zoom_range try: translation_range = tuple(translation_range) if len(translation_range) != 2: raise ValueError('expect translation range to have shape [2,], but got {}'.format(translation_range)) except TypeError: translation_range = (-translation_range, translation_range) self._translation_range = tuple(map(lambda x : x * 2, translation_range)) self._target_shape = np.array(target_shape) self._orig_shape = np.array(orig_shape) self._batch_size = batch_size self._unit = np.flip(self._target_shape - 1, axis=0).reshape([2,1]) / np.flip(self._orig_shape - 1, axis=0).reshape([1,2]) self._relative = relative_angle is not None if self._relative: self._relative_scale = relative_scale self._relative_angle = tuple(map(lambda x : x / 180 * math.pi * relative_angle, angle_range) ) self._relative_translation = tuple(map(lambda x: x * relative_translation, self._translation_range)) if relative_translation is not None else None self._aspect_range = aspect_range def _get_relative_transform(self, F): aspect_ratio = (self._target_shape[0] - 1) / (self._target_shape[1] - 1) rotation = F.random.uniform(*self._relative_angle, shape=(self._batch_size)) scale = F.random.uniform(*self._relative_scale, shape=(self._batch_size)) affine_params = [scale * rotation.cos(), scale * -rotation.sin() * aspect_ratio, F.zeros_like(scale), scale * rotation.sin() / aspect_ratio, scale * rotation.cos(), F.zeros_like(scale), F.zeros_like(scale), F.zeros_like(scale), F.ones_like(scale)] affine = F.reshape(F.stack(*affine_params, axis=1), [0, 3, 3]) inverse = F.stack( rotation.cos() / scale, rotation.sin() / scale, -rotation.sin() / scale, rotation.cos() / scale, axis=1 ) inverse = F.reshape(inverse, [0, 2, 2]) return affine, inverse def hybrid_forward(self, F, img1, img2, flow, mask): rotation = F.random.uniform(*self._angle_range, shape=(self._batch_size)) aspect_ratio = F.random.uniform(*self._aspect_range, shape=(self._batch_size)) if self._aspect_range is not None else 1 scale = F.random.uniform(*self._scale_range, shape=(self._batch_size)) os = (self._orig_shape[0] - 1, self._orig_shape[1] - 1) ts = (self._target_shape[0] - 1, self._target_shape[1] - 1) abs_rotation = F.abs(rotation) scale = F.minimum(scale, os[1] / (aspect_ratio * (ts[0] * F.sin(abs_rotation) + ts[1] * F.cos(abs_rotation)))) scale = F.minimum(scale, os[0] / (ts[0] * F.cos(abs_rotation) + ts[1] * F.sin(abs_rotation))) pad_x, pad_y = 1 - scale * self._unit[0, 0], 1 - scale * self._unit[1, 1] translation_x = F.random.uniform(-1, 1, shape=(self._batch_size,)) * pad_x + F.random.uniform(*self._translation_range, shape=(self._batch_size)) translation_y = F.random.uniform(-1, 1, shape=(self._batch_size,)) * pad_y + F.random.uniform(*self._translation_range, shape=(self._batch_size)) affine_params = [scale * aspect_ratio * rotation.cos() * self._unit[0, 0], scale * aspect_ratio * -rotation.sin() * self._unit[1, 0], translation_x, scale * rotation.sin() * self._unit[0, 1], scale * rotation.cos() * self._unit[1, 1], translation_y] affine_params = F.stack(*affine_params, axis=1) affine_inverse = F.stack( rotation.cos() / (scale * aspect_ratio), rotation.sin() / (scale * aspect_ratio), -rotation.sin() / scale, rotation.cos() / scale, axis=1 ) linv = F.reshape(affine_inverse, [0, 2, 2]) mask = mask.broadcast_like(flow.slice_axis(axis = 1, begin = 0, end = 1)) rel_affine, rel_inverse = self._get_relative_transform(F) affine_2 = F.reshape(F.batch_dot(F.reshape(affine_params, [0, 2, 3]), rel_affine), [0, 6]) if self._relative_translation is not None: rel_translation = F.random.uniform(*self._relative_translation, shape=(self._batch_size, 2, 1, 1)) rel_scale = F.concat(F.ones([self._batch_size, 1, 1, 1]) * (self._orig_shape[1] - 1) / 2, F.ones([self._batch_size, 1, 1, 1]) * (self._orig_shape[0] - 1) / 2, dim=1) flow = F.broadcast_minus(flow, rel_translation * rel_scale) concat_img = F.concat(img1, mask, F.broadcast_mul(flow, mask), dim=1) grid = F.GridGenerator(data=affine_params, transform_type='affine', target_shape=list(self._target_shape)) force_translation = F.maximum(grid.max(axis=(2, 3), keepdims=True) - 1, 0) + F.minimum(grid.min(axis=(2, 3), keepdims=True) + 1, 0) grid = F.broadcast_minus(grid, force_translation) grid = grid.clip(-1, 1) concat_img = F.BilinearSampler(data=concat_img, grid=grid) img1 = F.slice_axis(concat_img, axis=1, begin=0, end=3) mask = F.slice_axis(concat_img, axis=1, begin=3, end=4) flow = F.slice_axis(concat_img, axis=1, begin=4, end=6) flow = F.broadcast_div(flow, F.maximum(mask, 1e-8)) # relative grid_2 = F.GridGenerator(data=affine_2, transform_type='affine', target_shape=list(self._target_shape)) grid_2 = F.broadcast_minus(grid_2, force_translation) if self._relative_translation is not None: grid_2 = F.broadcast_add(grid_2, rel_translation) img2 = F.BilinearSampler(data=img2, grid=grid_2) inverse_2 = F.batch_dot(rel_inverse, linv) flow = F.reshape_like(F.batch_dot(inverse_2, F.reshape(flow, (0, 0, -3))), flow) scale = F.stack(F.ones([self._batch_size]) * (self._target_shape[1] - 1) / 2, F.zeros([self._batch_size]), F.zeros([self._batch_size]), F.ones([self._batch_size]) * (self._target_shape[0] - 1) / 2, axis=1) scale = F.reshape(scale, [0, 2, 2]) I = F.reshape(F.one_hot(F.arange(0, 2), depth=2), [1, 2, 2]) grid = F.GridGenerator(data=F.reshape(F.one_hot(F.arange(0, 2), depth=3), [1, 6]), transform_type='affine', target_shape=list(self._target_shape)) grid = F.reshape(F.repeat(grid, axis=0, repeats=self._batch_size), [0, 0, -3]) factor = F.batch_dot(F.broadcast_minus(rel_inverse, I), scale) flow = flow + F.reshape_like(F.batch_dot(factor, grid), flow) return img1, img2, flow, mask
python
from __future__ import absolute_import import os import sys import pytest from collections import defaultdict myPath = os.path.abspath(os.getcwd()) sys.path.insert(0, myPath) from salt.exceptions import ArgumentValueError import hubblestack.extmods.fdg.process class TestProcess(): """ Class used to test the functions in ``process.py`` """ def test__compare_invalidComp_raiseException(self): """ Test that given invalid ``comp``, the function raises an ArgumentValueError exception """ with pytest.raises(ArgumentValueError) as e_info: hubblestack.extmods.fdg.process._compare('foo', 1, 2) def test__compare_geCompt_validReturn(self): """ Test that given correct values, the function outputs the correct result """ # ge = greater equal ret = hubblestack.extmods.fdg.process._compare('ge', 1, 2) assert ret is False ret = hubblestack.extmods.fdg.process._compare('ge', 2, 2) assert ret is True ret = hubblestack.extmods.fdg.process._compare('ge', 2, 1) assert ret is True # gt = greater than ret = hubblestack.extmods.fdg.process._compare('gt', 10, 2) assert ret is True ret = hubblestack.extmods.fdg.process._compare('gt', 1, 2) assert ret is False ret = hubblestack.extmods.fdg.process._compare('gt', 2, 2) assert ret is False # lt = lower than ret = hubblestack.extmods.fdg.process._compare('lt', 1, 2) assert ret is True ret = hubblestack.extmods.fdg.process._compare('lt', 2, 2) assert ret is False ret = hubblestack.extmods.fdg.process._compare('lt', 2, 1) assert ret is False # le = lower equal ret = hubblestack.extmods.fdg.process._compare('le', 1, 2) assert ret is True ret = hubblestack.extmods.fdg.process._compare('le', 2, 2) assert ret is True ret = hubblestack.extmods.fdg.process._compare('le', 2, 1) assert ret is False # eq = equal ret = hubblestack.extmods.fdg.process._compare('eq', 1, 2) assert ret is False ret = hubblestack.extmods.fdg.process._compare('eq', 1, 1) assert ret is True ret = hubblestack.extmods.fdg.process._compare('eq', 2, 1) assert ret is False # ne = not equal ret = hubblestack.extmods.fdg.process._compare('ne', 1, 2) assert ret is True ret = hubblestack.extmods.fdg.process._compare('ne', 2, 1) assert ret is True ret = hubblestack.extmods.fdg.process._compare('ne', 1, 1) assert ret is False def test__filterDict_invalidFilterRules_returnNone(self): """ Test that given invalid ``filter_values``, the function returns None """ expected_ret = None ret = hubblestack.extmods.fdg.process._filter_dict( {1: 'a', 2: 'b'}, False, {'invalid': 1, 'data': 2}) assert expected_ret == ret def test__filterDict_filterKeysValidFilterRules_returnFilteredDict(self): """ Test that given valid ``filter_values``, the function correctly filters a dict by keys """ expected_ret = {2: 'b', 4: 'd'} ret = hubblestack.extmods.fdg.process._filter_dict( {1: 'a', 2: 'b', 3: 'c', 4: 'd'}, False, {'gt': 1, 'le': 4, 'ne': 3}) assert expected_ret == ret expected_ret = {'a': 1, 'b': 2} ret = hubblestack.extmods.fdg.process._filter_dict( {'a': 1, 'b': 2, 'c': 3, 'd': 4}, False, {'ge': 'a', 'lt': 'd', 'ne': 'c'}) assert expected_ret == ret def test__filterDict_filterValuesValidFilterRules_returnFilteredDict(self): """ Test that given valid ``filter_values``, the function correctly filters a dict by values """ expected_ret = {'b': 2, 'd': 4} ret = hubblestack.extmods.fdg.process._filter_dict( {'a': 1, 'b': 2, 'c': 3, 'd': 4}, True, {'gt': 1, 'le': 4, 'ne': 3}) assert expected_ret == ret expected_ret = {1: 'a', 2: 'b'} ret = hubblestack.extmods.fdg.process._filter_dict( {1: 'a', 2: 'b', 3: 'c', 4: 'd'}, True, {'ge': 'a', 'lt': 'd', 'ne': 'c'}) assert expected_ret == ret def test__filterDict_emptyFilterRules_returnUnfilteredDict(self): """ Test that given empty ``filter_rules``, the function leaves the dict intact """ expected_ret = {1: 'a', 2: 'b'} ret = hubblestack.extmods.fdg.process._filter_dict({1: 'a', 2: 'b'}, True, {}) assert expected_ret == ret def test_filterDict_invalidDict_emptyReturn(self): """ Test that given invalid types for ``starting_dict`` or ``chained``, the function returns False and None """ expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.filter_dict( starting_dict=[1, 2, 3], chained={1: 'a', 2: 'b'}) assert expected_status == status assert expected_ret == ret status, ret = hubblestack.extmods.fdg.process.filter_dict( starting_dict={1: 'a', 2: 'b'}, chained=[1, 2]) assert expected_status == status assert expected_ret == ret def test_filterDict_validDictFilterKeys_returnFilteredDict(self): """ Test that given correct input, the function correctly filters by keys """ expected_status, expected_ret = True, {1: 'a', 2: 'b', 4: 'd'} status, ret = hubblestack.extmods.fdg.process.filter_dict( starting_dict={1: 'a', 2: 'b', 3: 'c'}, chained={1: 'b', 3: 'd', 4: 'd'}, ge=1, ne=3) assert expected_status == status assert expected_ret == ret def test_filterDict_validDictFilterValues_returnFilteredDict(self): """ Test that given correct input, the function correctly filters by values """ expected_status, expected_ret = True, {3: 'c', 4: 'd'} status, ret = hubblestack.extmods.fdg.process.filter_dict( starting_dict={1: 'a', 2: 'b', 3: 'c'}, filter_values=True, chained={1: 'b', 3: 'd', 4: 'd'}, gt='a', ne='b', le='d') assert expected_status == status assert expected_ret == ret def test__filter_invalidComp_returnNone(self): """ Test that given invalid input, the function returns None """ expected_ret = None ret = hubblestack.extmods.fdg.process._filter([1, 2, 3], {'foo': 1}) assert expected_ret == ret def test__filter_validArguments_returnFilteredSeq(self): """ Test that given valid arguments of different types, the function returns the filtered sequence """ # list expected_ret = [2, 4] seq = [1, 2, 3, 4] ret = hubblestack.extmods.fdg.process._filter(seq, {"gt": 1, "ne": 3, "le": 4}) assert expected_ret == ret # set seq = set(seq) ret = hubblestack.extmods.fdg.process._filter(seq, {"gt": 1, "ne": 3, "le": 4}) assert expected_ret == ret # string seq = "test string" expected_ret = ['e', 's', ' ', 's', 'r', 'i', 'n', 'g'] ret = hubblestack.extmods.fdg.process._filter(seq, {"ne": 't'}) assert expected_ret == ret def test_filterSeq_invalidSeq_returnNone(self): """ Test that given invalid input, the function returns None """ # invalid ``starting_seq`` expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.filter_seq( starting_seq=1, chained=[2, 3, 4], ge=1, lt=4) assert expected_status == status assert expected_ret == ret # invalid ``chained`` status, ret = hubblestack.extmods.fdg.process.filter_seq( starting_seq=[1, 2], chained=4, ge=1, lt=4) assert expected_status == status assert expected_ret == ret def test_filterSeq_validSeq_returnFilteredSeq(self): """Test that given valid input of different types, the function returns True and the filtered sequence """ # list expected_status, expected_ret = True, [2, 4] chained = [1, 2] seq = [3, 4] status, ret = hubblestack.extmods.fdg.process.filter_seq( starting_seq=seq, chained=chained, gt=1, ne=3, le=4) assert expected_ret == ret assert expected_status == status # set expected_status, expected_ret = True, [3] seq = set(seq) chained = set(chained) status, ret = hubblestack.extmods.fdg.process.filter_seq( starting_seq=seq, chained=chained, ge=1, ne=2, lt=4, eq=3) assert expected_ret == ret assert expected_status == status # string expected_status, expected_ret = True, ['e', 's', ' ', 's', 'r', 'i', 'n', 'g'] seq = 'test {}' chained = 'string' status, ret = hubblestack.extmods.fdg.process.filter_seq( starting_seq=seq, chained=chained, ne='t') assert expected_ret == ret assert expected_status == status def test_getIndex_invalidArguments_returnNone(self): """ Test that given invalid arguments, the function returns None """ # invalid ``chained`` expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.get_index( starting_list=[1, 2, 3]) assert expected_status == status assert expected_ret == ret # index out of range expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.get_index( index=4, chained=[1, 2, 3]) assert expected_status == status assert expected_ret == ret # invalid ``chained`` type expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.get_index( chained=set([1, 2, 3])) assert expected_status == status assert expected_ret == ret def test_getIndex_validData_returnValue(self): """ Test that given valid arguments, the function extracts the correct value """ expected_status = True status, ret = hubblestack.extmods.fdg.process.get_index( index=-1, starting_list=[1, 2], chained=[3, 4]) assert expected_status == status assert ret == 2 status, ret = hubblestack.extmods.fdg.process.get_index( starting_list=[1, 2], chained=[3, 4]) assert expected_status == status assert ret == 3 status, ret = hubblestack.extmods.fdg.process.get_index( index=2, starting_list=[1, 2], chained=[3, 4]) assert expected_status == status assert ret == 1 def test_getKey_invalidArguments_returnNone(self): """ Test that given invalid arguments, the function returns None """ # invalid ``chained`` type expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.get_key( key='a', chained=['a', 'b', 'c']) assert expected_status == status assert expected_ret == ret # invalid key expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.get_key( key='d', chained=['a', 'b', 'c']) assert expected_status == status assert expected_ret == ret def test_getKey_validKey_returnValue(self): """ Test that given valid arguments, the function returns the correct value """ expected_status, expected_ret = True, 1 status, ret = hubblestack.extmods.fdg.process.get_key( key='b', starting_dict={'b': 1, 'c': 2}, chained={'a': 1, 'b': 2}) assert expected_status == status assert expected_ret == ret def test_join_invalidArgumentType_returnNone(self): """ Test that given invalid arguments, the function returns None """ # invalid ``chained`` expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.join( chained=1) assert expected_status == status assert expected_ret == ret # invalid ``sep`` status, ret = hubblestack.extmods.fdg.process.join( sep=[1, 2], chained=['foo', 'bar']) assert expected_status == status assert expected_ret == ret def test_join_validArguments_returnString(self): """ Test that given valid arguments, the function will return the joined string """ # no ``sep`` expected_status, expected_ret = True, 'testwordstogether' status, ret = hubblestack.extmods.fdg.process.join( words='together', chained=['test', 'words']) assert expected_status == status assert expected_ret == ret # valid ``sep`` status, ret = hubblestack.extmods.fdg.process.join( words=['words', 'together'], sep='-', chained=['test', 'more']) assert expected_status == status assert ret == 'test-more-words-together' def test__sort_invalidSeq_returnNone(self): """ Test that given invalid arguments, the function returns None """ # invalid ``seq`` expected_ret = None ret = hubblestack.extmods.fdg.process._sort( seq=1, desc=True, lexico=False) assert expected_ret == ret # invalid ``desc`` ret = hubblestack.extmods.fdg.process._sort( seq=[2, 1], desc='yes', lexico=False) assert expected_ret == ret # invalid ``lexico`` ret = hubblestack.extmods.fdg.process._sort( seq=[1, 2, 12, 13], desc=False, lexico=True) assert expected_ret == ret def test__sort_validArguments_returnSortedSeq(self): """ Test that given valid arguments, the function correctly sorts them with different parameters """ ret = hubblestack.extmods.fdg.process._sort( seq=['b', 'a', 'Z'], desc=False, lexico=False) assert ret == ['Z', 'a', 'b'] ret = hubblestack.extmods.fdg.process._sort( seq={'a': 1, 'b': 2, 'B': 3}, desc=True, lexico=False) assert ret == ['b', 'a', 'B'] ret = hubblestack.extmods.fdg.process._sort( seq=set(['b', 'A', 'C']), desc=False, lexico=True) assert ret == ['A', 'b', 'C'] def test_sort_invalidArgument_returnNone(self): """ Test that given invalid arguments, the function returns None """ expected_status, expected_ret = False, None # invalid ``chained`` status, ret = hubblestack.extmods.fdg.process.sort(seq=2, chained=1) assert expected_status == status assert expected_ret == ret # invalid ``desc`` status, ret = hubblestack.extmods.fdg.process.sort( chained=[1, 2, 3], desc='yes') assert expected_status == status assert expected_ret == ret # invalid ``lexico`` status, ret = hubblestack.extmods.fdg.process.sort( chained=[1, 2, 3], lexico=True) assert expected_status == status assert expected_ret == ret def test_sort_validArguments_returnSortedSeq(self): """ Test that given valid arguments, the function correctly sorts them with different parameters """ expected_status = True # desc list status, ret = hubblestack.extmods.fdg.process.sort( seq=[1, 2], desc=True, chained=[3]) assert expected_status == status assert ret == [3, 2, 1] # dict status, ret = hubblestack.extmods.fdg.process.sort(chained={2: 'a', 1: 'b', 3: 'c'}) assert expected_status == status assert ret == [1, 2, 3] # desc set status, ret = hubblestack.extmods.fdg.process.sort( seq=['A', 'B'], chained=set(['a', 'b']), desc=True) assert expected_status == status assert ret == ['b', 'a', 'B', 'A'] # lexicographic string status, ret = hubblestack.extmods.fdg.process.sort( seq='A{}B', chained='ab', lexico=True) assert expected_status == status assert ret == ['A', 'a', 'b', 'B'] def test__split_invalidArguments_returnNone(self): """ Test that given invalid arguments, the function returns None """ expected_ret = None ret = hubblestack.extmods.fdg.process._split([1, 2, 3], " ", False) assert ret == expected_ret ret = hubblestack.extmods.fdg.process._split("foo bar", [1, 2, 3], False) assert ret == expected_ret ret = hubblestack.extmods.fdg.process._split([1, 2, 3], " ", True) assert ret == expected_ret ret = hubblestack.extmods.fdg.process._split("foo bar", [1, 2, 3], True) assert ret == expected_ret def test__split_validArguments_returnList(self): """ Test that given valid arguments, the function correctly splits the string into a list """ # simple ``sep`` expected_ret = ['foo', 'bar'] ret = hubblestack.extmods.fdg.process._split("foo bar", " ", False) assert ret == expected_ret # ``sep`` simple regex ret = hubblestack.extmods.fdg.process._split("foo bar", " ", True) assert ret == expected_ret # regex ret = hubblestack.extmods.fdg.process._split("foo bar", "\s+", True) assert ret == expected_ret # invalid ``sep`` ret = hubblestack.extmods.fdg.process._split("foo bar", "?", False) assert ret == ['foo bar'] def test_split_invalidArguments_returnNone(self): """ Test that given invalid arguments, the function returns None """ expected_status, expected_ret = False, None # invalid ``words`` status, ret = hubblestack.extmods.fdg.process.split([1, 2, 3], chained='ab') assert ret == expected_ret assert status == expected_status status, ret = hubblestack.extmods.fdg.process.split({1: 'a', 2: 'b'}, chained='ab') assert ret == expected_ret assert status == expected_status # invalid ``words`` & ``chained`` status, ret = hubblestack.extmods.fdg.process.split(1, chained=12) assert ret == expected_ret assert status == expected_status status, ret = hubblestack.extmods.fdg.process.split('foo bar', regex=True) assert ret == expected_ret assert status == expected_status def test_split_validArguments_returnList(self): """ Test that given valid arguments, the function correctly splits in all scenarios """ # valid regex status, ret = hubblestack.extmods.fdg.process.split( phrase="a1b2c3d", sep="\d+", regex=True) assert status is True assert ret == ['a', 'b', 'c', 'd'] # invalid regex status, ret = hubblestack.extmods.fdg.process.split( phrase="a1b2{}", sep="\d+", regex=False, chained='c3d') assert status is False assert ret == ['a1b2c3d'] # simple sep status, ret = hubblestack.extmods.fdg.process.split( phrase="a1 b2 {}", sep=" ", chained='c3 d') assert status is True assert ret == ['a1', 'b2', 'c3', 'd'] # no sep status, ret = hubblestack.extmods.fdg.process.split( phrase="a1 b2 \n{}", chained='c3 d') assert status is True assert ret == ['a1', 'b2', 'c3', 'd'] def test_dictToList_invalidArguments_returnNone(self): """ Test that given invalid arguments, the function returns None """ expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.dict_to_list( starting_dict={1: 'a'}, chained=[1, 2, 3]) assert status == expected_status assert ret == expected_ret status, ret = hubblestack.extmods.fdg.process.dict_to_list( starting_dict='foo', chained={1: 'a', 2: 'b'}) assert status == expected_status assert ret == expected_ret def test_dictToList_validArguments_returnList(self): """ Test that given valid arguments, the function outputs a valid list """ # flat dict status, ret = hubblestack.extmods.fdg.process.dict_to_list( starting_dict={1: 'a'}, update_chained=False, chained={1: 'b', 2: 'c'}) assert status is True assert ret == [(1, 'b'), (2, 'c')] # nested dict status, ret = hubblestack.extmods.fdg.process.dict_to_list( starting_dict={1: 'a', 3: {1: 'a'}}, chained={1: 'b', 2: 'c'}) assert status is True assert ret == [(1, 'a'), (2, 'c'), (3, {1: 'a'})] # empty dict status, ret = hubblestack.extmods.fdg.process.dict_to_list(chained={}) assert status is False assert ret == [] def test__dictConvertNone_invalidArguments_returnNone(self): """ Test that given invalid arguments, the function returns None """ ret = hubblestack.extmods.fdg.process._dict_convert_none([1, 2, 3]) assert ret == None ret = hubblestack.extmods.fdg.process._dict_convert_none(1) assert ret == None ret = hubblestack.extmods.fdg.process._dict_convert_none(defaultdict()) assert ret == {} def test__dictConvertNone_validArgumentRecursive_returnDict(self): """ Test that given valid arguments, the function converts empty strings to None in all scenarios """ # flat dict ret = hubblestack.extmods.fdg.process._dict_convert_none( {1: "", 2: 'a', 3: "None", 4: None}) assert ret == {1: None, 2: 'a', 3: "None", 4: None} # nested dicts ret = hubblestack.extmods.fdg.process._dict_convert_none( {'a': {'aa': {'aaa': 3, 'bbb': {'bbbb': 4, 'cccc': ''}, 'ccc': ''}, 'bb': ''}, 'b': ''}) assert ret == {'a': {'aa': {'aaa': 3, 'bbb': {'bbbb': 4, 'cccc': None}, 'ccc': None}, 'bb': None}, 'b': None} # nested dicts & seqs ret = hubblestack.extmods.fdg.process._dict_convert_none( {'a': [{'b': ({'c': ['d', {'e': ''}], 'f': ''}, {'g': ''}), 'h': ''}, 'i'], 'j': ''}) assert ret == {'a': [{'b': [{'c': ['d', {'e': None}], 'f': None}, {'g': None}], 'h': None}, 'i'], 'j': None} def test__seqConvertNone_invalidArguments_returnNone(self): """ Test that given invalid arguments, the function returns None """ ret = hubblestack.extmods.fdg.process._seq_convert_none({1: 'a', 2: 'b'}) assert ret == None ret = hubblestack.extmods.fdg.process._seq_convert_none(1) assert ret == None ret = hubblestack.extmods.fdg.process._seq_convert_none(True) assert ret == None def test__seqConvertNone_validArgumentRecursive_returnList(self): """ Test that given valid arguments, the function correctly converts empty strings to None in all scenarios """ # flat seq ret = hubblestack.extmods.fdg.process._seq_convert_none( ['a', {1: ''}, 'b', {1: ''}, 'c']) assert ret == ['a', {1: None}, 'b', {1: None}, 'c'] # nested seq & dict ret = hubblestack.extmods.fdg.process._seq_convert_none( ('a', [{1: '', 2: [3, (4, {1: '', 2: {3: ''}})]}, 'b'], 'c')) assert ret == ['a', [{1: None, 2: [3, [4, {1: None, 2: {3: None}}]]}, 'b'], 'c'] def test_dictConvertNone_invalidArgument_returnNone(self): """ Test that given invalid arguments, the function returns None """ expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.dict_convert_none( chained='foo bar') assert status == expected_status assert ret == expected_ret status, ret = hubblestack.extmods.fdg.process.dict_convert_none( chained={1: 'a'}, starting_seq=[1, 2]) assert status == expected_status assert ret == expected_ret status, ret = hubblestack.extmods.fdg.process.dict_convert_none( chained=[]) assert status == expected_status assert ret == [] def test_dictConvertNone_validArguments_returnDict(self): """ Test that given valid arguments, the function returns a valid dict with None instead of empty strings """ # flat dict status, ret = hubblestack.extmods.fdg.process.dict_convert_none( chained={1: 'a', 2: '', 3: 'b', 4: ''}) assert ret == {1: 'a', 2: None, 3: 'b', 4: None} assert status is True # nested dict & tuple status, ret = hubblestack.extmods.fdg.process.dict_convert_none( chained={'a': [{'b': ({'c': {'e': ''}, 'f': ''}, {'g': ''}), 'h': ''}, 'i']}, starting_seq={'j': ''}) assert status is True assert ret == {'a': [{'b': [{'c': {'e': None}, 'f': None}, {'g': None}], 'h': None}, 'i'], 'j': None} # nested dict, list & tuple status, ret = hubblestack.extmods.fdg.process.dict_convert_none( chained=('a', [{1: '', 2: [3, (4, {1: '', 2: {3: ''}})]}, 'b'], 'c')) assert status is True assert ret == ['a', [{1: None, 2: [3, [4, {1: None, 2: {3: None}}]]}, 'b'], 'c'] # nested dict & list status, ret = hubblestack.extmods.fdg.process.dict_convert_none( chained=['a', {1: ''}, 'b'], starting_seq=[{1: ''}, 'c']) assert status is True assert ret == ['a', {1: None}, 'b', {1: None}, 'c'] def test_printString_invalidArguments_returnNone(self): """ Test that given invalid arguments, the function returns None """ expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.print_string( starting_string=['foo', 'bar']) assert status == expected_status assert ret == expected_ret status, ret = hubblestack.extmods.fdg.process.print_string( starting_string='') assert status == expected_status assert ret == '' def test_printString_validArguments_returnString(self): """ Test that given valid arguments, the function returns the correct string """ status, ret = hubblestack.extmods.fdg.process.print_string( starting_string='foo', chained='bar') assert status is True assert ret == 'foo' status, ret = hubblestack.extmods.fdg.process.print_string( starting_string='foo {}', chained=['b', 'a', 'r']) assert status is True assert ret == "foo ['b', 'a', 'r']" def test__sterilizeDict_invalidArguments_returnNone(self): """ Test that given invalid arguments, the function returns None """ ret = hubblestack.extmods.fdg.process._sterilize_dict( dictionary=[1, 2]) assert ret is None ret = hubblestack.extmods.fdg.process._sterilize_dict( dictionary={}) assert ret == {} ret = hubblestack.extmods.fdg.process._sterilize_dict( dictionary=12) assert ret is None def test__sterilizeDict_validArgumentRecursive_returnDict(self): """ Test tgat given valid arguments, the function correctly removes keys containing values of None """ # flat dict ret = hubblestack.extmods.fdg.process._sterilize_dict( {1: None, 2: 'a'}) assert ret == {2: 'a'} # nested dicts ret = hubblestack.extmods.fdg.process._sterilize_dict( {1: None, 2: {3: {4: None, 5: 'a'}, 6: None, 7: 'b'}, 8: 'c', 9: {10: None}}) assert ret == {2: {3: {5: 'a'}, 7: 'b'}, 8: 'c', 9: {}} # nested dicts & sequences ret = hubblestack.extmods.fdg.process._sterilize_dict( {1: None, 2: {3: [4, {5: None}], 6: {7: ('b', {9: None}), 8: None}}}) assert ret == {2: {3: [4, {}], 6: {7: ['b', {}]}}} def test__sterilizeSeq_invalidArguments_returnNone(self): """ Test that given invalid arguments, the function returns None """ ret = hubblestack.extmods.fdg.process._sterilize_seq( {1: 'a', 2: ['b']}) assert ret == None ret = hubblestack.extmods.fdg.process._sterilize_seq(12) assert ret == None ret = hubblestack.extmods.fdg.process._sterilize_seq([]) assert ret == [] def test__sterilizeSeq_validArgumentRecursive_returnNone(self): """ Test that given valid arguments, the function finds nested dicts and removes keys with values of None """ # flat seq ret = hubblestack.extmods.fdg.process._sterilize_seq( [1, 2, set([1, 2, 1]), (1, 2)]) assert ret == [1, 2, [1, 2], [1, 2]] # nested dicts & seq ret = hubblestack.extmods.fdg.process._sterilize_seq( [{1: None, 2: {3: ({4: None, 5: 'a'}, [None, {6: None, 7: 'b'}]), 8: 'c', 9: {10: None}}}]) assert ret == [{2: {3: [{5: 'a'}, [None, {7: 'b'}]], 8: 'c', 9: {}}}] def test_removeDictNone_invalidArgument_returnNone(self): """ Test that given invalid arguments, the function returns None """ # invalid ``starting_seq`` expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.dict_remove_none( starting_seq=[1, 2, 3], chained={1: 'a', 2: 'b'}) assert status == expected_status assert ret == expected_ret # invalid ``chained`` & valid ``starting_seq`` status, ret = hubblestack.extmods.fdg.process.dict_remove_none( starting_seq=[1, 2, 3], chained="123") assert status == expected_status assert ret == expected_ret # invalid ``chained`` status, ret = hubblestack.extmods.fdg.process.dict_remove_none( chained="123") assert status == expected_status assert ret == expected_ret def test_dictRemoveNone_validArguments_returnSeq(self): """ Test that given valid arguments, the function finds nested dicts and removes keys with values of None """ # flat dict status, ret = hubblestack.extmods.fdg.process.dict_remove_none( chained={1: None, 2: 'a', 3: None, 4: 'b'}) assert status is True assert ret == {2: 'a', 4: 'b'} # flat seq status, ret = hubblestack.extmods.fdg.process.dict_remove_none( chained=[{1: None}, {2: 'a', 3: None}], starting_seq=[5, None, {4: 'b'}]) assert status is True assert ret == [{}, {2: 'a'}, 5, None, {4: 'b'}] # nested sequences & dicts status, ret = hubblestack.extmods.fdg.process.dict_remove_none( starting_seq=[{1: None, 2: {3: ({4: None, 5: 'a'}, [None, {6: None, 7: 'b'}]), 8: 'c'}}], chained=[{9: {10: None, 11: set([1, 2, 1])}}, 11]) assert status is True assert ret == [{9: {11: [1, 2]}}, 11, {2: {3: [{5: 'a'}, [None, {7: 'b'}]], 8: 'c'}}] # nested dicts & sequences status, ret = hubblestack.extmods.fdg.process.dict_remove_none( starting_seq={1: None, 2: {3: ({4: None, 5: 'a'}, [None, {6: None, 7: 'b'}]), 8: 'c'}}, chained={9: {10: None, 11: set([1, 2, 1])}, 11: None}) assert status is True assert ret == {2: {3: [{5: 'a'}, [None, {7: 'b'}]], 8: 'c'}, 9: {11: [1, 2]}} def test_encodeBase64_invalidArguments_emptyReturn(self): """ Test that given invalid arguments, the function returns None """ # invalid `starting_string` expected_status, expected_ret = False, None status, ret = hubblestack.extmods.fdg.process.encode_base64( starting_string=123, chained="foo") assert status == expected_status assert ret == expected_ret status, ret = hubblestack.extmods.fdg.process.encode_base64( starting_string=['a', 'c'], format_chained=False) assert status == expected_status assert ret == expected_ret status, ret = hubblestack.extmods.fdg.process.encode_base64( starting_string='', format_chained=False) assert status == expected_status assert ret == '' def test_encodeBase64_validArguments_returnString(self): """ Test that given valid arguments, the function correctly encodes the string and returns it """ status, ret = hubblestack.extmods.fdg.process.encode_base64( starting_string="foo {}", chained="bar") assert status assert ret == 'Zm9vIGJhcg==' status, ret = hubblestack.extmods.fdg.process.encode_base64( starting_string="foo", chained="bar") assert status assert ret == 'Zm9v' status, ret = hubblestack.extmods.fdg.process.encode_base64( starting_string="foo {}", format_chained=False, chained="bar") assert status assert ret == 'Zm9vIHt9'
python
from pprint import pprint def sort_with_index(arr): arr_with_index = [] for i, item in enumerate(arr): arr_with_index.append((i, item)) arr_with_index.sort(key=lambda it: -it[1]) return arr_with_index def assign(jobs_with_index, n_not_fulfilled_jobs, n_machines): assignment = {} assigned_jobs = set() i = 0 while i < n_machines and i < n_not_fulfilled_jobs: current_job = -1 for job_idx, job in jobs_with_index: if job_idx not in assigned_jobs and job > 0: current_job = job_idx break assigned_jobs.add(current_job) assignment[current_job] = i i += 1 return assignment def level(jobs, machines): n_jobs = len(jobs) n_machines = len(machines) n_not_fulfilled_jobs = len(jobs) assignment = [] t = 0 while n_not_fulfilled_jobs > 0: jobs_with_index = sort_with_index(jobs) current_assignment = assign(jobs_with_index, n_not_fulfilled_jobs, n_machines=n_machines) dt1 = min(jobs[job_id] / machines[machine_id] for job_id, machine_id in current_assignment.items()) assignment_list = sorted(current_assignment.items(), key=lambda assg: assg[0]) dt2 = None for start_idx, (job_i, machine_i) in enumerate(assignment_list): for job_j, machine_j in assignment_list[start_idx + 1:]: if jobs[job_i] <= jobs[job_j] or machines[machine_i] <= machines[machine_j]: continue current_dt2 = (jobs[job_i] - jobs[job_j]) / (machines[machine_i] - machines[machine_j]) if dt2 is None or current_dt2 < dt2: dt2 = current_dt2 dt = min(dt1, dt2) if dt2 is not None else dt1 for job_idx in range(n_jobs): if jobs[job_idx] > 0: if job_idx in current_assignment: machine_idx = current_assignment[job_idx] assignment.append((job_idx, machine_idx, t, t + dt)) jobs[job_idx] -= machines[machine_idx] * dt if jobs[job_idx] <= 0: n_not_fulfilled_jobs -= 1 t += dt return assignment def solve(): jobs = [7, 8, 4, 9, 12, 5, 3, 9, 5, 12, 7, 5, 8] machines = [3, 4, 3, 2] return level(jobs, machines) print(solve()) pprint([(3, 2, 0, 3.0), (4, 0, 0, 3.0), (7, 3, 0, 3.0), (9, 1, 0, 3.0), (0, 2, 3.0, 4.0), (1, 0, 3.0, 4.0), (10, 3, 3.0, 4.0), (12, 1, 3.0, 4.0), (1, 0, 4.0, 5.25), (5, 1, 4.0, 5.25), (8, 2, 4.0, 5.25), (10, 3, 4.0, 5.25), (0, 1, 5.25, 6.25), (2, 2, 5.25, 6.25), (11, 0, 5.25, 6.25), (12, 3, 5.25, 6.25), (4, 0, 6.25, 6.5), (6, 1, 6.25, 6.5), (7, 2, 6.25, 6.5), (10, 3, 6.25, 6.5), (4, 0, 6.5, 6.625), (6, 2, 6.5, 6.625), (7, 1, 6.5, 6.625), (10, 3, 6.5, 6.625), (4, 2, 6.625, 6.75), (7, 3, 6.625, 6.75), (11, 0, 6.625, 6.75), (12, 1, 6.625, 6.75), (4, 3, 6.75, 7.15625), (6, 1, 6.75, 7.15625), (10, 0, 6.75, 7.15625), (11, 2, 6.75, 7.15625), (1, 2, 7.15625, 7.40625), (7, 0, 7.15625, 7.40625), (8, 3, 7.15625, 7.40625), (12, 1, 7.15625, 7.40625), (2, 0, 7.40625, 7.59375), (4, 3, 7.40625, 7.59375), (7, 1, 7.40625, 7.59375), (8, 2, 7.40625, 7.59375), (1, 1, 7.59375, 7.625), (2, 3, 7.59375, 7.625), (10, 0, 7.59375, 7.625), (12, 2, 7.59375, 7.625), (1, 3, 7.625, 7.7265625), (10, 0, 7.625, 7.7265625), (11, 1, 7.625, 7.7265625), (12, 2, 7.625, 7.7265625), (1, 3, 7.7265625, 7.7890625), (2, 0, 7.7265625, 7.7890625), (4, 1, 7.7265625, 7.7890625), (8, 2, 7.7265625, 7.7890625), (2, 0, 7.7890625, 7.8203125), (4, 3, 7.7890625, 7.8203125), (10, 1, 7.7890625, 7.8203125), (12, 2, 7.7890625, 7.8203125), (1, 1, 7.8203125, 7.822916666666667), (2, 0, 7.8203125, 7.822916666666667), (10, 2, 7.8203125, 7.822916666666667), (12, 3, 7.8203125, 7.822916666666667), (1, 1, 7.822916666666667, 7.823784722222222), (2, 0, 7.822916666666667, 7.823784722222222), (12, 2, 7.822916666666667, 7.823784722222222), (1, 1, 7.823784722222222, 7.83203125), (2, 0, 7.823784722222222, 7.83203125), (2, 0, 7.83203125, 7.8515625), (2, 0, 7.8515625, 7.8515625)])
python
''' Copyright 2022 Airbus SAS Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ''' ''' mode: python; py-indent-offset: 4; tab-width: 4; coding: utf-8 ''' import unittest from sos_trades_core.execution_engine.execution_engine import ExecutionEngine from sos_trades_core.execution_engine.sos_discipline import SoSDiscipline class TestNSManager(unittest.TestCase): """ Namespace manager test class """ def setUp(self): ''' Initialize third data needed for testing ''' self.name = 'MyCase' self.exec_eng = ExecutionEngine(self.name) def test_01_nsm_basic(self): nsm = self.exec_eng.ns_manager test = {} ns_key1 = 'ns_ac' ns1_value = 'toto.AC' ns1 = {ns_key1: ns1_value} test.update(ns1) nsm.add_ns_def(ns1) ns_key2 = 'ns_bc' ns2_value = 'toto.bc' ns2 = {ns_key2: 'toto.bc'} test.update(ns2) nsm.add_ns_def(ns2) self.assertEqual(nsm.shared_ns_dict[ns_key1].get_value(), ns1_value) self.assertEqual(nsm.shared_ns_dict[ns_key2].get_value(), ns2_value) # ns already exists with same value nsm.add_ns_def(ns1) self.assertEqual(nsm.shared_ns_dict[ns_key1].get_value(), ns1_value) # ns already exists but different value ns1_val2 = {ns_key1: ns2_value} nsm.add_ns_def(ns1_val2) self.assertEqual(nsm.shared_ns_dict[ns_key1].get_value(), ns2_value) # reset and redo nsm.reset_current_disc_ns() ns2_val1 = {ns_key2: ns1_value} nsm.add_ns_def(ns2_val1) self.assertEqual(nsm.shared_ns_dict[ns_key2].get_value(), ns1_value) def test_02_nsm_check_ns_dict(self): nsm = self.exec_eng.ns_manager nsm.set_current_disc_ns('T.E') ns1 = {'ns_ac': 'AC'} nsm.add_ns_def(ns1) disc = SoSDiscipline('toto', self.exec_eng) nsm.create_disc_ns_info(disc) self.assertEqual(nsm.shared_ns_dict['ns_ac'].get_value(), 'AC') ns_dict = nsm.get_disc_ns_info(disc) self.assertEqual(ns_dict['local_ns'].get_value(), 'T.E.toto') self.assertListEqual(list(ns_dict.keys()), ['local_ns', 'others_ns']) self.assertEqual(ns_dict['others_ns']['ns_ac'].get_value(), 'AC') def test_03_nsm_current_ns_reset(self): nsm = self.exec_eng.ns_manager nsm.reset_current_disc_ns() self.assertEqual(nsm.current_disc_ns, None) def test_04_nsm_change_disc_ns(self): nsm = self.exec_eng.ns_manager nsm.set_current_disc_ns('T.E') nsm.change_disc_ns('..') self.assertEqual(nsm.current_disc_ns, 'T') nsm.change_disc_ns('..') self.assertEqual(nsm.current_disc_ns, None) nsm.change_disc_ns('SA') self.assertEqual(nsm.current_disc_ns, 'SA') nsm.change_disc_ns('toto') self.assertEqual(nsm.current_disc_ns, 'SA.toto')
python
#!python """ ANNOTATE FUNCTIONS WITH TIME AND SPACE COMPLEXITY!!!!! """ def linear_search(array, item): """return the first index of item in array or None if item is not found""" return linear_search_iterative(array, item) # return linear_search_recursive(array, item) def linear_search_iterative(array, item): """Time complexity: O(n) because you iterate through n amount of items in array Space Complexity: O(n) because there are n amount of items""" # loop over all array values until item is found for index, value in enumerate(array): #O(n) if item == value: #O(1) return index # found O(1) return None # not found O(1) def linear_search_recursive(array, item, index=0): """Time complexity: O(n) because you are returning the function continuously until index equals to nth-item """ if len(array) <= index: return index if array[index] == item: return index else: return linear_search_recursive(array, item, index + 1) def binary_search(array, item): """return the index of item in sorted array or None if item is not found""" return binary_search_iterative(array, item) # return binary_search_recursive(array, item) def binary_search_iterative(array, item): """Time Complexity: O(log*n) because you are constantly dividing the length of array by 2 until array length is 1 Space Complexity: O(1) """ left, right = 0, len(array) - 1 if len(array) == 0: return None while left <= right: middle = left + (right - left) // 2 if item == array[middle]: return middle elif item > array[middle]: left = middle + 1 else: right = middle - 1 return None def binary_search_recursive(array, item, left=None, right=None): """Time Complexity: O(log*n) Space Complexity: 0(log*n) recursion call stack space""" # TODO: implement binary search recursively here if left is None and right is None: left, right = 0, len(array) - 1 middle = left + (right - left) // 2 if left > right: return None if array[middle] == item: return middle elif item > array[middle]: return binary_search_recursive(array, item, middle + 1, right) else: return binary_search_recursive(array, item, left, middle - 1)
python
import json import sys import os from time import sleep import wxpy class Greeting: def __init__(self, name, puid, greeting='{name}新年快乐!狗年大吉!'): self.name = name self.puid = puid self._greeting = greeting def toJSON(self): # return str(self.__dict__) return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4, ensure_ascii=False) def items(self): return { 'name': self.name, 'puid': self.puid, 'greeting': self._greeting } @property def greeting(self): return self._greeting.format(name=self.name) class Greetings(list): """docstring for Greetings.""" def __init__(self): super(Greetings, self).__init__() def toJSON(self): return json.dumps(self, default=lambda o: o.items(), sort_keys=True, indent=4, ensure_ascii=False) def fromJSON(self, json_object): self.clear() greetings = json.loads(json_object) for g in greetings: self.append(Greeting(**g)) def send_greeting(bot: wxpy.Bot, greeting: Greeting): people = wxpy.ensure_one(bot.friends().search(puid=greeting.puid)) print("Sending {} to {}……".format(people.name, greeting.greeting)) people.send(greeting.greeting) def test(): g = Greetings() g.append(Greeting('赵奔', '123', '')) g.append(Greeting('赵奔2', '232', '', '{}hao')) json_object = g.toJSON() # print(json_object) g.fromJSON(json_object) # print(g.toJSON()) return g def show_help(): print('Usage:') print(os.path.basename(__file__), end=' ') print('[list] [send]') print(''' list\tgenerate friends list and keep that you want to send send\tsend message to those friends ''') def main(): if len(sys.argv) < 2: show_help() sys.exit(1) if ('list' not in sys.argv) and ('send' not in sys.argv): show_help() sys.exit(1) bot = wxpy.Bot(cache_path=True, console_qr=False) bot.enable_puid() if 'list' in sys.argv: greetings = Greetings() for friend in bot.friends(): greetings.append( Greeting( name=friend.name, puid=friend.puid, ) ) with open('friends.json', 'w', encoding='utf8') as f: f.write(greetings.toJSON()) if 'send' in sys.argv: greetings = Greetings() with open('friends.json', encoding='utf8') as f: greetings.fromJSON(f.read()) for i in greetings: try: send_greeting(bot, i) except Exception as e: print(e) sleep(0.5) wxpy.embed() if __name__ == "__main__": main()
python
# ---------------------------------------------------------------------- # | # | CentOsShell.py # | # | David Brownell <[email protected]> # | 2019-08-30 19:25:23 # | # ---------------------------------------------------------------------- # | # | Copyright David Brownell 2019-22. # | Distributed under the Boost Software License, Version 1.0. # | (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) # | # ---------------------------------------------------------------------- """Contains the CentOsShell object""" import os import CommonEnvironment from CommonEnvironment.Interface import staticderived, override, DerivedProperty from CommonEnvironment.Shell.LinuxShellImpl import LinuxShellImpl # ---------------------------------------------------------------------- _script_fullpath = CommonEnvironment.ThisFullpath() _script_dir, _script_name = os.path.split(_script_fullpath) # ---------------------------------------------------------------------- # <Method '<...>' is abstract in class '<...>' but is not overridden> pylint: disable = W0223 @staticderived class CentOsShell(LinuxShellImpl): """Shell for CentOS systems""" Name = DerivedProperty("CentOS") # ---------------------------------------------------------------------- @staticderived @override class CommandVisitor(LinuxShellImpl.CommandVisitor): try: import distro if int(distro.major_version()) < 7: # ---------------------------------------------------------------------- @classmethod @override def OnSymbolicLink(cls, command): # Older versions of CentOS do not support relative paths return super(CentOsShell.CommandVisitor, cls).OnSymbolicLink( command, no_relative_flag=True, ) except ImportError: pass
python
class ReasonCode: """Default server reason codes.""" # General error GENERAL_ERROR = 1 # General session error SESSION_ERROR = 11 # The session resource is already registered SESSION_REGISTRATION_ERROR = 12 # An authentication error occurred SESSION_AUTHENTICATION_FAILED = 13 # An error occurred while unregistering the session in the server SESSION_UNREGISTER_FAILED = 14 # The required action is invalid for current session state SESSION_INVALID_ACTION_FOR_STATE = 15 # The session negotiation has timed out SESSION_NEGOTIATION_TIMEOUT = 16 # Invalid selected negotiation options SESSION_NEGOTIATION_INVALID_OPTIONS = 17 # Invalid session mode requested SESSION_INVALID_SESSION_MODE_REQUESTED = 18 # General validation error VALIDATION_ERROR = 21 # The envelope document is null VALIDATION_EMPTY_DOCUMENT = 22 # The envelope document MIME type is invalid VALIDATION_INVALID_RESOURCE = 23 # The request status is invalid VALIDATION_INVALID_STATUS = 24 # The request identity is invalid VALIDATION_INVALID_IDENTITY = 25 # The envelope originator or destination is invalid VALIDATION_INVALID_RECIPIENTS = 26 # The command method is invalid VALIDATION_INVALID_METHOD = 27 # The command URI format is invalid VALIDATION_INVALID_URI = 27 # General authorization error AUTHORIZATION_ERROR = 31 # The sender is not authorized to send messages to the message destination AUTHORIZATION_UNAUTHORIZED_SENDER = 32 # The destination doesn't have an active account AUTHORIZATION_DESTINATION_ACCOUNT_NOT_FOUND = 33 # The envelope quota limit has been exceeded AUTHORIZATION_QUOTA_THRESHOLD_EXCEEDED = 34 # General routing error ROUTING_ERROR = 41 # The message destination was not found ROUTING_DESTINATION_NOT_FOUND = 42 # The message destination gateway was not found ROUTING_GATEWAY_NOT_FOUND = 43 # The message destination was not found ROUTING_ROUTE_NOT_FOUND = 44 # General dispatching error DISPATCH_ERROR = 51 # General command processing error COMMAND_PROCESSING_ERROR = 61 # There's no command processor available for process the request COMMAND_RESOURCE_NOT_SUPPORTED = 62 # The command method is not supported COMMAND_METHOD_NOT_SUPPORTED = 63 # The command method has an invalid argument value COMMAND_INVALID_ARGUMENT = 64 # The requested command is not valid for current session mode COMMAND_INVALID_SESSION_MODE = 65 # The command method was not allowed COMMAND_NOT_ALLOWED = 66 # The command resource was not found COMMAND_RESOURCE_NOT_FOUND = 67 # General message processing error MESSAGE_PROCESSING_ERROR = 61 # The message content type is not supported MESSAGE_UNSUPPORTED_CONTENT_TYPE = 71 # General gateway processing error GATEWAY_ERROR = 81 # The content type is not supported by the gateway GATEWAY_CONTENT_TYPE_NOT_SUPPORTED = 82 # The message destination was not found on gateway GATEWAY_DESTINATION_NOT_FOUND = 83 # The functionality is not supported by the gateway GATEWAY_NOT_SUPPORTED = 84 # General application processing error APPLICATION_ERROR = 101
python
from django.db import models from subscribers import mailchimp class AbstractSubscriber(models.Model): email = models.EmailField(blank=True, null=True) created_on = models.DateField(auto_now_add=True) objects = models.Manager() class Meta: abstract = True def __str__(self): return self.email def clean(self): if self.email is not None: klass = mailchimp.MailChimp() class EmailSubscriber(AbstractSubscriber): """People who subscribed to the website""" pass
python
from possum import * spec = possum() spec._generateParams(N=30000, fluxMin=0.1, noiseMax=0.2, pcomplex=0.5, seed=923743) spec._simulateNspec(save=True, dir='data/train/V2/', timeit=True)
python
from django.db import models from djangae.tasks.deferred import defer from djangae.test import TestCase, TaskFailedError def test_task(*args, **kwargs): pass def assert_cache_wiped(instance): field = DeferModelA._meta.get_field("b") assert(field.get_cached_value(instance, None) is None) class DeferModelA(models.Model): b = models.ForeignKey("DeferModelB", on_delete=models.CASCADE) class Meta: app_label = "djangae" class DeferModelB(models.Model): class Meta: app_label = "djangae" class DeferTests(TestCase): def test_wipe_related_caches(self): b = DeferModelB.objects.create() a = DeferModelA.objects.create(b=b) a.b # Make sure we access it cache_name = DeferModelA._meta.get_field("b").get_cache_name() self.assertTrue(getattr(a, cache_name)) defer(assert_cache_wiped, a) # Should raise an assertion error if the cache existed try: self.process_task_queues() except TaskFailedError as e: raise e.original_exception # Should not have wiped the cache for us! self.assertIsNotNone(getattr(a, cache_name, None)) def test_queues_task(self): initial_count = self.get_task_count() defer(test_task) self.assertEqual(self.get_task_count(), initial_count + 1)
python
# Copyright 2019 Google LLC. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Tests for tfx.components.infra_validator.model_server_clients.tensorflow_serving_client.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from typing import Any, Dict, Text from unittest import mock import grpc import tensorflow as tf from tfx.components.infra_validator import error_types from tfx.components.infra_validator import types from tfx.components.infra_validator.model_server_clients import tensorflow_serving_client from google.protobuf import json_format from tensorflow_serving.apis import classification_pb2 from tensorflow_serving.apis import get_model_status_pb2 from tensorflow_serving.apis import regression_pb2 def _make_response( payload: Dict[Text, Any]) -> get_model_status_pb2.GetModelStatusResponse: result = get_model_status_pb2.GetModelStatusResponse() json_format.ParseDict(payload, result) return result class TensorflowServingClientTest(tf.test.TestCase): def setUp(self): super(TensorflowServingClientTest, self).setUp() self.model_stub_patcher = mock.patch('tensorflow_serving.apis.model_service_pb2_grpc.ModelServiceStub') # pylint: disable=line-too-long self.model_stub_cls = self.model_stub_patcher.start() self.model_stub = self.model_stub_cls.return_value self.prediction_stub_patcher = mock.patch('tensorflow_serving.apis.prediction_service_pb2_grpc.PredictionServiceStub') # pylint: disable=line-too-long self.prediction_stub_cls = self.prediction_stub_patcher.start() self.prediction_stub = self.prediction_stub_cls.return_value def tearDown(self): super(TensorflowServingClientTest, self).tearDown() self.model_stub_patcher.stop() self.prediction_stub_patcher.stop() def testGetModelState_ReturnsReady_IfAllAvailable(self): # Prepare stub and client. self.model_stub.GetModelStatus.return_value = _make_response({ 'model_version_status': [ {'state': 'AVAILABLE'}, {'state': 'AVAILABLE'}, {'state': 'AVAILABLE'} ] }) client = tensorflow_serving_client.TensorFlowServingClient( 'localhost:1234', 'a_model_name') # Call. result = client._GetServingStatus() # Check result. self.assertEqual(result, types.ModelServingStatus.READY) def testGetModelState_ReturnsNotReady_IfAnyStateNotAvailable(self): # Prepare stub and client. self.model_stub.GetModelStatus.return_value = _make_response({ 'model_version_status': [ {'state': 'AVAILABLE'}, {'state': 'AVAILABLE'}, {'state': 'LOADING'} ] }) client = tensorflow_serving_client.TensorFlowServingClient( 'localhost:1234', 'a_model_name') # Call. result = client._GetServingStatus() # Check result. self.assertEqual(result, types.ModelServingStatus.NOT_READY) def testGetModelState_ReturnsUnavailable_IfAnyStateEnded(self): # Prepare stub and client. self.model_stub.GetModelStatus.return_value = _make_response({ 'model_version_status': [ {'state': 'AVAILABLE'}, {'state': 'AVAILABLE'}, {'state': 'END'} ] }) client = tensorflow_serving_client.TensorFlowServingClient( 'localhost:1234', 'a_model_name') # Call. result = client._GetServingStatus() # Check result. self.assertEqual(result, types.ModelServingStatus.UNAVAILABLE) def testGetModelState_ReturnsNotReady_IfEmptyState(self): # Prepare stub and client. self.model_stub.GetModelStatus.return_value = _make_response({ 'model_version_status': [] # Empty }) client = tensorflow_serving_client.TensorFlowServingClient( 'localhost:1234', 'a_model_name') # Calls result = client._GetServingStatus() # Check result. self.assertEqual(result, types.ModelServingStatus.NOT_READY) def testGetModelState_ReturnsNotReady_IfServerUnavailable(self): # Prepare stub and client. self.model_stub.GetModelStatus.side_effect = grpc.RpcError client = tensorflow_serving_client.TensorFlowServingClient( 'localhost:1234', 'a_model_name') # Call. result = client._GetServingStatus() # Check result. self.assertEqual(result, types.ModelServingStatus.NOT_READY) def testIssueRequests_NoErrorIfSucceeded(self): # Prepare requests and client. r1 = classification_pb2.ClassificationRequest() r2 = classification_pb2.ClassificationRequest() r3 = regression_pb2.RegressionRequest() client = tensorflow_serving_client.TensorFlowServingClient( 'localhost:1234', 'a_model_name') # Call. client.SendRequests([r1, r2, r3]) # Check calls self.prediction_stub.Classify.assert_called_with(r1) self.prediction_stub.Classify.assert_called_with(r2) self.prediction_stub.Regress.assert_called_with(r3) def testIssueRequests_RaiseValueErrorOnUnrecognizedRequestType(self): # Prepare requests and client. not_a_request = 'i am a request' client = tensorflow_serving_client.TensorFlowServingClient( 'localhost:1234', 'a_model_name') # Call with self.assertRaises(error_types.ValidationFailed): client.SendRequests([not_a_request]) def testIssueRequests_RaiseRpcErrorIfRpcFailed(self): # Prepare client and a side effect. request = classification_pb2.ClassificationRequest() client = tensorflow_serving_client.TensorFlowServingClient( 'localhost:1234', 'a_model_name') self.prediction_stub.Classify.side_effect = grpc.RpcError # Call. with self.assertRaises(error_types.ValidationFailed): client.SendRequests([request]) if __name__ == '__main__': tf.test.main()
python
""" MSX SDK MSX SDK client. # noqa: E501 The version of the OpenAPI document: 1.0.9 Generated by: https://openapi-generator.tech """ import unittest import python_msx_sdk from python_msx_sdk.api.workflow_events_api import WorkflowEventsApi # noqa: E501 class TestWorkflowEventsApi(unittest.TestCase): """WorkflowEventsApi unit test stubs""" def setUp(self): self.api = WorkflowEventsApi() # noqa: E501 def tearDown(self): pass def test_create_workflow_event(self): """Test case for create_workflow_event Creates a new workflow event. # noqa: E501 """ pass def test_delete_workflow_event(self): """Test case for delete_workflow_event Deletes a workflow event. # noqa: E501 """ pass def test_get_workflow_event(self): """Test case for get_workflow_event Returns a workflow event. # noqa: E501 """ pass def test_get_workflow_events_list(self): """Test case for get_workflow_events_list Returns a list of workflow events. # noqa: E501 """ pass def test_update_workflow_event(self): """Test case for update_workflow_event Updates a workflow event. # noqa: E501 """ pass if __name__ == '__main__': unittest.main()
python
# coding: utf-8 __author__ = 'Paul Cunningham' __email__ = '[email protected]' __copyright = 'Copyright 2017, Paul Cunningham' __license__ = 'MIT License' __version__ = '0.1' from .select2 import Select2
python
import numpy as np import pandas as pd returns = prices.pct_change() returns.dropna() returns.std() deviations = (returns - returns.mean())**2 squared_deviations = deviations ** 2 variance = squared_deviations.mean() volatility = np.sqrt(variance) me_m = pd.read_csv('./Data/Portfolios_Formed_on_ME_monthly_EW.csv', header=0, index_col=0, parse_dates=True, na_values=-99.99) rets = me_m[['Lo 10', 'Hi 10']] rets.columns = ['SmallCap', 'LargeCap'] rets = rets / 100 rets.plot.line() rets.head() rets.index = pd.to_datetime(rets.index, format='%Y%m') rets.head() rets.index = rets.index.to_period('M') rets['1975'] wealth_index = 1000 * (1+rets['LargeCap']).cumprod() wealth_index.plot.line() previous_peaks = wealth_index.cummax() previous_peaks.plot.line() drawdown = (wealth_index - previous_peaks) / previous_peaks drawdown.plot() drawdown.min() drawdown['1975':].min() drawdown['1975':].idxmin() def drawdown(return_series: pd.Series): """ Takes a time series of asset returns Computes and returns a DataFrame that contains: the wealth index the previous peaks percent drawdowns :param return_series: :return: """ wealth_index = 1000 * (1+return_series).cumprod() previous_peaks = wealth_index.cummax() drawdowns = (wealth_index - previous_peaks) / previous_peaks return pd.DataFrame( { "Wealth": wealth_index, "Peaks": previous_peaks, "Drawdown": drawdowns } ) drawdown(rets['LargeCap']).head() drawdown(rets['LargeCap'])[['Wealth', 'Peaks']].plot() import pandas as pd import EDHEC.edhec_risk_kit as erk hfi = erk.get_hfi_returns() hfi.head() pd.concat([hfi.mean(), hfi.median(), hfi.mean()>hfi.median()], axis='columns') erk.skewness(hfi).sort_values() import scipy.stats scipy.stats.skew(hfi) import numpy as np normal_rets = np.random.normal(0, .15, size=(263, 1)) erk.skewness(normal_rets) erk.kurtosis(normal_rets) erk.kurtosis(hfi) scipy.stats.kurtosis(normal_rets) scipy.stats.jarque_bera(normal_rets) scipy.stats.jarque_bera(hfi) erk.is_normal(normal_rets) hfi.aggregate(erk.is_normal) ffme = erk.get_ffme_returns() erk.skewness(ffme) erk.kurtosis(ffme) hfi.std(ddof=0) hfi[hfi<0].std(ddof=0) erk.semideviation(hfi) # Historical VaR # Parametric VaR - Gaussian # Modified Cornish-Fisher VaR np.percentile(hfi, q=5, axis=0) hfi.apply(lambda x: np.percentile(x, q=5, axis=0)) erk.var_historic(hfi) from scipy.stats import norm z = norm.ppf(.05) hfi.mean() + z*hfi.std(ddof=0) erk.var_gaussian(hfi) var_list = [erk.var_gaussian(hfi), erk.var_gaussian(hfi, modified=True), erk.var_historic(hfi)] comparison = pd.concat(var_list, axis=1) comparison.columns = ['Gaussian', 'Cornish-Fisher', 'Historic'] comparison.plot.bar(title='EDHEC Hedge Fund Indices: VaR Comparison') erk.cvar_historic(hfi)
python
import logging from django.contrib.auth.backends import ( RemoteUserBackend, get_user_model, ) from django.contrib.auth.models import ( Group, ) from django.utils.translation import ugettext as _ from rest_framework import exceptions from rest_framework_auth0.settings import ( auth0_api_settings, ) from rest_framework_auth0.utils import ( get_auth_token, get_client_setting, get_groups_from_payload, decode_auth_token, ) from rest_framework.authentication import ( BaseAuthentication, ) get_username_from_payload = auth0_api_settings.GET_USERNAME_HANDLER logger = logging.getLogger(__name__) class Auth0JSONWebTokenAuthentication(BaseAuthentication, RemoteUserBackend): """ Clients should authenticate by passing the token key in the "Authorization" HTTP header, prepended with the string specified in the setting `AUTH_HEADER_PREFIX`. For example: Authorization: JWT eyJhbGciOiAiSFMyNTYiLCAidHlwIj By default, the ``authenticate_credentials`` method creates ``User`` objects for usernames that don't already exist in the database. Subclasses can disable this behavior by setting the ``create_unknown_user`` attribute to ``False``. """ www_authenticate_realm = 'api' # Create a User object if not already in the database? create_unknown_user = True def authenticate(self, request): """ You should pass a header of your request: clientcode: web This function initialize the settings of JWT with the specific client's informations. """ client = None payload = None logger.debug("authenticating user using Auth0JSONWebTokenAuthentication") client = get_client_setting(request) auth_token = get_auth_token(request) if auth_token is None: return None payload = decode_auth_token( client=client, auth_token=auth_token ) # Add request param to authenticated_credentials() call user = self.authenticate_credentials(request, payload) return (user, payload) def authenticate_credentials(self, request, payload): """ Returns an active user that matches the payload's user id and email. """ UserModel = get_user_model() remote_user = get_username_from_payload(payload) if not remote_user: msg = _('Invalid payload.') logger.info( "{message}".format( message=msg ) ) raise exceptions.AuthenticationFailed(msg) # RemoteUserBackend behavior: # return user = None if auth0_api_settings.REPLACE_PIPE_FOR_DOTS_IN_USERNAME: username = self.clean_username(remote_user) else: username = remote_user logger.debug( "username = {username}".format( username=username ) ) if self.create_unknown_user: user, created = UserModel._default_manager.get_or_create(**{ UserModel.USERNAME_FIELD: username }) if created: user = self.configure_user(request, user) else: try: user = UserModel._default_manager.get_by_natural_key(username) except UserModel.DoesNotExist: msg = _('Invalid signature.') raise exceptions.AuthenticationFailed(msg) # RemoteUserBackend behavior: # pass user = self.configure_user_permissions(user, payload) return user if self.user_can_authenticate(user) else None def authenticate_header(self, request): """ Return a string to be used as the value of the `WWW-Authenticate` header in a `401 Unauthenticated` response, or `None` if the authentication scheme should return `403 Permission Denied` responses. """ return '{0} realm="{1}"'.format( auth0_api_settings.AUTH_HEADER_PREFIX, self.www_authenticate_realm ) def configure_user_permissions(self, user, payload): """ Validate if AUTHORIZATION_EXTENSION is enabled, defaults to False If AUTHORIZATION_EXTENSION is enabled, created and associated groups with the current user (the user of the token). """ if auth0_api_settings.AUTHORIZATION_EXTENSION: logger.debug( "Using Auth0 Authorization Extension" ) logger.debug( "Clearing groups for user: {username}".format( username=user.username ) ) user.groups.clear() try: logger.debug( "Getting groups from payload" ) groups = get_groups_from_payload(payload) logger.debug( "Groups: {groups}".format( groups=groups ) ) except Exception: # No groups where defined in Auth0? logger.warning( "No groups were defined for user: {username}".format( username=user.username ) ) return user for user_group in groups: group, created = Group.objects.get_or_create(name=user_group) logger.debug( "Associating group {group} with user {username}".format( group=group, username=user.username ) ) user.groups.add(group) return user def clean_username(self, username): """ Cleans the "username" prior to using it to get or create the user object. Returns the cleaned username. Auth0 default username (user_id) field returns, e.g. auth0|123456789...xyz which contains illegal characters ('|'). """ logger.debug("Cleaning username") username = username.replace('|', '.') logger.debug( "Clean username: {username}".format( username=username ) ) return username
python
from direct.directnotify import DirectNotifyGlobal from direct.distributed.DistributedObjectUD import DistributedObjectUD class AccountUD(DistributedObjectUD): notify = DirectNotifyGlobal.directNotify.newCategory("AccountUD")
python
from turtle import color import matplotlib.pyplot as plt from matplotlib import patches import numpy as np import math th = np.linspace(0, 2*np.pi, 1000) r=1 c=r*np.cos(th) d=r*np.sin(th) figure, axes = plt.subplots(1) axes.plot(c,d) axes.set_aspect(1) plt.title("sensor position") plt.plot(1,0,'o',color="blue",) plt.plot(0.6234898,0.78183148,'o',color="blue") plt.plot(-0.22252093,0.97492791,'o',color="blue") plt.plot(-0.90096887,0.43388374,'o',color="blue") plt.plot(-0.90096887,-0.43388374,'o',color="blue") plt.plot(-0.22252093,-0.97492791,'o',color="blue") plt.plot(0.6234898,-0.78183148,'o',color="blue") plt.plot(c,d,color="red") plt.plot([0,1],[0,0],color="red") plt.show()
python
#!/usr/bin/python3 import itertools import os import re _RE_INCLUDE = re.compile('#include ([<"])([^"<>]+)') _LIB_BY_HEADER = { 'curl/curl.h': 'curl', 're2/re2.h': 're2', 'sqlite3.h': 'sqlite3', } def dfs(root, get_children): todo = [root] visited = {id(root)} while todo: item = todo.pop() yield item for child in get_children(item): if id(child) not in visited: visited.add(id(child)) todo.append(child) class File: def __init__(self, path): self.path = path self.implemented_header_path = None self.path_without_ext, ext = os.path.splitext(self.path) self.is_source = ext == '.cpp' self.is_test = self.is_source and self.path_without_ext.endswith('_test') self.has_main_function = False self.headers_paths = [] self.headers = [] self.sources = [] self.library = None self.external_libs = [] self._load_content() def _get_path_from_root(self, path): return path if '/' in path else os.path.join(os.path.dirname(self.path), path) def _load_content(self): with open(self.path) as f: for line in f: include_match = _RE_INCLUDE.match(line) if include_match: header = include_match.group(2) if include_match.group(1) == '"': self.headers_paths.append(self._get_path_from_root(header)) elif header in _LIB_BY_HEADER: self.external_libs.append(_LIB_BY_HEADER[header]) elif self.is_source and line.startswith('int main('): self.has_main_function = True elif line.startswith('// IMPLEMENTS:'): self.implemented_header_path = self._get_path_from_root(line[len('// IMPLEMENTS:'):].strip()) def resolve_direct_dependencies(self, all_files): self.headers = [all_files[path] for path in self.headers_paths] if self.is_source: header = all_files.get(self.implemented_header_path or self.path_without_ext + '.h') if header: header.sources.append(self) def get_code_dependencies(self): deps = [header.path for header in dfs(self, lambda file: file.headers)] return [deps[0]] + sorted(deps[1:]) def get_bin_dependencies(self): objects = [] libraries = set() external_libs = set() for file in dfs(self, lambda file: itertools.chain(file.headers, file.sources)): if file.library: libraries.add((file.library.sort_key, file.library.path)) elif file.is_source: objects.append(file.path_without_ext + '.o') external_libs.update(file.external_libs) return ([objects[0]] + sorted(objects[1:]) + [path for _, path in sorted(libraries)], sorted(external_libs)) def add_to_library(self, library): if self.has_main_function: raise RuntimeError(f'File with main function added to library: {self.path}') self.library = library if self.is_source: library.objects.add(self.path_without_ext + '.o') def add_to_library_rec(self, library): def add_rec(file): file.add_to_library(library) for child in itertools.chain(file.headers, file.sources): if not child.library: add_rec(child) add_rec(self) class Library: def __init__(self, path, sort_key): self.path = path self.sort_key = sort_key self.objects = set() def enum_targets(): for (dir_path, dir_names, file_names) in os.walk('.'): if dir_path == '.': dir_names.remove('.git') for file_name in file_names: _, extension = os.path.splitext(file_name) if extension in ['.h', '.cpp']: yield os.path.join(dir_path[2:], file_name) def format_rule(target, dependencies, command, max_line_length=120): content = target + ':' length = len(content) for dependency in dependencies: length += len(dependency) + 3 if length > max_line_length: content += ' \\\n\t' + dependency length = 8 + len(dependency) else: content += ' ' + dependency content += f'\n\t{command}\n' return content def replace_section(content, start_marker, end_marker, section_content): start = content.find(start_marker) if start == -1: raise RuntimeError(f'"{start_marker}" not found') start += len(start_marker) end = content.find(end_marker) if end == -1: raise RuntimeError(f'"{end_marker}" not found') return content[:start] + section_content + content[end:] def main(): all_files = {} for path in enum_targets(): all_files[path] = File(path) for file in all_files.values(): file.resolve_direct_dependencies(all_files) mwclient_lib = Library('mwclient/libmwclient.a', 2) wikiutil_lib = Library('orlodrimbot/wikiutil/libwikiutil.a', 1) for file in all_files.values(): if file.path.startswith('mwclient/') and not file.is_test and not file.path.startswith('mwclient/tests/'): file.add_to_library_rec(mwclient_lib) elif file.path.startswith('orlodrimbot/wikiutil/') and not file.is_test: file.add_to_library(wikiutil_lib) rules = [] tests = [] binaries = [] for path, file in sorted(all_files.items()): if not file.is_source: continue rules.append(format_rule(file.path_without_ext + '.o', file.get_code_dependencies(), '$(CXX) $(CXXFLAGS) -c -o $@ $<')) if file.has_main_function: objects, external_libs = file.get_bin_dependencies() external_libs_command = ''.join(' -l' + lib for lib in external_libs) rules.append(format_rule(file.path_without_ext, objects, '$(CXX) -o $@ $^' + external_libs_command)) if file.is_test: tests.append(file.path_without_ext) else: binaries.append(file.path_without_ext) for library in [mwclient_lib, wikiutil_lib]: rules.append(format_rule(library.path, sorted(library.objects), 'ar rcs $@ $^')) with open('Makefile', 'r') as f: content = f.read() content = replace_section(content, '# autogenerated-lists-begin\n', '# autogenerated-lists-end\n', 'BINARIES= \\\n\t{binaries}\nTESTS= \\\n\t{tests}\n'.format( binaries=' \\\n\t'.join(binaries), tests=' \\\n\t'.join(tests))) content = replace_section(content, '# autogenerated-rules-begin\n', '# autogenerated-rules-end\n', ''.join(rules)) with open('Makefile', 'w') as f: f.write(content) if __name__ == '__main__': main()
python
from django.contrib import admin from django.urls import path from .views import IndexClassView, index urlpatterns = [ path("", index, name="home"), path( "class", IndexClassView.as_view(template_name="index.html"), name="home_class" ), path( "class2", IndexClassView.as_view(template_name="index2.html"), name="home_class2", ), path("admin/", admin.site.urls), ]
python
# -*- coding:utf-8 -*- from __future__ import print_function import math import numpy as np import os import sys sys.path.insert(0, '../facealign') sys.path.insert(0, '../util') from caffe_extractor import CaffeExtractor def model_centerface(do_mirror): model_dir = './models/centerface/' model_proto = model_dir + 'face_deploy.prototxt' model_path = model_dir + 'face_model.caffemodel' image_size = (96, 112) extractor = CaffeExtractor(model_proto, model_path, do_mirror = do_mirror, featLayer='fc5') return extractor, image_size def model_sphereface(do_mirror): model_dir = './models/sphereface/' model_proto = model_dir + 'sphereface_deploy.prototxt' model_path = model_dir + 'sphereface_model.caffemodel' image_size = (96, 112) extractor = CaffeExtractor(model_proto, model_path, do_mirror = do_mirror, featLayer='fc5') return extractor, image_size def model_AMSoftmax(do_mirror): model_dir = './models/AMSoftmax/' if do_mirror: model_proto = model_dir + 'face_deploy_mirror_normalize.prototxt' else: model_proto = model_dir + 'deploy.prototxt' model_path = model_dir + 'face_train_test_iter_30000.caffemodel' image_size = (96, 112) extractor = CaffeExtractor(model_proto, model_path, do_mirror = False, featLayer='fc5') return extractor, image_size def model_arcface(do_mirror): model_dir = './models/arcface/' model_proto = model_dir + 'model.prototxt' model_path = model_dir + 'model-r50-am.caffemodel' image_size = (112, 112) extractor = CaffeExtractor(model_proto, model_path, do_mirror = do_mirror, featLayer='fc1') return extractor, image_size def model_mobileface(do_mirror): model_dir = './models/mobilefacenet/' model_proto = model_dir + 'mobilefacenet-res2-6-10-2-dim128-opencv.prototxt' model_path = model_dir + 'mobilefacenet-res2-6-10-2-dim128.caffemodel' image_size = (112, 112) extractor = CaffeExtractor(model_proto, model_path, do_mirror = do_mirror, featLayer='fc1') return extractor, image_size def model_mobileface2(do_mirror): model_dir = './models/mobilefacenet/' model_proto = model_dir + 'model.prototxt' model_path = model_dir + 'model.caffemodel' image_size = (112, 112) extractor = CaffeExtractor(model_proto, model_path, do_mirror = do_mirror, featLayer='fc1') return extractor, image_size def model_factory(name, do_mirror): model_dict = { 'centerface':model_centerface, 'sphereface':model_sphereface, 'AMSoftmax' :model_AMSoftmax, 'arcface' :model_arcface, 'mobileface':model_mobileface, 'mobileface2':model_mobileface2 } model_func = model_dict[name] return model_func(do_mirror)
python
from raytracer.tuple import ( tuple, point, vector, magnitude, normalize, dot, cross, reflect, Color, ) from raytracer.rays import Ray from raytracer.spheres import Sphere from raytracer.intersections import Intersection, intersections, hit, prepare_computations from raytracer.lights import PointLight from raytracer.materials import Material, lighting from raytracer.transformations import ( translation, scaling, rotation_x, rotation_y, rotation_z, shearing, ) from raytracer.util import equal from raytracer.world import World, default_world, shade_hit, color_at from raytracer.matrices import Matrix, I from raytracer.patterns import DummyPattern, StripePattern, GradientPattern, RingPattern, CheckersPattern black = Color(0, 0, 0) white = Color(1, 1, 1) def test_creating_a_stripe_pattern(): pattern = StripePattern(white, black) assert pattern.a == white assert pattern.b == black def test_a_stripe_pattern_is_constant_in_y(): pattern = StripePattern(white, black) assert pattern.pattern_at(point(0, 0, 0)) == white assert pattern.pattern_at(point(0, 1, 0)) == white assert pattern.pattern_at(point(0, 2, 0)) == white def test_a_stripe_pattern_is_constant_in_b(): pattern = StripePattern(white, black) assert pattern.pattern_at(point(0, 0, 0)) == white assert pattern.pattern_at(point(0, 0, 1)) == white assert pattern.pattern_at(point(0, 0, 2)) == white def test_a_stripe_alternates_in_x(): pattern = StripePattern(white, black) assert pattern.pattern_at(point(0.0, 0, 0)) == white assert pattern.pattern_at(point(0.9, 0, 0)) == white assert pattern.pattern_at(point(1.0, 0, 0)) == black assert pattern.pattern_at(point(-0.1, 0, 0)) == black assert pattern.pattern_at(point(-1.0, 0, 0)) == black assert pattern.pattern_at(point(-1.1, 0, 0)) == white def test_lighting_with_a_pattern_applied(): m = Material() m.pattern = StripePattern(white, black) m.ambient = 1 m.diffuse = 0 m.specular = 0 eyev = vector(0, 0, -1) normalv = vector(0, 0, -1) light = PointLight(point(0, 0, -10), white) object = Sphere() c1 = lighting(m, object, light, point(0.9, 0, 0), eyev, normalv, False) c2 = lighting(m, object, light, point(1.1, 0, 0), eyev, normalv, False) assert c1 == white assert c2 == black def test_stripes_with_an_object_transformation(): object = Sphere() object.set_transform(scaling(2, 2, 2)) pattern = StripePattern(white, black) c = pattern.pattern_at_shape(object, point(1.5, 0, 0)) assert c == white def test_stripes_with_a_pattern_transformation(): object = Sphere() pattern = StripePattern(white, black) pattern.set_pattern_transform(scaling(2, 2, 2)) c = pattern.pattern_at_shape(object, point(1.5, 0, 0)) assert c == white def test_stripes_with_both_an_object_and_a_pattern_transformation(): object = Sphere() object.set_transform(scaling(2, 2, 2)) pattern = StripePattern(white, black) pattern.set_pattern_transform(translation(0.5, 0, 0)) c = pattern.pattern_at_shape(object, point(2.5, 0, 0)) assert c == white def test_the_default_pattern_transformation(): pattern = DummyPattern() assert pattern.transform == I def test_assigning_a_transformation(): pattern = DummyPattern() pattern.set_pattern_transform(translation(1, 2, 3)) assert pattern.transform == translation(1, 2, 3) def test_a_pattern_with_an_object_transformation(): shape = Sphere() shape.set_transform(scaling(2, 2, 2)) pattern = DummyPattern() c = pattern.pattern_at_shape(shape, point(2, 3, 4)) assert c == Color(1, 1.5, 2) def test_a_pattern_with_a_pattern_transformation(): shape = Sphere() pattern = DummyPattern() pattern.set_pattern_transform(scaling(2, 2, 2)) c = pattern.pattern_at_shape(shape, point(2, 3, 4)) assert c == Color(1, 1.5, 2) def test_a_pattern_with_both_an_object_and_a_pattern_transformation(): shape = Sphere() shape.set_transform(scaling(2, 2, 2)) pattern = DummyPattern() pattern.set_pattern_transform(translation(0.5, 1, 1.5)) c = pattern.pattern_at_shape(shape, point(2.5, 3, 3.5)) assert c == Color(0.75, 0.5, 0.25) def test_a_gradient_linearly_interpolates_between_colors(): pattern = GradientPattern(white, black) pattern.pattern_at(point(0, 0, 0)) == white assert pattern.pattern_at(point(0.25, 0, 0)) == Color(0.75, 0.75, 0.75) assert pattern.pattern_at(point(0.5, 0, 0)) == Color(0.5, 0.5, 0.5) assert pattern.pattern_at(point(0.75, 0, 0)) == Color(0.25, 0.25, 0.25) def test_a_ring_should_extend_in_both_x_and_z(): pattern = RingPattern(white, black) assert pattern.pattern_at(point(0, 0, 0)) == white assert pattern.pattern_at(point(1, 0, 0)) == black assert pattern.pattern_at(point(0, 0, 1)) == black # 0.708 = just slightly more than sqrt(2)/2 assert pattern.pattern_at(point(0.708, 0, 0.708)) == black def test_checkers_should_repeat_in_x(): pattern = CheckersPattern(white, black) assert pattern.pattern_at(point(0, 0, 0)) == white assert pattern.pattern_at(point(0.99, 0, 0)) == white assert pattern.pattern_at(point(1.01, 0, 0)) == black def test_checkers_should_repeat_in_y(): pattern = CheckersPattern(white, black) assert pattern.pattern_at(point(0, 0, 0)) == white assert pattern.pattern_at(point(0, 0.99, 0)) == white assert pattern.pattern_at(point(0, 1.01, 0)) == black def test_checkers_should_repeat_in_z(): pattern = CheckersPattern(white, black) assert pattern.pattern_at(point(0, 0, 0)) == white assert pattern.pattern_at(point(0, 0, 0.99)) == white assert pattern.pattern_at(point(0, 0, 1.01)) == black
python