How to find out if a HDF5 data set''s type is numeric? - hdf5

I would like to find out if a HDF5 data set's data type is numeric.
A way i can do this is by comparing the datatype to all H5T_NATIVE_XXX types:
hid_t hDataSet = H5Dopen1(loc_id, name);
hid_t hType = H5Dget_type(hDataSet);
if (H5Tequal(hType, H5T_NATIVE_CHAR) ||
H5Tequal(hType, H5T_NATIVE_SHORT) ||
H5Tequal(hType, H5T_NATIVE_INT) ||
...
H5Tequal(hType, H5T_NATIVE_DOUBLE) {
// handle the numeric dataset
} else {
// can't handle non-numeric
}
Is there a simpler way to find out whether a data set has a numeric datatype?

Related

Is it possible to dynamically define enumarated type based on another attributes values in IBM DOORS?

I'm trying to dynamically populate an attribute enumerated list based on another attributes values in IBM DOORS.
the image represent the list of items defined as object text
I want to dynamically update type enum based on the list captured in the picture as "Configuration Item 1", "Configuration Item 2"...
The user add/update a configuration item, then a previously defined enumurated type gets automatically updated with the same addition/update.
sure, it's possible. Basically you need to define several arrays, among them are
names aka codes, i.e. the entry you see
values i.e. the integer number assigned with the entry
colours i.e. the associated colors with the value
optionally a replacement map which defines how existing values shall be migrated to the new definition (i.e. how to migrate "red, green, yellow" to "madder, crimson, pink, lemon, pear, olive, jade, malachit, mantis")
and with these arrays you use perm AttrType modify(AttrType at, string name, [string codes[], int values[], int colors[], string descs[] string URI[], [int arrMaps[],]] string &errMess) command on attribute types.
Attached is a script from our library that copies enumerations from one module to another - this might be helpful as a starting point for your script. Also, there's an example how to modify attribute types in the DXL Reference manual, look for "modify(attribute type)" in the chapter "Attribute Type manipulation"
void updateEnumeration__(AttrType sat, at, Module targm)
// AttrType sat, // IN: original (source) type
// AttrType at, // IN: copy (target) type, null if none exists yet
// Module targm) // IN: target module
//
// DESCRIPTION: Creates a copy of the type sat in the target module targm.
// If a copy already exists ('at'), it is updated. Any enumerations
// which are in the copy and have been removed from the source type
// are kept in the copy, to prevent errors in reading attribute
// values which use these enumerations in the target module.
{
string errmess = ""
int enumerationSize = sat.size
int index, oldIndex
if (!null at)
{
// Work out the size of the merged enumerations list
for (oldIndex = 0; oldIndex < at.size; oldIndex++)
{
if (matchIndex(at.strings[oldIndex], sat) == -1)
{
// Add one for each old name which isn't used
enumerationSize++
}
}
}
string enumNames[enumerationSize]
int enumValues[enumerationSize]
int enumCols[enumerationSize]
Module savedModule = current
current = targm
bool bRealColours = getRealColorOptionForTypes()
setRealColorOptionForTypes(true)
for (index = 0; index < sat.size; index++)
{
enumNames[index] = sat.strings[index]
enumValues[index] = sat.values[index]
enumCols[index] = sat.colours[index]
}
if (null at)
{
at = create(sat.name "", enumNames, enumValues, enumCols, errmess)
if (errmess != "")
{
reportError("typeError", "Cannot create attribute type " (sat.name "") " : " errmess);
}
}
else
{
// Add any unused old enumerations to the end of the merged list
oldIndex = 0
while (index < enumerationSize)
{
// Skip enumerations which are still used
while (matchIndex(at.strings[oldIndex],sat) != -1)
{
oldIndex++
}
// copy the next unmatched old enumeration
enumNames[index] = at.strings[oldIndex]
enumValues[index] = at.values[oldIndex]
enumCols[index] = at.colours[oldIndex]
oldIndex++
index++
}
// Populate the index mapping array
int enumOldIndices[enumerationSize]
for (index = 0; index < enumerationSize; index++)
{
enumOldIndices[index] = matchIndex(enumNames[index],at)
}
// Ready to go: update the type definition...
modify(at, sat.name "", enumNames, enumValues, enumCols, enumOldIndices, errmess)
if (errmess != "")
{
reportError("typeError", "Cannot modify attribute type " (sat.name "") " : " errmess);
}
}
setRealColorOptionForTypes(bRealColours)
current = savedModule
} // updateEnumeration__

Using an 'is' expression when the right-hand operand is a variable?

I am trying to write a function that takes two arguments: givenType and targetType. If these two arguments match, I want givenType to be returned, otherwise null.
For this objective, I am trying to utilize Dart's is expression (maybe there is a better way to go about it, I am open to suggestions). Initially, I thought it would be as simple as writing this:
matchesTarget(givenType, targetType) {
if (givenType is targetType) {
return givenType;
}
return null;
}
But this produces an error:
The name 'targetType' isn't a type and can't be used in an 'is'
expression. Try correcting the name to match an existing
type.dart(type_test_with_non_type)
I tried looking up what satisfies an is expression but cannot seem to find it in the documentation. It seems like it needs its right-hand operand to be known at compile-time (hoping this is wrong, but it does not seem like I can use a variable), but if so, how else can I achieve the desired effect?
I cant guess the purpose of the function (or the scenario where it would be used, so if you can clarify it would be great). First of all, I don't know if you are passing "types" as arguments. And yes, you need to specify in compile time the right hand argument of the is function.
Meanwhile, if you are passing types, with one change, you can check if the types passed to your function at runtime.
matchesTarget(Type givenType, Type targetType) {
print('${givenType.runtimeType} ${targetType.runtimeType}');
if (givenType == targetType) {
return givenType;
}
return null;
}
main(){
var a = int; //this is a Type
var b = String; //this is also a Type
print(matchesTarget(a,b)); //You are passing different Types, so it will return null
var c = int; //this is also a Type
print(matchesTarget(a,c)); //You are passing same Types, so it will return int
}
But if you are passing variables, the solution is pretty similar:
matchesTarget(givenVar, targetVar) {
print('${givenVar.runtimeType} ${targetVar.runtimeType}');
if (givenVar.runtimeType == targetVar.runtimeType) {
return givenVar.runtimeType;
}
return null;
}
main(){
var a = 10; //this is a variable (int)
var b = "hello"; //this is also a variable (String)
print(matchesTarget(a,b)); //this will return null
var c = 12; //this is also a variable (int)
print(matchesTarget(a,c)); //this will return int
}
The Final Answer
matchesTarget(givenVar, targetType) {
print('${givenVar.runtimeType} ${targetType}');
if (givenVar.runtimeType == targetType) {
return givenVar;
}
return null;
}
main(){
var a = 10; //this is a variable (int)
var b = String; //this is a type (String)
print(matchesTarget(a,b)); //this will return null because 'a' isnt a String
var c = int; //this is also a type (int)
print(matchesTarget(a,c)); //this will return the value of 'a' (10)
}
The as, is, and is! operators are handy for checking types at runtime.
The is operator in Dart can be only used for type checking and not checking if two values are equal.
The result of obj is T is true if obj implements the interface specified by T. For example, obj is Object is always true.
See the below code for an example of how to use the is operator
if (emp is Person) {
// Type check
emp.firstName = 'Bob';
}
Even the error message that you're getting says that
The name 'targetType' isn't a type and can't be used in an 'is'
expression.
So the bottomline is that you can use is only for checking if a variable or value belongs to a particular data type.
For checking equality, you can use the == operator if comparing primitive types, or write your own method for comparing the values. Hope this helps!

How to check if enum type parameter with multiple values has a particular enum type

I have a method returning enum.
-(EnumType)supportedEnum {
return EnumTypeA | EnumTypeB | EnumTypeC;
}
In another method, I am comparing if supported enum contains a particular enum type as below:
if ([Instance supportedEnum] == EnumTypeA) {
NSLog("Class contains EnumTypeA");
}
But it is not working as this condition is somehow false.
What am I doing wrong?
You appear to be trying to define and use an enum as an option set. To do this you must first define your enum literals to have values which are powers of 2 which means, as the underlying hardware uses binary, that each value has exactly one bit set in it's value. You can also set the underlying type of the enum to one of: uint8_t, uint16_t, uint32_t or uint64_t; allowing your enum to have up to 8, 16, 32 or 64 literals respectively. For example:
typedef enum : uint8_t
{
EnumTypeA = 0x1,
EnumTypeB = 0x2,
EnumTypeC = 0x4,
EnumTypeD = 0x8,
EnumTypeE = 0x10
} EnumType;
Note: hexadecimal constants were used as they more clearly show only one bit is set in each value, but you and use decimal (or octal) if you prefer.
Note: Objective-C also provides a macro NS_OPTIONS that can be used to help define option set enums, using it is a matter of choice. If you intend to inter-operate with Swift using it might be recommended. Look in Apple's documentation for it use.
You combine your literals to produce a set value using bitwise-or, |, as you did in your method:
- (EnumType)supportedEnum
{
return EnumTypeA | EnumTypeB | EnumTypeC;
}
which returns a value of EnumType with exactly 3 bits set.
To test for a particular value being present you use bitwise-and, &, e.g.:
if ([instance supportedEnum] & EnumTypeA)
{
NSLog("Class contains EnumTypeA");
}
This works as if tests for its predicate expression being not equal to zero (this in an (Objective-)C peculiarity in that if does not take a boolean valued predicate but an integral valued one).
If you wish to test if any one or more of a set of enum literals is present you combine bitwise and and or. E.g. to test for EnumTypeA and/or EnumTypeC you would write:
if ([instance supportedEnum] & (EnumTypeA | EnumTypeC) ) ...
To test for all of a set of literals being present you must add an equality comparison. E.g.:
if ( ([instance supportedEnum] & (EnumTypeA | EnumTypeC)) == (EnumTypeA | EnumTypeC) ) ...
tests for both of EnumTypeA and EnumTypeC.
Using the bitwise and, or, xor and inverse operators, their compound assignment versions, and (in)equality operators you can set or clear individual literals in an enum value; union etc. two or more values; and test for any combination of literals being present/set and/or absent/clear.
HTH
You need to check reachability enum code
Here is the sample of enum it is just for your reference. don't use reachability code it is just for enum sample.
First you need to create variable with your enum "EnumType"
then you need to check your variable with "EnumTypeA", "EnumTypeB", "EnumTypeC"
- (void)updateInterfaceWithReachability:(Reachability *)reachability {
NetworkStatus netStatus = [reachability currentReachabilityStatus];
BOOL connectionRequired = [reachability connectionRequired];
switch (netStatus) {
case NotReachable: {
connectionRequired = NO;
self.isReachable = false;
}
break;
case ReachableViaWWAN: {
self.isReachable = true;
}
break;
case ReachableViaWiFi: {
self.isReachable = true;
}
break;
default: {
self.isReachable = false;
}
break;
}
NSLog(#"\nisReachable = %d\n",isReachable);
}
Reason your condition is always failing is because what you are doing in supportedEnum method is an Binary OR (|) operator.
so supportedEnum method returns 3 (0|1|2 = 3 and 1|2|3 = 3 )
you are checking that with EnumTypeA, thats why your condtion alwasy fails.
you can do with switch case or if-else as #vivek method. but but to clarify how to properly implement what you implemented. you can convert your enum to NSNumber and perform contains check.
Note: doing if-else or switch in case of a large number of enums will make your code bit long.
Eg:
typedef enum : NSUInteger {
EnumTypeA,
EnumTypeB,
EnumTypeC,
EnumTypeD
} EnumType;
//Here I am just converting the enum to Array of NSNumber.
-(NSArray*)supportedEnums {
return #[#(EnumTypeA), #(EnumTypeB), #(EnumTypeC)];
}
//Check by performing a contains check in the array.
if ([[self supportedEnums] containsObject:#(EnumTypeA)]) {
NSLog(#"Class contains EnumTypeA");
}else {
NSLog(#"Error in condition");
}

2 same content arrays are not equal in Dart?

I have a question.
When working with Dart, I can't check to see if 2 arrays are equal.
(in other languages, I can do with ==)
In fact, I just can do that == with String or number.
List arr1 = [1,2,3];
List arr2 = [1,2,3];
if (arr1 == arr2) {
print("equal");
} else {
print("not equal");
}
// Output: not equal.
So I wonder how does that make sense. I mean, How we can do if the == is just work for the cases of String or number (if the values compared are the same).
How do I have to do if I want to check that kind of comparison (equal) for List, Map, ..
It just work for String & number.
arr1 and arr2 are different instances of an object of type List. By default different instances are always different.
When a class implements a custom == operator it can override this behavior. Some classes have a custom implementation by default like int and String.
This can easily be done for immutable objects but not for mutable. One reason is that usually the hashCode is calculated from the values stroed in a class and the hashCode must not change for an instance because this can for example cause that an instance stored in a map can't be retrieved anymore when the hashcode of the key changed.
As a workaround there is a library that provides helper functions to compare lists/iterables.
import 'package:collection/equality.dart';
void main(List<String> args) {
if (const IterableEquality().equals([1,2,3],[1,2,3])) {
// if (const SetEquality().equals([1,2,3].toSet(),[1,2,3].toSet())) {
print("Equal");
} else {
print("Not equal");
}
}

How can I write custom comparison (definition for binary operator Equal) for entityframework object to an int?

I'm getting this error:
ex = {"The binary operator Equal is not defined for the types 'MySite.Domain.DomainModel.EntityFramework.NickName' and 'System.Int32'."}
What I tried to do was do a select all where the NickNameId = someIntPassedIn... the problem is that the NickNameId is a foreign key, so when it compares the someIntPassedIn to the NickNameId it pulls the whole NickName object that the NickNameId refers to and tries to compare the int to that object.
I need a solution here to allow it to compare the int to the NickName object's Id... so
A) How can I define the binary operator Equal for comparing these two objects
OR
B) How can I compare it directly to the id instead of the whole object?
You don't have to read this, but here's the SelectAllByKey method incase it helps: (I passed in "NickNameId" and "1")
public IList<E> SelectAllByKey(string columnName, string key)
{
KeyProperty = columnName;
int id;
Expression rightExpr = null;
if (int.TryParse(key, out id))
{
rightExpr = Expression.Constant(id);
}
else
{
rightExpr = Expression.Constant(key);
}
// First we define the parameter that we are going to use the clause.
var xParam = Expression.Parameter(typeof(E), typeof(E).Name);
MemberExpression leftExpr = MemberExpression.Property(xParam, this._KeyProperty);
int temp;
BinaryExpression binaryExpr = MemberExpression.Equal(leftExpr, rightExpr);
//Create Lambda Expression for the selection
Expression<Func<E, bool>> lambdaExpr = Expression.Lambda<Func<E, bool>>(binaryExpr, new ParameterExpression[] { xParam });
//Searching ....
IList<E> resultCollection = ((IRepository<E, C>)this).SelectAll(new Specification<E>(lambdaExpr));
if (null != resultCollection && resultCollection.Count() > 0)
{
//return valid single result
return resultCollection;
}//end if
return null;
}
Let me know if you need any more info.
Thanks,
Matt
You should call SelectAllByKey('NickName.ID','1').
Since ID is property of property, you could use this extension method:
public static MemberExpression PropertyOfProperty(this Expression expr,string propertyName)
{
var properties = propertyName.Split('.');
MemberExpression expression = null;
foreach (var property in properties)
{
if (expression == null)
expression = Expression.Property(expr, property);
else
expression = Expression.Property(expression, property);
}
return expression;
}
The accepted answer seems way too complicated for the problem at hand, if I'm reading this correctly.
If I understand you correctly, you're trying to run a query like:
var q = from e in Context.SomeEntities
where e.NickNameId == someIntPassedIn
select e;
...but this won't work, because e.NickNameId is an entity, not an integer.
To reference the Id property, you can just refer to it, like this:
var q = from e in Context.SomeEntities
where e.NickNameId.Id == someIntPassedIn
select e;
Update: If you can't use strong-typed properties due to your level of abstraction (per your comment), then use query builder methods:
var q = (ObjectQuery<T>)Repository.SelectSomething();
return q.Where("it.NickName.Id = " + someIntPassedIn.ToString());
You can adapt this as you see fit, but the general point is that the EF already knows how to translate strings to property members.

Resources